blob: 68e9edb940e7a1648394d9105bccd6bb41f1f1cf [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Michael Jurka18014792010-10-14 09:01:34 -070021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070023import android.animation.PropertyValuesHolder;
Chet Haase00397b12010-10-07 11:13:10 -070024import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070025import android.animation.ValueAnimator;
26import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040028import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070031import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070033import android.graphics.Point;
34import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Rect;
36import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070037import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070038import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.MotionEvent;
42import android.view.View;
43import android.view.ViewDebug;
44import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070046import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Adam Cohen66396872011-04-15 17:50:36 -070049import com.android.launcher.R;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070050
Adam Cohenc0dcf592011-06-01 15:30:43 -070051import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070052import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053
Michael Jurkabdb5c532011-02-01 15:05:06 -080054public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070055 static final String TAG = "CellLayout";
56
Winson Chung4b825dcd2011-06-19 12:41:22 -070057 private int mOriginalCellWidth;
58 private int mOriginalCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 private int mCellWidth;
60 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070061
Adam Cohend22015c2010-07-26 22:02:18 -070062 private int mCountX;
63 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064
65 private int mWidthGap;
66 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070067 private int mMaxGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
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
Adam Cohenbfbfd262011-06-13 16:55:12 -0700121 private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new
122 HashMap<CellLayout.LayoutParams, ObjectAnimator>();
123
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700124 // When a drag operation is in progress, holds the nearest cell to the touch point
125 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Joe Onorato4be866d2010-10-10 11:26:02 -0700127 private boolean mDragging = false;
128
Patrick Dubroyce34a972010-10-19 10:34:32 -0700129 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800130 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 public CellLayout(Context context) {
133 this(context, null);
134 }
135
136 public CellLayout(Context context, AttributeSet attrs) {
137 this(context, attrs, 0);
138 }
139
140 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
141 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700142
143 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
144 // the user where a dragged item will land when dropped.
145 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
148
Winson Chung4b825dcd2011-06-19 12:41:22 -0700149 mOriginalCellWidth =
150 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
151 mOriginalCellHeight =
152 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
153 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
154 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
155 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Winson Chungaafa03c2010-06-11 17:34:16 -0700156
Adam Cohend22015c2010-07-26 22:02:18 -0700157 mCountX = LauncherModel.getCellCountX();
158 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700159 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
161 a.recycle();
162
163 setAlwaysDrawnWithCacheEnabled(false);
164
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700165 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700166
Winson Chungb26f3d62011-06-02 10:49:29 -0700167 mNormalBackground = res.getDrawable(R.drawable.homescreen_large_blue);
168 mActiveBackground = res.getDrawable(R.drawable.homescreen_large_green);
169 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_large_green_strong);
Michael Jurka33945b22010-12-21 18:19:38 -0800170
Winson Chungb26f3d62011-06-02 10:49:29 -0700171 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
172 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
173 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
174 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
Michael Jurka33945b22010-12-21 18:19:38 -0800175
Winson Chungb26f3d62011-06-02 10:49:29 -0700176 mNormalBackground.setFilterBitmap(true);
177 mActiveBackground.setFilterBitmap(true);
178 mActiveGlowBackground.setFilterBitmap(true);
179 mNormalBackgroundMini.setFilterBitmap(true);
180 mNormalGlowBackgroundMini.setFilterBitmap(true);
181 mActiveBackgroundMini.setFilterBitmap(true);
182 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700183
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700184 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700185
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700186 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700187 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700188
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700189 // Set up the animation for fading the crosshairs in and out
190 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700191 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700192 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700193 public void onAnimationUpdate(ValueAnimator animation) {
194 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700195 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700196 }
197 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700198 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700199
Joe Onorato4be866d2010-10-10 11:26:02 -0700200 for (int i = 0; i < mDragOutlines.length; i++) {
201 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700202 }
203
204 // When dragging things around the home screens, we show a green outline of
205 // where the item will land. The outlines gradually fade out, leaving a trail
206 // behind the drag path.
207 // Set up all the animations that are used to implement this fading.
208 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700209 final float fromAlphaValue = 0;
210 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700211
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700212 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700213
214 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700215 final InterruptibleInOutAnimator anim =
216 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700217 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700218 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700219 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700220 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700221 final Bitmap outline = (Bitmap)anim.getTag();
222
223 // If an animation is started and then stopped very quickly, we can still
224 // get spurious updates we've cleared the tag. Guard against this.
225 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700226 if (false) {
227 Object val = animation.getAnimatedValue();
228 Log.d(TAG, "anim " + thisIndex + " update: " + val +
229 ", isStopped " + anim.isStopped());
230 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700231 // Try to prevent it from continuing to run
232 animation.cancel();
233 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700234 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700235 final int left = mDragOutlines[thisIndex].x;
236 final int top = mDragOutlines[thisIndex].y;
237 CellLayout.this.invalidate(left, top,
238 left + outline.getWidth(), top + outline.getHeight());
239 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700240 }
241 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700242 // The animation holds a reference to the drag outline bitmap as long is it's
243 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700244 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700245 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700246 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700247 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700248 anim.setTag(null);
249 }
250 }
251 });
252 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700253 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700254
Michael Jurka18014792010-10-14 09:01:34 -0700255 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800256 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700257 setHoverScale(1.0f);
258 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800259
Michael Jurka8c920dd2011-01-20 14:16:56 -0800260 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700261 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800262 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700263 }
264
Michael Jurkaf6440da2011-04-05 14:50:34 -0700265 static int widthInPortrait(Resources r, int numCells) {
266 // We use this method from Workspace to figure out how many rows/columns Launcher should
267 // have. We ignore the left/right padding on CellLayout because it turns out in our design
268 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700269 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700270 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
271 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700272
Winson Chung4b825dcd2011-06-19 12:41:22 -0700273 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700274 }
275
Michael Jurkaf6440da2011-04-05 14:50:34 -0700276 static int heightInLandscape(Resources r, int numCells) {
277 // We use this method from Workspace to figure out how many rows/columns Launcher should
278 // have. We ignore the left/right padding on CellLayout because it turns out in our design
279 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700280 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700281 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
282 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700283
Winson Chung4b825dcd2011-06-19 12:41:22 -0700284 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700285 }
286
Adam Cohen2801caf2011-05-13 20:57:39 -0700287 public void enableHardwareLayers() {
288 mChildren.enableHardwareLayers();
289 }
290
291 public void setGridSize(int x, int y) {
292 mCountX = x;
293 mCountY = y;
294 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700295 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700296 }
297
Patrick Dubroy96864c32011-03-10 17:17:23 -0800298 private void invalidateBubbleTextView(BubbleTextView icon) {
299 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700300 invalidate(icon.getLeft() + getPaddingLeft() - padding,
301 icon.getTop() + getPaddingTop() - padding,
302 icon.getRight() + getPaddingLeft() + padding,
303 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800304 }
305
306 void setPressedOrFocusedIcon(BubbleTextView icon) {
307 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
308 // requires an expanded clip rect (due to the glow's blur radius)
309 BubbleTextView oldIcon = mPressedOrFocusedIcon;
310 mPressedOrFocusedIcon = icon;
311 if (oldIcon != null) {
312 invalidateBubbleTextView(oldIcon);
313 }
314 if (mPressedOrFocusedIcon != null) {
315 invalidateBubbleTextView(mPressedOrFocusedIcon);
316 }
317 }
318
Winson Chung6e314082011-01-27 16:46:51 -0800319 public CellLayoutChildren getChildrenLayout() {
320 if (getChildCount() > 0) {
321 return (CellLayoutChildren) getChildAt(0);
322 }
323 return null;
324 }
325
Michael Jurka33945b22010-12-21 18:19:38 -0800326 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
327 if (mIsDefaultDropTarget != isDefaultDropTarget) {
328 mIsDefaultDropTarget = isDefaultDropTarget;
329 invalidate();
330 }
331 }
332
Michael Jurka33945b22010-12-21 18:19:38 -0800333 void setIsDragOccuring(boolean isDragOccuring) {
334 if (mIsDragOccuring != isDragOccuring) {
335 mIsDragOccuring = isDragOccuring;
336 invalidate();
337 }
338 }
339
340 void setIsDragOverlapping(boolean isDragOverlapping) {
341 if (mIsDragOverlapping != isDragOverlapping) {
342 mIsDragOverlapping = isDragOverlapping;
343 invalidate();
344 }
345 }
346
347 boolean getIsDragOverlapping() {
348 return mIsDragOverlapping;
349 }
350
351 private void updateGlowRect() {
352 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700353 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
354 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800355 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700356 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
357 invalidate();
358 }
359
360 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800361 if (scaleFactor != mGlowBackgroundScale) {
362 mGlowBackgroundScale = scaleFactor;
363 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800364 if (getParent() != null) {
365 ((View) getParent()).invalidate();
366 }
Michael Jurka18014792010-10-14 09:01:34 -0700367 }
368 }
369
370 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800371 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700372 }
373
374 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800375 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700376 }
377
378 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800379 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700380 invalidate();
381 }
382
383 void animateDrop() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700384 Resources res = getResources();
385 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
386 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
387 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
388 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
389 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
390 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
Michael Jurka18014792010-10-14 09:01:34 -0700391
Winson Chungb26f3d62011-06-02 10:49:29 -0700392 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
393 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDuration));
Michael Jurka18014792010-10-14 09:01:34 -0700394
Winson Chungb26f3d62011-06-02 10:49:29 -0700395 AnimatorSet bouncer = new AnimatorSet();
396 bouncer.play(scaleUp).before(scaleDown);
397 bouncer.play(scaleUp).with(alphaFadeOut);
398 bouncer.addListener(new AnimatorListenerAdapter() {
399 @Override
400 public void onAnimationStart(Animator animation) {
401 setIsDragOverlapping(true);
402 }
403 @Override
404 public void onAnimationEnd(Animator animation) {
405 setIsDragOverlapping(false);
406 setHoverScale(1.0f);
407 setHoverAlpha(1.0f);
408 }
409 });
410 bouncer.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
412
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700413 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700414 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700415 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
416 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
417 // When we're small, we are either drawn normally or in the "accepts drops" state (during
418 // a drag). However, we also drag the mini hover background *over* one of those two
419 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700420 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700421 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800422 boolean mini = getScaleX() < 0.5f;
423
424 if (mIsDragOverlapping) {
425 // In the mini case, we draw the active_glow bg *over* the active background
426 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
427 } else if (mIsDragOccuring && mAcceptsDrops) {
428 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800429 } else if (mIsDefaultDropTarget && mini) {
430 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700431 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800432 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700433 }
Michael Jurka33945b22010-12-21 18:19:38 -0800434
435 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
436 bg.setBounds(mBackgroundRect);
437 bg.draw(canvas);
438
439 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700440 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800441 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700442 // If the hover background's scale is greater than 1, we'll be drawing outside
443 // the bounds of this CellLayout. Get around that by temporarily increasing the
444 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800445 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700446 Rect clipRect = canvas.getClipBounds();
447 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
448 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
449 canvas.save(Canvas.CLIP_SAVE_FLAG);
450 canvas.clipRect(-marginX, -marginY,
451 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
452 modifiedClipRect = true;
453 }
454
Michael Jurka33945b22010-12-21 18:19:38 -0800455 mActiveGlowBackgroundMini.setAlpha(
456 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
457 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
458 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700459 if (modifiedClipRect) {
460 canvas.restore();
461 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700462 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700463 }
Romain Guya6abce82009-11-10 02:54:41 -0800464
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700465 if (mCrosshairsVisibility > 0.0f) {
466 final int countX = mCountX;
467 final int countY = mCountY;
468
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700469 final float MAX_ALPHA = 0.4f;
470 final int MAX_VISIBLE_DISTANCE = 600;
471 final float DISTANCE_MULTIPLIER = 0.002f;
472
473 final Drawable d = mCrosshairsDrawable;
474 final int width = d.getIntrinsicWidth();
475 final int height = d.getIntrinsicHeight();
476
Winson Chung4b825dcd2011-06-19 12:41:22 -0700477 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700478 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700479 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700480 for (int row = 0; row <= countY; row++) {
481 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
482 float dist = mTmpPointF.length();
483 // Crosshairs further from the drag point are more faint
484 float alpha = Math.min(MAX_ALPHA,
485 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
486 if (alpha > 0.0f) {
487 d.setBounds(x, y, x + width, y + height);
488 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
489 d.draw(canvas);
490 }
491 y += mCellHeight + mHeightGap;
492 }
493 x += mCellWidth + mWidthGap;
494 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700495 }
Winson Chung150fbab2010-09-29 17:14:26 -0700496
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700497 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700498 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700499 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700500 if (alpha > 0) {
501 final Point p = mDragOutlines[i];
502 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700503 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700504 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700505 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700506 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800507
508 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
509 // requires an expanded clip rect (due to the glow's blur radius)
510 if (mPressedOrFocusedIcon != null) {
511 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
512 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
513 if (b != null) {
514 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700515 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
516 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800517 null);
518 }
519 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700520 }
521
522 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700523 public void cancelLongPress() {
524 super.cancelLongPress();
525
526 // Cancel long press for all children
527 final int count = getChildCount();
528 for (int i = 0; i < count; i++) {
529 final View child = getChildAt(i);
530 child.cancelLongPress();
531 }
532 }
533
Michael Jurkadee05892010-07-27 10:01:56 -0700534 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
535 mInterceptTouchListener = listener;
536 }
537
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700539 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 }
541
542 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700543 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 }
545
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700546 public boolean addViewToCellLayout(
547 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700548 final LayoutParams lp = params;
549
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 // Generate an id for each view, this assumes we have at most 256x256 cells
551 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700552 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700553 // If the horizontal or vertical span is set to -1, it is taken to
554 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700555 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
556 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557
Winson Chungaafa03c2010-06-11 17:34:16 -0700558 child.setId(childId);
559
Michael Jurka8c920dd2011-01-20 14:16:56 -0800560 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700561
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700562 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700563
Winson Chungaafa03c2010-06-11 17:34:16 -0700564 return true;
565 }
566 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700568
Michael Jurkabea15192010-11-17 12:33:46 -0800569 public void setAcceptsDrops(boolean acceptsDrops) {
570 if (mAcceptsDrops != acceptsDrops) {
571 mAcceptsDrops = acceptsDrops;
572 invalidate();
573 }
574 }
575
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700577 public void removeAllViews() {
578 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800579 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700580 }
581
582 @Override
583 public void removeAllViewsInLayout() {
584 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800585 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700586 }
587
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700588 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800589 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700590 }
591
Michael Jurka0280c3b2010-09-17 15:00:07 -0700592 @Override
593 public void removeView(View view) {
594 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800595 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700596 }
597
598 @Override
599 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800600 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
601 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700602 }
603
604 @Override
605 public void removeViewInLayout(View view) {
606 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800607 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700608 }
609
610 @Override
611 public void removeViews(int start, int count) {
612 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800613 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700614 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800615 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700616 }
617
618 @Override
619 public void removeViewsInLayout(int start, int count) {
620 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800621 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700622 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800623 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700624 }
625
Michael Jurka8c920dd2011-01-20 14:16:56 -0800626 public void drawChildren(Canvas canvas) {
627 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 }
629
Michael Jurkaabded662011-03-04 12:06:57 -0800630 void buildChildrenLayer() {
631 mChildren.buildLayer();
632 }
633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 @Override
635 protected void onAttachedToWindow() {
636 super.onAttachedToWindow();
637 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
638 }
639
Michael Jurkaaf442092010-06-10 17:01:57 -0700640 public void setTagToCellInfoForPoint(int touchX, int touchY) {
641 final CellInfo cellInfo = mCellInfo;
642 final Rect frame = mRect;
643 final int x = touchX + mScrollX;
644 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800645 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700646
647 boolean found = false;
648 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800649 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800650 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700651
Adam Cohen1b607ed2011-03-03 17:26:50 -0800652 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
653 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700654 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700655
656 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
657 // offset that by this CellLayout's padding to test an (x,y) point that is relative
658 // to this view.
659 final int tmpXY[] = mTmpXY;
660 child.getLocationOnScreen(tmpXY);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700661 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700662
Michael Jurkaaf442092010-06-10 17:01:57 -0700663 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700664 cellInfo.cell = child;
665 cellInfo.cellX = lp.cellX;
666 cellInfo.cellY = lp.cellY;
667 cellInfo.spanX = lp.cellHSpan;
668 cellInfo.spanY = lp.cellVSpan;
669 cellInfo.valid = true;
670 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700671 break;
672 }
673 }
674 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700675
Michael Jurkaaf442092010-06-10 17:01:57 -0700676 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700677 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700678 pointToCellExact(x, y, cellXY);
679
Michael Jurkaaf442092010-06-10 17:01:57 -0700680 cellInfo.cell = null;
681 cellInfo.cellX = cellXY[0];
682 cellInfo.cellY = cellXY[1];
683 cellInfo.spanX = 1;
684 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700685 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
686 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700687 }
688 setTag(cellInfo);
689 }
690
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 @Override
692 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700693 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
694 return true;
695 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 final int action = ev.getAction();
697 final CellInfo cellInfo = mCellInfo;
698
699 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700700 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 } else if (action == MotionEvent.ACTION_UP) {
702 cellInfo.cell = null;
703 cellInfo.cellX = -1;
704 cellInfo.cellY = -1;
705 cellInfo.spanX = 0;
706 cellInfo.spanY = 0;
707 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 setTag(cellInfo);
709 }
710
711 return false;
712 }
713
714 @Override
715 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700716 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 }
718
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700719 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700720 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 * @param x X coordinate of the point
722 * @param y Y coordinate of the point
723 * @param result Array of 2 ints to hold the x and y coordinate of the cell
724 */
725 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700726 final int hStartPadding = getPaddingLeft();
727 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728
729 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
730 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
731
Adam Cohend22015c2010-07-26 22:02:18 -0700732 final int xAxis = mCountX;
733 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734
735 if (result[0] < 0) result[0] = 0;
736 if (result[0] >= xAxis) result[0] = xAxis - 1;
737 if (result[1] < 0) result[1] = 0;
738 if (result[1] >= yAxis) result[1] = yAxis - 1;
739 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700740
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 /**
742 * Given a point, return the cell that most closely encloses that point
743 * @param x X coordinate of the point
744 * @param y Y coordinate of the point
745 * @param result Array of 2 ints to hold the x and y coordinate of the cell
746 */
747 void pointToCellRounded(int x, int y, int[] result) {
748 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
749 }
750
751 /**
752 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700753 *
754 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700756 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 * @param result Array of 2 ints to hold the x and y coordinate of the point
758 */
759 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700760 final int hStartPadding = getPaddingLeft();
761 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762
763 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
764 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
765 }
766
Adam Cohene3e27a82011-04-15 12:07:39 -0700767 /**
768 * Given a cell coordinate, return the point that represents the upper left corner of that cell
769 *
770 * @param cellX X coordinate of the cell
771 * @param cellY Y coordinate of the cell
772 *
773 * @param result Array of 2 ints to hold the x and y coordinate of the point
774 */
775 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700776 final int hStartPadding = getPaddingLeft();
777 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700778
779 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
780 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
781 }
782
Romain Guy84f296c2009-11-04 15:00:44 -0800783 int getCellWidth() {
784 return mCellWidth;
785 }
786
787 int getCellHeight() {
788 return mCellHeight;
789 }
790
Adam Cohend4844c32011-02-18 19:25:06 -0800791 int getWidthGap() {
792 return mWidthGap;
793 }
794
795 int getHeightGap() {
796 return mHeightGap;
797 }
798
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700799 Rect getContentRect(Rect r) {
800 if (r == null) {
801 r = new Rect();
802 }
803 int left = getPaddingLeft();
804 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700805 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
806 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700807 r.set(left, top, right, bottom);
808 return r;
809 }
810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 @Override
812 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
813 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700814
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700816 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
817
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
819 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700820
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
822 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
823 }
824
Adam Cohend22015c2010-07-26 22:02:18 -0700825 int numWidthGaps = mCountX - 1;
826 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827
Winson Chungece7f5b2010-10-22 14:54:12 -0700828 if (mWidthGap < 0 || mHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700829 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
830 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
831 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
832 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
833 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
834 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
835 int remainingHSpace = hFreeSpace - (numWidthGaps * mWidthGap);
836 int remainingVSpace = vFreeSpace - (numHeightGaps * mHeightGap);
837 mCellWidth = mOriginalCellWidth + remainingHSpace / mCountX;
838 mCellHeight = mOriginalCellHeight + remainingVSpace / mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839
Winson Chung4b825dcd2011-06-19 12:41:22 -0700840 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Winson Chungece7f5b2010-10-22 14:54:12 -0700841 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700842
Michael Jurka8c920dd2011-01-20 14:16:56 -0800843 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
844 int newWidth = widthSpecSize;
845 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700846 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700847 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700848 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700849 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700850 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700851 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700852 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800853
854 int count = getChildCount();
855 for (int i = 0; i < count; i++) {
856 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700857 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
858 mPaddingRight, MeasureSpec.EXACTLY);
859 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
860 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800861 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
862 }
863 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
865
866 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700867 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800870 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700871 child.layout(mPaddingLeft, mPaddingTop,
872 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 }
874 }
875
876 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700877 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
878 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700879 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800880 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700881 }
882
883 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800885 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 }
887
888 @Override
889 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800890 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 }
892
Michael Jurka5f1c5092010-09-03 14:15:02 -0700893 public float getBackgroundAlpha() {
894 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700895 }
896
Michael Jurka742574b2011-02-02 23:51:01 -0800897 public void setFastBackgroundAlpha(float alpha) {
898 mBackgroundAlpha = alpha;
899 }
900
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700901 public void setBackgroundAlphaMultiplier(float multiplier) {
902 mBackgroundAlphaMultiplier = multiplier;
903 }
904
Adam Cohenddb82192010-11-10 16:32:54 -0800905 public float getBackgroundAlphaMultiplier() {
906 return mBackgroundAlphaMultiplier;
907 }
908
Michael Jurka5f1c5092010-09-03 14:15:02 -0700909 public void setBackgroundAlpha(float alpha) {
910 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700911 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700912 }
913
Michael Jurka5f1c5092010-09-03 14:15:02 -0700914 // Need to return true to let the view system know we know how to handle alpha-- this is
915 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
916 // versions
917 @Override
918 protected boolean onSetAlpha(int alpha) {
919 return true;
920 }
921
922 public void setAlpha(float alpha) {
923 setChildrenAlpha(alpha);
924 super.setAlpha(alpha);
925 }
926
Michael Jurka742574b2011-02-02 23:51:01 -0800927 public void setFastAlpha(float alpha) {
928 setFastChildrenAlpha(alpha);
929 super.setFastAlpha(alpha);
930 }
931
Michael Jurkadee05892010-07-27 10:01:56 -0700932 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700933 final int childCount = getChildCount();
934 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700935 getChildAt(i).setAlpha(alpha);
936 }
937 }
938
Michael Jurka742574b2011-02-02 23:51:01 -0800939 private void setFastChildrenAlpha(float alpha) {
940 final int childCount = getChildCount();
941 for (int i = 0; i < childCount; i++) {
942 getChildAt(i).setFastAlpha(alpha);
943 }
944 }
945
Patrick Dubroy440c3602010-07-13 17:50:32 -0700946 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800947 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700948 }
949
Adam Cohen76fc0852011-06-17 13:26:23 -0700950 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
951 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700952 CellLayoutChildren clc = getChildrenLayout();
953 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
954 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
955 final ItemInfo info = (ItemInfo) child.getTag();
956
957 // We cancel any existing animations
958 if (mReorderAnimators.containsKey(lp)) {
959 mReorderAnimators.get(lp).cancel();
960 mReorderAnimators.remove(lp);
961 }
962
963 int oldX = lp.x;
964 int oldY = lp.y;
965 mOccupied[lp.cellX][lp.cellY] = false;
966 mOccupied[cellX][cellY] = true;
967
968 lp.isLockedToGrid = true;
969 lp.cellX = info.cellX = cellX;
970 lp.cellY = info.cellY = cellY;
971 clc.setupLp(lp);
972 lp.isLockedToGrid = false;
973 int newX = lp.x;
974 int newY = lp.y;
975
Adam Cohen76fc0852011-06-17 13:26:23 -0700976 lp.x = oldX;
977 lp.y = oldY;
978 child.requestLayout();
979
Adam Cohenbfbfd262011-06-13 16:55:12 -0700980 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
981 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
982 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
983 oa.setDuration(duration);
984 mReorderAnimators.put(lp, oa);
985 oa.addUpdateListener(new AnimatorUpdateListener() {
986 public void onAnimationUpdate(ValueAnimator animation) {
987 child.requestLayout();
988 }
989 });
990 oa.addListener(new AnimatorListenerAdapter() {
991 boolean cancelled = false;
992 public void onAnimationEnd(Animator animation) {
993 // If the animation was cancelled, it means that another animation
994 // has interrupted this one, and we don't want to lock the item into
995 // place just yet.
996 if (!cancelled) {
997 lp.isLockedToGrid = true;
998 }
999 if (mReorderAnimators.containsKey(lp)) {
1000 mReorderAnimators.remove(lp);
1001 }
1002 }
1003 public void onAnimationCancel(Animator animation) {
1004 cancelled = true;
1005 }
1006 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001007 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001008 oa.start();
1009 return true;
1010 }
1011 return false;
1012 }
1013
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001014 /**
1015 * Estimate where the top left cell of the dragged item will land if it is dropped.
1016 *
1017 * @param originX The X value of the top left corner of the item
1018 * @param originY The Y value of the top left corner of the item
1019 * @param spanX The number of horizontal cells that the item spans
1020 * @param spanY The number of vertical cells that the item spans
1021 * @param result The estimated drop cell X and Y.
1022 */
1023 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001024 final int countX = mCountX;
1025 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001026
Michael Jurkaa63c4522010-08-19 13:52:27 -07001027 // pointToCellRounded takes the top left of a cell but will pad that with
1028 // cellWidth/2 and cellHeight/2 when finding the matching cell
1029 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001030
1031 // If the item isn't fully on this screen, snap to the edges
1032 int rightOverhang = result[0] + spanX - countX;
1033 if (rightOverhang > 0) {
1034 result[0] -= rightOverhang; // Snap to right
1035 }
1036 result[0] = Math.max(0, result[0]); // Snap to left
1037 int bottomOverhang = result[1] + spanY - countY;
1038 if (bottomOverhang > 0) {
1039 result[1] -= bottomOverhang; // Snap to bottom
1040 }
1041 result[1] = Math.max(0, result[1]); // Snap to top
1042 }
1043
Joe Onorato4be866d2010-10-10 11:26:02 -07001044 void visualizeDropLocation(
1045 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1046
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001047 final int oldDragCellX = mDragCell[0];
1048 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001049 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001050 if (v != null) {
1051 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1052 } else {
1053 mDragCenter.set(originX, originY);
1054 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001055
Adam Cohen2801caf2011-05-13 20:57:39 -07001056 if (dragOutline == null && v == null) {
1057 if (mCrosshairsDrawable != null) {
1058 invalidate();
1059 }
1060 return;
1061 }
1062
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001063 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001064 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001065 final int[] topLeft = mTmpPoint;
1066 cellToPoint(nearest[0], nearest[1], topLeft);
1067
Joe Onorato4be866d2010-10-10 11:26:02 -07001068 int left = topLeft[0];
1069 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001070
Winson Chunga9abd0e2010-10-27 17:18:37 -07001071 if (v != null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001072 // When drawing the drag outline, it did not account for margin offsets
1073 // added by the view's parent.
1074 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1075 left += lp.leftMargin;
1076 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001077
Adam Cohen99e8b402011-03-25 19:23:43 -07001078 // Offsets due to the size difference between the View and the dragOutline.
1079 // There is a size difference to account for the outer blur, which may lie
1080 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001081 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1082 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001083 } else {
1084 // Center the drag outline in the cell
Winson Chung4b576be2011-04-27 17:40:20 -07001085 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1086 - dragOutline.getWidth()) / 2;
1087 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1088 - dragOutline.getHeight()) / 2;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001089 }
Winson Chung150fbab2010-09-29 17:14:26 -07001090
Joe Onorato4be866d2010-10-10 11:26:02 -07001091 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001092 mDragOutlineAnims[oldIndex].animateOut();
1093 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001094
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001095 mDragOutlines[mDragOutlineCurrent].set(left, top);
1096 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1097 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001098 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001099
1100 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1101 if (mCrosshairsDrawable != null) {
1102 invalidate();
1103 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001104 }
1105
Adam Cohene0310962011-04-18 16:15:31 -07001106 public void clearDragOutlines() {
1107 final int oldIndex = mDragOutlineCurrent;
1108 mDragOutlineAnims[oldIndex].animateOut();
1109 mDragCell[0] = -1;
1110 mDragCell[1] = -1;
1111 }
1112
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001114 * Find a vacant area that will fit the given bounds nearest the requested
1115 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001116 *
Romain Guy51afc022009-05-04 18:03:43 -07001117 * @param pixelX The X location at which you want to search for a vacant area.
1118 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001119 * @param spanX Horizontal span of the object.
1120 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001121 * @param result Array in which to place the result, or null (in which case a new array will
1122 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001123 * @return The X, Y cell of a vacant area that can contain this object,
1124 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001126 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001127 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1128 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001129 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001130
Michael Jurka6a1435d2010-09-27 17:35:12 -07001131 /**
1132 * Find a vacant area that will fit the given bounds nearest the requested
1133 * cell location. Uses Euclidean distance to score multiple vacant areas.
1134 *
1135 * @param pixelX The X location at which you want to search for a vacant area.
1136 * @param pixelY The Y location at which you want to search for a vacant area.
1137 * @param spanX Horizontal span of the object.
1138 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001139 * @param ignoreOccupied If true, the result can be an occupied cell
1140 * @param result Array in which to place the result, or null (in which case a new array will
1141 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001142 * @return The X, Y cell of a vacant area that can contain this object,
1143 * nearest the requested location.
1144 */
Adam Cohendf035382011-04-11 17:22:04 -07001145 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1146 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001147 // mark space take by ignoreView as available (method checks if ignoreView is null)
1148 markCellsAsUnoccupiedForView(ignoreView);
1149
Adam Cohene3e27a82011-04-15 12:07:39 -07001150 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1151 // to the center of the item, but we are searching based on the top-left cell, so
1152 // we translate the point over to correspond to the top-left.
1153 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1154 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1155
Jeff Sharkey70864282009-04-07 21:08:40 -07001156 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001157 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001158 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001159
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001160 final int countX = mCountX;
1161 final int countY = mCountY;
1162 final boolean[][] occupied = mOccupied;
1163
Winson Chungbbc60d82010-11-11 16:34:41 -08001164 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001165 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001166 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001167 if (ignoreOccupied) {
1168 for (int i = 0; i < spanX; i++) {
1169 for (int j = 0; j < spanY; j++) {
1170 if (occupied[x + i][y + j]) {
1171 // small optimization: we can skip to after the column we
1172 // just found an occupied cell
1173 x += i;
1174 continue inner;
1175 }
Michael Jurkac28de512010-08-13 11:27:44 -07001176 }
1177 }
1178 }
Winson Chung0be025d2011-05-23 17:45:09 -07001179 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001180 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181
Michael Jurkac28de512010-08-13 11:27:44 -07001182 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1183 + Math.pow(cellXY[1] - pixelY, 2));
1184 if (distance <= bestDistance) {
1185 bestDistance = distance;
1186 bestXY[0] = x;
1187 bestXY[1] = y;
1188 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 }
1190 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001191 // re-mark space taken by ignoreView as occupied
1192 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193
Adam Cohenc0dcf592011-06-01 15:30:43 -07001194 // Return -1, -1 if no suitable location found
1195 if (bestDistance == Double.MAX_VALUE) {
1196 bestXY[0] = -1;
1197 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001198 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001199 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001201
Adam Cohendf035382011-04-11 17:22:04 -07001202 /**
1203 * Find a vacant area that will fit the given bounds nearest the requested
1204 * cell location. Uses Euclidean distance to score multiple vacant areas.
1205 *
1206 * @param pixelX The X location at which you want to search for a vacant area.
1207 * @param pixelY The Y location at which you want to search for a vacant area.
1208 * @param spanX Horizontal span of the object.
1209 * @param spanY Vertical span of the object.
1210 * @param ignoreView Considers space occupied by this view as unoccupied
1211 * @param result Previously returned value to possibly recycle.
1212 * @return The X, Y cell of a vacant area that can contain this object,
1213 * nearest the requested location.
1214 */
1215 int[] findNearestVacantArea(
1216 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1217 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1218 }
1219
1220 /**
1221 * Find a starting cell position that will fit the given bounds nearest the requested
1222 * cell location. Uses Euclidean distance to score multiple vacant areas.
1223 *
1224 * @param pixelX The X location at which you want to search for a vacant area.
1225 * @param pixelY The Y location at which you want to search for a vacant area.
1226 * @param spanX Horizontal span of the object.
1227 * @param spanY Vertical span of the object.
1228 * @param ignoreView Considers space occupied by this view as unoccupied
1229 * @param result Previously returned value to possibly recycle.
1230 * @return The X, Y cell of a vacant area that can contain this object,
1231 * nearest the requested location.
1232 */
1233 int[] findNearestArea(
1234 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1235 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1236 }
1237
Michael Jurka0280c3b2010-09-17 15:00:07 -07001238 boolean existsEmptyCell() {
1239 return findCellForSpan(null, 1, 1);
1240 }
1241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001243 * Finds the upper-left coordinate of the first rectangle in the grid that can
1244 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1245 * then this method will only return coordinates for rectangles that contain the cell
1246 * (intersectX, intersectY)
1247 *
1248 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1249 * can be found.
1250 * @param spanX The horizontal span of the cell we want to find.
1251 * @param spanY The vertical span of the cell we want to find.
1252 *
1253 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001254 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001255 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1256 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1257 }
1258
1259 /**
1260 * Like above, but ignores any cells occupied by the item "ignoreView"
1261 *
1262 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1263 * can be found.
1264 * @param spanX The horizontal span of the cell we want to find.
1265 * @param spanY The vertical span of the cell we want to find.
1266 * @param ignoreView The home screen item we should treat as not occupying any space
1267 * @return
1268 */
1269 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1270 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1271 }
1272
1273 /**
1274 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1275 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1276 *
1277 * @param spanX The horizontal span of the cell we want to find.
1278 * @param spanY The vertical span of the cell we want to find.
1279 * @param ignoreView The home screen item we should treat as not occupying any space
1280 * @param intersectX The X coordinate of the cell that we should try to overlap
1281 * @param intersectX The Y coordinate of the cell that we should try to overlap
1282 *
1283 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1284 */
1285 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1286 int intersectX, int intersectY) {
1287 return findCellForSpanThatIntersectsIgnoring(
1288 cellXY, spanX, spanY, intersectX, intersectY, null);
1289 }
1290
1291 /**
1292 * The superset of the above two methods
1293 */
1294 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1295 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001296 // mark space take by ignoreView as available (method checks if ignoreView is null)
1297 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001298
Michael Jurka28750fb2010-09-24 17:43:49 -07001299 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001300 while (true) {
1301 int startX = 0;
1302 if (intersectX >= 0) {
1303 startX = Math.max(startX, intersectX - (spanX - 1));
1304 }
1305 int endX = mCountX - (spanX - 1);
1306 if (intersectX >= 0) {
1307 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1308 }
1309 int startY = 0;
1310 if (intersectY >= 0) {
1311 startY = Math.max(startY, intersectY - (spanY - 1));
1312 }
1313 int endY = mCountY - (spanY - 1);
1314 if (intersectY >= 0) {
1315 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1316 }
1317
Winson Chungbbc60d82010-11-11 16:34:41 -08001318 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001319 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001320 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001321 for (int i = 0; i < spanX; i++) {
1322 for (int j = 0; j < spanY; j++) {
1323 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001324 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001325 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001326 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001327 continue inner;
1328 }
1329 }
1330 }
1331 if (cellXY != null) {
1332 cellXY[0] = x;
1333 cellXY[1] = y;
1334 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001335 foundCell = true;
1336 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001337 }
1338 }
1339 if (intersectX == -1 && intersectY == -1) {
1340 break;
1341 } else {
1342 // if we failed to find anything, try again but without any requirements of
1343 // intersecting
1344 intersectX = -1;
1345 intersectY = -1;
1346 continue;
1347 }
1348 }
1349
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001350 // re-mark space taken by ignoreView as occupied
1351 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001352 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001353 }
1354
1355 /**
1356 * Called when drag has left this CellLayout or has been completed (successfully or not)
1357 */
1358 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001359 // This can actually be called when we aren't in a drag, e.g. when adding a new
1360 // item to this layout via the customize drawer.
1361 // Guard against that case.
1362 if (mDragging) {
1363 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001364
Joe Onorato4be866d2010-10-10 11:26:02 -07001365 // Fade out the drag indicators
1366 if (mCrosshairsAnimator != null) {
1367 mCrosshairsAnimator.animateOut();
1368 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001369 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001370
1371 // Invalidate the drag data
1372 mDragCell[0] = -1;
1373 mDragCell[1] = -1;
1374 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1375 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1376
Michael Jurka33945b22010-12-21 18:19:38 -08001377 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001378 }
1379
1380 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001381 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001382 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001383 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 *
1385 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001387 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001388 if (child != null) {
1389 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001390 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001391 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001392 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001393 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001394 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001395 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 }
1397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 /**
1399 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001400 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 * @param child The child that is being dragged
1402 */
1403 void onDragChild(View child) {
1404 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1405 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001406 }
1407
1408 /**
1409 * A drag event has begun over this layout.
1410 * It may have begun over this layout (in which case onDragChild is called first),
1411 * or it may have begun on another layout.
1412 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001413 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001414 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001415 // Fade in the drag indicators
1416 if (mCrosshairsAnimator != null) {
1417 mCrosshairsAnimator.animateIn();
1418 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001419 }
1420 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001422
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001425 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 * @param cellX X coordinate of upper left corner expressed as a cell position
1427 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001428 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001430 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001432 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 final int cellWidth = mCellWidth;
1434 final int cellHeight = mCellHeight;
1435 final int widthGap = mWidthGap;
1436 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001437
Winson Chung4b825dcd2011-06-19 12:41:22 -07001438 final int hStartPadding = getPaddingLeft();
1439 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001440
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1442 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1443
1444 int x = hStartPadding + cellX * (cellWidth + widthGap);
1445 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001446
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001447 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001449
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001451 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001453 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 * @param width Width in pixels
1455 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001456 * @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 -08001457 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001458 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001459 return rectToCell(getResources(), width, height, result);
1460 }
1461
1462 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 // Always assume we're working with the smallest span to make sure we
1464 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001465 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1466 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001468
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 // Always round up to next largest cell
1470 int spanX = (width + smallerSize) / smallerSize;
1471 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001472
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001473 if (result == null) {
1474 return new int[] { spanX, spanY };
1475 }
1476 result[0] = spanX;
1477 result[1] = spanY;
1478 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
1480
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001481 public int[] cellSpansToSize(int hSpans, int vSpans) {
1482 int[] size = new int[2];
1483 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1484 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1485 return size;
1486 }
1487
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001489 * Calculate the grid spans needed to fit given item
1490 */
1491 public void calculateSpans(ItemInfo info) {
1492 final int minWidth;
1493 final int minHeight;
1494
1495 if (info instanceof LauncherAppWidgetInfo) {
1496 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1497 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1498 } else if (info instanceof PendingAddWidgetInfo) {
1499 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1500 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1501 } else {
1502 // It's not a widget, so it must be 1x1
1503 info.spanX = info.spanY = 1;
1504 return;
1505 }
1506 int[] spans = rectToCell(minWidth, minHeight, null);
1507 info.spanX = spans[0];
1508 info.spanY = spans[1];
1509 }
1510
1511 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 * Find the first vacant cell, if there is one.
1513 *
1514 * @param vacant Holds the x and y coordinate of the vacant cell
1515 * @param spanX Horizontal cell span.
1516 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001517 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 * @return True if a vacant cell was found
1519 */
1520 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521
Michael Jurka0280c3b2010-09-17 15:00:07 -07001522 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 }
1524
1525 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1526 int xCount, int yCount, boolean[][] occupied) {
1527
Adam Cohen2801caf2011-05-13 20:57:39 -07001528 for (int y = 0; y < yCount; y++) {
1529 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 boolean available = !occupied[x][y];
1531out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1532 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1533 available = available && !occupied[i][j];
1534 if (!available) break out;
1535 }
1536 }
1537
1538 if (available) {
1539 vacant[0] = x;
1540 vacant[1] = y;
1541 return true;
1542 }
1543 }
1544 }
1545
1546 return false;
1547 }
1548
Michael Jurka0280c3b2010-09-17 15:00:07 -07001549 private void clearOccupiedCells() {
1550 for (int x = 0; x < mCountX; x++) {
1551 for (int y = 0; y < mCountY; y++) {
1552 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
1554 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001555 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556
Adam Cohen1b607ed2011-03-03 17:26:50 -08001557 /**
1558 * Given a view, determines how much that view can be expanded in all directions, in terms of
1559 * whether or not there are other items occupying adjacent cells. Used by the
1560 * AppWidgetResizeFrame to determine how the widget can be resized.
1561 */
Adam Cohend4844c32011-02-18 19:25:06 -08001562 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001563 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001564 boolean flag;
1565
Adam Cohen1b607ed2011-03-03 17:26:50 -08001566 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001567 for (int x = lp.cellX - 1; x >= 0; x--) {
1568 flag = false;
1569 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1570 if (mOccupied[x][y]) flag = true;
1571 }
1572 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001573 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001574 }
1575
Adam Cohen1b607ed2011-03-03 17:26:50 -08001576 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001577 for (int y = lp.cellY - 1; y >= 0; y--) {
1578 flag = false;
1579 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1580 if (mOccupied[x][y]) flag = true;
1581 }
1582 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001583 expandability[AppWidgetResizeFrame.TOP]++;
1584 }
Adam Cohend4844c32011-02-18 19:25:06 -08001585
Adam Cohen1b607ed2011-03-03 17:26:50 -08001586 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001587 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1588 flag = false;
1589 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1590 if (mOccupied[x][y]) flag = true;
1591 }
1592 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001593 expandability[AppWidgetResizeFrame.RIGHT]++;
1594 }
Adam Cohend4844c32011-02-18 19:25:06 -08001595
Adam Cohen1b607ed2011-03-03 17:26:50 -08001596 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001597 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1598 flag = false;
1599 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1600 if (mOccupied[x][y]) flag = true;
1601 }
1602 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001603 expandability[AppWidgetResizeFrame.BOTTOM]++;
1604 }
Adam Cohend4844c32011-02-18 19:25:06 -08001605 }
1606
Michael Jurka0280c3b2010-09-17 15:00:07 -07001607 public void onMove(View view, int newCellX, int newCellY) {
1608 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1609 markCellsAsUnoccupiedForView(view);
1610 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1611 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612
Adam Cohend4844c32011-02-18 19:25:06 -08001613 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001614 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001615 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1616 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1617 }
1618
Adam Cohend4844c32011-02-18 19:25:06 -08001619 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001620 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001621 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1622 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1623 }
1624
1625 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1626 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1627 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1628 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 }
1630 }
1631 }
1632
Adam Cohen2801caf2011-05-13 20:57:39 -07001633 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001634 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001635 (Math.max((mCountX - 1), 0) * mWidthGap);
1636 }
1637
1638 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001639 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001640 (Math.max((mCountY - 1), 0) * mHeightGap);
1641 }
1642
Michael Jurka66d72172011-04-12 16:29:25 -07001643 public boolean isOccupied(int x, int y) {
1644 if (x < mCountX && y < mCountY) {
1645 return mOccupied[x][y];
1646 } else {
1647 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1648 }
1649 }
1650
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 @Override
1652 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1653 return new CellLayout.LayoutParams(getContext(), attrs);
1654 }
1655
1656 @Override
1657 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1658 return p instanceof CellLayout.LayoutParams;
1659 }
1660
1661 @Override
1662 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1663 return new CellLayout.LayoutParams(p);
1664 }
1665
Winson Chungaafa03c2010-06-11 17:34:16 -07001666 public static class CellLayoutAnimationController extends LayoutAnimationController {
1667 public CellLayoutAnimationController(Animation animation, float delay) {
1668 super(animation, delay);
1669 }
1670
1671 @Override
1672 protected long getDelayForView(View view) {
1673 return (int) (Math.random() * 150);
1674 }
1675 }
1676
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1678 /**
1679 * Horizontal location of the item in the grid.
1680 */
1681 @ViewDebug.ExportedProperty
1682 public int cellX;
1683
1684 /**
1685 * Vertical location of the item in the grid.
1686 */
1687 @ViewDebug.ExportedProperty
1688 public int cellY;
1689
1690 /**
1691 * Number of cells spanned horizontally by the item.
1692 */
1693 @ViewDebug.ExportedProperty
1694 public int cellHSpan;
1695
1696 /**
1697 * Number of cells spanned vertically by the item.
1698 */
1699 @ViewDebug.ExportedProperty
1700 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001701
Adam Cohen1b607ed2011-03-03 17:26:50 -08001702 /**
1703 * Indicates whether the item will set its x, y, width and height parameters freely,
1704 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1705 */
Adam Cohend4844c32011-02-18 19:25:06 -08001706 public boolean isLockedToGrid = true;
1707
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 /**
1709 * Is this item currently being dragged
1710 */
1711 public boolean isDragging;
1712
1713 // X coordinate of the view in the layout.
1714 @ViewDebug.ExportedProperty
1715 int x;
1716 // Y coordinate of the view in the layout.
1717 @ViewDebug.ExportedProperty
1718 int y;
1719
Patrick Dubroyce34a972010-10-19 10:34:32 -07001720 /**
1721 * The old X coordinate of this item, relative to its current parent.
1722 * Used to animate the item into its new position.
1723 */
1724 int oldX;
1725
1726 /**
1727 * The old Y coordinate of this item, relative to its current parent.
1728 * Used to animate the item into its new position.
1729 */
1730 int oldY;
1731
Romain Guy84f296c2009-11-04 15:00:44 -08001732 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001733
Michael Jurkad3ef3062010-11-23 16:23:58 -08001734 boolean animateDrop;
1735
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 public LayoutParams(Context c, AttributeSet attrs) {
1737 super(c, attrs);
1738 cellHSpan = 1;
1739 cellVSpan = 1;
1740 }
1741
1742 public LayoutParams(ViewGroup.LayoutParams source) {
1743 super(source);
1744 cellHSpan = 1;
1745 cellVSpan = 1;
1746 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001747
1748 public LayoutParams(LayoutParams source) {
1749 super(source);
1750 this.cellX = source.cellX;
1751 this.cellY = source.cellY;
1752 this.cellHSpan = source.cellHSpan;
1753 this.cellVSpan = source.cellVSpan;
1754 }
1755
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001757 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 this.cellX = cellX;
1759 this.cellY = cellY;
1760 this.cellHSpan = cellHSpan;
1761 this.cellVSpan = cellVSpan;
1762 }
1763
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001764 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001765 if (isLockedToGrid) {
1766 final int myCellHSpan = cellHSpan;
1767 final int myCellVSpan = cellVSpan;
1768 final int myCellX = cellX;
1769 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001770
Adam Cohend4844c32011-02-18 19:25:06 -08001771 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1772 leftMargin - rightMargin;
1773 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1774 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001775 x = myCellX * (cellWidth + widthGap) + leftMargin;
1776 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001777 }
1778 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001779
Winson Chungaafa03c2010-06-11 17:34:16 -07001780 public String toString() {
1781 return "(" + this.cellX + ", " + this.cellY + ")";
1782 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001783
1784 public void setWidth(int width) {
1785 this.width = width;
1786 }
1787
1788 public int getWidth() {
1789 return width;
1790 }
1791
1792 public void setHeight(int height) {
1793 this.height = height;
1794 }
1795
1796 public int getHeight() {
1797 return height;
1798 }
1799
1800 public void setX(int x) {
1801 this.x = x;
1802 }
1803
1804 public int getX() {
1805 return x;
1806 }
1807
1808 public void setY(int y) {
1809 this.y = y;
1810 }
1811
1812 public int getY() {
1813 return y;
1814 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 }
1816
Michael Jurka0280c3b2010-09-17 15:00:07 -07001817 // This class stores info for two purposes:
1818 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1819 // its spanX, spanY, and the screen it is on
1820 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1821 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1822 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001823 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001825 int cellX = -1;
1826 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 int spanX;
1828 int spanY;
1829 int screen;
1830 boolean valid;
1831
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 @Override
1833 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001834 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1835 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
1837 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838}