blob: c1aa2d5897b28f03b135de405efef5fc8433d3d7 [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];
Adam Cohen76fc0852011-06-17 13:26:23 -0700301 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700302 }
303
Patrick Dubroy96864c32011-03-10 17:17:23 -0800304 private void invalidateBubbleTextView(BubbleTextView icon) {
305 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700306 invalidate(icon.getLeft() + getLeftPadding() - padding,
307 icon.getTop() + getTopPadding() - padding,
308 icon.getRight() + getLeftPadding() + padding,
309 icon.getBottom() + getTopPadding() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800310 }
311
312 void setPressedOrFocusedIcon(BubbleTextView icon) {
313 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
314 // requires an expanded clip rect (due to the glow's blur radius)
315 BubbleTextView oldIcon = mPressedOrFocusedIcon;
316 mPressedOrFocusedIcon = icon;
317 if (oldIcon != null) {
318 invalidateBubbleTextView(oldIcon);
319 }
320 if (mPressedOrFocusedIcon != null) {
321 invalidateBubbleTextView(mPressedOrFocusedIcon);
322 }
323 }
324
Winson Chung6e314082011-01-27 16:46:51 -0800325 public CellLayoutChildren getChildrenLayout() {
326 if (getChildCount() > 0) {
327 return (CellLayoutChildren) getChildAt(0);
328 }
329 return null;
330 }
331
Michael Jurka33945b22010-12-21 18:19:38 -0800332 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
333 if (mIsDefaultDropTarget != isDefaultDropTarget) {
334 mIsDefaultDropTarget = isDefaultDropTarget;
335 invalidate();
336 }
337 }
338
Michael Jurka33945b22010-12-21 18:19:38 -0800339 void setIsDragOccuring(boolean isDragOccuring) {
340 if (mIsDragOccuring != isDragOccuring) {
341 mIsDragOccuring = isDragOccuring;
342 invalidate();
343 }
344 }
345
346 void setIsDragOverlapping(boolean isDragOverlapping) {
347 if (mIsDragOverlapping != isDragOverlapping) {
348 mIsDragOverlapping = isDragOverlapping;
349 invalidate();
350 }
351 }
352
353 boolean getIsDragOverlapping() {
354 return mIsDragOverlapping;
355 }
356
357 private void updateGlowRect() {
358 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700359 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
360 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800361 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700362 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
363 invalidate();
364 }
365
366 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800367 if (scaleFactor != mGlowBackgroundScale) {
368 mGlowBackgroundScale = scaleFactor;
369 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800370 if (getParent() != null) {
371 ((View) getParent()).invalidate();
372 }
Michael Jurka18014792010-10-14 09:01:34 -0700373 }
374 }
375
376 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800377 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700378 }
379
380 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800381 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700382 }
383
384 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800385 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700386 invalidate();
387 }
388
389 void animateDrop() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700390 Resources res = getResources();
391 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
392 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
393 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
394 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
395 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
396 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
Michael Jurka18014792010-10-14 09:01:34 -0700397
Winson Chungb26f3d62011-06-02 10:49:29 -0700398 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
399 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDuration));
Michael Jurka18014792010-10-14 09:01:34 -0700400
Winson Chungb26f3d62011-06-02 10:49:29 -0700401 AnimatorSet bouncer = new AnimatorSet();
402 bouncer.play(scaleUp).before(scaleDown);
403 bouncer.play(scaleUp).with(alphaFadeOut);
404 bouncer.addListener(new AnimatorListenerAdapter() {
405 @Override
406 public void onAnimationStart(Animator animation) {
407 setIsDragOverlapping(true);
408 }
409 @Override
410 public void onAnimationEnd(Animator animation) {
411 setIsDragOverlapping(false);
412 setHoverScale(1.0f);
413 setHoverAlpha(1.0f);
414 }
415 });
416 bouncer.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 }
418
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700419 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700420 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700421 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
422 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
423 // When we're small, we are either drawn normally or in the "accepts drops" state (during
424 // a drag). However, we also drag the mini hover background *over* one of those two
425 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700426 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700427 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800428 boolean mini = getScaleX() < 0.5f;
429
430 if (mIsDragOverlapping) {
431 // In the mini case, we draw the active_glow bg *over* the active background
432 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
433 } else if (mIsDragOccuring && mAcceptsDrops) {
434 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800435 } else if (mIsDefaultDropTarget && mini) {
436 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700437 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800438 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700439 }
Michael Jurka33945b22010-12-21 18:19:38 -0800440
441 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
442 bg.setBounds(mBackgroundRect);
443 bg.draw(canvas);
444
445 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700446 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800447 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700448 // If the hover background's scale is greater than 1, we'll be drawing outside
449 // the bounds of this CellLayout. Get around that by temporarily increasing the
450 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800451 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700452 Rect clipRect = canvas.getClipBounds();
453 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
454 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
455 canvas.save(Canvas.CLIP_SAVE_FLAG);
456 canvas.clipRect(-marginX, -marginY,
457 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
458 modifiedClipRect = true;
459 }
460
Michael Jurka33945b22010-12-21 18:19:38 -0800461 mActiveGlowBackgroundMini.setAlpha(
462 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
463 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
464 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700465 if (modifiedClipRect) {
466 canvas.restore();
467 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700468 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700469 }
Romain Guya6abce82009-11-10 02:54:41 -0800470
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700471 if (mCrosshairsVisibility > 0.0f) {
472 final int countX = mCountX;
473 final int countY = mCountY;
474
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700475 final float MAX_ALPHA = 0.4f;
476 final int MAX_VISIBLE_DISTANCE = 600;
477 final float DISTANCE_MULTIPLIER = 0.002f;
478
479 final Drawable d = mCrosshairsDrawable;
480 final int width = d.getIntrinsicWidth();
481 final int height = d.getIntrinsicHeight();
482
483 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
484 for (int col = 0; col <= countX; col++) {
485 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
486 for (int row = 0; row <= countY; row++) {
487 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
488 float dist = mTmpPointF.length();
489 // Crosshairs further from the drag point are more faint
490 float alpha = Math.min(MAX_ALPHA,
491 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
492 if (alpha > 0.0f) {
493 d.setBounds(x, y, x + width, y + height);
494 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
495 d.draw(canvas);
496 }
497 y += mCellHeight + mHeightGap;
498 }
499 x += mCellWidth + mWidthGap;
500 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700501 }
Winson Chung150fbab2010-09-29 17:14:26 -0700502
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700503 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700504 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700505 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700506 if (alpha > 0) {
507 final Point p = mDragOutlines[i];
508 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700509 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700510 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700511 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700512 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800513
514 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
515 // requires an expanded clip rect (due to the glow's blur radius)
516 if (mPressedOrFocusedIcon != null) {
517 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
518 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
519 if (b != null) {
520 canvas.drawBitmap(b,
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700521 mPressedOrFocusedIcon.getLeft() + getLeftPadding() - padding,
522 mPressedOrFocusedIcon.getTop() + getTopPadding() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800523 null);
524 }
525 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700526 }
527
528 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700529 public void cancelLongPress() {
530 super.cancelLongPress();
531
532 // Cancel long press for all children
533 final int count = getChildCount();
534 for (int i = 0; i < count; i++) {
535 final View child = getChildAt(i);
536 child.cancelLongPress();
537 }
538 }
539
Michael Jurkadee05892010-07-27 10:01:56 -0700540 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
541 mInterceptTouchListener = listener;
542 }
543
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700545 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 }
547
548 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700549 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 }
551
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700552 public boolean addViewToCellLayout(
553 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700554 final LayoutParams lp = params;
555
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 // Generate an id for each view, this assumes we have at most 256x256 cells
557 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700558 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700559 // If the horizontal or vertical span is set to -1, it is taken to
560 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700561 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
562 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563
Winson Chungaafa03c2010-06-11 17:34:16 -0700564 child.setId(childId);
565
Michael Jurka8c920dd2011-01-20 14:16:56 -0800566 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700567
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700568 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700569
Winson Chungaafa03c2010-06-11 17:34:16 -0700570 return true;
571 }
572 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700574
Michael Jurkabea15192010-11-17 12:33:46 -0800575 public void setAcceptsDrops(boolean acceptsDrops) {
576 if (mAcceptsDrops != acceptsDrops) {
577 mAcceptsDrops = acceptsDrops;
578 invalidate();
579 }
580 }
581
Michael Jurka3e7c7632010-10-02 16:01:03 -0700582 public boolean getAcceptsDrops() {
583 return mAcceptsDrops;
584 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585
586 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700587 public void removeAllViews() {
588 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800589 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700590 }
591
592 @Override
593 public void removeAllViewsInLayout() {
594 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800595 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700596 }
597
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700598 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800599 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700600 }
601
Michael Jurka0280c3b2010-09-17 15:00:07 -0700602 @Override
603 public void removeView(View view) {
604 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800605 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700606 }
607
608 @Override
609 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800610 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
611 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700612 }
613
614 @Override
615 public void removeViewInLayout(View view) {
616 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800617 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700618 }
619
620 @Override
621 public void removeViews(int start, int count) {
622 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800623 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700624 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800625 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700626 }
627
628 @Override
629 public void removeViewsInLayout(int start, int count) {
630 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800631 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700632 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800633 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700634 }
635
Michael Jurka8c920dd2011-01-20 14:16:56 -0800636 public void drawChildren(Canvas canvas) {
637 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 }
639
Michael Jurkaabded662011-03-04 12:06:57 -0800640 void buildChildrenLayer() {
641 mChildren.buildLayer();
642 }
643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 @Override
645 protected void onAttachedToWindow() {
646 super.onAttachedToWindow();
647 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
648 }
649
Michael Jurkaaf442092010-06-10 17:01:57 -0700650 public void setTagToCellInfoForPoint(int touchX, int touchY) {
651 final CellInfo cellInfo = mCellInfo;
652 final Rect frame = mRect;
653 final int x = touchX + mScrollX;
654 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800655 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700656
657 boolean found = false;
658 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800659 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800660 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700661
Adam Cohen1b607ed2011-03-03 17:26:50 -0800662 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
663 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700664 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700665
666 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
667 // offset that by this CellLayout's padding to test an (x,y) point that is relative
668 // to this view.
669 final int tmpXY[] = mTmpXY;
670 child.getLocationOnScreen(tmpXY);
671 frame.offset(mLeftPadding, mTopPadding);
672
Michael Jurkaaf442092010-06-10 17:01:57 -0700673 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700674 cellInfo.cell = child;
675 cellInfo.cellX = lp.cellX;
676 cellInfo.cellY = lp.cellY;
677 cellInfo.spanX = lp.cellHSpan;
678 cellInfo.spanY = lp.cellVSpan;
679 cellInfo.valid = true;
680 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700681 break;
682 }
683 }
684 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700685
Michael Jurkaaf442092010-06-10 17:01:57 -0700686 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700687 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700688 pointToCellExact(x, y, cellXY);
689
Michael Jurkaaf442092010-06-10 17:01:57 -0700690 cellInfo.cell = null;
691 cellInfo.cellX = cellXY[0];
692 cellInfo.cellY = cellXY[1];
693 cellInfo.spanX = 1;
694 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700695 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
696 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700697 }
698 setTag(cellInfo);
699 }
700
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 @Override
702 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700703 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
704 return true;
705 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 final int action = ev.getAction();
707 final CellInfo cellInfo = mCellInfo;
708
709 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700710 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 } else if (action == MotionEvent.ACTION_UP) {
712 cellInfo.cell = null;
713 cellInfo.cellX = -1;
714 cellInfo.cellY = -1;
715 cellInfo.spanX = 0;
716 cellInfo.spanY = 0;
717 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 setTag(cellInfo);
719 }
720
721 return false;
722 }
723
724 @Override
725 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700726 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 }
728
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700729 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700730 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 * @param x X coordinate of the point
732 * @param y Y coordinate of the point
733 * @param result Array of 2 ints to hold the x and y coordinate of the cell
734 */
735 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700736 final int hStartPadding = getLeftPadding();
737 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738
739 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
740 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
741
Adam Cohend22015c2010-07-26 22:02:18 -0700742 final int xAxis = mCountX;
743 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744
745 if (result[0] < 0) result[0] = 0;
746 if (result[0] >= xAxis) result[0] = xAxis - 1;
747 if (result[1] < 0) result[1] = 0;
748 if (result[1] >= yAxis) result[1] = yAxis - 1;
749 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700750
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 /**
752 * Given a point, return the cell that most closely encloses that point
753 * @param x X coordinate of the point
754 * @param y Y coordinate of the point
755 * @param result Array of 2 ints to hold the x and y coordinate of the cell
756 */
757 void pointToCellRounded(int x, int y, int[] result) {
758 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
759 }
760
761 /**
762 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700763 *
764 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700766 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 * @param result Array of 2 ints to hold the x and y coordinate of the point
768 */
769 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700770 final int hStartPadding = getLeftPadding();
771 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772
773 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
774 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
775 }
776
Adam Cohene3e27a82011-04-15 12:07:39 -0700777 /**
778 * Given a cell coordinate, return the point that represents the upper left corner of that cell
779 *
780 * @param cellX X coordinate of the cell
781 * @param cellY Y coordinate of the cell
782 *
783 * @param result Array of 2 ints to hold the x and y coordinate of the point
784 */
785 void cellToCenterPoint(int cellX, int cellY, int[] result) {
786 final int hStartPadding = getLeftPadding();
787 final int vStartPadding = getTopPadding();
788
789 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
790 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
791 }
792
Romain Guy84f296c2009-11-04 15:00:44 -0800793 int getCellWidth() {
794 return mCellWidth;
795 }
796
797 int getCellHeight() {
798 return mCellHeight;
799 }
800
Adam Cohend4844c32011-02-18 19:25:06 -0800801 int getWidthGap() {
802 return mWidthGap;
803 }
804
805 int getHeightGap() {
806 return mHeightGap;
807 }
808
Romain Guy1a304a12009-11-10 00:02:32 -0800809 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700810 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800811 }
812
813 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700814 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800815 }
816
817 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700818 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800819 }
820
821 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700822 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800823 }
824
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700825 Rect getContentRect(Rect r) {
826 if (r == null) {
827 r = new Rect();
828 }
829 int left = getPaddingLeft();
830 int top = getPaddingTop();
831 int right = left + getWidth() - mLeftPadding - mRightPadding;
832 int bottom = top + getHeight() - mTopPadding - mBottomPadding;
833 r.set(left, top, right, bottom);
834 return r;
835 }
836
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 @Override
838 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
839 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700840
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700842 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
843
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
845 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700846
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
848 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
849 }
850
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 final int cellWidth = mCellWidth;
852 final int cellHeight = mCellHeight;
853
Adam Cohend22015c2010-07-26 22:02:18 -0700854 int numWidthGaps = mCountX - 1;
855 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856
Winson Chungece7f5b2010-10-22 14:54:12 -0700857 if (mWidthGap < 0 || mHeightGap < 0) {
858 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700859 mHeightGap = numHeightGaps > 0 ? vSpaceLeft / numHeightGaps : 0;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860
Winson Chungece7f5b2010-10-22 14:54:12 -0700861 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
Adam Cohen2801caf2011-05-13 20:57:39 -0700862 mWidthGap = numWidthGaps > 0 ? hSpaceLeft / numWidthGaps : 0;
Winson Chungaafa03c2010-06-11 17:34:16 -0700863
Winson Chungece7f5b2010-10-22 14:54:12 -0700864 // center it around the min gaps
865 int minGap = Math.min(mWidthGap, mHeightGap);
866 mWidthGap = mHeightGap = minGap;
867 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700868
Michael Jurka8c920dd2011-01-20 14:16:56 -0800869 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
870 int newWidth = widthSpecSize;
871 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700872 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800873 newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700874 ((mCountX - 1) * mWidthGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800875 newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700876 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700877 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700878 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800879
880 int count = getChildCount();
881 for (int i = 0; i < count; i++) {
882 View child = getChildAt(i);
Adam Cohen2801caf2011-05-13 20:57:39 -0700883 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mLeftPadding -
884 mRightPadding, MeasureSpec.EXACTLY);
885 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mTopPadding -
886 mBottomPadding, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800887 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
888 }
889 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 }
891
892 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700893 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800896 View child = getChildAt(i);
Adam Cohenbb60e2e2011-04-26 16:48:26 -0700897 child.layout(mLeftPadding, mTopPadding, r - l - mRightPadding , b - t - mBottomPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
899 }
900
901 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700902 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
903 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700904 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800905 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700906 }
907
908 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800910 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 }
912
913 @Override
914 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800915 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 }
917
Michael Jurka5f1c5092010-09-03 14:15:02 -0700918 public float getBackgroundAlpha() {
919 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700920 }
921
Michael Jurka742574b2011-02-02 23:51:01 -0800922 public void setFastBackgroundAlpha(float alpha) {
923 mBackgroundAlpha = alpha;
924 }
925
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700926 public void setBackgroundAlphaMultiplier(float multiplier) {
927 mBackgroundAlphaMultiplier = multiplier;
928 }
929
Adam Cohenddb82192010-11-10 16:32:54 -0800930 public float getBackgroundAlphaMultiplier() {
931 return mBackgroundAlphaMultiplier;
932 }
933
Michael Jurka5f1c5092010-09-03 14:15:02 -0700934 public void setBackgroundAlpha(float alpha) {
935 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700936 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700937 }
938
Michael Jurka5f1c5092010-09-03 14:15:02 -0700939 // Need to return true to let the view system know we know how to handle alpha-- this is
940 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
941 // versions
942 @Override
943 protected boolean onSetAlpha(int alpha) {
944 return true;
945 }
946
947 public void setAlpha(float alpha) {
948 setChildrenAlpha(alpha);
949 super.setAlpha(alpha);
950 }
951
Michael Jurka742574b2011-02-02 23:51:01 -0800952 public void setFastAlpha(float alpha) {
953 setFastChildrenAlpha(alpha);
954 super.setFastAlpha(alpha);
955 }
956
Michael Jurkadee05892010-07-27 10:01:56 -0700957 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700958 final int childCount = getChildCount();
959 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700960 getChildAt(i).setAlpha(alpha);
961 }
962 }
963
Michael Jurka742574b2011-02-02 23:51:01 -0800964 private void setFastChildrenAlpha(float alpha) {
965 final int childCount = getChildCount();
966 for (int i = 0; i < childCount; i++) {
967 getChildAt(i).setFastAlpha(alpha);
968 }
969 }
970
Patrick Dubroy440c3602010-07-13 17:50:32 -0700971 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800972 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700973 }
974
Adam Cohen76fc0852011-06-17 13:26:23 -0700975 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
976 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700977 CellLayoutChildren clc = getChildrenLayout();
978 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
979 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
980 final ItemInfo info = (ItemInfo) child.getTag();
981
982 // We cancel any existing animations
983 if (mReorderAnimators.containsKey(lp)) {
984 mReorderAnimators.get(lp).cancel();
985 mReorderAnimators.remove(lp);
986 }
987
988 int oldX = lp.x;
989 int oldY = lp.y;
990 mOccupied[lp.cellX][lp.cellY] = false;
991 mOccupied[cellX][cellY] = true;
992
993 lp.isLockedToGrid = true;
994 lp.cellX = info.cellX = cellX;
995 lp.cellY = info.cellY = cellY;
996 clc.setupLp(lp);
997 lp.isLockedToGrid = false;
998 int newX = lp.x;
999 int newY = lp.y;
1000
Adam Cohen76fc0852011-06-17 13:26:23 -07001001 lp.x = oldX;
1002 lp.y = oldY;
1003 child.requestLayout();
1004
Adam Cohenbfbfd262011-06-13 16:55:12 -07001005 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
1006 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
1007 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
1008 oa.setDuration(duration);
1009 mReorderAnimators.put(lp, oa);
1010 oa.addUpdateListener(new AnimatorUpdateListener() {
1011 public void onAnimationUpdate(ValueAnimator animation) {
1012 child.requestLayout();
1013 }
1014 });
1015 oa.addListener(new AnimatorListenerAdapter() {
1016 boolean cancelled = false;
1017 public void onAnimationEnd(Animator animation) {
1018 // If the animation was cancelled, it means that another animation
1019 // has interrupted this one, and we don't want to lock the item into
1020 // place just yet.
1021 if (!cancelled) {
1022 lp.isLockedToGrid = true;
1023 }
1024 if (mReorderAnimators.containsKey(lp)) {
1025 mReorderAnimators.remove(lp);
1026 }
1027 }
1028 public void onAnimationCancel(Animator animation) {
1029 cancelled = true;
1030 }
1031 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001032 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001033 oa.start();
1034 return true;
1035 }
1036 return false;
1037 }
1038
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001039 /**
1040 * Estimate where the top left cell of the dragged item will land if it is dropped.
1041 *
1042 * @param originX The X value of the top left corner of the item
1043 * @param originY The Y value of the top left corner of the item
1044 * @param spanX The number of horizontal cells that the item spans
1045 * @param spanY The number of vertical cells that the item spans
1046 * @param result The estimated drop cell X and Y.
1047 */
1048 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001049 final int countX = mCountX;
1050 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001051
Michael Jurkaa63c4522010-08-19 13:52:27 -07001052 // pointToCellRounded takes the top left of a cell but will pad that with
1053 // cellWidth/2 and cellHeight/2 when finding the matching cell
1054 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001055
1056 // If the item isn't fully on this screen, snap to the edges
1057 int rightOverhang = result[0] + spanX - countX;
1058 if (rightOverhang > 0) {
1059 result[0] -= rightOverhang; // Snap to right
1060 }
1061 result[0] = Math.max(0, result[0]); // Snap to left
1062 int bottomOverhang = result[1] + spanY - countY;
1063 if (bottomOverhang > 0) {
1064 result[1] -= bottomOverhang; // Snap to bottom
1065 }
1066 result[1] = Math.max(0, result[1]); // Snap to top
1067 }
1068
Joe Onorato4be866d2010-10-10 11:26:02 -07001069 void visualizeDropLocation(
1070 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1071
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001072 final int oldDragCellX = mDragCell[0];
1073 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001074 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001075 if (v != null) {
1076 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1077 } else {
1078 mDragCenter.set(originX, originY);
1079 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001080
Adam Cohen2801caf2011-05-13 20:57:39 -07001081 if (dragOutline == null && v == null) {
1082 if (mCrosshairsDrawable != null) {
1083 invalidate();
1084 }
1085 return;
1086 }
1087
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001088 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001089 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001090 final int[] topLeft = mTmpPoint;
1091 cellToPoint(nearest[0], nearest[1], topLeft);
1092
Joe Onorato4be866d2010-10-10 11:26:02 -07001093 int left = topLeft[0];
1094 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001095
Winson Chunga9abd0e2010-10-27 17:18:37 -07001096 if (v != null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001097 // When drawing the drag outline, it did not account for margin offsets
1098 // added by the view's parent.
1099 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1100 left += lp.leftMargin;
1101 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001102
Adam Cohen99e8b402011-03-25 19:23:43 -07001103 // Offsets due to the size difference between the View and the dragOutline.
1104 // There is a size difference to account for the outer blur, which may lie
1105 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001106 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1107 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001108 } else {
1109 // Center the drag outline in the cell
Winson Chung4b576be2011-04-27 17:40:20 -07001110 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1111 - dragOutline.getWidth()) / 2;
1112 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1113 - dragOutline.getHeight()) / 2;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001114 }
Winson Chung150fbab2010-09-29 17:14:26 -07001115
Joe Onorato4be866d2010-10-10 11:26:02 -07001116 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001117 mDragOutlineAnims[oldIndex].animateOut();
1118 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001119
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001120 mDragOutlines[mDragOutlineCurrent].set(left, top);
1121 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1122 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001123 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001124
1125 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1126 if (mCrosshairsDrawable != null) {
1127 invalidate();
1128 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001129 }
1130
Adam Cohene0310962011-04-18 16:15:31 -07001131 public void clearDragOutlines() {
1132 final int oldIndex = mDragOutlineCurrent;
1133 mDragOutlineAnims[oldIndex].animateOut();
1134 mDragCell[0] = -1;
1135 mDragCell[1] = -1;
1136 }
1137
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001139 * Find a vacant area that will fit the given bounds nearest the requested
1140 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001141 *
Romain Guy51afc022009-05-04 18:03:43 -07001142 * @param pixelX The X location at which you want to search for a vacant area.
1143 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001144 * @param spanX Horizontal span of the object.
1145 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001146 * @param result Array in which to place the result, or null (in which case a new array will
1147 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001148 * @return The X, Y cell of a vacant area that can contain this object,
1149 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001151 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001152 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1153 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001154 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001155
Michael Jurka6a1435d2010-09-27 17:35:12 -07001156 /**
1157 * Find a vacant area that will fit the given bounds nearest the requested
1158 * cell location. Uses Euclidean distance to score multiple vacant areas.
1159 *
1160 * @param pixelX The X location at which you want to search for a vacant area.
1161 * @param pixelY The Y location at which you want to search for a vacant area.
1162 * @param spanX Horizontal span of the object.
1163 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001164 * @param ignoreOccupied If true, the result can be an occupied cell
1165 * @param result Array in which to place the result, or null (in which case a new array will
1166 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001167 * @return The X, Y cell of a vacant area that can contain this object,
1168 * nearest the requested location.
1169 */
Adam Cohendf035382011-04-11 17:22:04 -07001170 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1171 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001172 // mark space take by ignoreView as available (method checks if ignoreView is null)
1173 markCellsAsUnoccupiedForView(ignoreView);
1174
Adam Cohene3e27a82011-04-15 12:07:39 -07001175 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1176 // to the center of the item, but we are searching based on the top-left cell, so
1177 // we translate the point over to correspond to the top-left.
1178 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1179 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1180
Jeff Sharkey70864282009-04-07 21:08:40 -07001181 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001182 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001183 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001184
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001185 final int countX = mCountX;
1186 final int countY = mCountY;
1187 final boolean[][] occupied = mOccupied;
1188
Winson Chungbbc60d82010-11-11 16:34:41 -08001189 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001190 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001191 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001192 if (ignoreOccupied) {
1193 for (int i = 0; i < spanX; i++) {
1194 for (int j = 0; j < spanY; j++) {
1195 if (occupied[x + i][y + j]) {
1196 // small optimization: we can skip to after the column we
1197 // just found an occupied cell
1198 x += i;
1199 continue inner;
1200 }
Michael Jurkac28de512010-08-13 11:27:44 -07001201 }
1202 }
1203 }
Winson Chung0be025d2011-05-23 17:45:09 -07001204 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001205 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206
Michael Jurkac28de512010-08-13 11:27:44 -07001207 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1208 + Math.pow(cellXY[1] - pixelY, 2));
1209 if (distance <= bestDistance) {
1210 bestDistance = distance;
1211 bestXY[0] = x;
1212 bestXY[1] = y;
1213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 }
1215 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001216 // re-mark space taken by ignoreView as occupied
1217 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218
Adam Cohenc0dcf592011-06-01 15:30:43 -07001219 // Return -1, -1 if no suitable location found
1220 if (bestDistance == Double.MAX_VALUE) {
1221 bestXY[0] = -1;
1222 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001223 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001224 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001226
Adam Cohendf035382011-04-11 17:22:04 -07001227 /**
1228 * Find a vacant area that will fit the given bounds nearest the requested
1229 * cell location. Uses Euclidean distance to score multiple vacant areas.
1230 *
1231 * @param pixelX The X location at which you want to search for a vacant area.
1232 * @param pixelY The Y location at which you want to search for a vacant area.
1233 * @param spanX Horizontal span of the object.
1234 * @param spanY Vertical span of the object.
1235 * @param ignoreView Considers space occupied by this view as unoccupied
1236 * @param result Previously returned value to possibly recycle.
1237 * @return The X, Y cell of a vacant area that can contain this object,
1238 * nearest the requested location.
1239 */
1240 int[] findNearestVacantArea(
1241 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1242 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1243 }
1244
1245 /**
1246 * Find a starting cell position that will fit the given bounds nearest the requested
1247 * cell location. Uses Euclidean distance to score multiple vacant areas.
1248 *
1249 * @param pixelX The X location at which you want to search for a vacant area.
1250 * @param pixelY The Y location at which you want to search for a vacant area.
1251 * @param spanX Horizontal span of the object.
1252 * @param spanY Vertical span of the object.
1253 * @param ignoreView Considers space occupied by this view as unoccupied
1254 * @param result Previously returned value to possibly recycle.
1255 * @return The X, Y cell of a vacant area that can contain this object,
1256 * nearest the requested location.
1257 */
1258 int[] findNearestArea(
1259 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1260 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1261 }
1262
Michael Jurka0280c3b2010-09-17 15:00:07 -07001263 boolean existsEmptyCell() {
1264 return findCellForSpan(null, 1, 1);
1265 }
1266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001268 * Finds the upper-left coordinate of the first rectangle in the grid that can
1269 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1270 * then this method will only return coordinates for rectangles that contain the cell
1271 * (intersectX, intersectY)
1272 *
1273 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1274 * can be found.
1275 * @param spanX The horizontal span of the cell we want to find.
1276 * @param spanY The vertical span of the cell we want to find.
1277 *
1278 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001279 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001280 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1281 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1282 }
1283
1284 /**
1285 * Like above, but ignores any cells occupied by the item "ignoreView"
1286 *
1287 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1288 * can be found.
1289 * @param spanX The horizontal span of the cell we want to find.
1290 * @param spanY The vertical span of the cell we want to find.
1291 * @param ignoreView The home screen item we should treat as not occupying any space
1292 * @return
1293 */
1294 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1295 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1296 }
1297
1298 /**
1299 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1300 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1301 *
1302 * @param spanX The horizontal span of the cell we want to find.
1303 * @param spanY The vertical span of the cell we want to find.
1304 * @param ignoreView The home screen item we should treat as not occupying any space
1305 * @param intersectX The X coordinate of the cell that we should try to overlap
1306 * @param intersectX The Y coordinate of the cell that we should try to overlap
1307 *
1308 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1309 */
1310 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1311 int intersectX, int intersectY) {
1312 return findCellForSpanThatIntersectsIgnoring(
1313 cellXY, spanX, spanY, intersectX, intersectY, null);
1314 }
1315
1316 /**
1317 * The superset of the above two methods
1318 */
1319 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1320 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001321 // mark space take by ignoreView as available (method checks if ignoreView is null)
1322 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001323
Michael Jurka28750fb2010-09-24 17:43:49 -07001324 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001325 while (true) {
1326 int startX = 0;
1327 if (intersectX >= 0) {
1328 startX = Math.max(startX, intersectX - (spanX - 1));
1329 }
1330 int endX = mCountX - (spanX - 1);
1331 if (intersectX >= 0) {
1332 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1333 }
1334 int startY = 0;
1335 if (intersectY >= 0) {
1336 startY = Math.max(startY, intersectY - (spanY - 1));
1337 }
1338 int endY = mCountY - (spanY - 1);
1339 if (intersectY >= 0) {
1340 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1341 }
1342
Winson Chungbbc60d82010-11-11 16:34:41 -08001343 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001344 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001345 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001346 for (int i = 0; i < spanX; i++) {
1347 for (int j = 0; j < spanY; j++) {
1348 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001349 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001350 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001351 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001352 continue inner;
1353 }
1354 }
1355 }
1356 if (cellXY != null) {
1357 cellXY[0] = x;
1358 cellXY[1] = y;
1359 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001360 foundCell = true;
1361 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001362 }
1363 }
1364 if (intersectX == -1 && intersectY == -1) {
1365 break;
1366 } else {
1367 // if we failed to find anything, try again but without any requirements of
1368 // intersecting
1369 intersectX = -1;
1370 intersectY = -1;
1371 continue;
1372 }
1373 }
1374
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001375 // re-mark space taken by ignoreView as occupied
1376 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001377 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001378 }
1379
1380 /**
1381 * Called when drag has left this CellLayout or has been completed (successfully or not)
1382 */
1383 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001384 // This can actually be called when we aren't in a drag, e.g. when adding a new
1385 // item to this layout via the customize drawer.
1386 // Guard against that case.
1387 if (mDragging) {
1388 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001389
Joe Onorato4be866d2010-10-10 11:26:02 -07001390 // Fade out the drag indicators
1391 if (mCrosshairsAnimator != null) {
1392 mCrosshairsAnimator.animateOut();
1393 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001394 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001395
1396 // Invalidate the drag data
1397 mDragCell[0] = -1;
1398 mDragCell[1] = -1;
1399 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1400 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1401
Michael Jurka33945b22010-12-21 18:19:38 -08001402 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001403 }
1404
1405 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001406 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001407 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001408 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 *
1410 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08001412 void onDropChild(View child, boolean animate) {
Romain Guyd94533d2009-08-17 10:01:15 -07001413 if (child != null) {
1414 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001415 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001416 lp.dropped = true;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001417 lp.animateDrop = animate;
Patrick Dubroye3887cc2011-01-20 10:43:40 -08001418 child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001419 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001420 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
1422
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 /**
1424 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001425 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 * @param child The child that is being dragged
1427 */
1428 void onDragChild(View child) {
1429 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1430 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001431 }
1432
1433 /**
1434 * A drag event has begun over this layout.
1435 * It may have begun over this layout (in which case onDragChild is called first),
1436 * or it may have begun on another layout.
1437 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001438 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001439 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001440 // Fade in the drag indicators
1441 if (mCrosshairsAnimator != null) {
1442 mCrosshairsAnimator.animateIn();
1443 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001444 }
1445 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001450 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 * @param cellX X coordinate of upper left corner expressed as a cell position
1452 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001453 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001455 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001457 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 final int cellWidth = mCellWidth;
1459 final int cellHeight = mCellHeight;
1460 final int widthGap = mWidthGap;
1461 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001462
1463 final int hStartPadding = getLeftPadding();
1464 final int vStartPadding = getTopPadding();
1465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1467 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1468
1469 int x = hStartPadding + cellX * (cellWidth + widthGap);
1470 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001471
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001472 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001476 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001478 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 * @param width Width in pixels
1480 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001481 * @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 -08001482 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001483 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001484 return rectToCell(getResources(), width, height, result);
1485 }
1486
1487 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 // Always assume we're working with the smallest span to make sure we
1489 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001490 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1491 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001493
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 // Always round up to next largest cell
1495 int spanX = (width + smallerSize) / smallerSize;
1496 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001497
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001498 if (result == null) {
1499 return new int[] { spanX, spanY };
1500 }
1501 result[0] = spanX;
1502 result[1] = spanY;
1503 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 }
1505
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001506 public int[] cellSpansToSize(int hSpans, int vSpans) {
1507 int[] size = new int[2];
1508 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1509 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1510 return size;
1511 }
1512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001514 * Calculate the grid spans needed to fit given item
1515 */
1516 public void calculateSpans(ItemInfo info) {
1517 final int minWidth;
1518 final int minHeight;
1519
1520 if (info instanceof LauncherAppWidgetInfo) {
1521 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1522 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1523 } else if (info instanceof PendingAddWidgetInfo) {
1524 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1525 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1526 } else {
1527 // It's not a widget, so it must be 1x1
1528 info.spanX = info.spanY = 1;
1529 return;
1530 }
1531 int[] spans = rectToCell(minWidth, minHeight, null);
1532 info.spanX = spans[0];
1533 info.spanY = spans[1];
1534 }
1535
1536 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 * Find the first vacant cell, if there is one.
1538 *
1539 * @param vacant Holds the x and y coordinate of the vacant cell
1540 * @param spanX Horizontal cell span.
1541 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001542 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 * @return True if a vacant cell was found
1544 */
1545 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
1549
1550 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1551 int xCount, int yCount, boolean[][] occupied) {
1552
Adam Cohen2801caf2011-05-13 20:57:39 -07001553 for (int y = 0; y < yCount; y++) {
1554 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 boolean available = !occupied[x][y];
1556out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1557 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1558 available = available && !occupied[i][j];
1559 if (!available) break out;
1560 }
1561 }
1562
1563 if (available) {
1564 vacant[0] = x;
1565 vacant[1] = y;
1566 return true;
1567 }
1568 }
1569 }
1570
1571 return false;
1572 }
1573
Michael Jurka0280c3b2010-09-17 15:00:07 -07001574 private void clearOccupiedCells() {
1575 for (int x = 0; x < mCountX; x++) {
1576 for (int y = 0; y < mCountY; y++) {
1577 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 }
1579 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001580 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581
Adam Cohen1b607ed2011-03-03 17:26:50 -08001582 /**
1583 * Given a view, determines how much that view can be expanded in all directions, in terms of
1584 * whether or not there are other items occupying adjacent cells. Used by the
1585 * AppWidgetResizeFrame to determine how the widget can be resized.
1586 */
Adam Cohend4844c32011-02-18 19:25:06 -08001587 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001588 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001589 boolean flag;
1590
Adam Cohen1b607ed2011-03-03 17:26:50 -08001591 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001592 for (int x = lp.cellX - 1; x >= 0; x--) {
1593 flag = false;
1594 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1595 if (mOccupied[x][y]) flag = true;
1596 }
1597 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001598 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001599 }
1600
Adam Cohen1b607ed2011-03-03 17:26:50 -08001601 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001602 for (int y = lp.cellY - 1; y >= 0; y--) {
1603 flag = false;
1604 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1605 if (mOccupied[x][y]) flag = true;
1606 }
1607 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001608 expandability[AppWidgetResizeFrame.TOP]++;
1609 }
Adam Cohend4844c32011-02-18 19:25:06 -08001610
Adam Cohen1b607ed2011-03-03 17:26:50 -08001611 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001612 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1613 flag = false;
1614 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1615 if (mOccupied[x][y]) flag = true;
1616 }
1617 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001618 expandability[AppWidgetResizeFrame.RIGHT]++;
1619 }
Adam Cohend4844c32011-02-18 19:25:06 -08001620
Adam Cohen1b607ed2011-03-03 17:26:50 -08001621 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001622 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1623 flag = false;
1624 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1625 if (mOccupied[x][y]) flag = true;
1626 }
1627 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001628 expandability[AppWidgetResizeFrame.BOTTOM]++;
1629 }
Adam Cohend4844c32011-02-18 19:25:06 -08001630 }
1631
Michael Jurka0280c3b2010-09-17 15:00:07 -07001632 public void onMove(View view, int newCellX, int newCellY) {
1633 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1634 markCellsAsUnoccupiedForView(view);
1635 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1636 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637
Adam Cohend4844c32011-02-18 19:25:06 -08001638 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001639 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001640 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1641 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1642 }
1643
Adam Cohend4844c32011-02-18 19:25:06 -08001644 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001645 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001646 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1647 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1648 }
1649
1650 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1651 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1652 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1653 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 }
1655 }
1656 }
1657
Adam Cohen2801caf2011-05-13 20:57:39 -07001658 public int getDesiredWidth() {
1659 return mLeftPadding + mRightPadding + (mCountX * mCellWidth) +
1660 (Math.max((mCountX - 1), 0) * mWidthGap);
1661 }
1662
1663 public int getDesiredHeight() {
1664 return mTopPadding + mBottomPadding + (mCountY * mCellHeight) +
1665 (Math.max((mCountY - 1), 0) * mHeightGap);
1666 }
1667
Michael Jurka66d72172011-04-12 16:29:25 -07001668 public boolean isOccupied(int x, int y) {
1669 if (x < mCountX && y < mCountY) {
1670 return mOccupied[x][y];
1671 } else {
1672 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1673 }
1674 }
1675
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 @Override
1677 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1678 return new CellLayout.LayoutParams(getContext(), attrs);
1679 }
1680
1681 @Override
1682 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1683 return p instanceof CellLayout.LayoutParams;
1684 }
1685
1686 @Override
1687 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1688 return new CellLayout.LayoutParams(p);
1689 }
1690
Winson Chungaafa03c2010-06-11 17:34:16 -07001691 public static class CellLayoutAnimationController extends LayoutAnimationController {
1692 public CellLayoutAnimationController(Animation animation, float delay) {
1693 super(animation, delay);
1694 }
1695
1696 @Override
1697 protected long getDelayForView(View view) {
1698 return (int) (Math.random() * 150);
1699 }
1700 }
1701
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1703 /**
1704 * Horizontal location of the item in the grid.
1705 */
1706 @ViewDebug.ExportedProperty
1707 public int cellX;
1708
1709 /**
1710 * Vertical location of the item in the grid.
1711 */
1712 @ViewDebug.ExportedProperty
1713 public int cellY;
1714
1715 /**
1716 * Number of cells spanned horizontally by the item.
1717 */
1718 @ViewDebug.ExportedProperty
1719 public int cellHSpan;
1720
1721 /**
1722 * Number of cells spanned vertically by the item.
1723 */
1724 @ViewDebug.ExportedProperty
1725 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001726
Adam Cohen1b607ed2011-03-03 17:26:50 -08001727 /**
1728 * Indicates whether the item will set its x, y, width and height parameters freely,
1729 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1730 */
Adam Cohend4844c32011-02-18 19:25:06 -08001731 public boolean isLockedToGrid = true;
1732
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 /**
1734 * Is this item currently being dragged
1735 */
1736 public boolean isDragging;
1737
1738 // X coordinate of the view in the layout.
1739 @ViewDebug.ExportedProperty
1740 int x;
1741 // Y coordinate of the view in the layout.
1742 @ViewDebug.ExportedProperty
1743 int y;
1744
Patrick Dubroyce34a972010-10-19 10:34:32 -07001745 /**
1746 * The old X coordinate of this item, relative to its current parent.
1747 * Used to animate the item into its new position.
1748 */
1749 int oldX;
1750
1751 /**
1752 * The old Y coordinate of this item, relative to its current parent.
1753 * Used to animate the item into its new position.
1754 */
1755 int oldY;
1756
Romain Guy84f296c2009-11-04 15:00:44 -08001757 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001758
Michael Jurkad3ef3062010-11-23 16:23:58 -08001759 boolean animateDrop;
1760
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 public LayoutParams(Context c, AttributeSet attrs) {
1762 super(c, attrs);
1763 cellHSpan = 1;
1764 cellVSpan = 1;
1765 }
1766
1767 public LayoutParams(ViewGroup.LayoutParams source) {
1768 super(source);
1769 cellHSpan = 1;
1770 cellVSpan = 1;
1771 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001772
1773 public LayoutParams(LayoutParams source) {
1774 super(source);
1775 this.cellX = source.cellX;
1776 this.cellY = source.cellY;
1777 this.cellHSpan = source.cellHSpan;
1778 this.cellVSpan = source.cellVSpan;
1779 }
1780
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001782 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 this.cellX = cellX;
1784 this.cellY = cellY;
1785 this.cellHSpan = cellHSpan;
1786 this.cellVSpan = cellVSpan;
1787 }
1788
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001789 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001790 if (isLockedToGrid) {
1791 final int myCellHSpan = cellHSpan;
1792 final int myCellVSpan = cellVSpan;
1793 final int myCellX = cellX;
1794 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001795
Adam Cohend4844c32011-02-18 19:25:06 -08001796 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1797 leftMargin - rightMargin;
1798 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1799 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001800 x = myCellX * (cellWidth + widthGap) + leftMargin;
1801 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001802 }
1803 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001804
Winson Chungaafa03c2010-06-11 17:34:16 -07001805 public String toString() {
1806 return "(" + this.cellX + ", " + this.cellY + ")";
1807 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001808
1809 public void setWidth(int width) {
1810 this.width = width;
1811 }
1812
1813 public int getWidth() {
1814 return width;
1815 }
1816
1817 public void setHeight(int height) {
1818 this.height = height;
1819 }
1820
1821 public int getHeight() {
1822 return height;
1823 }
1824
1825 public void setX(int x) {
1826 this.x = x;
1827 }
1828
1829 public int getX() {
1830 return x;
1831 }
1832
1833 public void setY(int y) {
1834 this.y = y;
1835 }
1836
1837 public int getY() {
1838 return y;
1839 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 }
1841
Michael Jurka0280c3b2010-09-17 15:00:07 -07001842 // This class stores info for two purposes:
1843 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1844 // its spanX, spanY, and the screen it is on
1845 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1846 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1847 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001848 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001850 int cellX = -1;
1851 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 int spanX;
1853 int spanY;
1854 int screen;
1855 boolean valid;
1856
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 @Override
1858 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001859 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1860 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 }
1862 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863}