blob: a9ba88d94045d1e1b369d2088381755a299b47cd [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
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057 private int mCellWidth;
58 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070059
Winson Chungaafa03c2010-06-11 17:34:16 -070060 private int mLeftPadding;
61 private int mRightPadding;
62 private int mTopPadding;
63 private int mBottomPadding;
64
Adam Cohend22015c2010-07-26 22:02:18 -070065 private int mCountX;
66 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
68 private int mWidthGap;
69 private int mHeightGap;
70
71 private final Rect mRect = new Rect();
72 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070073
Patrick Dubroyde7658b2010-09-27 11:15:43 -070074 // These are temporary variables to prevent having to allocate a new object just to
75 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070076 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070077 private final int[] mTmpPoint = new int[2];
78 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070079
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 boolean[][] mOccupied;
81
Michael Jurkadee05892010-07-27 10:01:56 -070082 private OnTouchListener mInterceptTouchListener;
83
Michael Jurka5f1c5092010-09-03 14:15:02 -070084 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070085 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070086
Michael Jurka33945b22010-12-21 18:19:38 -080087 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080088 private Drawable mActiveBackground;
89 private Drawable mActiveGlowBackground;
90 private Drawable mNormalBackgroundMini;
91 private Drawable mNormalGlowBackgroundMini;
92 private Drawable mActiveBackgroundMini;
93 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -070094 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -080095 private Rect mGlowBackgroundRect;
96 private float mGlowBackgroundScale;
97 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -070098
Adam Cohendf035382011-04-11 17:22:04 -070099 private boolean mAcceptsDrops = true;
Michael Jurka33945b22010-12-21 18:19:38 -0800100 // If we're actively dragging something over this screen, mIsDragOverlapping is true
101 private boolean mIsDragOverlapping = false;
102 private boolean mIsDragOccuring = false;
103 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700104 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700105
Winson Chung150fbab2010-09-29 17:14:26 -0700106 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700107 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Winson Chung63257c12011-05-05 17:06:13 -0700108 private Point[] mDragOutlines = new Point[4];
Chet Haase472b2812010-10-14 07:02:04 -0700109 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700110 private InterruptibleInOutAnimator[] mDragOutlineAnims =
111 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700112
113 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700114 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700115 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700116
Patrick Dubroy96864c32011-03-10 17:17:23 -0800117 private BubbleTextView mPressedOrFocusedIcon;
118
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700119 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700120 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700121 private float mCrosshairsVisibility = 0.0f;
122
Adam Cohenbfbfd262011-06-13 16:55:12 -0700123 private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new
124 HashMap<CellLayout.LayoutParams, ObjectAnimator>();
125
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700126 // When a drag operation is in progress, holds the nearest cell to the touch point
127 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Joe Onorato4be866d2010-10-10 11:26:02 -0700129 private boolean mDragging = false;
130
Patrick Dubroyce34a972010-10-19 10:34:32 -0700131 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800132 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 public CellLayout(Context context) {
135 this(context, null);
136 }
137
138 public CellLayout(Context context, AttributeSet attrs) {
139 this(context, attrs, 0);
140 }
141
142 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
143 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700144
145 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
146 // the user where a dragged item will land when dropped.
147 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
150
151 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
152 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700153 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
154 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700155
Adam Cohend22015c2010-07-26 22:02:18 -0700156 mLeftPadding =
157 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
158 mRightPadding =
159 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
160 mTopPadding =
161 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
162 mBottomPadding =
163 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700164
Adam Cohend22015c2010-07-26 22:02:18 -0700165 mCountX = LauncherModel.getCellCountX();
166 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700167 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
169 a.recycle();
170
171 setAlwaysDrawnWithCacheEnabled(false);
172
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700173 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700174
Winson Chungb26f3d62011-06-02 10:49:29 -0700175 mNormalBackground = res.getDrawable(R.drawable.homescreen_large_blue);
176 mActiveBackground = res.getDrawable(R.drawable.homescreen_large_green);
177 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_large_green_strong);
Michael Jurka33945b22010-12-21 18:19:38 -0800178
Winson Chungb26f3d62011-06-02 10:49:29 -0700179 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
180 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
181 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
182 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
Michael Jurka33945b22010-12-21 18:19:38 -0800183
Winson Chungb26f3d62011-06-02 10:49:29 -0700184 mNormalBackground.setFilterBitmap(true);
185 mActiveBackground.setFilterBitmap(true);
186 mActiveGlowBackground.setFilterBitmap(true);
187 mNormalBackgroundMini.setFilterBitmap(true);
188 mNormalGlowBackgroundMini.setFilterBitmap(true);
189 mActiveBackgroundMini.setFilterBitmap(true);
190 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700191
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700192 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700193
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700194 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700195 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700196
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700197 // Set up the animation for fading the crosshairs in and out
198 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700199 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700200 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700201 public void onAnimationUpdate(ValueAnimator animation) {
202 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700203 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700204 }
205 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700206 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700207
Joe Onorato4be866d2010-10-10 11:26:02 -0700208 for (int i = 0; i < mDragOutlines.length; i++) {
209 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700210 }
211
212 // When dragging things around the home screens, we show a green outline of
213 // where the item will land. The outlines gradually fade out, leaving a trail
214 // behind the drag path.
215 // Set up all the animations that are used to implement this fading.
216 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700217 final float fromAlphaValue = 0;
218 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700219
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700220 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700221
222 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700223 final InterruptibleInOutAnimator anim =
224 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700225 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700226 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700227 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700228 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700229 final Bitmap outline = (Bitmap)anim.getTag();
230
231 // If an animation is started and then stopped very quickly, we can still
232 // get spurious updates we've cleared the tag. Guard against this.
233 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700234 if (false) {
235 Object val = animation.getAnimatedValue();
236 Log.d(TAG, "anim " + thisIndex + " update: " + val +
237 ", isStopped " + anim.isStopped());
238 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700239 // Try to prevent it from continuing to run
240 animation.cancel();
241 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700242 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700243 final int left = mDragOutlines[thisIndex].x;
244 final int top = mDragOutlines[thisIndex].y;
245 CellLayout.this.invalidate(left, top,
246 left + outline.getWidth(), top + outline.getHeight());
247 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700248 }
249 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700250 // The animation holds a reference to the drag outline bitmap as long is it's
251 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700252 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700253 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700254 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700255 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700256 anim.setTag(null);
257 }
258 }
259 });
260 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700261 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700262
Michael Jurka18014792010-10-14 09:01:34 -0700263 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800264 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700265 setHoverScale(1.0f);
266 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800267
Michael Jurka8c920dd2011-01-20 14:16:56 -0800268 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700269 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800270 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700271 }
272
Michael Jurkaf6440da2011-04-05 14:50:34 -0700273 static int widthInPortrait(Resources r, int numCells) {
274 // We use this method from Workspace to figure out how many rows/columns Launcher should
275 // have. We ignore the left/right padding on CellLayout because it turns out in our design
276 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700277 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
278 int widthGap = r.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
Michael Jurkaf6440da2011-04-05 14:50:34 -0700279
Michael Jurka32271c32011-06-01 17:03:13 -0700280 return widthGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700281 }
282
Michael Jurkaf6440da2011-04-05 14:50:34 -0700283 static int heightInLandscape(Resources r, int numCells) {
284 // We use this method from Workspace to figure out how many rows/columns Launcher should
285 // have. We ignore the left/right padding on CellLayout because it turns out in our design
286 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700287 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
288 int heightGap = r.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
Michael Jurkaf6440da2011-04-05 14:50:34 -0700289
Michael Jurka32271c32011-06-01 17:03:13 -0700290 return heightGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700291 }
292
Adam Cohen2801caf2011-05-13 20:57:39 -0700293 public void enableHardwareLayers() {
294 mChildren.enableHardwareLayers();
295 }
296
297 public void setGridSize(int x, int y) {
298 mCountX = x;
299 mCountY = y;
300 mOccupied = new boolean[mCountX][mCountY];
301 }
302
Patrick Dubroy96864c32011-03-10 17:17:23 -0800303 private void invalidateBubbleTextView(BubbleTextView icon) {
304 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700305 invalidate(icon.getLeft() + getLeftPadding() - padding,
306 icon.getTop() + getTopPadding() - padding,
307 icon.getRight() + getLeftPadding() + padding,
308 icon.getBottom() + getTopPadding() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800309 }
310
311 void setPressedOrFocusedIcon(BubbleTextView icon) {
312 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
313 // requires an expanded clip rect (due to the glow's blur radius)
314 BubbleTextView oldIcon = mPressedOrFocusedIcon;
315 mPressedOrFocusedIcon = icon;
316 if (oldIcon != null) {
317 invalidateBubbleTextView(oldIcon);
318 }
319 if (mPressedOrFocusedIcon != null) {
320 invalidateBubbleTextView(mPressedOrFocusedIcon);
321 }
322 }
323
Winson Chung6e314082011-01-27 16:46:51 -0800324 public CellLayoutChildren getChildrenLayout() {
325 if (getChildCount() > 0) {
326 return (CellLayoutChildren) getChildAt(0);
327 }
328 return null;
329 }
330
Michael Jurka33945b22010-12-21 18:19:38 -0800331 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
332 if (mIsDefaultDropTarget != isDefaultDropTarget) {
333 mIsDefaultDropTarget = isDefaultDropTarget;
334 invalidate();
335 }
336 }
337
Michael Jurka33945b22010-12-21 18:19:38 -0800338 void setIsDragOccuring(boolean isDragOccuring) {
339 if (mIsDragOccuring != isDragOccuring) {
340 mIsDragOccuring = isDragOccuring;
341 invalidate();
342 }
343 }
344
345 void setIsDragOverlapping(boolean isDragOverlapping) {
346 if (mIsDragOverlapping != isDragOverlapping) {
347 mIsDragOverlapping = isDragOverlapping;
348 invalidate();
349 }
350 }
351
352 boolean getIsDragOverlapping() {
353 return mIsDragOverlapping;
354 }
355
356 private void updateGlowRect() {
357 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700358 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
359 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800360 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700361 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
362 invalidate();
363 }
364
365 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800366 if (scaleFactor != mGlowBackgroundScale) {
367 mGlowBackgroundScale = scaleFactor;
368 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800369 if (getParent() != null) {
370 ((View) getParent()).invalidate();
371 }
Michael Jurka18014792010-10-14 09:01:34 -0700372 }
373 }
374
375 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800376 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700377 }
378
379 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800380 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700381 }
382
383 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800384 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700385 invalidate();
386 }
387
388 void animateDrop() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700389 Resources res = getResources();
390 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
391 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
392 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
393 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
394 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
395 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
Michael Jurka18014792010-10-14 09:01:34 -0700396
Winson Chungb26f3d62011-06-02 10:49:29 -0700397 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
398 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDuration));
Michael Jurka18014792010-10-14 09:01:34 -0700399
Winson Chungb26f3d62011-06-02 10:49:29 -0700400 AnimatorSet bouncer = new AnimatorSet();
401 bouncer.play(scaleUp).before(scaleDown);
402 bouncer.play(scaleUp).with(alphaFadeOut);
403 bouncer.addListener(new AnimatorListenerAdapter() {
404 @Override
405 public void onAnimationStart(Animator animation) {
406 setIsDragOverlapping(true);
407 }
408 @Override
409 public void onAnimationEnd(Animator animation) {
410 setIsDragOverlapping(false);
411 setHoverScale(1.0f);
412 setHoverAlpha(1.0f);
413 }
414 });
415 bouncer.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 }
417
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700418 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700419 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700420 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
421 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
422 // When we're small, we are either drawn normally or in the "accepts drops" state (during
423 // a drag). However, we also drag the mini hover background *over* one of those two
424 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700425 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700426 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800427 boolean mini = getScaleX() < 0.5f;
428
429 if (mIsDragOverlapping) {
430 // In the mini case, we draw the active_glow bg *over* the active background
431 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
432 } else if (mIsDragOccuring && mAcceptsDrops) {
433 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800434 } else if (mIsDefaultDropTarget && mini) {
435 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700436 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800437 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700438 }
Michael Jurka33945b22010-12-21 18:19:38 -0800439
440 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
441 bg.setBounds(mBackgroundRect);
442 bg.draw(canvas);
443
444 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700445 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800446 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700447 // If the hover background's scale is greater than 1, we'll be drawing outside
448 // the bounds of this CellLayout. Get around that by temporarily increasing the
449 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800450 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700451 Rect clipRect = canvas.getClipBounds();
452 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
453 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
454 canvas.save(Canvas.CLIP_SAVE_FLAG);
455 canvas.clipRect(-marginX, -marginY,
456 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
457 modifiedClipRect = true;
458 }
459
Michael Jurka33945b22010-12-21 18:19:38 -0800460 mActiveGlowBackgroundMini.setAlpha(
461 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
462 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
463 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700464 if (modifiedClipRect) {
465 canvas.restore();
466 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700467 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700468 }
Romain Guya6abce82009-11-10 02:54:41 -0800469
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700470 if (mCrosshairsVisibility > 0.0f) {
471 final int countX = mCountX;
472 final int countY = mCountY;
473
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700474 final float MAX_ALPHA = 0.4f;
475 final int MAX_VISIBLE_DISTANCE = 600;
476 final float DISTANCE_MULTIPLIER = 0.002f;
477
478 final Drawable d = mCrosshairsDrawable;
479 final int width = d.getIntrinsicWidth();
480 final int height = d.getIntrinsicHeight();
481
482 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
483 for (int col = 0; col <= countX; col++) {
484 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
485 for (int row = 0; row <= countY; row++) {
486 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
487 float dist = mTmpPointF.length();
488 // Crosshairs further from the drag point are more faint
489 float alpha = Math.min(MAX_ALPHA,
490 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
491 if (alpha > 0.0f) {
492 d.setBounds(x, y, x + width, y + height);
493 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
494 d.draw(canvas);
495 }
496 y += mCellHeight + mHeightGap;
497 }
498 x += mCellWidth + mWidthGap;
499 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700500 }
Winson Chung150fbab2010-09-29 17:14:26 -0700501
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700502 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700503 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700504 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700505 if (alpha > 0) {
506 final Point p = mDragOutlines[i];
507 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700508 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700509 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700510 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700511 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800512
513 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
514 // requires an expanded clip rect (due to the glow's blur radius)
515 if (mPressedOrFocusedIcon != null) {
516 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
517 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
518 if (b != null) {
519 canvas.drawBitmap(b,
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700520 mPressedOrFocusedIcon.getLeft() + getLeftPadding() - padding,
521 mPressedOrFocusedIcon.getTop() + getTopPadding() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800522 null);
523 }
524 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700525 }
526
527 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700528 public void cancelLongPress() {
529 super.cancelLongPress();
530
531 // Cancel long press for all children
532 final int count = getChildCount();
533 for (int i = 0; i < count; i++) {
534 final View child = getChildAt(i);
535 child.cancelLongPress();
536 }
537 }
538
Michael Jurkadee05892010-07-27 10:01:56 -0700539 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
540 mInterceptTouchListener = listener;
541 }
542
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700544 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 }
546
547 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700548 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 }
550
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700551 public boolean addViewToCellLayout(
552 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700553 final LayoutParams lp = params;
554
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 // Generate an id for each view, this assumes we have at most 256x256 cells
556 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700557 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700558 // If the horizontal or vertical span is set to -1, it is taken to
559 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700560 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
561 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562
Winson Chungaafa03c2010-06-11 17:34:16 -0700563 child.setId(childId);
564
Michael Jurka8c920dd2011-01-20 14:16:56 -0800565 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700566
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700567 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700568
Winson Chungaafa03c2010-06-11 17:34:16 -0700569 return true;
570 }
571 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700573
Michael Jurkabea15192010-11-17 12:33:46 -0800574 public void setAcceptsDrops(boolean acceptsDrops) {
575 if (mAcceptsDrops != acceptsDrops) {
576 mAcceptsDrops = acceptsDrops;
577 invalidate();
578 }
579 }
580
Michael Jurka3e7c7632010-10-02 16:01:03 -0700581 public boolean getAcceptsDrops() {
582 return mAcceptsDrops;
583 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584
585 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700586 public void removeAllViews() {
587 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800588 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700589 }
590
591 @Override
592 public void removeAllViewsInLayout() {
593 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800594 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700595 }
596
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700597 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800598 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700599 }
600
Michael Jurka0280c3b2010-09-17 15:00:07 -0700601 @Override
602 public void removeView(View view) {
603 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800604 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700605 }
606
607 @Override
608 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800609 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
610 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700611 }
612
613 @Override
614 public void removeViewInLayout(View view) {
615 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800616 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700617 }
618
619 @Override
620 public void removeViews(int start, int count) {
621 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800622 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700623 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800624 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700625 }
626
627 @Override
628 public void removeViewsInLayout(int start, int count) {
629 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800630 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700631 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800632 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700633 }
634
Michael Jurka8c920dd2011-01-20 14:16:56 -0800635 public void drawChildren(Canvas canvas) {
636 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
638
Michael Jurkaabded662011-03-04 12:06:57 -0800639 void buildChildrenLayer() {
640 mChildren.buildLayer();
641 }
642
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 @Override
644 protected void onAttachedToWindow() {
645 super.onAttachedToWindow();
646 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
647 }
648
Michael Jurkaaf442092010-06-10 17:01:57 -0700649 public void setTagToCellInfoForPoint(int touchX, int touchY) {
650 final CellInfo cellInfo = mCellInfo;
651 final Rect frame = mRect;
652 final int x = touchX + mScrollX;
653 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800654 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700655
656 boolean found = false;
657 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800658 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800659 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700660
Adam Cohen1b607ed2011-03-03 17:26:50 -0800661 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
662 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700663 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700664
665 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
666 // offset that by this CellLayout's padding to test an (x,y) point that is relative
667 // to this view.
668 final int tmpXY[] = mTmpXY;
669 child.getLocationOnScreen(tmpXY);
670 frame.offset(mLeftPadding, mTopPadding);
671
Michael Jurkaaf442092010-06-10 17:01:57 -0700672 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700673 cellInfo.cell = child;
674 cellInfo.cellX = lp.cellX;
675 cellInfo.cellY = lp.cellY;
676 cellInfo.spanX = lp.cellHSpan;
677 cellInfo.spanY = lp.cellVSpan;
678 cellInfo.valid = true;
679 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700680 break;
681 }
682 }
683 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700684
Michael Jurkaaf442092010-06-10 17:01:57 -0700685 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700686 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700687 pointToCellExact(x, y, cellXY);
688
Michael Jurkaaf442092010-06-10 17:01:57 -0700689 cellInfo.cell = null;
690 cellInfo.cellX = cellXY[0];
691 cellInfo.cellY = cellXY[1];
692 cellInfo.spanX = 1;
693 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700694 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
695 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700696 }
697 setTag(cellInfo);
698 }
699
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 @Override
701 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700702 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
703 return true;
704 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 final int action = ev.getAction();
706 final CellInfo cellInfo = mCellInfo;
707
708 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700709 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 } else if (action == MotionEvent.ACTION_UP) {
711 cellInfo.cell = null;
712 cellInfo.cellX = -1;
713 cellInfo.cellY = -1;
714 cellInfo.spanX = 0;
715 cellInfo.spanY = 0;
716 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 setTag(cellInfo);
718 }
719
720 return false;
721 }
722
723 @Override
724 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700725 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 }
727
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700728 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700729 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 * @param x X coordinate of the point
731 * @param y Y coordinate of the point
732 * @param result Array of 2 ints to hold the x and y coordinate of the cell
733 */
734 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700735 final int hStartPadding = getLeftPadding();
736 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737
738 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
739 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
740
Adam Cohend22015c2010-07-26 22:02:18 -0700741 final int xAxis = mCountX;
742 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743
744 if (result[0] < 0) result[0] = 0;
745 if (result[0] >= xAxis) result[0] = xAxis - 1;
746 if (result[1] < 0) result[1] = 0;
747 if (result[1] >= yAxis) result[1] = yAxis - 1;
748 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 /**
751 * Given a point, return the cell that most closely encloses that point
752 * @param x X coordinate of the point
753 * @param y Y coordinate of the point
754 * @param result Array of 2 ints to hold the x and y coordinate of the cell
755 */
756 void pointToCellRounded(int x, int y, int[] result) {
757 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
758 }
759
760 /**
761 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700762 *
763 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700765 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 * @param result Array of 2 ints to hold the x and y coordinate of the point
767 */
768 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700769 final int hStartPadding = getLeftPadding();
770 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771
772 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
773 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
774 }
775
Adam Cohene3e27a82011-04-15 12:07:39 -0700776 /**
777 * Given a cell coordinate, return the point that represents the upper left corner of that cell
778 *
779 * @param cellX X coordinate of the cell
780 * @param cellY Y coordinate of the cell
781 *
782 * @param result Array of 2 ints to hold the x and y coordinate of the point
783 */
784 void cellToCenterPoint(int cellX, int cellY, int[] result) {
785 final int hStartPadding = getLeftPadding();
786 final int vStartPadding = getTopPadding();
787
788 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
789 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
790 }
791
Romain Guy84f296c2009-11-04 15:00:44 -0800792 int getCellWidth() {
793 return mCellWidth;
794 }
795
796 int getCellHeight() {
797 return mCellHeight;
798 }
799
Adam Cohend4844c32011-02-18 19:25:06 -0800800 int getWidthGap() {
801 return mWidthGap;
802 }
803
804 int getHeightGap() {
805 return mHeightGap;
806 }
807
Romain Guy1a304a12009-11-10 00:02:32 -0800808 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700809 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800810 }
811
812 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700813 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800814 }
815
816 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700817 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800818 }
819
820 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700821 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800822 }
823
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700824 Rect getContentRect(Rect r) {
825 if (r == null) {
826 r = new Rect();
827 }
828 int left = getPaddingLeft();
829 int top = getPaddingTop();
830 int right = left + getWidth() - mLeftPadding - mRightPadding;
831 int bottom = top + getHeight() - mTopPadding - mBottomPadding;
832 r.set(left, top, right, bottom);
833 return r;
834 }
835
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 @Override
837 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
838 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700839
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700841 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
842
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
844 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
847 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
848 }
849
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 final int cellWidth = mCellWidth;
851 final int cellHeight = mCellHeight;
852
Adam Cohend22015c2010-07-26 22:02:18 -0700853 int numWidthGaps = mCountX - 1;
854 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855
Winson Chungece7f5b2010-10-22 14:54:12 -0700856 if (mWidthGap < 0 || mHeightGap < 0) {
857 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700858 mHeightGap = numHeightGaps > 0 ? vSpaceLeft / numHeightGaps : 0;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859
Winson Chungece7f5b2010-10-22 14:54:12 -0700860 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohen2801caf2011-05-13 20:57:39 -0700861 mWidthGap = numWidthGaps > 0 ? hSpaceLeft / numWidthGaps : 0;
Winson Chungaafa03c2010-06-11 17:34:16 -0700862
Winson Chungece7f5b2010-10-22 14:54:12 -0700863 // center it around the min gaps
864 int minGap = Math.min(mWidthGap, mHeightGap);
865 mWidthGap = mHeightGap = minGap;
866 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700867
Michael Jurka8c920dd2011-01-20 14:16:56 -0800868 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
869 int newWidth = widthSpecSize;
870 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700871 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800872 newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700873 ((mCountX - 1) * mWidthGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800874 newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700875 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700876 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700877 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800878
879 int count = getChildCount();
880 for (int i = 0; i < count; i++) {
881 View child = getChildAt(i);
Adam Cohen2801caf2011-05-13 20:57:39 -0700882 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mLeftPadding -
883 mRightPadding, MeasureSpec.EXACTLY);
884 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mTopPadding -
885 mBottomPadding, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800886 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
887 }
888 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 }
890
891 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700892 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800895 View child = getChildAt(i);
Adam Cohenbb60e2e2011-04-26 16:48:26 -0700896 child.layout(mLeftPadding, mTopPadding, r - l - mRightPadding , b - t - mBottomPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 }
898 }
899
900 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700901 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
902 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700903 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800904 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700905 }
906
907 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800909 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 }
911
912 @Override
913 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800914 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 }
916
Michael Jurka5f1c5092010-09-03 14:15:02 -0700917 public float getBackgroundAlpha() {
918 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700919 }
920
Michael Jurka742574b2011-02-02 23:51:01 -0800921 public void setFastBackgroundAlpha(float alpha) {
922 mBackgroundAlpha = alpha;
923 }
924
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700925 public void setBackgroundAlphaMultiplier(float multiplier) {
926 mBackgroundAlphaMultiplier = multiplier;
927 }
928
Adam Cohenddb82192010-11-10 16:32:54 -0800929 public float getBackgroundAlphaMultiplier() {
930 return mBackgroundAlphaMultiplier;
931 }
932
Michael Jurka5f1c5092010-09-03 14:15:02 -0700933 public void setBackgroundAlpha(float alpha) {
934 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700935 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700936 }
937
Michael Jurka5f1c5092010-09-03 14:15:02 -0700938 // Need to return true to let the view system know we know how to handle alpha-- this is
939 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
940 // versions
941 @Override
942 protected boolean onSetAlpha(int alpha) {
943 return true;
944 }
945
946 public void setAlpha(float alpha) {
947 setChildrenAlpha(alpha);
948 super.setAlpha(alpha);
949 }
950
Michael Jurka742574b2011-02-02 23:51:01 -0800951 public void setFastAlpha(float alpha) {
952 setFastChildrenAlpha(alpha);
953 super.setFastAlpha(alpha);
954 }
955
Michael Jurkadee05892010-07-27 10:01:56 -0700956 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700957 final int childCount = getChildCount();
958 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700959 getChildAt(i).setAlpha(alpha);
960 }
961 }
962
Michael Jurka742574b2011-02-02 23:51:01 -0800963 private void setFastChildrenAlpha(float alpha) {
964 final int childCount = getChildCount();
965 for (int i = 0; i < childCount; i++) {
966 getChildAt(i).setFastAlpha(alpha);
967 }
968 }
969
Patrick Dubroy440c3602010-07-13 17:50:32 -0700970 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800971 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700972 }
973
Adam Cohenbfbfd262011-06-13 16:55:12 -0700974 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration) {
975 CellLayoutChildren clc = getChildrenLayout();
976 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
977 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
978 final ItemInfo info = (ItemInfo) child.getTag();
979
980 // We cancel any existing animations
981 if (mReorderAnimators.containsKey(lp)) {
982 mReorderAnimators.get(lp).cancel();
983 mReorderAnimators.remove(lp);
984 }
985
986 int oldX = lp.x;
987 int oldY = lp.y;
988 mOccupied[lp.cellX][lp.cellY] = false;
989 mOccupied[cellX][cellY] = true;
990
991 lp.isLockedToGrid = true;
992 lp.cellX = info.cellX = cellX;
993 lp.cellY = info.cellY = cellY;
994 clc.setupLp(lp);
995 lp.isLockedToGrid = false;
996 int newX = lp.x;
997 int newY = lp.y;
998
999 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
1000 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
1001 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
1002 oa.setDuration(duration);
1003 mReorderAnimators.put(lp, oa);
1004 oa.addUpdateListener(new AnimatorUpdateListener() {
1005 public void onAnimationUpdate(ValueAnimator animation) {
1006 child.requestLayout();
1007 }
1008 });
1009 oa.addListener(new AnimatorListenerAdapter() {
1010 boolean cancelled = false;
1011 public void onAnimationEnd(Animator animation) {
1012 // If the animation was cancelled, it means that another animation
1013 // has interrupted this one, and we don't want to lock the item into
1014 // place just yet.
1015 if (!cancelled) {
1016 lp.isLockedToGrid = true;
1017 }
1018 if (mReorderAnimators.containsKey(lp)) {
1019 mReorderAnimators.remove(lp);
1020 }
1021 }
1022 public void onAnimationCancel(Animator animation) {
1023 cancelled = true;
1024 }
1025 });
1026 oa.start();
1027 return true;
1028 }
1029 return false;
1030 }
1031
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001032 /**
1033 * Estimate where the top left cell of the dragged item will land if it is dropped.
1034 *
1035 * @param originX The X value of the top left corner of the item
1036 * @param originY The Y value of the top left corner of the item
1037 * @param spanX The number of horizontal cells that the item spans
1038 * @param spanY The number of vertical cells that the item spans
1039 * @param result The estimated drop cell X and Y.
1040 */
1041 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001042 final int countX = mCountX;
1043 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001044
Michael Jurkaa63c4522010-08-19 13:52:27 -07001045 // pointToCellRounded takes the top left of a cell but will pad that with
1046 // cellWidth/2 and cellHeight/2 when finding the matching cell
1047 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001048
1049 // If the item isn't fully on this screen, snap to the edges
1050 int rightOverhang = result[0] + spanX - countX;
1051 if (rightOverhang > 0) {
1052 result[0] -= rightOverhang; // Snap to right
1053 }
1054 result[0] = Math.max(0, result[0]); // Snap to left
1055 int bottomOverhang = result[1] + spanY - countY;
1056 if (bottomOverhang > 0) {
1057 result[1] -= bottomOverhang; // Snap to bottom
1058 }
1059 result[1] = Math.max(0, result[1]); // Snap to top
1060 }
1061
Joe Onorato4be866d2010-10-10 11:26:02 -07001062 void visualizeDropLocation(
1063 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1064
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001065 final int oldDragCellX = mDragCell[0];
1066 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001067 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001068 if (v != null) {
1069 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1070 } else {
1071 mDragCenter.set(originX, originY);
1072 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001073
Adam Cohen2801caf2011-05-13 20:57:39 -07001074 if (dragOutline == null && v == null) {
1075 if (mCrosshairsDrawable != null) {
1076 invalidate();
1077 }
1078 return;
1079 }
1080
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001081 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001082 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001083 final int[] topLeft = mTmpPoint;
1084 cellToPoint(nearest[0], nearest[1], topLeft);
1085
Joe Onorato4be866d2010-10-10 11:26:02 -07001086 int left = topLeft[0];
1087 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001088
Winson Chunga9abd0e2010-10-27 17:18:37 -07001089 if (v != null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001090 // When drawing the drag outline, it did not account for margin offsets
1091 // added by the view's parent.
1092 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1093 left += lp.leftMargin;
1094 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001095
Adam Cohen99e8b402011-03-25 19:23:43 -07001096 // Offsets due to the size difference between the View and the dragOutline.
1097 // There is a size difference to account for the outer blur, which may lie
1098 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001099 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1100 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001101 } else {
1102 // Center the drag outline in the cell
Winson Chung4b576be2011-04-27 17:40:20 -07001103 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1104 - dragOutline.getWidth()) / 2;
1105 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1106 - dragOutline.getHeight()) / 2;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001107 }
Winson Chung150fbab2010-09-29 17:14:26 -07001108
Joe Onorato4be866d2010-10-10 11:26:02 -07001109 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001110 mDragOutlineAnims[oldIndex].animateOut();
1111 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001112
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001113 mDragOutlines[mDragOutlineCurrent].set(left, top);
1114 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1115 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001116 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001117
1118 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1119 if (mCrosshairsDrawable != null) {
1120 invalidate();
1121 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001122 }
1123
Adam Cohene0310962011-04-18 16:15:31 -07001124 public void clearDragOutlines() {
1125 final int oldIndex = mDragOutlineCurrent;
1126 mDragOutlineAnims[oldIndex].animateOut();
1127 mDragCell[0] = -1;
1128 mDragCell[1] = -1;
1129 }
1130
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001132 * Find a vacant area that will fit the given bounds nearest the requested
1133 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001134 *
Romain Guy51afc022009-05-04 18:03:43 -07001135 * @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.
Jeff Sharkey70864282009-04-07 21:08:40 -07001137 * @param spanX Horizontal span of the object.
1138 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001139 * @param result Array in which to place the result, or null (in which case a new array will
1140 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001141 * @return The X, Y cell of a vacant area that can contain this object,
1142 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001144 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001145 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1146 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001147 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001148
Michael Jurka6a1435d2010-09-27 17:35:12 -07001149 /**
1150 * Find a vacant area that will fit the given bounds nearest the requested
1151 * cell location. Uses Euclidean distance to score multiple vacant areas.
1152 *
1153 * @param pixelX The X location at which you want to search for a vacant area.
1154 * @param pixelY The Y location at which you want to search for a vacant area.
1155 * @param spanX Horizontal span of the object.
1156 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001157 * @param ignoreOccupied If true, the result can be an occupied cell
1158 * @param result Array in which to place the result, or null (in which case a new array will
1159 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001160 * @return The X, Y cell of a vacant area that can contain this object,
1161 * nearest the requested location.
1162 */
Adam Cohendf035382011-04-11 17:22:04 -07001163 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1164 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001165 // mark space take by ignoreView as available (method checks if ignoreView is null)
1166 markCellsAsUnoccupiedForView(ignoreView);
1167
Adam Cohene3e27a82011-04-15 12:07:39 -07001168 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1169 // to the center of the item, but we are searching based on the top-left cell, so
1170 // we translate the point over to correspond to the top-left.
1171 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1172 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1173
Jeff Sharkey70864282009-04-07 21:08:40 -07001174 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001175 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001176 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001177
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001178 final int countX = mCountX;
1179 final int countY = mCountY;
1180 final boolean[][] occupied = mOccupied;
1181
Winson Chungbbc60d82010-11-11 16:34:41 -08001182 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001183 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001184 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001185 if (ignoreOccupied) {
1186 for (int i = 0; i < spanX; i++) {
1187 for (int j = 0; j < spanY; j++) {
1188 if (occupied[x + i][y + j]) {
1189 // small optimization: we can skip to after the column we
1190 // just found an occupied cell
1191 x += i;
1192 continue inner;
1193 }
Michael Jurkac28de512010-08-13 11:27:44 -07001194 }
1195 }
1196 }
Winson Chung0be025d2011-05-23 17:45:09 -07001197 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001198 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199
Michael Jurkac28de512010-08-13 11:27:44 -07001200 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1201 + Math.pow(cellXY[1] - pixelY, 2));
1202 if (distance <= bestDistance) {
1203 bestDistance = distance;
1204 bestXY[0] = x;
1205 bestXY[1] = y;
1206 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 }
1208 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001209 // re-mark space taken by ignoreView as occupied
1210 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211
Adam Cohenc0dcf592011-06-01 15:30:43 -07001212 // Return -1, -1 if no suitable location found
1213 if (bestDistance == Double.MAX_VALUE) {
1214 bestXY[0] = -1;
1215 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001216 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001217 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001219
Adam Cohendf035382011-04-11 17:22:04 -07001220 /**
1221 * Find a vacant area 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[] findNearestVacantArea(
1234 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1235 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1236 }
1237
1238 /**
1239 * Find a starting cell position that will fit the given bounds nearest the requested
1240 * cell location. Uses Euclidean distance to score multiple vacant areas.
1241 *
1242 * @param pixelX The X location at which you want to search for a vacant area.
1243 * @param pixelY The Y location at which you want to search for a vacant area.
1244 * @param spanX Horizontal span of the object.
1245 * @param spanY Vertical span of the object.
1246 * @param ignoreView Considers space occupied by this view as unoccupied
1247 * @param result Previously returned value to possibly recycle.
1248 * @return The X, Y cell of a vacant area that can contain this object,
1249 * nearest the requested location.
1250 */
1251 int[] findNearestArea(
1252 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1253 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1254 }
1255
Michael Jurka0280c3b2010-09-17 15:00:07 -07001256 boolean existsEmptyCell() {
1257 return findCellForSpan(null, 1, 1);
1258 }
1259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001261 * Finds the upper-left coordinate of the first rectangle in the grid that can
1262 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1263 * then this method will only return coordinates for rectangles that contain the cell
1264 * (intersectX, intersectY)
1265 *
1266 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1267 * can be found.
1268 * @param spanX The horizontal span of the cell we want to find.
1269 * @param spanY The vertical span of the cell we want to find.
1270 *
1271 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001272 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001273 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1274 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1275 }
1276
1277 /**
1278 * Like above, but ignores any cells occupied by the item "ignoreView"
1279 *
1280 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1281 * can be found.
1282 * @param spanX The horizontal span of the cell we want to find.
1283 * @param spanY The vertical span of the cell we want to find.
1284 * @param ignoreView The home screen item we should treat as not occupying any space
1285 * @return
1286 */
1287 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1288 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1289 }
1290
1291 /**
1292 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1293 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1294 *
1295 * @param spanX The horizontal span of the cell we want to find.
1296 * @param spanY The vertical span of the cell we want to find.
1297 * @param ignoreView The home screen item we should treat as not occupying any space
1298 * @param intersectX The X coordinate of the cell that we should try to overlap
1299 * @param intersectX The Y coordinate of the cell that we should try to overlap
1300 *
1301 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1302 */
1303 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1304 int intersectX, int intersectY) {
1305 return findCellForSpanThatIntersectsIgnoring(
1306 cellXY, spanX, spanY, intersectX, intersectY, null);
1307 }
1308
1309 /**
1310 * The superset of the above two methods
1311 */
1312 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1313 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001314 // mark space take by ignoreView as available (method checks if ignoreView is null)
1315 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001316
Michael Jurka28750fb2010-09-24 17:43:49 -07001317 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001318 while (true) {
1319 int startX = 0;
1320 if (intersectX >= 0) {
1321 startX = Math.max(startX, intersectX - (spanX - 1));
1322 }
1323 int endX = mCountX - (spanX - 1);
1324 if (intersectX >= 0) {
1325 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1326 }
1327 int startY = 0;
1328 if (intersectY >= 0) {
1329 startY = Math.max(startY, intersectY - (spanY - 1));
1330 }
1331 int endY = mCountY - (spanY - 1);
1332 if (intersectY >= 0) {
1333 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1334 }
1335
Winson Chungbbc60d82010-11-11 16:34:41 -08001336 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001337 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001338 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001339 for (int i = 0; i < spanX; i++) {
1340 for (int j = 0; j < spanY; j++) {
1341 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001342 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001343 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001344 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001345 continue inner;
1346 }
1347 }
1348 }
1349 if (cellXY != null) {
1350 cellXY[0] = x;
1351 cellXY[1] = y;
1352 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001353 foundCell = true;
1354 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001355 }
1356 }
1357 if (intersectX == -1 && intersectY == -1) {
1358 break;
1359 } else {
1360 // if we failed to find anything, try again but without any requirements of
1361 // intersecting
1362 intersectX = -1;
1363 intersectY = -1;
1364 continue;
1365 }
1366 }
1367
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001368 // re-mark space taken by ignoreView as occupied
1369 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001370 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001371 }
1372
1373 /**
1374 * Called when drag has left this CellLayout or has been completed (successfully or not)
1375 */
1376 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001377 // This can actually be called when we aren't in a drag, e.g. when adding a new
1378 // item to this layout via the customize drawer.
1379 // Guard against that case.
1380 if (mDragging) {
1381 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001382
Joe Onorato4be866d2010-10-10 11:26:02 -07001383 // Fade out the drag indicators
1384 if (mCrosshairsAnimator != null) {
1385 mCrosshairsAnimator.animateOut();
1386 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001387 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001388
1389 // Invalidate the drag data
1390 mDragCell[0] = -1;
1391 mDragCell[1] = -1;
1392 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1393 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1394
Michael Jurka33945b22010-12-21 18:19:38 -08001395 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001396 }
1397
1398 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001399 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001400 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001401 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 *
1403 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001405 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001406 if (child != null) {
1407 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001408 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001409 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001410 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001411 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001412 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
1415
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 /**
1417 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001418 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 * @param child The child that is being dragged
1420 */
1421 void onDragChild(View child) {
1422 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1423 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001424 }
1425
1426 /**
1427 * A drag event has begun over this layout.
1428 * It may have begun over this layout (in which case onDragChild is called first),
1429 * or it may have begun on another layout.
1430 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001431 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001432 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001433 // Fade in the drag indicators
1434 if (mCrosshairsAnimator != null) {
1435 mCrosshairsAnimator.animateIn();
1436 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001437 }
1438 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001440
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001443 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 * @param cellX X coordinate of upper left corner expressed as a cell position
1445 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001446 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001448 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001450 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 final int cellWidth = mCellWidth;
1452 final int cellHeight = mCellHeight;
1453 final int widthGap = mWidthGap;
1454 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001455
1456 final int hStartPadding = getLeftPadding();
1457 final int vStartPadding = getTopPadding();
1458
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1460 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1461
1462 int x = hStartPadding + cellX * (cellWidth + widthGap);
1463 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001464
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001465 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001469 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001471 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 * @param width Width in pixels
1473 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001474 * @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 -08001475 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001476 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001477 return rectToCell(getResources(), width, height, result);
1478 }
1479
1480 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 // Always assume we're working with the smallest span to make sure we
1482 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001483 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1484 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001486
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 // Always round up to next largest cell
1488 int spanX = (width + smallerSize) / smallerSize;
1489 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001490
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001491 if (result == null) {
1492 return new int[] { spanX, spanY };
1493 }
1494 result[0] = spanX;
1495 result[1] = spanY;
1496 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 }
1498
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001499 public int[] cellSpansToSize(int hSpans, int vSpans) {
1500 int[] size = new int[2];
1501 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1502 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1503 return size;
1504 }
1505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001507 * Calculate the grid spans needed to fit given item
1508 */
1509 public void calculateSpans(ItemInfo info) {
1510 final int minWidth;
1511 final int minHeight;
1512
1513 if (info instanceof LauncherAppWidgetInfo) {
1514 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1515 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1516 } else if (info instanceof PendingAddWidgetInfo) {
1517 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1518 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1519 } else {
1520 // It's not a widget, so it must be 1x1
1521 info.spanX = info.spanY = 1;
1522 return;
1523 }
1524 int[] spans = rectToCell(minWidth, minHeight, null);
1525 info.spanX = spans[0];
1526 info.spanY = spans[1];
1527 }
1528
1529 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 * Find the first vacant cell, if there is one.
1531 *
1532 * @param vacant Holds the x and y coordinate of the vacant cell
1533 * @param spanX Horizontal cell span.
1534 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001535 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 * @return True if a vacant cell was found
1537 */
1538 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539
Michael Jurka0280c3b2010-09-17 15:00:07 -07001540 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
1542
1543 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1544 int xCount, int yCount, boolean[][] occupied) {
1545
Adam Cohen2801caf2011-05-13 20:57:39 -07001546 for (int y = 0; y < yCount; y++) {
1547 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 boolean available = !occupied[x][y];
1549out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1550 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1551 available = available && !occupied[i][j];
1552 if (!available) break out;
1553 }
1554 }
1555
1556 if (available) {
1557 vacant[0] = x;
1558 vacant[1] = y;
1559 return true;
1560 }
1561 }
1562 }
1563
1564 return false;
1565 }
1566
Michael Jurka0280c3b2010-09-17 15:00:07 -07001567 private void clearOccupiedCells() {
1568 for (int x = 0; x < mCountX; x++) {
1569 for (int y = 0; y < mCountY; y++) {
1570 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 }
1572 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001573 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574
Adam Cohen1b607ed2011-03-03 17:26:50 -08001575 /**
1576 * Given a view, determines how much that view can be expanded in all directions, in terms of
1577 * whether or not there are other items occupying adjacent cells. Used by the
1578 * AppWidgetResizeFrame to determine how the widget can be resized.
1579 */
Adam Cohend4844c32011-02-18 19:25:06 -08001580 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001581 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001582 boolean flag;
1583
Adam Cohen1b607ed2011-03-03 17:26:50 -08001584 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001585 for (int x = lp.cellX - 1; x >= 0; x--) {
1586 flag = false;
1587 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1588 if (mOccupied[x][y]) flag = true;
1589 }
1590 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001591 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001592 }
1593
Adam Cohen1b607ed2011-03-03 17:26:50 -08001594 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001595 for (int y = lp.cellY - 1; y >= 0; y--) {
1596 flag = false;
1597 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1598 if (mOccupied[x][y]) flag = true;
1599 }
1600 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001601 expandability[AppWidgetResizeFrame.TOP]++;
1602 }
Adam Cohend4844c32011-02-18 19:25:06 -08001603
Adam Cohen1b607ed2011-03-03 17:26:50 -08001604 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001605 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1606 flag = false;
1607 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1608 if (mOccupied[x][y]) flag = true;
1609 }
1610 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001611 expandability[AppWidgetResizeFrame.RIGHT]++;
1612 }
Adam Cohend4844c32011-02-18 19:25:06 -08001613
Adam Cohen1b607ed2011-03-03 17:26:50 -08001614 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001615 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1616 flag = false;
1617 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1618 if (mOccupied[x][y]) flag = true;
1619 }
1620 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001621 expandability[AppWidgetResizeFrame.BOTTOM]++;
1622 }
Adam Cohend4844c32011-02-18 19:25:06 -08001623 }
1624
Michael Jurka0280c3b2010-09-17 15:00:07 -07001625 public void onMove(View view, int newCellX, int newCellY) {
1626 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1627 markCellsAsUnoccupiedForView(view);
1628 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1629 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630
Adam Cohend4844c32011-02-18 19:25:06 -08001631 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001632 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001633 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1634 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1635 }
1636
Adam Cohend4844c32011-02-18 19:25:06 -08001637 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001638 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001639 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1640 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1641 }
1642
1643 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1644 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1645 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1646 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 }
1648 }
1649 }
1650
Adam Cohen2801caf2011-05-13 20:57:39 -07001651 public int getDesiredWidth() {
1652 return mLeftPadding + mRightPadding + (mCountX * mCellWidth) +
1653 (Math.max((mCountX - 1), 0) * mWidthGap);
1654 }
1655
1656 public int getDesiredHeight() {
1657 return mTopPadding + mBottomPadding + (mCountY * mCellHeight) +
1658 (Math.max((mCountY - 1), 0) * mHeightGap);
1659 }
1660
Michael Jurka66d72172011-04-12 16:29:25 -07001661 public boolean isOccupied(int x, int y) {
1662 if (x < mCountX && y < mCountY) {
1663 return mOccupied[x][y];
1664 } else {
1665 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1666 }
1667 }
1668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 @Override
1670 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1671 return new CellLayout.LayoutParams(getContext(), attrs);
1672 }
1673
1674 @Override
1675 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1676 return p instanceof CellLayout.LayoutParams;
1677 }
1678
1679 @Override
1680 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1681 return new CellLayout.LayoutParams(p);
1682 }
1683
Winson Chungaafa03c2010-06-11 17:34:16 -07001684 public static class CellLayoutAnimationController extends LayoutAnimationController {
1685 public CellLayoutAnimationController(Animation animation, float delay) {
1686 super(animation, delay);
1687 }
1688
1689 @Override
1690 protected long getDelayForView(View view) {
1691 return (int) (Math.random() * 150);
1692 }
1693 }
1694
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1696 /**
1697 * Horizontal location of the item in the grid.
1698 */
1699 @ViewDebug.ExportedProperty
1700 public int cellX;
1701
1702 /**
1703 * Vertical location of the item in the grid.
1704 */
1705 @ViewDebug.ExportedProperty
1706 public int cellY;
1707
1708 /**
1709 * Number of cells spanned horizontally by the item.
1710 */
1711 @ViewDebug.ExportedProperty
1712 public int cellHSpan;
1713
1714 /**
1715 * Number of cells spanned vertically by the item.
1716 */
1717 @ViewDebug.ExportedProperty
1718 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001719
Adam Cohen1b607ed2011-03-03 17:26:50 -08001720 /**
1721 * Indicates whether the item will set its x, y, width and height parameters freely,
1722 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1723 */
Adam Cohend4844c32011-02-18 19:25:06 -08001724 public boolean isLockedToGrid = true;
1725
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 /**
1727 * Is this item currently being dragged
1728 */
1729 public boolean isDragging;
1730
1731 // X coordinate of the view in the layout.
1732 @ViewDebug.ExportedProperty
1733 int x;
1734 // Y coordinate of the view in the layout.
1735 @ViewDebug.ExportedProperty
1736 int y;
1737
Patrick Dubroyce34a972010-10-19 10:34:32 -07001738 /**
1739 * The old X coordinate of this item, relative to its current parent.
1740 * Used to animate the item into its new position.
1741 */
1742 int oldX;
1743
1744 /**
1745 * The old Y coordinate of this item, relative to its current parent.
1746 * Used to animate the item into its new position.
1747 */
1748 int oldY;
1749
Romain Guy84f296c2009-11-04 15:00:44 -08001750 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001751
Michael Jurkad3ef3062010-11-23 16:23:58 -08001752 boolean animateDrop;
1753
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 public LayoutParams(Context c, AttributeSet attrs) {
1755 super(c, attrs);
1756 cellHSpan = 1;
1757 cellVSpan = 1;
1758 }
1759
1760 public LayoutParams(ViewGroup.LayoutParams source) {
1761 super(source);
1762 cellHSpan = 1;
1763 cellVSpan = 1;
1764 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001765
1766 public LayoutParams(LayoutParams source) {
1767 super(source);
1768 this.cellX = source.cellX;
1769 this.cellY = source.cellY;
1770 this.cellHSpan = source.cellHSpan;
1771 this.cellVSpan = source.cellVSpan;
1772 }
1773
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001775 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 this.cellX = cellX;
1777 this.cellY = cellY;
1778 this.cellHSpan = cellHSpan;
1779 this.cellVSpan = cellVSpan;
1780 }
1781
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001782 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001783 if (isLockedToGrid) {
1784 final int myCellHSpan = cellHSpan;
1785 final int myCellVSpan = cellVSpan;
1786 final int myCellX = cellX;
1787 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001788
Adam Cohend4844c32011-02-18 19:25:06 -08001789 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1790 leftMargin - rightMargin;
1791 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1792 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001793 x = myCellX * (cellWidth + widthGap) + leftMargin;
1794 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001795 }
1796 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001797
Winson Chungaafa03c2010-06-11 17:34:16 -07001798 public String toString() {
1799 return "(" + this.cellX + ", " + this.cellY + ")";
1800 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001801
1802 public void setWidth(int width) {
1803 this.width = width;
1804 }
1805
1806 public int getWidth() {
1807 return width;
1808 }
1809
1810 public void setHeight(int height) {
1811 this.height = height;
1812 }
1813
1814 public int getHeight() {
1815 return height;
1816 }
1817
1818 public void setX(int x) {
1819 this.x = x;
1820 }
1821
1822 public int getX() {
1823 return x;
1824 }
1825
1826 public void setY(int y) {
1827 this.y = y;
1828 }
1829
1830 public int getY() {
1831 return y;
1832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 }
1834
Michael Jurka0280c3b2010-09-17 15:00:07 -07001835 // This class stores info for two purposes:
1836 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1837 // its spanX, spanY, and the screen it is on
1838 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1839 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1840 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001841 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001843 int cellX = -1;
1844 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 int spanX;
1846 int spanY;
1847 int screen;
1848 boolean valid;
1849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 @Override
1851 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001852 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1853 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 }
1855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856}