blob: f189cd58e9d395415a32a01295f492e910c928a0 [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.
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.
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
Adam Cohen2801caf2011-05-13 20:57:39 -0700324 public void enableHardwareLayers() {
325 mChildren.enableHardwareLayers();
326 }
327
328 public void setGridSize(int x, int y) {
329 mCountX = x;
330 mCountY = y;
331 mOccupied = new boolean[mCountX][mCountY];
332 }
333
Patrick Dubroy96864c32011-03-10 17:17:23 -0800334 private void invalidateBubbleTextView(BubbleTextView icon) {
335 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700336 invalidate(icon.getLeft() + getLeftPadding() - padding,
337 icon.getTop() + getTopPadding() - padding,
338 icon.getRight() + getLeftPadding() + padding,
339 icon.getBottom() + getTopPadding() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800340 }
341
342 void setPressedOrFocusedIcon(BubbleTextView icon) {
343 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
344 // requires an expanded clip rect (due to the glow's blur radius)
345 BubbleTextView oldIcon = mPressedOrFocusedIcon;
346 mPressedOrFocusedIcon = icon;
347 if (oldIcon != null) {
348 invalidateBubbleTextView(oldIcon);
349 }
350 if (mPressedOrFocusedIcon != null) {
351 invalidateBubbleTextView(mPressedOrFocusedIcon);
352 }
353 }
354
Winson Chung6e314082011-01-27 16:46:51 -0800355 public CellLayoutChildren getChildrenLayout() {
356 if (getChildCount() > 0) {
357 return (CellLayoutChildren) getChildAt(0);
358 }
359 return null;
360 }
361
Michael Jurka33945b22010-12-21 18:19:38 -0800362 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
363 if (mIsDefaultDropTarget != isDefaultDropTarget) {
364 mIsDefaultDropTarget = isDefaultDropTarget;
365 invalidate();
366 }
367 }
368
Michael Jurka33945b22010-12-21 18:19:38 -0800369 void setIsDragOccuring(boolean isDragOccuring) {
370 if (mIsDragOccuring != isDragOccuring) {
371 mIsDragOccuring = isDragOccuring;
372 invalidate();
373 }
374 }
375
376 void setIsDragOverlapping(boolean isDragOverlapping) {
377 if (mIsDragOverlapping != isDragOverlapping) {
378 mIsDragOverlapping = isDragOverlapping;
379 invalidate();
380 }
381 }
382
383 boolean getIsDragOverlapping() {
384 return mIsDragOverlapping;
385 }
386
387 private void updateGlowRect() {
388 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700389 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
390 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800391 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700392 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
393 invalidate();
394 }
395
396 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800397 if (scaleFactor != mGlowBackgroundScale) {
398 mGlowBackgroundScale = scaleFactor;
399 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800400 if (getParent() != null) {
401 ((View) getParent()).invalidate();
402 }
Michael Jurka18014792010-10-14 09:01:34 -0700403 }
404 }
405
406 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800407 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700408 }
409
410 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800411 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700412 }
413
414 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800415 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700416 invalidate();
417 }
418
419 void animateDrop() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700420 if (LauncherApplication.isScreenLarge()) {
Michael Jurka18014792010-10-14 09:01:34 -0700421 Resources res = getResources();
422 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
423 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
424 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
425 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
426 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
427 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
428
429 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
430 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
431
432 AnimatorSet bouncer = new AnimatorSet();
433 bouncer.play(scaleUp).before(scaleDown);
434 bouncer.play(scaleUp).with(alphaFadeOut);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800435 bouncer.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700436 @Override
Michael Jurka18014792010-10-14 09:01:34 -0700437 public void onAnimationStart(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800438 setIsDragOverlapping(true);
Michael Jurka18014792010-10-14 09:01:34 -0700439 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700440 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800441 public void onAnimationEnd(Animator animation) {
Michael Jurka33945b22010-12-21 18:19:38 -0800442 setIsDragOverlapping(false);
Michael Jurka18014792010-10-14 09:01:34 -0700443 setHoverScale(1.0f);
444 setHoverAlpha(1.0f);
445 }
446 });
447 bouncer.start();
448 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 }
450
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700451 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700452 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700453 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
454 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
455 // When we're small, we are either drawn normally or in the "accepts drops" state (during
456 // a drag). However, we also drag the mini hover background *over* one of those two
457 // backgrounds
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700458 if (LauncherApplication.isScreenLarge() && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700459 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800460 boolean mini = getScaleX() < 0.5f;
461
462 if (mIsDragOverlapping) {
463 // In the mini case, we draw the active_glow bg *over* the active background
464 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
465 } else if (mIsDragOccuring && mAcceptsDrops) {
466 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800467 } else if (mIsDefaultDropTarget && mini) {
468 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700469 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800470 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700471 }
Michael Jurka33945b22010-12-21 18:19:38 -0800472
473 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
474 bg.setBounds(mBackgroundRect);
475 bg.draw(canvas);
476
477 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700478 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800479 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700480 // If the hover background's scale is greater than 1, we'll be drawing outside
481 // the bounds of this CellLayout. Get around that by temporarily increasing the
482 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800483 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700484 Rect clipRect = canvas.getClipBounds();
485 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
486 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
487 canvas.save(Canvas.CLIP_SAVE_FLAG);
488 canvas.clipRect(-marginX, -marginY,
489 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
490 modifiedClipRect = true;
491 }
492
Michael Jurka33945b22010-12-21 18:19:38 -0800493 mActiveGlowBackgroundMini.setAlpha(
494 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
495 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
496 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700497 if (modifiedClipRect) {
498 canvas.restore();
499 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700500 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700501 }
Romain Guya6abce82009-11-10 02:54:41 -0800502
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700503 if (mCrosshairsVisibility > 0.0f) {
504 final int countX = mCountX;
505 final int countY = mCountY;
506
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700507 final float MAX_ALPHA = 0.4f;
508 final int MAX_VISIBLE_DISTANCE = 600;
509 final float DISTANCE_MULTIPLIER = 0.002f;
510
511 final Drawable d = mCrosshairsDrawable;
512 final int width = d.getIntrinsicWidth();
513 final int height = d.getIntrinsicHeight();
514
515 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
516 for (int col = 0; col <= countX; col++) {
517 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
518 for (int row = 0; row <= countY; row++) {
519 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
520 float dist = mTmpPointF.length();
521 // Crosshairs further from the drag point are more faint
522 float alpha = Math.min(MAX_ALPHA,
523 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
524 if (alpha > 0.0f) {
525 d.setBounds(x, y, x + width, y + height);
526 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
527 d.draw(canvas);
528 }
529 y += mCellHeight + mHeightGap;
530 }
531 x += mCellWidth + mWidthGap;
532 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700533 }
Winson Chung150fbab2010-09-29 17:14:26 -0700534
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700535 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700536 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700537 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700538 if (alpha > 0) {
539 final Point p = mDragOutlines[i];
540 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700541 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700542 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700543 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700544 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800545
546 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
547 // requires an expanded clip rect (due to the glow's blur radius)
548 if (mPressedOrFocusedIcon != null) {
549 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
550 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
551 if (b != null) {
552 canvas.drawBitmap(b,
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700553 mPressedOrFocusedIcon.getLeft() + getLeftPadding() - padding,
554 mPressedOrFocusedIcon.getTop() + getTopPadding() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800555 null);
556 }
557 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700558 }
559
560 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700561 public void cancelLongPress() {
562 super.cancelLongPress();
563
564 // Cancel long press for all children
565 final int count = getChildCount();
566 for (int i = 0; i < count; i++) {
567 final View child = getChildAt(i);
568 child.cancelLongPress();
569 }
570 }
571
Michael Jurkadee05892010-07-27 10:01:56 -0700572 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
573 mInterceptTouchListener = listener;
574 }
575
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700577 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 }
579
580 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700581 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 }
583
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700584 public boolean addViewToCellLayout(
585 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700586 final LayoutParams lp = params;
587
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 // Generate an id for each view, this assumes we have at most 256x256 cells
589 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700590 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700591 // If the horizontal or vertical span is set to -1, it is taken to
592 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700593 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
594 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595
Winson Chungaafa03c2010-06-11 17:34:16 -0700596 child.setId(childId);
597
Michael Jurka8c920dd2011-01-20 14:16:56 -0800598 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700599
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700600 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700601
Winson Chungaafa03c2010-06-11 17:34:16 -0700602 return true;
603 }
604 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700606
Michael Jurkabea15192010-11-17 12:33:46 -0800607 public void setAcceptsDrops(boolean acceptsDrops) {
608 if (mAcceptsDrops != acceptsDrops) {
609 mAcceptsDrops = acceptsDrops;
610 invalidate();
611 }
612 }
613
Michael Jurka3e7c7632010-10-02 16:01:03 -0700614 public boolean getAcceptsDrops() {
615 return mAcceptsDrops;
616 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617
618 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700619 public void removeAllViews() {
620 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800621 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700622 }
623
624 @Override
625 public void removeAllViewsInLayout() {
626 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800627 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700628 }
629
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700630 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800631 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700632 }
633
Michael Jurka0280c3b2010-09-17 15:00:07 -0700634 @Override
635 public void removeView(View view) {
636 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800637 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700638 }
639
640 @Override
641 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800642 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
643 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644 }
645
646 @Override
647 public void removeViewInLayout(View view) {
648 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800649 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700650 }
651
652 @Override
653 public void removeViews(int start, int count) {
654 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800655 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800657 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700658 }
659
660 @Override
661 public void removeViewsInLayout(int start, int count) {
662 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800663 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800665 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 }
667
Michael Jurka8c920dd2011-01-20 14:16:56 -0800668 public void drawChildren(Canvas canvas) {
669 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
671
Michael Jurkaabded662011-03-04 12:06:57 -0800672 void buildChildrenLayer() {
673 mChildren.buildLayer();
674 }
675
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 @Override
677 protected void onAttachedToWindow() {
678 super.onAttachedToWindow();
679 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
680 }
681
Michael Jurkaaf442092010-06-10 17:01:57 -0700682 public void setTagToCellInfoForPoint(int touchX, int touchY) {
683 final CellInfo cellInfo = mCellInfo;
684 final Rect frame = mRect;
685 final int x = touchX + mScrollX;
686 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800687 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700688
689 boolean found = false;
690 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800691 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800692 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700693
Adam Cohen1b607ed2011-03-03 17:26:50 -0800694 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
695 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700696 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700697
698 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
699 // offset that by this CellLayout's padding to test an (x,y) point that is relative
700 // to this view.
701 final int tmpXY[] = mTmpXY;
702 child.getLocationOnScreen(tmpXY);
703 frame.offset(mLeftPadding, mTopPadding);
704
Michael Jurkaaf442092010-06-10 17:01:57 -0700705 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700706 cellInfo.cell = child;
707 cellInfo.cellX = lp.cellX;
708 cellInfo.cellY = lp.cellY;
709 cellInfo.spanX = lp.cellHSpan;
710 cellInfo.spanY = lp.cellVSpan;
711 cellInfo.valid = true;
712 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700713 break;
714 }
715 }
716 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700717
Michael Jurkaaf442092010-06-10 17:01:57 -0700718 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700719 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700720 pointToCellExact(x, y, cellXY);
721
Michael Jurkaaf442092010-06-10 17:01:57 -0700722 cellInfo.cell = null;
723 cellInfo.cellX = cellXY[0];
724 cellInfo.cellY = cellXY[1];
725 cellInfo.spanX = 1;
726 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700727 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
728 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700729 }
730 setTag(cellInfo);
731 }
732
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 @Override
734 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700735 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
736 return true;
737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 final int action = ev.getAction();
739 final CellInfo cellInfo = mCellInfo;
740
741 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700742 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 } else if (action == MotionEvent.ACTION_UP) {
744 cellInfo.cell = null;
745 cellInfo.cellX = -1;
746 cellInfo.cellY = -1;
747 cellInfo.spanX = 0;
748 cellInfo.spanY = 0;
749 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 setTag(cellInfo);
751 }
752
753 return false;
754 }
755
756 @Override
757 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700758 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 }
760
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700761 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700762 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 * @param x X coordinate of the point
764 * @param y Y coordinate of the point
765 * @param result Array of 2 ints to hold the x and y coordinate of the cell
766 */
767 void pointToCellExact(int x, int y, 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] = (x - hStartPadding) / (mCellWidth + mWidthGap);
772 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
773
Adam Cohend22015c2010-07-26 22:02:18 -0700774 final int xAxis = mCountX;
775 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776
777 if (result[0] < 0) result[0] = 0;
778 if (result[0] >= xAxis) result[0] = xAxis - 1;
779 if (result[1] < 0) result[1] = 0;
780 if (result[1] >= yAxis) result[1] = yAxis - 1;
781 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 /**
784 * Given a point, return the cell that most closely encloses that point
785 * @param x X coordinate of the point
786 * @param y Y coordinate of the point
787 * @param result Array of 2 ints to hold the x and y coordinate of the cell
788 */
789 void pointToCellRounded(int x, int y, int[] result) {
790 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
791 }
792
793 /**
794 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700795 *
796 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700798 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 * @param result Array of 2 ints to hold the x and y coordinate of the point
800 */
801 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700802 final int hStartPadding = getLeftPadding();
803 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804
805 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
806 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
807 }
808
Adam Cohene3e27a82011-04-15 12:07:39 -0700809 /**
810 * Given a cell coordinate, return the point that represents the upper left corner of that cell
811 *
812 * @param cellX X coordinate of the cell
813 * @param cellY Y coordinate of the cell
814 *
815 * @param result Array of 2 ints to hold the x and y coordinate of the point
816 */
817 void cellToCenterPoint(int cellX, int cellY, int[] result) {
818 final int hStartPadding = getLeftPadding();
819 final int vStartPadding = getTopPadding();
820
821 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
822 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
823 }
824
Romain Guy84f296c2009-11-04 15:00:44 -0800825 int getCellWidth() {
826 return mCellWidth;
827 }
828
829 int getCellHeight() {
830 return mCellHeight;
831 }
832
Adam Cohend4844c32011-02-18 19:25:06 -0800833 int getWidthGap() {
834 return mWidthGap;
835 }
836
837 int getHeightGap() {
838 return mHeightGap;
839 }
840
Romain Guy1a304a12009-11-10 00:02:32 -0800841 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700842 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800843 }
844
845 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700846 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800847 }
848
849 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700850 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800851 }
852
853 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700854 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800855 }
856
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700857 Rect getContentRect(Rect r) {
858 if (r == null) {
859 r = new Rect();
860 }
861 int left = getPaddingLeft();
862 int top = getPaddingTop();
863 int right = left + getWidth() - mLeftPadding - mRightPadding;
864 int bottom = top + getHeight() - mTopPadding - mBottomPadding;
865 r.set(left, top, right, bottom);
866 return r;
867 }
868
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 @Override
870 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
871 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700872
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700874 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
875
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
877 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700878
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
880 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
881 }
882
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 final int cellWidth = mCellWidth;
884 final int cellHeight = mCellHeight;
885
Adam Cohend22015c2010-07-26 22:02:18 -0700886 int numWidthGaps = mCountX - 1;
887 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888
Winson Chungece7f5b2010-10-22 14:54:12 -0700889 if (mWidthGap < 0 || mHeightGap < 0) {
890 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700891 mHeightGap = numHeightGaps > 0 ? vSpaceLeft / numHeightGaps : 0;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892
Winson Chungece7f5b2010-10-22 14:54:12 -0700893 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohen2801caf2011-05-13 20:57:39 -0700894 mWidthGap = numWidthGaps > 0 ? hSpaceLeft / numWidthGaps : 0;
Winson Chungaafa03c2010-06-11 17:34:16 -0700895
Winson Chungece7f5b2010-10-22 14:54:12 -0700896 // center it around the min gaps
897 int minGap = Math.min(mWidthGap, mHeightGap);
898 mWidthGap = mHeightGap = minGap;
899 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700900
Michael Jurka8c920dd2011-01-20 14:16:56 -0800901 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
902 int newWidth = widthSpecSize;
903 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700904 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800905 newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700906 ((mCountX - 1) * mWidthGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800907 newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700908 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700909 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700910 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800911
912 int count = getChildCount();
913 for (int i = 0; i < count; i++) {
914 View child = getChildAt(i);
Adam Cohen2801caf2011-05-13 20:57:39 -0700915 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mLeftPadding -
916 mRightPadding, MeasureSpec.EXACTLY);
917 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mTopPadding -
918 mBottomPadding, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800919 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
920 }
921 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 }
923
924 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700925 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800928 View child = getChildAt(i);
Adam Cohenbb60e2e2011-04-26 16:48:26 -0700929 child.layout(mLeftPadding, mTopPadding, r - l - mRightPadding , b - t - mBottomPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 }
931 }
932
933 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700934 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
935 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700936 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800937 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700938 }
939
940 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800942 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 }
944
945 @Override
946 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800947 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 }
949
Michael Jurka5f1c5092010-09-03 14:15:02 -0700950 public float getBackgroundAlpha() {
951 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700952 }
953
Michael Jurka742574b2011-02-02 23:51:01 -0800954 public void setFastBackgroundAlpha(float alpha) {
955 mBackgroundAlpha = alpha;
956 }
957
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700958 public void setBackgroundAlphaMultiplier(float multiplier) {
959 mBackgroundAlphaMultiplier = multiplier;
960 }
961
Adam Cohenddb82192010-11-10 16:32:54 -0800962 public float getBackgroundAlphaMultiplier() {
963 return mBackgroundAlphaMultiplier;
964 }
965
Michael Jurka5f1c5092010-09-03 14:15:02 -0700966 public void setBackgroundAlpha(float alpha) {
967 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700968 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700969 }
970
Michael Jurka5f1c5092010-09-03 14:15:02 -0700971 // Need to return true to let the view system know we know how to handle alpha-- this is
972 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
973 // versions
974 @Override
975 protected boolean onSetAlpha(int alpha) {
976 return true;
977 }
978
979 public void setAlpha(float alpha) {
980 setChildrenAlpha(alpha);
981 super.setAlpha(alpha);
982 }
983
Michael Jurka742574b2011-02-02 23:51:01 -0800984 public void setFastAlpha(float alpha) {
985 setFastChildrenAlpha(alpha);
986 super.setFastAlpha(alpha);
987 }
988
Michael Jurkadee05892010-07-27 10:01:56 -0700989 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700990 final int childCount = getChildCount();
991 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700992 getChildAt(i).setAlpha(alpha);
993 }
994 }
995
Michael Jurka742574b2011-02-02 23:51:01 -0800996 private void setFastChildrenAlpha(float alpha) {
997 final int childCount = getChildCount();
998 for (int i = 0; i < childCount; i++) {
999 getChildAt(i).setFastAlpha(alpha);
1000 }
1001 }
1002
Patrick Dubroy440c3602010-07-13 17:50:32 -07001003 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001004 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001005 }
1006
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001007 /**
1008 * Estimate where the top left cell of the dragged item will land if it is dropped.
1009 *
1010 * @param originX The X value of the top left corner of the item
1011 * @param originY The Y value of the top left corner of the item
1012 * @param spanX The number of horizontal cells that the item spans
1013 * @param spanY The number of vertical cells that the item spans
1014 * @param result The estimated drop cell X and Y.
1015 */
1016 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001017 final int countX = mCountX;
1018 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001019
Michael Jurkaa63c4522010-08-19 13:52:27 -07001020 // pointToCellRounded takes the top left of a cell but will pad that with
1021 // cellWidth/2 and cellHeight/2 when finding the matching cell
1022 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001023
1024 // If the item isn't fully on this screen, snap to the edges
1025 int rightOverhang = result[0] + spanX - countX;
1026 if (rightOverhang > 0) {
1027 result[0] -= rightOverhang; // Snap to right
1028 }
1029 result[0] = Math.max(0, result[0]); // Snap to left
1030 int bottomOverhang = result[1] + spanY - countY;
1031 if (bottomOverhang > 0) {
1032 result[1] -= bottomOverhang; // Snap to bottom
1033 }
1034 result[1] = Math.max(0, result[1]); // Snap to top
1035 }
1036
Joe Onorato4be866d2010-10-10 11:26:02 -07001037 void visualizeDropLocation(
1038 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1039
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001040 final int oldDragCellX = mDragCell[0];
1041 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001042 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001043 if (v != null) {
1044 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1045 } else {
1046 mDragCenter.set(originX, originY);
1047 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001048
Adam Cohen2801caf2011-05-13 20:57:39 -07001049 if (dragOutline == null && v == null) {
1050 if (mCrosshairsDrawable != null) {
1051 invalidate();
1052 }
1053 return;
1054 }
1055
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001056 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001057 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001058 final int[] topLeft = mTmpPoint;
1059 cellToPoint(nearest[0], nearest[1], topLeft);
1060
Joe Onorato4be866d2010-10-10 11:26:02 -07001061 int left = topLeft[0];
1062 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001063
Winson Chunga9abd0e2010-10-27 17:18:37 -07001064 if (v != null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001065 // When drawing the drag outline, it did not account for margin offsets
1066 // added by the view's parent.
1067 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1068 left += lp.leftMargin;
1069 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001070
Adam Cohen99e8b402011-03-25 19:23:43 -07001071 // Offsets due to the size difference between the View and the dragOutline.
1072 // There is a size difference to account for the outer blur, which may lie
1073 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001074 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1075 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001076 } else {
1077 // Center the drag outline in the cell
Winson Chung4b576be2011-04-27 17:40:20 -07001078 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1079 - dragOutline.getWidth()) / 2;
1080 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1081 - dragOutline.getHeight()) / 2;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001082 }
Winson Chung150fbab2010-09-29 17:14:26 -07001083
Joe Onorato4be866d2010-10-10 11:26:02 -07001084 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001085 mDragOutlineAnims[oldIndex].animateOut();
1086 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001087
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001088 mDragOutlines[mDragOutlineCurrent].set(left, top);
1089 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1090 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001091 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001092
1093 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1094 if (mCrosshairsDrawable != null) {
1095 invalidate();
1096 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001097 }
1098
Adam Cohene0310962011-04-18 16:15:31 -07001099 public void clearDragOutlines() {
1100 final int oldIndex = mDragOutlineCurrent;
1101 mDragOutlineAnims[oldIndex].animateOut();
1102 mDragCell[0] = -1;
1103 mDragCell[1] = -1;
1104 }
1105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001107 * Find a vacant area that will fit the given bounds nearest the requested
1108 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001109 *
Romain Guy51afc022009-05-04 18:03:43 -07001110 * @param pixelX The X location at which you want to search for a vacant area.
1111 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001112 * @param spanX Horizontal span of the object.
1113 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001114 * @param result Array in which to place the result, or null (in which case a new array will
1115 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001116 * @return The X, Y cell of a vacant area that can contain this object,
1117 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001119 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001120 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1121 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001122 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001123
Michael Jurka6a1435d2010-09-27 17:35:12 -07001124 /**
1125 * Find a vacant area that will fit the given bounds nearest the requested
1126 * cell location. Uses Euclidean distance to score multiple vacant areas.
1127 *
1128 * @param pixelX The X location at which you want to search for a vacant area.
1129 * @param pixelY The Y location at which you want to search for a vacant area.
1130 * @param spanX Horizontal span of the object.
1131 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001132 * @param ignoreOccupied If true, the result can be an occupied cell
1133 * @param result Array in which to place the result, or null (in which case a new array will
1134 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001135 * @return The X, Y cell of a vacant area that can contain this object,
1136 * nearest the requested location.
1137 */
Adam Cohendf035382011-04-11 17:22:04 -07001138 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1139 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001140 // mark space take by ignoreView as available (method checks if ignoreView is null)
1141 markCellsAsUnoccupiedForView(ignoreView);
1142
Adam Cohene3e27a82011-04-15 12:07:39 -07001143 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1144 // to the center of the item, but we are searching based on the top-left cell, so
1145 // we translate the point over to correspond to the top-left.
1146 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1147 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1148
Jeff Sharkey70864282009-04-07 21:08:40 -07001149 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001150 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001151 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001152
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001153 final int countX = mCountX;
1154 final int countY = mCountY;
1155 final boolean[][] occupied = mOccupied;
1156
Winson Chungbbc60d82010-11-11 16:34:41 -08001157 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001158 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001159 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001160 if (ignoreOccupied) {
1161 for (int i = 0; i < spanX; i++) {
1162 for (int j = 0; j < spanY; j++) {
1163 if (occupied[x + i][y + j]) {
1164 // small optimization: we can skip to after the column we
1165 // just found an occupied cell
1166 x += i;
1167 continue inner;
1168 }
Michael Jurkac28de512010-08-13 11:27:44 -07001169 }
1170 }
1171 }
Winson Chung0be025d2011-05-23 17:45:09 -07001172 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001173 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174
Michael Jurkac28de512010-08-13 11:27:44 -07001175 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1176 + Math.pow(cellXY[1] - pixelY, 2));
1177 if (distance <= bestDistance) {
1178 bestDistance = distance;
1179 bestXY[0] = x;
1180 bestXY[1] = y;
1181 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 }
1183 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001184 // re-mark space taken by ignoreView as occupied
1185 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186
Winson Chungaafa03c2010-06-11 17:34:16 -07001187 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001188 if (bestDistance < Double.MAX_VALUE) {
1189 return bestXY;
1190 } else {
1191 return null;
1192 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001194
Adam Cohendf035382011-04-11 17:22:04 -07001195 /**
1196 * Find a vacant area that will fit the given bounds nearest the requested
1197 * cell location. Uses Euclidean distance to score multiple vacant areas.
1198 *
1199 * @param pixelX The X location at which you want to search for a vacant area.
1200 * @param pixelY The Y location at which you want to search for a vacant area.
1201 * @param spanX Horizontal span of the object.
1202 * @param spanY Vertical span of the object.
1203 * @param ignoreView Considers space occupied by this view as unoccupied
1204 * @param result Previously returned value to possibly recycle.
1205 * @return The X, Y cell of a vacant area that can contain this object,
1206 * nearest the requested location.
1207 */
1208 int[] findNearestVacantArea(
1209 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1210 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1211 }
1212
1213 /**
1214 * Find a starting cell position that will fit the given bounds nearest the requested
1215 * cell location. Uses Euclidean distance to score multiple vacant areas.
1216 *
1217 * @param pixelX The X location at which you want to search for a vacant area.
1218 * @param pixelY The Y location at which you want to search for a vacant area.
1219 * @param spanX Horizontal span of the object.
1220 * @param spanY Vertical span of the object.
1221 * @param ignoreView Considers space occupied by this view as unoccupied
1222 * @param result Previously returned value to possibly recycle.
1223 * @return The X, Y cell of a vacant area that can contain this object,
1224 * nearest the requested location.
1225 */
1226 int[] findNearestArea(
1227 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1228 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1229 }
1230
Michael Jurka0280c3b2010-09-17 15:00:07 -07001231 boolean existsEmptyCell() {
1232 return findCellForSpan(null, 1, 1);
1233 }
1234
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001236 * Finds the upper-left coordinate of the first rectangle in the grid that can
1237 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1238 * then this method will only return coordinates for rectangles that contain the cell
1239 * (intersectX, intersectY)
1240 *
1241 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1242 * can be found.
1243 * @param spanX The horizontal span of the cell we want to find.
1244 * @param spanY The vertical span of the cell we want to find.
1245 *
1246 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001247 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001248 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1249 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1250 }
1251
1252 /**
1253 * Like above, but ignores any cells occupied by the item "ignoreView"
1254 *
1255 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1256 * can be found.
1257 * @param spanX The horizontal span of the cell we want to find.
1258 * @param spanY The vertical span of the cell we want to find.
1259 * @param ignoreView The home screen item we should treat as not occupying any space
1260 * @return
1261 */
1262 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1263 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1264 }
1265
1266 /**
1267 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1268 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1269 *
1270 * @param spanX The horizontal span of the cell we want to find.
1271 * @param spanY The vertical span of the cell we want to find.
1272 * @param ignoreView The home screen item we should treat as not occupying any space
1273 * @param intersectX The X coordinate of the cell that we should try to overlap
1274 * @param intersectX The Y coordinate of the cell that we should try to overlap
1275 *
1276 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1277 */
1278 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1279 int intersectX, int intersectY) {
1280 return findCellForSpanThatIntersectsIgnoring(
1281 cellXY, spanX, spanY, intersectX, intersectY, null);
1282 }
1283
1284 /**
1285 * The superset of the above two methods
1286 */
1287 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1288 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001289 // mark space take by ignoreView as available (method checks if ignoreView is null)
1290 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001291
Michael Jurka28750fb2010-09-24 17:43:49 -07001292 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001293 while (true) {
1294 int startX = 0;
1295 if (intersectX >= 0) {
1296 startX = Math.max(startX, intersectX - (spanX - 1));
1297 }
1298 int endX = mCountX - (spanX - 1);
1299 if (intersectX >= 0) {
1300 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1301 }
1302 int startY = 0;
1303 if (intersectY >= 0) {
1304 startY = Math.max(startY, intersectY - (spanY - 1));
1305 }
1306 int endY = mCountY - (spanY - 1);
1307 if (intersectY >= 0) {
1308 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1309 }
1310
Winson Chungbbc60d82010-11-11 16:34:41 -08001311 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001312 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001313 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001314 for (int i = 0; i < spanX; i++) {
1315 for (int j = 0; j < spanY; j++) {
1316 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001317 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001318 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001319 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001320 continue inner;
1321 }
1322 }
1323 }
1324 if (cellXY != null) {
1325 cellXY[0] = x;
1326 cellXY[1] = y;
1327 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001328 foundCell = true;
1329 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001330 }
1331 }
1332 if (intersectX == -1 && intersectY == -1) {
1333 break;
1334 } else {
1335 // if we failed to find anything, try again but without any requirements of
1336 // intersecting
1337 intersectX = -1;
1338 intersectY = -1;
1339 continue;
1340 }
1341 }
1342
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001343 // re-mark space taken by ignoreView as occupied
1344 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001345 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001346 }
1347
1348 /**
1349 * Called when drag has left this CellLayout or has been completed (successfully or not)
1350 */
1351 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001352 // This can actually be called when we aren't in a drag, e.g. when adding a new
1353 // item to this layout via the customize drawer.
1354 // Guard against that case.
1355 if (mDragging) {
1356 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001357
Joe Onorato4be866d2010-10-10 11:26:02 -07001358 // Fade out the drag indicators
1359 if (mCrosshairsAnimator != null) {
1360 mCrosshairsAnimator.animateOut();
1361 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001362 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001363
1364 // Invalidate the drag data
1365 mDragCell[0] = -1;
1366 mDragCell[1] = -1;
1367 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1368 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1369
Michael Jurka33945b22010-12-21 18:19:38 -08001370 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001371 }
1372
1373 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001374 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001375 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001376 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 *
1378 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001380 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001381 if (child != null) {
1382 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001383 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001384 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001385 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001386 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001387 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001388 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
1390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 /**
1392 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001393 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 * @param child The child that is being dragged
1395 */
1396 void onDragChild(View child) {
1397 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1398 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001399 }
1400
1401 /**
1402 * A drag event has begun over this layout.
1403 * It may have begun over this layout (in which case onDragChild is called first),
1404 * or it may have begun on another layout.
1405 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001406 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001407 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001408 // Fade in the drag indicators
1409 if (mCrosshairsAnimator != null) {
1410 mCrosshairsAnimator.animateIn();
1411 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001412 }
1413 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001415
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001418 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 * @param cellX X coordinate of upper left corner expressed as a cell position
1420 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001421 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001423 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001425 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 final int cellWidth = mCellWidth;
1427 final int cellHeight = mCellHeight;
1428 final int widthGap = mWidthGap;
1429 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001430
1431 final int hStartPadding = getLeftPadding();
1432 final int vStartPadding = getTopPadding();
1433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1435 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1436
1437 int x = hStartPadding + cellX * (cellWidth + widthGap);
1438 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001439
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001440 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001442
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001444 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001446 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 * @param width Width in pixels
1448 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001449 * @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 -08001450 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001451 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001452 return rectToCell(getResources(), width, height, result);
1453 }
1454
1455 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 // Always assume we're working with the smallest span to make sure we
1457 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001458 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1459 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001461
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 // Always round up to next largest cell
1463 int spanX = (width + smallerSize) / smallerSize;
1464 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001465
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001466 if (result == null) {
1467 return new int[] { spanX, spanY };
1468 }
1469 result[0] = spanX;
1470 result[1] = spanY;
1471 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001474 public int[] cellSpansToSize(int hSpans, int vSpans) {
1475 int[] size = new int[2];
1476 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1477 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1478 return size;
1479 }
1480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001482 * Calculate the grid spans needed to fit given item
1483 */
1484 public void calculateSpans(ItemInfo info) {
1485 final int minWidth;
1486 final int minHeight;
1487
1488 if (info instanceof LauncherAppWidgetInfo) {
1489 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1490 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1491 } else if (info instanceof PendingAddWidgetInfo) {
1492 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1493 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1494 } else {
1495 // It's not a widget, so it must be 1x1
1496 info.spanX = info.spanY = 1;
1497 return;
1498 }
1499 int[] spans = rectToCell(minWidth, minHeight, null);
1500 info.spanX = spans[0];
1501 info.spanY = spans[1];
1502 }
1503
1504 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 * Find the first vacant cell, if there is one.
1506 *
1507 * @param vacant Holds the x and y coordinate of the vacant cell
1508 * @param spanX Horizontal cell span.
1509 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001510 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 * @return True if a vacant cell was found
1512 */
1513 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514
Michael Jurka0280c3b2010-09-17 15:00:07 -07001515 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
1517
1518 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1519 int xCount, int yCount, boolean[][] occupied) {
1520
Adam Cohen2801caf2011-05-13 20:57:39 -07001521 for (int y = 0; y < yCount; y++) {
1522 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 boolean available = !occupied[x][y];
1524out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1525 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1526 available = available && !occupied[i][j];
1527 if (!available) break out;
1528 }
1529 }
1530
1531 if (available) {
1532 vacant[0] = x;
1533 vacant[1] = y;
1534 return true;
1535 }
1536 }
1537 }
1538
1539 return false;
1540 }
1541
Michael Jurka0280c3b2010-09-17 15:00:07 -07001542 private void clearOccupiedCells() {
1543 for (int x = 0; x < mCountX; x++) {
1544 for (int y = 0; y < mCountY; y++) {
1545 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
1547 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
Adam Cohen1b607ed2011-03-03 17:26:50 -08001550 /**
1551 * Given a view, determines how much that view can be expanded in all directions, in terms of
1552 * whether or not there are other items occupying adjacent cells. Used by the
1553 * AppWidgetResizeFrame to determine how the widget can be resized.
1554 */
Adam Cohend4844c32011-02-18 19:25:06 -08001555 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001556 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001557 boolean flag;
1558
Adam Cohen1b607ed2011-03-03 17:26:50 -08001559 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001560 for (int x = lp.cellX - 1; x >= 0; x--) {
1561 flag = false;
1562 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1563 if (mOccupied[x][y]) flag = true;
1564 }
1565 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001566 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001567 }
1568
Adam Cohen1b607ed2011-03-03 17:26:50 -08001569 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001570 for (int y = lp.cellY - 1; y >= 0; y--) {
1571 flag = false;
1572 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1573 if (mOccupied[x][y]) flag = true;
1574 }
1575 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001576 expandability[AppWidgetResizeFrame.TOP]++;
1577 }
Adam Cohend4844c32011-02-18 19:25:06 -08001578
Adam Cohen1b607ed2011-03-03 17:26:50 -08001579 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001580 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1581 flag = false;
1582 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1583 if (mOccupied[x][y]) flag = true;
1584 }
1585 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001586 expandability[AppWidgetResizeFrame.RIGHT]++;
1587 }
Adam Cohend4844c32011-02-18 19:25:06 -08001588
Adam Cohen1b607ed2011-03-03 17:26:50 -08001589 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001590 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1591 flag = false;
1592 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1593 if (mOccupied[x][y]) flag = true;
1594 }
1595 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001596 expandability[AppWidgetResizeFrame.BOTTOM]++;
1597 }
Adam Cohend4844c32011-02-18 19:25:06 -08001598 }
1599
Michael Jurka0280c3b2010-09-17 15:00:07 -07001600 public void onMove(View view, int newCellX, int newCellY) {
1601 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1602 markCellsAsUnoccupiedForView(view);
1603 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1604 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605
Adam Cohend4844c32011-02-18 19:25:06 -08001606 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001607 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001608 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1609 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1610 }
1611
Adam Cohend4844c32011-02-18 19:25:06 -08001612 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001613 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001614 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1615 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1616 }
1617
1618 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1619 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1620 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1621 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 }
1623 }
1624 }
1625
Adam Cohen2801caf2011-05-13 20:57:39 -07001626 public int getDesiredWidth() {
1627 return mLeftPadding + mRightPadding + (mCountX * mCellWidth) +
1628 (Math.max((mCountX - 1), 0) * mWidthGap);
1629 }
1630
1631 public int getDesiredHeight() {
1632 return mTopPadding + mBottomPadding + (mCountY * mCellHeight) +
1633 (Math.max((mCountY - 1), 0) * mHeightGap);
1634 }
1635
Michael Jurka66d72172011-04-12 16:29:25 -07001636 public boolean isOccupied(int x, int y) {
1637 if (x < mCountX && y < mCountY) {
1638 return mOccupied[x][y];
1639 } else {
1640 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1641 }
1642 }
1643
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 @Override
1645 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1646 return new CellLayout.LayoutParams(getContext(), attrs);
1647 }
1648
1649 @Override
1650 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1651 return p instanceof CellLayout.LayoutParams;
1652 }
1653
1654 @Override
1655 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1656 return new CellLayout.LayoutParams(p);
1657 }
1658
Winson Chungaafa03c2010-06-11 17:34:16 -07001659 public static class CellLayoutAnimationController extends LayoutAnimationController {
1660 public CellLayoutAnimationController(Animation animation, float delay) {
1661 super(animation, delay);
1662 }
1663
1664 @Override
1665 protected long getDelayForView(View view) {
1666 return (int) (Math.random() * 150);
1667 }
1668 }
1669
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1671 /**
1672 * Horizontal location of the item in the grid.
1673 */
1674 @ViewDebug.ExportedProperty
1675 public int cellX;
1676
1677 /**
1678 * Vertical location of the item in the grid.
1679 */
1680 @ViewDebug.ExportedProperty
1681 public int cellY;
1682
1683 /**
1684 * Number of cells spanned horizontally by the item.
1685 */
1686 @ViewDebug.ExportedProperty
1687 public int cellHSpan;
1688
1689 /**
1690 * Number of cells spanned vertically by the item.
1691 */
1692 @ViewDebug.ExportedProperty
1693 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001694
Adam Cohen1b607ed2011-03-03 17:26:50 -08001695 /**
1696 * Indicates whether the item will set its x, y, width and height parameters freely,
1697 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1698 */
Adam Cohend4844c32011-02-18 19:25:06 -08001699 public boolean isLockedToGrid = true;
1700
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 /**
1702 * Is this item currently being dragged
1703 */
1704 public boolean isDragging;
1705
1706 // X coordinate of the view in the layout.
1707 @ViewDebug.ExportedProperty
1708 int x;
1709 // Y coordinate of the view in the layout.
1710 @ViewDebug.ExportedProperty
1711 int y;
1712
Patrick Dubroyce34a972010-10-19 10:34:32 -07001713 /**
1714 * The old X coordinate of this item, relative to its current parent.
1715 * Used to animate the item into its new position.
1716 */
1717 int oldX;
1718
1719 /**
1720 * The old Y coordinate of this item, relative to its current parent.
1721 * Used to animate the item into its new position.
1722 */
1723 int oldY;
1724
Romain Guy84f296c2009-11-04 15:00:44 -08001725 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001726
Michael Jurkad3ef3062010-11-23 16:23:58 -08001727 boolean animateDrop;
1728
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 public LayoutParams(Context c, AttributeSet attrs) {
1730 super(c, attrs);
1731 cellHSpan = 1;
1732 cellVSpan = 1;
1733 }
1734
1735 public LayoutParams(ViewGroup.LayoutParams source) {
1736 super(source);
1737 cellHSpan = 1;
1738 cellVSpan = 1;
1739 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001740
1741 public LayoutParams(LayoutParams source) {
1742 super(source);
1743 this.cellX = source.cellX;
1744 this.cellY = source.cellY;
1745 this.cellHSpan = source.cellHSpan;
1746 this.cellVSpan = source.cellVSpan;
1747 }
1748
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001750 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 this.cellX = cellX;
1752 this.cellY = cellY;
1753 this.cellHSpan = cellHSpan;
1754 this.cellVSpan = cellVSpan;
1755 }
1756
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001757 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001758 if (isLockedToGrid) {
1759 final int myCellHSpan = cellHSpan;
1760 final int myCellVSpan = cellVSpan;
1761 final int myCellX = cellX;
1762 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001763
Adam Cohend4844c32011-02-18 19:25:06 -08001764 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1765 leftMargin - rightMargin;
1766 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1767 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001768 x = myCellX * (cellWidth + widthGap) + leftMargin;
1769 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001770 }
1771 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001772
Winson Chungaafa03c2010-06-11 17:34:16 -07001773 public String toString() {
1774 return "(" + this.cellX + ", " + this.cellY + ")";
1775 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001776
1777 public void setWidth(int width) {
1778 this.width = width;
1779 }
1780
1781 public int getWidth() {
1782 return width;
1783 }
1784
1785 public void setHeight(int height) {
1786 this.height = height;
1787 }
1788
1789 public int getHeight() {
1790 return height;
1791 }
1792
1793 public void setX(int x) {
1794 this.x = x;
1795 }
1796
1797 public int getX() {
1798 return x;
1799 }
1800
1801 public void setY(int y) {
1802 this.y = y;
1803 }
1804
1805 public int getY() {
1806 return y;
1807 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 }
1809
Michael Jurka0280c3b2010-09-17 15:00:07 -07001810 // This class stores info for two purposes:
1811 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1812 // its spanX, spanY, and the screen it is on
1813 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1814 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1815 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001816 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001818 int cellX = -1;
1819 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 int spanX;
1821 int spanY;
1822 int screen;
1823 boolean valid;
1824
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 @Override
1826 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001827 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1828 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 }
1830 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831}