blob: 12b0158be877701878a131de19878b2cce63eebd [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Michael Jurka18014792010-10-14 09:01:34 -070021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
Chet Haase00397b12010-10-07 11:13:10 -070023import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070024import android.animation.ValueAnimator;
25import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040027import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070029import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070032import android.graphics.Point;
33import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.graphics.Rect;
35import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070036import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070037import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070039import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.view.MotionEvent;
41import android.view.View;
42import android.view.ViewDebug;
43import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070045import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047
Adam Cohen66396872011-04-15 17:50:36 -070048import com.android.launcher.R;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070049
Adam Cohenc0dcf592011-06-01 15:30:43 -070050import java.util.Arrays;
51
Michael Jurkabdb5c532011-02-01 15:05:06 -080052public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070053 static final String TAG = "CellLayout";
54
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 private int mCellWidth;
56 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070057
Winson Chungaafa03c2010-06-11 17:34:16 -070058 private int mLeftPadding;
59 private int mRightPadding;
60 private int mTopPadding;
61 private int mBottomPadding;
62
Adam Cohend22015c2010-07-26 22:02:18 -070063 private int mCountX;
64 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065
66 private int mWidthGap;
67 private int mHeightGap;
68
69 private final Rect mRect = new Rect();
70 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070071
Patrick Dubroyde7658b2010-09-27 11:15:43 -070072 // These are temporary variables to prevent having to allocate a new object just to
73 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070074 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070075 private final int[] mTmpPoint = new int[2];
76 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070077
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 boolean[][] mOccupied;
79
Michael Jurkadee05892010-07-27 10:01:56 -070080 private OnTouchListener mInterceptTouchListener;
81
Michael Jurka5f1c5092010-09-03 14:15:02 -070082 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070083 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070084
Michael Jurka33945b22010-12-21 18:19:38 -080085 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080086 private Drawable mActiveBackground;
87 private Drawable mActiveGlowBackground;
88 private Drawable mNormalBackgroundMini;
89 private Drawable mNormalGlowBackgroundMini;
90 private Drawable mActiveBackgroundMini;
91 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -070092 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -080093 private Rect mGlowBackgroundRect;
94 private float mGlowBackgroundScale;
95 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -070096
Adam Cohendf035382011-04-11 17:22:04 -070097 private boolean mAcceptsDrops = true;
Michael Jurka33945b22010-12-21 18:19:38 -080098 // If we're actively dragging something over this screen, mIsDragOverlapping is true
99 private boolean mIsDragOverlapping = false;
100 private boolean mIsDragOccuring = false;
101 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700102 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700103
Winson Chung150fbab2010-09-29 17:14:26 -0700104 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700105 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Winson Chung63257c12011-05-05 17:06:13 -0700106 private Point[] mDragOutlines = new Point[4];
Chet Haase472b2812010-10-14 07:02:04 -0700107 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700108 private InterruptibleInOutAnimator[] mDragOutlineAnims =
109 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700110
111 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700112 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700113 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700114
Patrick Dubroy96864c32011-03-10 17:17:23 -0800115 private BubbleTextView mPressedOrFocusedIcon;
116
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700117 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700118 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700119 private float mCrosshairsVisibility = 0.0f;
120
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700121 // When a drag operation is in progress, holds the nearest cell to the touch point
122 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato4be866d2010-10-10 11:26:02 -0700124 private boolean mDragging = false;
125
Patrick Dubroyce34a972010-10-19 10:34:32 -0700126 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800127 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 public CellLayout(Context context) {
130 this(context, null);
131 }
132
133 public CellLayout(Context context, AttributeSet attrs) {
134 this(context, attrs, 0);
135 }
136
137 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
138 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700139
140 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
141 // the user where a dragged item will land when dropped.
142 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
145
146 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
147 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700148 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
149 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700150
Adam Cohend22015c2010-07-26 22:02:18 -0700151 mLeftPadding =
152 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
153 mRightPadding =
154 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
155 mTopPadding =
156 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
157 mBottomPadding =
158 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700159
Adam Cohend22015c2010-07-26 22:02:18 -0700160 mCountX = LauncherModel.getCellCountX();
161 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700162 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
164 a.recycle();
165
166 setAlwaysDrawnWithCacheEnabled(false);
167
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700168 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700169
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.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700274 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
275 int widthGap = r.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
Michael Jurkaf6440da2011-04-05 14:50:34 -0700276
Michael Jurka32271c32011-06-01 17:03:13 -0700277 return widthGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700278 }
279
Michael Jurkaf6440da2011-04-05 14:50:34 -0700280 static int heightInLandscape(Resources r, int numCells) {
281 // We use this method from Workspace to figure out how many rows/columns Launcher should
282 // have. We ignore the left/right padding on CellLayout because it turns out in our design
283 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700284 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
285 int heightGap = r.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
Michael Jurkaf6440da2011-04-05 14:50:34 -0700286
Michael Jurka32271c32011-06-01 17:03:13 -0700287 return heightGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700288 }
289
Adam Cohen2801caf2011-05-13 20:57:39 -0700290 public void enableHardwareLayers() {
291 mChildren.enableHardwareLayers();
292 }
293
294 public void setGridSize(int x, int y) {
295 mCountX = x;
296 mCountY = y;
297 mOccupied = new boolean[mCountX][mCountY];
298 }
299
Patrick Dubroy96864c32011-03-10 17:17:23 -0800300 private void invalidateBubbleTextView(BubbleTextView icon) {
301 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700302 invalidate(icon.getLeft() + getLeftPadding() - padding,
303 icon.getTop() + getTopPadding() - padding,
304 icon.getRight() + getLeftPadding() + padding,
305 icon.getBottom() + getTopPadding() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800306 }
307
308 void setPressedOrFocusedIcon(BubbleTextView icon) {
309 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
310 // requires an expanded clip rect (due to the glow's blur radius)
311 BubbleTextView oldIcon = mPressedOrFocusedIcon;
312 mPressedOrFocusedIcon = icon;
313 if (oldIcon != null) {
314 invalidateBubbleTextView(oldIcon);
315 }
316 if (mPressedOrFocusedIcon != null) {
317 invalidateBubbleTextView(mPressedOrFocusedIcon);
318 }
319 }
320
Winson Chung6e314082011-01-27 16:46:51 -0800321 public CellLayoutChildren getChildrenLayout() {
322 if (getChildCount() > 0) {
323 return (CellLayoutChildren) getChildAt(0);
324 }
325 return null;
326 }
327
Michael Jurka33945b22010-12-21 18:19:38 -0800328 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
329 if (mIsDefaultDropTarget != isDefaultDropTarget) {
330 mIsDefaultDropTarget = isDefaultDropTarget;
331 invalidate();
332 }
333 }
334
Michael Jurka33945b22010-12-21 18:19:38 -0800335 void setIsDragOccuring(boolean isDragOccuring) {
336 if (mIsDragOccuring != isDragOccuring) {
337 mIsDragOccuring = isDragOccuring;
338 invalidate();
339 }
340 }
341
342 void setIsDragOverlapping(boolean isDragOverlapping) {
343 if (mIsDragOverlapping != isDragOverlapping) {
344 mIsDragOverlapping = isDragOverlapping;
345 invalidate();
346 }
347 }
348
349 boolean getIsDragOverlapping() {
350 return mIsDragOverlapping;
351 }
352
353 private void updateGlowRect() {
354 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700355 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
356 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800357 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700358 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
359 invalidate();
360 }
361
362 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800363 if (scaleFactor != mGlowBackgroundScale) {
364 mGlowBackgroundScale = scaleFactor;
365 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800366 if (getParent() != null) {
367 ((View) getParent()).invalidate();
368 }
Michael Jurka18014792010-10-14 09:01:34 -0700369 }
370 }
371
372 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800373 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700374 }
375
376 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800377 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700378 }
379
380 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800381 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700382 invalidate();
383 }
384
385 void animateDrop() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700386 if (LauncherApplication.isScreenLarge()) {
Michael Jurka18014792010-10-14 09:01:34 -0700387 Resources res = getResources();
388 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
389 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
390 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
391 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
392 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
393 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
394
395 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
396 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
397
398 AnimatorSet bouncer = new AnimatorSet();
399 bouncer.play(scaleUp).before(scaleDown);
400 bouncer.play(scaleUp).with(alphaFadeOut);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800401 bouncer.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700402 @Override
Michael Jurka18014792010-10-14 09:01:34 -0700403 public void onAnimationStart(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800404 setIsDragOverlapping(true);
Michael Jurka18014792010-10-14 09:01:34 -0700405 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700406 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800407 public void onAnimationEnd(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800408 setIsDragOverlapping(false);
Michael Jurka18014792010-10-14 09:01:34 -0700409 setHoverScale(1.0f);
410 setHoverAlpha(1.0f);
411 }
412 });
413 bouncer.start();
414 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 }
416
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700417 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700418 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700419 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
420 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
421 // When we're small, we are either drawn normally or in the "accepts drops" state (during
422 // a drag). However, we also drag the mini hover background *over* one of those two
423 // backgrounds
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700424 if (LauncherApplication.isScreenLarge() && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700425 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800426 boolean mini = getScaleX() < 0.5f;
427
428 if (mIsDragOverlapping) {
429 // In the mini case, we draw the active_glow bg *over* the active background
430 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
431 } else if (mIsDragOccuring && mAcceptsDrops) {
432 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800433 } else if (mIsDefaultDropTarget && mini) {
434 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700435 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800436 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700437 }
Michael Jurka33945b22010-12-21 18:19:38 -0800438
439 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
440 bg.setBounds(mBackgroundRect);
441 bg.draw(canvas);
442
443 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700444 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800445 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700446 // If the hover background's scale is greater than 1, we'll be drawing outside
447 // the bounds of this CellLayout. Get around that by temporarily increasing the
448 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800449 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700450 Rect clipRect = canvas.getClipBounds();
451 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
452 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
453 canvas.save(Canvas.CLIP_SAVE_FLAG);
454 canvas.clipRect(-marginX, -marginY,
455 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
456 modifiedClipRect = true;
457 }
458
Michael Jurka33945b22010-12-21 18:19:38 -0800459 mActiveGlowBackgroundMini.setAlpha(
460 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
461 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
462 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700463 if (modifiedClipRect) {
464 canvas.restore();
465 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700466 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700467 }
Romain Guya6abce82009-11-10 02:54:41 -0800468
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700469 if (mCrosshairsVisibility > 0.0f) {
470 final int countX = mCountX;
471 final int countY = mCountY;
472
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700473 final float MAX_ALPHA = 0.4f;
474 final int MAX_VISIBLE_DISTANCE = 600;
475 final float DISTANCE_MULTIPLIER = 0.002f;
476
477 final Drawable d = mCrosshairsDrawable;
478 final int width = d.getIntrinsicWidth();
479 final int height = d.getIntrinsicHeight();
480
481 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
482 for (int col = 0; col <= countX; col++) {
483 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
484 for (int row = 0; row <= countY; row++) {
485 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
486 float dist = mTmpPointF.length();
487 // Crosshairs further from the drag point are more faint
488 float alpha = Math.min(MAX_ALPHA,
489 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
490 if (alpha > 0.0f) {
491 d.setBounds(x, y, x + width, y + height);
492 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
493 d.draw(canvas);
494 }
495 y += mCellHeight + mHeightGap;
496 }
497 x += mCellWidth + mWidthGap;
498 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700499 }
Winson Chung150fbab2010-09-29 17:14:26 -0700500
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700501 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700502 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700503 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700504 if (alpha > 0) {
505 final Point p = mDragOutlines[i];
506 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700507 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700508 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700509 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700510 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800511
512 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
513 // requires an expanded clip rect (due to the glow's blur radius)
514 if (mPressedOrFocusedIcon != null) {
515 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
516 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
517 if (b != null) {
518 canvas.drawBitmap(b,
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700519 mPressedOrFocusedIcon.getLeft() + getLeftPadding() - padding,
520 mPressedOrFocusedIcon.getTop() + getTopPadding() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800521 null);
522 }
523 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700524 }
525
526 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700527 public void cancelLongPress() {
528 super.cancelLongPress();
529
530 // Cancel long press for all children
531 final int count = getChildCount();
532 for (int i = 0; i < count; i++) {
533 final View child = getChildAt(i);
534 child.cancelLongPress();
535 }
536 }
537
Michael Jurkadee05892010-07-27 10:01:56 -0700538 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
539 mInterceptTouchListener = listener;
540 }
541
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700543 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 }
545
546 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700547 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 }
549
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700550 public boolean addViewToCellLayout(
551 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700552 final LayoutParams lp = params;
553
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 // Generate an id for each view, this assumes we have at most 256x256 cells
555 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700556 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700557 // If the horizontal or vertical span is set to -1, it is taken to
558 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700559 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
560 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561
Winson Chungaafa03c2010-06-11 17:34:16 -0700562 child.setId(childId);
563
Michael Jurka8c920dd2011-01-20 14:16:56 -0800564 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700565
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700566 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700567
Winson Chungaafa03c2010-06-11 17:34:16 -0700568 return true;
569 }
570 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700572
Michael Jurkabea15192010-11-17 12:33:46 -0800573 public void setAcceptsDrops(boolean acceptsDrops) {
574 if (mAcceptsDrops != acceptsDrops) {
575 mAcceptsDrops = acceptsDrops;
576 invalidate();
577 }
578 }
579
Michael Jurka3e7c7632010-10-02 16:01:03 -0700580 public boolean getAcceptsDrops() {
581 return mAcceptsDrops;
582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583
584 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700585 public void removeAllViews() {
586 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800587 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700588 }
589
590 @Override
591 public void removeAllViewsInLayout() {
592 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800593 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700594 }
595
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700596 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800597 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700598 }
599
Michael Jurka0280c3b2010-09-17 15:00:07 -0700600 @Override
601 public void removeView(View view) {
602 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800603 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700604 }
605
606 @Override
607 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800608 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
609 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700610 }
611
612 @Override
613 public void removeViewInLayout(View view) {
614 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800615 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700616 }
617
618 @Override
619 public void removeViews(int start, int count) {
620 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800621 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700622 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800623 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700624 }
625
626 @Override
627 public void removeViewsInLayout(int start, int count) {
628 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800629 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700630 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800631 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700632 }
633
Michael Jurka8c920dd2011-01-20 14:16:56 -0800634 public void drawChildren(Canvas canvas) {
635 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 }
637
Michael Jurkaabded662011-03-04 12:06:57 -0800638 void buildChildrenLayer() {
639 mChildren.buildLayer();
640 }
641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 @Override
643 protected void onAttachedToWindow() {
644 super.onAttachedToWindow();
645 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
646 }
647
Michael Jurkaaf442092010-06-10 17:01:57 -0700648 public void setTagToCellInfoForPoint(int touchX, int touchY) {
649 final CellInfo cellInfo = mCellInfo;
650 final Rect frame = mRect;
651 final int x = touchX + mScrollX;
652 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800653 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700654
655 boolean found = false;
656 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800657 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800658 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700659
Adam Cohen1b607ed2011-03-03 17:26:50 -0800660 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
661 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700662 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700663
664 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
665 // offset that by this CellLayout's padding to test an (x,y) point that is relative
666 // to this view.
667 final int tmpXY[] = mTmpXY;
668 child.getLocationOnScreen(tmpXY);
669 frame.offset(mLeftPadding, mTopPadding);
670
Michael Jurkaaf442092010-06-10 17:01:57 -0700671 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700672 cellInfo.cell = child;
673 cellInfo.cellX = lp.cellX;
674 cellInfo.cellY = lp.cellY;
675 cellInfo.spanX = lp.cellHSpan;
676 cellInfo.spanY = lp.cellVSpan;
677 cellInfo.valid = true;
678 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700679 break;
680 }
681 }
682 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700683
Michael Jurkaaf442092010-06-10 17:01:57 -0700684 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700685 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700686 pointToCellExact(x, y, cellXY);
687
Michael Jurkaaf442092010-06-10 17:01:57 -0700688 cellInfo.cell = null;
689 cellInfo.cellX = cellXY[0];
690 cellInfo.cellY = cellXY[1];
691 cellInfo.spanX = 1;
692 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700693 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
694 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700695 }
696 setTag(cellInfo);
697 }
698
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 @Override
700 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700701 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
702 return true;
703 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 final int action = ev.getAction();
705 final CellInfo cellInfo = mCellInfo;
706
707 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700708 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 } else if (action == MotionEvent.ACTION_UP) {
710 cellInfo.cell = null;
711 cellInfo.cellX = -1;
712 cellInfo.cellY = -1;
713 cellInfo.spanX = 0;
714 cellInfo.spanY = 0;
715 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 setTag(cellInfo);
717 }
718
719 return false;
720 }
721
722 @Override
723 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700724 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 }
726
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700727 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700728 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 * @param x X coordinate of the point
730 * @param y Y coordinate of the point
731 * @param result Array of 2 ints to hold the x and y coordinate of the cell
732 */
733 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700734 final int hStartPadding = getLeftPadding();
735 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736
737 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
738 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
739
Adam Cohend22015c2010-07-26 22:02:18 -0700740 final int xAxis = mCountX;
741 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742
743 if (result[0] < 0) result[0] = 0;
744 if (result[0] >= xAxis) result[0] = xAxis - 1;
745 if (result[1] < 0) result[1] = 0;
746 if (result[1] >= yAxis) result[1] = yAxis - 1;
747 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700748
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 /**
750 * Given a point, return the cell that most closely encloses that point
751 * @param x X coordinate of the point
752 * @param y Y coordinate of the point
753 * @param result Array of 2 ints to hold the x and y coordinate of the cell
754 */
755 void pointToCellRounded(int x, int y, int[] result) {
756 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
757 }
758
759 /**
760 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700761 *
762 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700764 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 * @param result Array of 2 ints to hold the x and y coordinate of the point
766 */
767 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700768 final int hStartPadding = getLeftPadding();
769 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770
771 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
772 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
773 }
774
Adam Cohene3e27a82011-04-15 12:07:39 -0700775 /**
776 * Given a cell coordinate, return the point that represents the upper left corner of that cell
777 *
778 * @param cellX X coordinate of the cell
779 * @param cellY Y coordinate of the cell
780 *
781 * @param result Array of 2 ints to hold the x and y coordinate of the point
782 */
783 void cellToCenterPoint(int cellX, int cellY, int[] result) {
784 final int hStartPadding = getLeftPadding();
785 final int vStartPadding = getTopPadding();
786
787 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
788 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
789 }
790
Romain Guy84f296c2009-11-04 15:00:44 -0800791 int getCellWidth() {
792 return mCellWidth;
793 }
794
795 int getCellHeight() {
796 return mCellHeight;
797 }
798
Adam Cohend4844c32011-02-18 19:25:06 -0800799 int getWidthGap() {
800 return mWidthGap;
801 }
802
803 int getHeightGap() {
804 return mHeightGap;
805 }
806
Romain Guy1a304a12009-11-10 00:02:32 -0800807 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700808 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800809 }
810
811 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700812 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800813 }
814
815 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700816 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800817 }
818
819 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700820 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800821 }
822
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700823 Rect getContentRect(Rect r) {
824 if (r == null) {
825 r = new Rect();
826 }
827 int left = getPaddingLeft();
828 int top = getPaddingTop();
829 int right = left + getWidth() - mLeftPadding - mRightPadding;
830 int bottom = top + getHeight() - mTopPadding - mBottomPadding;
831 r.set(left, top, right, bottom);
832 return r;
833 }
834
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 @Override
836 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
837 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700838
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700840 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
841
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
843 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700844
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
846 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
847 }
848
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 final int cellWidth = mCellWidth;
850 final int cellHeight = mCellHeight;
851
Adam Cohend22015c2010-07-26 22:02:18 -0700852 int numWidthGaps = mCountX - 1;
853 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854
Winson Chungece7f5b2010-10-22 14:54:12 -0700855 if (mWidthGap < 0 || mHeightGap < 0) {
856 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700857 mHeightGap = numHeightGaps > 0 ? vSpaceLeft / numHeightGaps : 0;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858
Winson Chungece7f5b2010-10-22 14:54:12 -0700859 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohen2801caf2011-05-13 20:57:39 -0700860 mWidthGap = numWidthGaps > 0 ? hSpaceLeft / numWidthGaps : 0;
Winson Chungaafa03c2010-06-11 17:34:16 -0700861
Winson Chungece7f5b2010-10-22 14:54:12 -0700862 // center it around the min gaps
863 int minGap = Math.min(mWidthGap, mHeightGap);
864 mWidthGap = mHeightGap = minGap;
865 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700866
Michael Jurka8c920dd2011-01-20 14:16:56 -0800867 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
868 int newWidth = widthSpecSize;
869 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700870 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800871 newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700872 ((mCountX - 1) * mWidthGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800873 newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700874 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700875 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700876 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800877
878 int count = getChildCount();
879 for (int i = 0; i < count; i++) {
880 View child = getChildAt(i);
Adam Cohen2801caf2011-05-13 20:57:39 -0700881 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mLeftPadding -
882 mRightPadding, MeasureSpec.EXACTLY);
883 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mTopPadding -
884 mBottomPadding, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800885 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
886 }
887 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 }
889
890 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700891 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800894 View child = getChildAt(i);
Adam Cohenbb60e2e2011-04-26 16:48:26 -0700895 child.layout(mLeftPadding, mTopPadding, r - l - mRightPadding , b - t - mBottomPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 }
897 }
898
899 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700900 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
901 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700902 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800903 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700904 }
905
906 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800908 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 }
910
911 @Override
912 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800913 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 }
915
Michael Jurka5f1c5092010-09-03 14:15:02 -0700916 public float getBackgroundAlpha() {
917 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700918 }
919
Michael Jurka742574b2011-02-02 23:51:01 -0800920 public void setFastBackgroundAlpha(float alpha) {
921 mBackgroundAlpha = alpha;
922 }
923
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700924 public void setBackgroundAlphaMultiplier(float multiplier) {
925 mBackgroundAlphaMultiplier = multiplier;
926 }
927
Adam Cohenddb82192010-11-10 16:32:54 -0800928 public float getBackgroundAlphaMultiplier() {
929 return mBackgroundAlphaMultiplier;
930 }
931
Michael Jurka5f1c5092010-09-03 14:15:02 -0700932 public void setBackgroundAlpha(float alpha) {
933 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700934 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700935 }
936
Michael Jurka5f1c5092010-09-03 14:15:02 -0700937 // Need to return true to let the view system know we know how to handle alpha-- this is
938 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
939 // versions
940 @Override
941 protected boolean onSetAlpha(int alpha) {
942 return true;
943 }
944
945 public void setAlpha(float alpha) {
946 setChildrenAlpha(alpha);
947 super.setAlpha(alpha);
948 }
949
Michael Jurka742574b2011-02-02 23:51:01 -0800950 public void setFastAlpha(float alpha) {
951 setFastChildrenAlpha(alpha);
952 super.setFastAlpha(alpha);
953 }
954
Michael Jurkadee05892010-07-27 10:01:56 -0700955 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700956 final int childCount = getChildCount();
957 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700958 getChildAt(i).setAlpha(alpha);
959 }
960 }
961
Michael Jurka742574b2011-02-02 23:51:01 -0800962 private void setFastChildrenAlpha(float alpha) {
963 final int childCount = getChildCount();
964 for (int i = 0; i < childCount; i++) {
965 getChildAt(i).setFastAlpha(alpha);
966 }
967 }
968
Patrick Dubroy440c3602010-07-13 17:50:32 -0700969 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800970 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700971 }
972
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700973 /**
974 * Estimate where the top left cell of the dragged item will land if it is dropped.
975 *
976 * @param originX The X value of the top left corner of the item
977 * @param originY The Y value of the top left corner of the item
978 * @param spanX The number of horizontal cells that the item spans
979 * @param spanY The number of vertical cells that the item spans
980 * @param result The estimated drop cell X and Y.
981 */
982 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700983 final int countX = mCountX;
984 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700985
Michael Jurkaa63c4522010-08-19 13:52:27 -0700986 // pointToCellRounded takes the top left of a cell but will pad that with
987 // cellWidth/2 and cellHeight/2 when finding the matching cell
988 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700989
990 // If the item isn't fully on this screen, snap to the edges
991 int rightOverhang = result[0] + spanX - countX;
992 if (rightOverhang > 0) {
993 result[0] -= rightOverhang; // Snap to right
994 }
995 result[0] = Math.max(0, result[0]); // Snap to left
996 int bottomOverhang = result[1] + spanY - countY;
997 if (bottomOverhang > 0) {
998 result[1] -= bottomOverhang; // Snap to bottom
999 }
1000 result[1] = Math.max(0, result[1]); // Snap to top
1001 }
1002
Joe Onorato4be866d2010-10-10 11:26:02 -07001003 void visualizeDropLocation(
1004 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1005
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001006 final int oldDragCellX = mDragCell[0];
1007 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001008 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001009 if (v != null) {
1010 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1011 } else {
1012 mDragCenter.set(originX, originY);
1013 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001014
Adam Cohen2801caf2011-05-13 20:57:39 -07001015 if (dragOutline == null && v == null) {
1016 if (mCrosshairsDrawable != null) {
1017 invalidate();
1018 }
1019 return;
1020 }
1021
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001022 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001023 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001024 final int[] topLeft = mTmpPoint;
1025 cellToPoint(nearest[0], nearest[1], topLeft);
1026
Joe Onorato4be866d2010-10-10 11:26:02 -07001027 int left = topLeft[0];
1028 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001029
Winson Chunga9abd0e2010-10-27 17:18:37 -07001030 if (v != null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001031 // When drawing the drag outline, it did not account for margin offsets
1032 // added by the view's parent.
1033 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1034 left += lp.leftMargin;
1035 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001036
Adam Cohen99e8b402011-03-25 19:23:43 -07001037 // Offsets due to the size difference between the View and the dragOutline.
1038 // There is a size difference to account for the outer blur, which may lie
1039 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001040 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1041 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001042 } else {
1043 // Center the drag outline in the cell
Winson Chung4b576be2011-04-27 17:40:20 -07001044 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1045 - dragOutline.getWidth()) / 2;
1046 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1047 - dragOutline.getHeight()) / 2;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001048 }
Winson Chung150fbab2010-09-29 17:14:26 -07001049
Joe Onorato4be866d2010-10-10 11:26:02 -07001050 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001051 mDragOutlineAnims[oldIndex].animateOut();
1052 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001053
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001054 mDragOutlines[mDragOutlineCurrent].set(left, top);
1055 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1056 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001057 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001058
1059 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1060 if (mCrosshairsDrawable != null) {
1061 invalidate();
1062 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001063 }
1064
Adam Cohene0310962011-04-18 16:15:31 -07001065 public void clearDragOutlines() {
1066 final int oldIndex = mDragOutlineCurrent;
1067 mDragOutlineAnims[oldIndex].animateOut();
1068 mDragCell[0] = -1;
1069 mDragCell[1] = -1;
1070 }
1071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001073 * Find a vacant area that will fit the given bounds nearest the requested
1074 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001075 *
Romain Guy51afc022009-05-04 18:03:43 -07001076 * @param pixelX The X location at which you want to search for a vacant area.
1077 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001078 * @param spanX Horizontal span of the object.
1079 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001080 * @param result Array in which to place the result, or null (in which case a new array will
1081 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001082 * @return The X, Y cell of a vacant area that can contain this object,
1083 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001085 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001086 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1087 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001088 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001089
Michael Jurka6a1435d2010-09-27 17:35:12 -07001090 /**
1091 * Find a vacant area that will fit the given bounds nearest the requested
1092 * cell location. Uses Euclidean distance to score multiple vacant areas.
1093 *
1094 * @param pixelX The X location at which you want to search for a vacant area.
1095 * @param pixelY The Y location at which you want to search for a vacant area.
1096 * @param spanX Horizontal span of the object.
1097 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001098 * @param ignoreOccupied If true, the result can be an occupied cell
1099 * @param result Array in which to place the result, or null (in which case a new array will
1100 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001101 * @return The X, Y cell of a vacant area that can contain this object,
1102 * nearest the requested location.
1103 */
Adam Cohendf035382011-04-11 17:22:04 -07001104 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1105 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001106 // mark space take by ignoreView as available (method checks if ignoreView is null)
1107 markCellsAsUnoccupiedForView(ignoreView);
1108
Adam Cohene3e27a82011-04-15 12:07:39 -07001109 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1110 // to the center of the item, but we are searching based on the top-left cell, so
1111 // we translate the point over to correspond to the top-left.
1112 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1113 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1114
Jeff Sharkey70864282009-04-07 21:08:40 -07001115 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001116 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001117 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001118
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001119 final int countX = mCountX;
1120 final int countY = mCountY;
1121 final boolean[][] occupied = mOccupied;
1122
Winson Chungbbc60d82010-11-11 16:34:41 -08001123 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001124 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001125 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001126 if (ignoreOccupied) {
1127 for (int i = 0; i < spanX; i++) {
1128 for (int j = 0; j < spanY; j++) {
1129 if (occupied[x + i][y + j]) {
1130 // small optimization: we can skip to after the column we
1131 // just found an occupied cell
1132 x += i;
1133 continue inner;
1134 }
Michael Jurkac28de512010-08-13 11:27:44 -07001135 }
1136 }
1137 }
Winson Chung0be025d2011-05-23 17:45:09 -07001138 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001139 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140
Michael Jurkac28de512010-08-13 11:27:44 -07001141 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1142 + Math.pow(cellXY[1] - pixelY, 2));
1143 if (distance <= bestDistance) {
1144 bestDistance = distance;
1145 bestXY[0] = x;
1146 bestXY[1] = y;
1147 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 }
1149 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001150 // re-mark space taken by ignoreView as occupied
1151 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152
Adam Cohenc0dcf592011-06-01 15:30:43 -07001153 // Return -1, -1 if no suitable location found
1154 if (bestDistance == Double.MAX_VALUE) {
1155 bestXY[0] = -1;
1156 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001157 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001158 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001160
Adam Cohendf035382011-04-11 17:22:04 -07001161 /**
1162 * Find a vacant area that will fit the given bounds nearest the requested
1163 * cell location. Uses Euclidean distance to score multiple vacant areas.
1164 *
1165 * @param pixelX The X location at which you want to search for a vacant area.
1166 * @param pixelY The Y location at which you want to search for a vacant area.
1167 * @param spanX Horizontal span of the object.
1168 * @param spanY Vertical span of the object.
1169 * @param ignoreView Considers space occupied by this view as unoccupied
1170 * @param result Previously returned value to possibly recycle.
1171 * @return The X, Y cell of a vacant area that can contain this object,
1172 * nearest the requested location.
1173 */
1174 int[] findNearestVacantArea(
1175 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1176 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1177 }
1178
1179 /**
1180 * Find a starting cell position that will fit the given bounds nearest the requested
1181 * cell location. Uses Euclidean distance to score multiple vacant areas.
1182 *
1183 * @param pixelX The X location at which you want to search for a vacant area.
1184 * @param pixelY The Y location at which you want to search for a vacant area.
1185 * @param spanX Horizontal span of the object.
1186 * @param spanY Vertical span of the object.
1187 * @param ignoreView Considers space occupied by this view as unoccupied
1188 * @param result Previously returned value to possibly recycle.
1189 * @return The X, Y cell of a vacant area that can contain this object,
1190 * nearest the requested location.
1191 */
1192 int[] findNearestArea(
1193 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1194 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1195 }
1196
Michael Jurka0280c3b2010-09-17 15:00:07 -07001197 boolean existsEmptyCell() {
1198 return findCellForSpan(null, 1, 1);
1199 }
1200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001202 * Finds the upper-left coordinate of the first rectangle in the grid that can
1203 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1204 * then this method will only return coordinates for rectangles that contain the cell
1205 * (intersectX, intersectY)
1206 *
1207 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1208 * can be found.
1209 * @param spanX The horizontal span of the cell we want to find.
1210 * @param spanY The vertical span of the cell we want to find.
1211 *
1212 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001213 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001214 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1215 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1216 }
1217
1218 /**
1219 * Like above, but ignores any cells occupied by the item "ignoreView"
1220 *
1221 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1222 * can be found.
1223 * @param spanX The horizontal span of the cell we want to find.
1224 * @param spanY The vertical span of the cell we want to find.
1225 * @param ignoreView The home screen item we should treat as not occupying any space
1226 * @return
1227 */
1228 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1229 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1230 }
1231
1232 /**
1233 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1234 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1235 *
1236 * @param spanX The horizontal span of the cell we want to find.
1237 * @param spanY The vertical span of the cell we want to find.
1238 * @param ignoreView The home screen item we should treat as not occupying any space
1239 * @param intersectX The X coordinate of the cell that we should try to overlap
1240 * @param intersectX The Y coordinate of the cell that we should try to overlap
1241 *
1242 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1243 */
1244 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1245 int intersectX, int intersectY) {
1246 return findCellForSpanThatIntersectsIgnoring(
1247 cellXY, spanX, spanY, intersectX, intersectY, null);
1248 }
1249
1250 /**
1251 * The superset of the above two methods
1252 */
1253 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1254 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001255 // mark space take by ignoreView as available (method checks if ignoreView is null)
1256 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001257
Michael Jurka28750fb2010-09-24 17:43:49 -07001258 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001259 while (true) {
1260 int startX = 0;
1261 if (intersectX >= 0) {
1262 startX = Math.max(startX, intersectX - (spanX - 1));
1263 }
1264 int endX = mCountX - (spanX - 1);
1265 if (intersectX >= 0) {
1266 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1267 }
1268 int startY = 0;
1269 if (intersectY >= 0) {
1270 startY = Math.max(startY, intersectY - (spanY - 1));
1271 }
1272 int endY = mCountY - (spanY - 1);
1273 if (intersectY >= 0) {
1274 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1275 }
1276
Winson Chungbbc60d82010-11-11 16:34:41 -08001277 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001278 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001279 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001280 for (int i = 0; i < spanX; i++) {
1281 for (int j = 0; j < spanY; j++) {
1282 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001283 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001284 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001285 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001286 continue inner;
1287 }
1288 }
1289 }
1290 if (cellXY != null) {
1291 cellXY[0] = x;
1292 cellXY[1] = y;
1293 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001294 foundCell = true;
1295 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001296 }
1297 }
1298 if (intersectX == -1 && intersectY == -1) {
1299 break;
1300 } else {
1301 // if we failed to find anything, try again but without any requirements of
1302 // intersecting
1303 intersectX = -1;
1304 intersectY = -1;
1305 continue;
1306 }
1307 }
1308
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001309 // re-mark space taken by ignoreView as occupied
1310 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001311 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001312 }
1313
1314 /**
1315 * Called when drag has left this CellLayout or has been completed (successfully or not)
1316 */
1317 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001318 // This can actually be called when we aren't in a drag, e.g. when adding a new
1319 // item to this layout via the customize drawer.
1320 // Guard against that case.
1321 if (mDragging) {
1322 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001323
Joe Onorato4be866d2010-10-10 11:26:02 -07001324 // Fade out the drag indicators
1325 if (mCrosshairsAnimator != null) {
1326 mCrosshairsAnimator.animateOut();
1327 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001328 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001329
1330 // Invalidate the drag data
1331 mDragCell[0] = -1;
1332 mDragCell[1] = -1;
1333 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1334 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1335
Michael Jurka33945b22010-12-21 18:19:38 -08001336 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001337 }
1338
1339 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001340 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001341 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001342 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 *
1344 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001346 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001347 if (child != null) {
1348 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001349 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001350 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001351 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001352 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001353 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001354 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
1356
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 /**
1358 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001359 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 * @param child The child that is being dragged
1361 */
1362 void onDragChild(View child) {
1363 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1364 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001365 }
1366
1367 /**
1368 * A drag event has begun over this layout.
1369 * It may have begun over this layout (in which case onDragChild is called first),
1370 * or it may have begun on another layout.
1371 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001372 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001373 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001374 // Fade in the drag indicators
1375 if (mCrosshairsAnimator != null) {
1376 mCrosshairsAnimator.animateIn();
1377 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001378 }
1379 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001381
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001384 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 * @param cellX X coordinate of upper left corner expressed as a cell position
1386 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001387 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001389 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001391 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 final int cellWidth = mCellWidth;
1393 final int cellHeight = mCellHeight;
1394 final int widthGap = mWidthGap;
1395 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001396
1397 final int hStartPadding = getLeftPadding();
1398 final int vStartPadding = getTopPadding();
1399
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1401 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1402
1403 int x = hStartPadding + cellX * (cellWidth + widthGap);
1404 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001405
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001406 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001410 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001412 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 * @param width Width in pixels
1414 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001415 * @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 -08001416 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001417 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001418 return rectToCell(getResources(), width, height, result);
1419 }
1420
1421 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 // Always assume we're working with the smallest span to make sure we
1423 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001424 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1425 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001427
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 // Always round up to next largest cell
1429 int spanX = (width + smallerSize) / smallerSize;
1430 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001431
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001432 if (result == null) {
1433 return new int[] { spanX, spanY };
1434 }
1435 result[0] = spanX;
1436 result[1] = spanY;
1437 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
1439
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001440 public int[] cellSpansToSize(int hSpans, int vSpans) {
1441 int[] size = new int[2];
1442 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1443 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1444 return size;
1445 }
1446
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001448 * Calculate the grid spans needed to fit given item
1449 */
1450 public void calculateSpans(ItemInfo info) {
1451 final int minWidth;
1452 final int minHeight;
1453
1454 if (info instanceof LauncherAppWidgetInfo) {
1455 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1456 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1457 } else if (info instanceof PendingAddWidgetInfo) {
1458 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1459 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1460 } else {
1461 // It's not a widget, so it must be 1x1
1462 info.spanX = info.spanY = 1;
1463 return;
1464 }
1465 int[] spans = rectToCell(minWidth, minHeight, null);
1466 info.spanX = spans[0];
1467 info.spanY = spans[1];
1468 }
1469
1470 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 * Find the first vacant cell, if there is one.
1472 *
1473 * @param vacant Holds the x and y coordinate of the vacant cell
1474 * @param spanX Horizontal cell span.
1475 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001476 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 * @return True if a vacant cell was found
1478 */
1479 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480
Michael Jurka0280c3b2010-09-17 15:00:07 -07001481 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 }
1483
1484 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1485 int xCount, int yCount, boolean[][] occupied) {
1486
Adam Cohen2801caf2011-05-13 20:57:39 -07001487 for (int y = 0; y < yCount; y++) {
1488 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 boolean available = !occupied[x][y];
1490out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1491 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1492 available = available && !occupied[i][j];
1493 if (!available) break out;
1494 }
1495 }
1496
1497 if (available) {
1498 vacant[0] = x;
1499 vacant[1] = y;
1500 return true;
1501 }
1502 }
1503 }
1504
1505 return false;
1506 }
1507
Michael Jurka0280c3b2010-09-17 15:00:07 -07001508 private void clearOccupiedCells() {
1509 for (int x = 0; x < mCountX; x++) {
1510 for (int y = 0; y < mCountY; y++) {
1511 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 }
1513 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001514 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515
Adam Cohen1b607ed2011-03-03 17:26:50 -08001516 /**
1517 * Given a view, determines how much that view can be expanded in all directions, in terms of
1518 * whether or not there are other items occupying adjacent cells. Used by the
1519 * AppWidgetResizeFrame to determine how the widget can be resized.
1520 */
Adam Cohend4844c32011-02-18 19:25:06 -08001521 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001522 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001523 boolean flag;
1524
Adam Cohen1b607ed2011-03-03 17:26:50 -08001525 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001526 for (int x = lp.cellX - 1; x >= 0; x--) {
1527 flag = false;
1528 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1529 if (mOccupied[x][y]) flag = true;
1530 }
1531 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001532 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001533 }
1534
Adam Cohen1b607ed2011-03-03 17:26:50 -08001535 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001536 for (int y = lp.cellY - 1; y >= 0; y--) {
1537 flag = false;
1538 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1539 if (mOccupied[x][y]) flag = true;
1540 }
1541 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001542 expandability[AppWidgetResizeFrame.TOP]++;
1543 }
Adam Cohend4844c32011-02-18 19:25:06 -08001544
Adam Cohen1b607ed2011-03-03 17:26:50 -08001545 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001546 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1547 flag = false;
1548 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1549 if (mOccupied[x][y]) flag = true;
1550 }
1551 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001552 expandability[AppWidgetResizeFrame.RIGHT]++;
1553 }
Adam Cohend4844c32011-02-18 19:25:06 -08001554
Adam Cohen1b607ed2011-03-03 17:26:50 -08001555 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001556 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1557 flag = false;
1558 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1559 if (mOccupied[x][y]) flag = true;
1560 }
1561 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001562 expandability[AppWidgetResizeFrame.BOTTOM]++;
1563 }
Adam Cohend4844c32011-02-18 19:25:06 -08001564 }
1565
Michael Jurka0280c3b2010-09-17 15:00:07 -07001566 public void onMove(View view, int newCellX, int newCellY) {
1567 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1568 markCellsAsUnoccupiedForView(view);
1569 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1570 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571
Adam Cohend4844c32011-02-18 19:25:06 -08001572 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001573 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001574 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1575 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1576 }
1577
Adam Cohend4844c32011-02-18 19:25:06 -08001578 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001579 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001580 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1581 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1582 }
1583
1584 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1585 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1586 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1587 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 }
1589 }
1590 }
1591
Adam Cohen2801caf2011-05-13 20:57:39 -07001592 public int getDesiredWidth() {
1593 return mLeftPadding + mRightPadding + (mCountX * mCellWidth) +
1594 (Math.max((mCountX - 1), 0) * mWidthGap);
1595 }
1596
1597 public int getDesiredHeight() {
1598 return mTopPadding + mBottomPadding + (mCountY * mCellHeight) +
1599 (Math.max((mCountY - 1), 0) * mHeightGap);
1600 }
1601
Michael Jurka66d72172011-04-12 16:29:25 -07001602 public boolean isOccupied(int x, int y) {
1603 if (x < mCountX && y < mCountY) {
1604 return mOccupied[x][y];
1605 } else {
1606 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1607 }
1608 }
1609
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 @Override
1611 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1612 return new CellLayout.LayoutParams(getContext(), attrs);
1613 }
1614
1615 @Override
1616 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1617 return p instanceof CellLayout.LayoutParams;
1618 }
1619
1620 @Override
1621 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1622 return new CellLayout.LayoutParams(p);
1623 }
1624
Winson Chungaafa03c2010-06-11 17:34:16 -07001625 public static class CellLayoutAnimationController extends LayoutAnimationController {
1626 public CellLayoutAnimationController(Animation animation, float delay) {
1627 super(animation, delay);
1628 }
1629
1630 @Override
1631 protected long getDelayForView(View view) {
1632 return (int) (Math.random() * 150);
1633 }
1634 }
1635
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1637 /**
1638 * Horizontal location of the item in the grid.
1639 */
1640 @ViewDebug.ExportedProperty
1641 public int cellX;
1642
1643 /**
1644 * Vertical location of the item in the grid.
1645 */
1646 @ViewDebug.ExportedProperty
1647 public int cellY;
1648
1649 /**
1650 * Number of cells spanned horizontally by the item.
1651 */
1652 @ViewDebug.ExportedProperty
1653 public int cellHSpan;
1654
1655 /**
1656 * Number of cells spanned vertically by the item.
1657 */
1658 @ViewDebug.ExportedProperty
1659 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001660
Adam Cohen1b607ed2011-03-03 17:26:50 -08001661 /**
1662 * Indicates whether the item will set its x, y, width and height parameters freely,
1663 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1664 */
Adam Cohend4844c32011-02-18 19:25:06 -08001665 public boolean isLockedToGrid = true;
1666
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 /**
1668 * Is this item currently being dragged
1669 */
1670 public boolean isDragging;
1671
1672 // X coordinate of the view in the layout.
1673 @ViewDebug.ExportedProperty
1674 int x;
1675 // Y coordinate of the view in the layout.
1676 @ViewDebug.ExportedProperty
1677 int y;
1678
Patrick Dubroyce34a972010-10-19 10:34:32 -07001679 /**
1680 * The old X coordinate of this item, relative to its current parent.
1681 * Used to animate the item into its new position.
1682 */
1683 int oldX;
1684
1685 /**
1686 * The old Y coordinate of this item, relative to its current parent.
1687 * Used to animate the item into its new position.
1688 */
1689 int oldY;
1690
Romain Guy84f296c2009-11-04 15:00:44 -08001691 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001692
Michael Jurkad3ef3062010-11-23 16:23:58 -08001693 boolean animateDrop;
1694
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 public LayoutParams(Context c, AttributeSet attrs) {
1696 super(c, attrs);
1697 cellHSpan = 1;
1698 cellVSpan = 1;
1699 }
1700
1701 public LayoutParams(ViewGroup.LayoutParams source) {
1702 super(source);
1703 cellHSpan = 1;
1704 cellVSpan = 1;
1705 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001706
1707 public LayoutParams(LayoutParams source) {
1708 super(source);
1709 this.cellX = source.cellX;
1710 this.cellY = source.cellY;
1711 this.cellHSpan = source.cellHSpan;
1712 this.cellVSpan = source.cellVSpan;
1713 }
1714
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001716 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 this.cellX = cellX;
1718 this.cellY = cellY;
1719 this.cellHSpan = cellHSpan;
1720 this.cellVSpan = cellVSpan;
1721 }
1722
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001723 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001724 if (isLockedToGrid) {
1725 final int myCellHSpan = cellHSpan;
1726 final int myCellVSpan = cellVSpan;
1727 final int myCellX = cellX;
1728 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001729
Adam Cohend4844c32011-02-18 19:25:06 -08001730 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1731 leftMargin - rightMargin;
1732 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1733 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001734 x = myCellX * (cellWidth + widthGap) + leftMargin;
1735 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001736 }
1737 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001738
Winson Chungaafa03c2010-06-11 17:34:16 -07001739 public String toString() {
1740 return "(" + this.cellX + ", " + this.cellY + ")";
1741 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001742
1743 public void setWidth(int width) {
1744 this.width = width;
1745 }
1746
1747 public int getWidth() {
1748 return width;
1749 }
1750
1751 public void setHeight(int height) {
1752 this.height = height;
1753 }
1754
1755 public int getHeight() {
1756 return height;
1757 }
1758
1759 public void setX(int x) {
1760 this.x = x;
1761 }
1762
1763 public int getX() {
1764 return x;
1765 }
1766
1767 public void setY(int y) {
1768 this.y = y;
1769 }
1770
1771 public int getY() {
1772 return y;
1773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 }
1775
Michael Jurka0280c3b2010-09-17 15:00:07 -07001776 // This class stores info for two purposes:
1777 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1778 // its spanX, spanY, and the screen it is on
1779 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1780 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1781 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001782 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001784 int cellX = -1;
1785 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 int spanX;
1787 int spanY;
1788 int screen;
1789 boolean valid;
1790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 @Override
1792 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001793 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1794 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 }
1796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797}