blob: a9cdb87900cb5b3953e530970c0ffbdf94c82c93 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Michael Jurka18014792010-10-14 09:01:34 -070021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070023import android.animation.PropertyValuesHolder;
Chet Haase00397b12010-10-07 11:13:10 -070024import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070025import android.animation.ValueAnimator;
26import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040028import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070031import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070033import android.graphics.Point;
34import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Rect;
36import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070037import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070038import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.MotionEvent;
42import android.view.View;
43import android.view.ViewDebug;
44import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070046import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Adam Cohen66396872011-04-15 17:50:36 -070049import com.android.launcher.R;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070050
Adam Cohenc0dcf592011-06-01 15:30:43 -070051import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070052import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053
Michael Jurkabdb5c532011-02-01 15:05:06 -080054public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070055 static final String TAG = "CellLayout";
56
Winson Chung4b825dcd2011-06-19 12:41:22 -070057 private int mOriginalCellWidth;
58 private int mOriginalCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 private int mCellWidth;
60 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070061
Adam Cohend22015c2010-07-26 22:02:18 -070062 private int mCountX;
63 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064
65 private int mWidthGap;
66 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070067 private int mMaxGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
69 private final Rect mRect = new Rect();
70 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070071
Patrick Dubroyde7658b2010-09-27 11:15:43 -070072 // These are temporary variables to prevent having to allocate a new object just to
73 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070074 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070075 private final int[] mTmpPoint = new int[2];
76 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070077
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 boolean[][] mOccupied;
79
Michael Jurkadee05892010-07-27 10:01:56 -070080 private OnTouchListener mInterceptTouchListener;
81
Michael Jurka5f1c5092010-09-03 14:15:02 -070082 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070083 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070084
Michael Jurka33945b22010-12-21 18:19:38 -080085 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080086 private Drawable mActiveBackground;
87 private Drawable mActiveGlowBackground;
88 private Drawable mNormalBackgroundMini;
89 private Drawable mNormalGlowBackgroundMini;
90 private Drawable mActiveBackgroundMini;
91 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -070092 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -080093 private Rect mGlowBackgroundRect;
94 private float mGlowBackgroundScale;
95 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -070096
Adam Cohendf035382011-04-11 17:22:04 -070097 private boolean mAcceptsDrops = true;
Michael Jurka33945b22010-12-21 18:19:38 -080098 // If we're actively dragging something over this screen, mIsDragOverlapping is true
99 private boolean mIsDragOverlapping = false;
100 private boolean mIsDragOccuring = false;
101 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700102 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700103
Winson Chung150fbab2010-09-29 17:14:26 -0700104 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700105 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Winson Chung63257c12011-05-05 17:06:13 -0700106 private Point[] mDragOutlines = new Point[4];
Chet Haase472b2812010-10-14 07:02:04 -0700107 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700108 private InterruptibleInOutAnimator[] mDragOutlineAnims =
109 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700110
111 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700112 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700113 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700114
Patrick Dubroy96864c32011-03-10 17:17:23 -0800115 private BubbleTextView mPressedOrFocusedIcon;
116
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700117 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700118 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700119 private float mCrosshairsVisibility = 0.0f;
120
Adam Cohenbfbfd262011-06-13 16:55:12 -0700121 private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new
122 HashMap<CellLayout.LayoutParams, ObjectAnimator>();
123
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700124 // When a drag operation is in progress, holds the nearest cell to the touch point
125 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Joe Onorato4be866d2010-10-10 11:26:02 -0700127 private boolean mDragging = false;
128
Patrick Dubroyce34a972010-10-19 10:34:32 -0700129 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800130 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 public CellLayout(Context context) {
133 this(context, null);
134 }
135
136 public CellLayout(Context context, AttributeSet attrs) {
137 this(context, attrs, 0);
138 }
139
140 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
141 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700142
143 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
144 // the user where a dragged item will land when dropped.
145 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
148
Winson Chung4b825dcd2011-06-19 12:41:22 -0700149 mOriginalCellWidth =
150 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
151 mOriginalCellHeight =
152 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
153 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
154 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
155 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700156 mCountX = LauncherModel.getCellCountX();
157 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700158 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
160 a.recycle();
161
162 setAlwaysDrawnWithCacheEnabled(false);
163
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700164 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700165
Winson Chung967289b2011-06-30 18:09:30 -0700166 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
167 mActiveBackground = res.getDrawable(R.drawable.homescreen_green_normal_holo);
168 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_green_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800169
Winson Chungb26f3d62011-06-02 10:49:29 -0700170 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
171 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
172 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
173 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
Michael Jurka33945b22010-12-21 18:19:38 -0800174
Winson Chungb26f3d62011-06-02 10:49:29 -0700175 mNormalBackground.setFilterBitmap(true);
176 mActiveBackground.setFilterBitmap(true);
177 mActiveGlowBackground.setFilterBitmap(true);
178 mNormalBackgroundMini.setFilterBitmap(true);
179 mNormalGlowBackgroundMini.setFilterBitmap(true);
180 mActiveBackgroundMini.setFilterBitmap(true);
181 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700182
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700183 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700184
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700185 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700186 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700187
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700188 // Set up the animation for fading the crosshairs in and out
189 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700190 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700191 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700192 public void onAnimationUpdate(ValueAnimator animation) {
193 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700194 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700195 }
196 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700197 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700198
Joe Onorato4be866d2010-10-10 11:26:02 -0700199 for (int i = 0; i < mDragOutlines.length; i++) {
200 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700201 }
202
203 // When dragging things around the home screens, we show a green outline of
204 // where the item will land. The outlines gradually fade out, leaving a trail
205 // behind the drag path.
206 // Set up all the animations that are used to implement this fading.
207 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700208 final float fromAlphaValue = 0;
209 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700210
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700211 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700212
213 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700214 final InterruptibleInOutAnimator anim =
215 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700216 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700217 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700218 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700219 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700220 final Bitmap outline = (Bitmap)anim.getTag();
221
222 // If an animation is started and then stopped very quickly, we can still
223 // get spurious updates we've cleared the tag. Guard against this.
224 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700225 if (false) {
226 Object val = animation.getAnimatedValue();
227 Log.d(TAG, "anim " + thisIndex + " update: " + val +
228 ", isStopped " + anim.isStopped());
229 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700230 // Try to prevent it from continuing to run
231 animation.cancel();
232 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700233 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700234 final int left = mDragOutlines[thisIndex].x;
235 final int top = mDragOutlines[thisIndex].y;
236 CellLayout.this.invalidate(left, top,
237 left + outline.getWidth(), top + outline.getHeight());
238 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700239 }
240 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700241 // The animation holds a reference to the drag outline bitmap as long is it's
242 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700243 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700244 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700245 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700246 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700247 anim.setTag(null);
248 }
249 }
250 });
251 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700252 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700253
Michael Jurka18014792010-10-14 09:01:34 -0700254 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800255 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700256 setHoverScale(1.0f);
257 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800258
Michael Jurka8c920dd2011-01-20 14:16:56 -0800259 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700260 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800261 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700262 }
263
Michael Jurkaf6440da2011-04-05 14:50:34 -0700264 static int widthInPortrait(Resources r, int numCells) {
265 // We use this method from Workspace to figure out how many rows/columns Launcher should
266 // have. We ignore the left/right padding on CellLayout because it turns out in our design
267 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700268 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700269 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
270 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700271
Winson Chung4b825dcd2011-06-19 12:41:22 -0700272 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700273 }
274
Michael Jurkaf6440da2011-04-05 14:50:34 -0700275 static int heightInLandscape(Resources r, int numCells) {
276 // We use this method from Workspace to figure out how many rows/columns Launcher should
277 // have. We ignore the left/right padding on CellLayout because it turns out in our design
278 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700279 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700280 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
281 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700282
Winson Chung4b825dcd2011-06-19 12:41:22 -0700283 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700284 }
285
Adam Cohen2801caf2011-05-13 20:57:39 -0700286 public void enableHardwareLayers() {
287 mChildren.enableHardwareLayers();
288 }
289
290 public void setGridSize(int x, int y) {
291 mCountX = x;
292 mCountY = y;
293 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700294 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700295 }
296
Patrick Dubroy96864c32011-03-10 17:17:23 -0800297 private void invalidateBubbleTextView(BubbleTextView icon) {
298 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700299 invalidate(icon.getLeft() + getPaddingLeft() - padding,
300 icon.getTop() + getPaddingTop() - padding,
301 icon.getRight() + getPaddingLeft() + padding,
302 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800303 }
304
305 void setPressedOrFocusedIcon(BubbleTextView icon) {
306 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
307 // requires an expanded clip rect (due to the glow's blur radius)
308 BubbleTextView oldIcon = mPressedOrFocusedIcon;
309 mPressedOrFocusedIcon = icon;
310 if (oldIcon != null) {
311 invalidateBubbleTextView(oldIcon);
312 }
313 if (mPressedOrFocusedIcon != null) {
314 invalidateBubbleTextView(mPressedOrFocusedIcon);
315 }
316 }
317
Winson Chung6e314082011-01-27 16:46:51 -0800318 public CellLayoutChildren getChildrenLayout() {
319 if (getChildCount() > 0) {
320 return (CellLayoutChildren) getChildAt(0);
321 }
322 return null;
323 }
324
Michael Jurka33945b22010-12-21 18:19:38 -0800325 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
326 if (mIsDefaultDropTarget != isDefaultDropTarget) {
327 mIsDefaultDropTarget = isDefaultDropTarget;
328 invalidate();
329 }
330 }
331
Michael Jurka33945b22010-12-21 18:19:38 -0800332 void setIsDragOccuring(boolean isDragOccuring) {
333 if (mIsDragOccuring != isDragOccuring) {
334 mIsDragOccuring = isDragOccuring;
335 invalidate();
336 }
337 }
338
339 void setIsDragOverlapping(boolean isDragOverlapping) {
340 if (mIsDragOverlapping != isDragOverlapping) {
341 mIsDragOverlapping = isDragOverlapping;
342 invalidate();
343 }
344 }
345
346 boolean getIsDragOverlapping() {
347 return mIsDragOverlapping;
348 }
349
350 private void updateGlowRect() {
351 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700352 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
353 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800354 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700355 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
356 invalidate();
357 }
358
359 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800360 if (scaleFactor != mGlowBackgroundScale) {
361 mGlowBackgroundScale = scaleFactor;
362 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800363 if (getParent() != null) {
364 ((View) getParent()).invalidate();
365 }
Michael Jurka18014792010-10-14 09:01:34 -0700366 }
367 }
368
369 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800370 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700371 }
372
373 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800374 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700375 }
376
377 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800378 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700379 invalidate();
380 }
381
382 void animateDrop() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700383 Resources res = getResources();
384 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
385 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
386 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
387 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
388 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
389 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
Michael Jurka18014792010-10-14 09:01:34 -0700390
Winson Chungb26f3d62011-06-02 10:49:29 -0700391 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
392 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDuration));
Michael Jurka18014792010-10-14 09:01:34 -0700393
Winson Chungb26f3d62011-06-02 10:49:29 -0700394 AnimatorSet bouncer = new AnimatorSet();
395 bouncer.play(scaleUp).before(scaleDown);
396 bouncer.play(scaleUp).with(alphaFadeOut);
397 bouncer.addListener(new AnimatorListenerAdapter() {
398 @Override
399 public void onAnimationStart(Animator animation) {
400 setIsDragOverlapping(true);
401 }
402 @Override
403 public void onAnimationEnd(Animator animation) {
404 setIsDragOverlapping(false);
405 setHoverScale(1.0f);
406 setHoverAlpha(1.0f);
407 }
408 });
409 bouncer.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 }
411
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700412 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700413 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700414 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
415 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
416 // When we're small, we are either drawn normally or in the "accepts drops" state (during
417 // a drag). However, we also drag the mini hover background *over* one of those two
418 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700419 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700420 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800421 boolean mini = getScaleX() < 0.5f;
422
423 if (mIsDragOverlapping) {
424 // In the mini case, we draw the active_glow bg *over* the active background
425 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
426 } else if (mIsDragOccuring && mAcceptsDrops) {
427 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800428 } else if (mIsDefaultDropTarget && mini) {
429 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700430 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800431 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700432 }
Michael Jurka33945b22010-12-21 18:19:38 -0800433
434 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
435 bg.setBounds(mBackgroundRect);
436 bg.draw(canvas);
437
438 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700439 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800440 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700441 // If the hover background's scale is greater than 1, we'll be drawing outside
442 // the bounds of this CellLayout. Get around that by temporarily increasing the
443 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800444 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700445 Rect clipRect = canvas.getClipBounds();
446 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
447 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
448 canvas.save(Canvas.CLIP_SAVE_FLAG);
449 canvas.clipRect(-marginX, -marginY,
450 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
451 modifiedClipRect = true;
452 }
453
Michael Jurka33945b22010-12-21 18:19:38 -0800454 mActiveGlowBackgroundMini.setAlpha(
455 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
456 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
457 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700458 if (modifiedClipRect) {
459 canvas.restore();
460 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700461 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700462 }
Romain Guya6abce82009-11-10 02:54:41 -0800463
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700464 if (mCrosshairsVisibility > 0.0f) {
465 final int countX = mCountX;
466 final int countY = mCountY;
467
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700468 final float MAX_ALPHA = 0.4f;
469 final int MAX_VISIBLE_DISTANCE = 600;
470 final float DISTANCE_MULTIPLIER = 0.002f;
471
472 final Drawable d = mCrosshairsDrawable;
473 final int width = d.getIntrinsicWidth();
474 final int height = d.getIntrinsicHeight();
475
Winson Chung4b825dcd2011-06-19 12:41:22 -0700476 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700477 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700478 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700479 for (int row = 0; row <= countY; row++) {
480 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
481 float dist = mTmpPointF.length();
482 // Crosshairs further from the drag point are more faint
483 float alpha = Math.min(MAX_ALPHA,
484 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
485 if (alpha > 0.0f) {
486 d.setBounds(x, y, x + width, y + height);
487 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
488 d.draw(canvas);
489 }
490 y += mCellHeight + mHeightGap;
491 }
492 x += mCellWidth + mWidthGap;
493 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700494 }
Winson Chung150fbab2010-09-29 17:14:26 -0700495
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700496 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700497 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700498 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700499 if (alpha > 0) {
500 final Point p = mDragOutlines[i];
501 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700502 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700503 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700504 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700505 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800506
507 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
508 // requires an expanded clip rect (due to the glow's blur radius)
509 if (mPressedOrFocusedIcon != null) {
510 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
511 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
512 if (b != null) {
513 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700514 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
515 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800516 null);
517 }
518 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700519 }
520
521 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700522 public void cancelLongPress() {
523 super.cancelLongPress();
524
525 // Cancel long press for all children
526 final int count = getChildCount();
527 for (int i = 0; i < count; i++) {
528 final View child = getChildAt(i);
529 child.cancelLongPress();
530 }
531 }
532
Michael Jurkadee05892010-07-27 10:01:56 -0700533 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
534 mInterceptTouchListener = listener;
535 }
536
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700538 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 }
540
541 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700542 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 }
544
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700545 public boolean addViewToCellLayout(
546 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700547 final LayoutParams lp = params;
548
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 // Generate an id for each view, this assumes we have at most 256x256 cells
550 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700551 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700552 // If the horizontal or vertical span is set to -1, it is taken to
553 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700554 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
555 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556
Winson Chungaafa03c2010-06-11 17:34:16 -0700557 child.setId(childId);
558
Michael Jurka8c920dd2011-01-20 14:16:56 -0800559 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700560
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700561 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700562
Winson Chungaafa03c2010-06-11 17:34:16 -0700563 return true;
564 }
565 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700567
Michael Jurkabea15192010-11-17 12:33:46 -0800568 public void setAcceptsDrops(boolean acceptsDrops) {
569 if (mAcceptsDrops != acceptsDrops) {
570 mAcceptsDrops = acceptsDrops;
571 invalidate();
572 }
573 }
574
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700576 public void removeAllViews() {
577 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800578 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700579 }
580
581 @Override
582 public void removeAllViewsInLayout() {
583 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800584 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700585 }
586
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700587 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800588 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700589 }
590
Michael Jurka0280c3b2010-09-17 15:00:07 -0700591 @Override
592 public void removeView(View view) {
593 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800594 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700595 }
596
597 @Override
598 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800599 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
600 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700601 }
602
603 @Override
604 public void removeViewInLayout(View view) {
605 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800606 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700607 }
608
609 @Override
610 public void removeViews(int start, int count) {
611 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800612 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700613 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800614 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700615 }
616
617 @Override
618 public void removeViewsInLayout(int start, int count) {
619 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800620 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700621 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800622 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700623 }
624
Michael Jurka8c920dd2011-01-20 14:16:56 -0800625 public void drawChildren(Canvas canvas) {
626 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 }
628
Michael Jurkaabded662011-03-04 12:06:57 -0800629 void buildChildrenLayer() {
630 mChildren.buildLayer();
631 }
632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 @Override
634 protected void onAttachedToWindow() {
635 super.onAttachedToWindow();
636 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
637 }
638
Michael Jurkaaf442092010-06-10 17:01:57 -0700639 public void setTagToCellInfoForPoint(int touchX, int touchY) {
640 final CellInfo cellInfo = mCellInfo;
641 final Rect frame = mRect;
642 final int x = touchX + mScrollX;
643 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800644 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700645
646 boolean found = false;
647 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800648 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800649 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700650
Adam Cohen1b607ed2011-03-03 17:26:50 -0800651 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
652 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700653 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700654
655 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
656 // offset that by this CellLayout's padding to test an (x,y) point that is relative
657 // to this view.
658 final int tmpXY[] = mTmpXY;
659 child.getLocationOnScreen(tmpXY);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700660 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700661
Michael Jurkaaf442092010-06-10 17:01:57 -0700662 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700663 cellInfo.cell = child;
664 cellInfo.cellX = lp.cellX;
665 cellInfo.cellY = lp.cellY;
666 cellInfo.spanX = lp.cellHSpan;
667 cellInfo.spanY = lp.cellVSpan;
668 cellInfo.valid = true;
669 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700670 break;
671 }
672 }
673 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700674
Michael Jurkaaf442092010-06-10 17:01:57 -0700675 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700676 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700677 pointToCellExact(x, y, cellXY);
678
Michael Jurkaaf442092010-06-10 17:01:57 -0700679 cellInfo.cell = null;
680 cellInfo.cellX = cellXY[0];
681 cellInfo.cellY = cellXY[1];
682 cellInfo.spanX = 1;
683 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
685 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700686 }
687 setTag(cellInfo);
688 }
689
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 @Override
691 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700692 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
693 return true;
694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 final int action = ev.getAction();
696 final CellInfo cellInfo = mCellInfo;
697
698 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700699 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 } else if (action == MotionEvent.ACTION_UP) {
701 cellInfo.cell = null;
702 cellInfo.cellX = -1;
703 cellInfo.cellY = -1;
704 cellInfo.spanX = 0;
705 cellInfo.spanY = 0;
706 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 setTag(cellInfo);
708 }
709
710 return false;
711 }
712
713 @Override
714 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700715 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 }
717
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700718 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700719 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 * @param x X coordinate of the point
721 * @param y Y coordinate of the point
722 * @param result Array of 2 ints to hold the x and y coordinate of the cell
723 */
724 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700725 final int hStartPadding = getPaddingLeft();
726 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727
728 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
729 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
730
Adam Cohend22015c2010-07-26 22:02:18 -0700731 final int xAxis = mCountX;
732 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733
734 if (result[0] < 0) result[0] = 0;
735 if (result[0] >= xAxis) result[0] = xAxis - 1;
736 if (result[1] < 0) result[1] = 0;
737 if (result[1] >= yAxis) result[1] = yAxis - 1;
738 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700739
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 /**
741 * Given a point, return the cell that most closely encloses that point
742 * @param x X coordinate of the point
743 * @param y Y coordinate of the point
744 * @param result Array of 2 ints to hold the x and y coordinate of the cell
745 */
746 void pointToCellRounded(int x, int y, int[] result) {
747 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
748 }
749
750 /**
751 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700752 *
753 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700755 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 * @param result Array of 2 ints to hold the x and y coordinate of the point
757 */
758 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700759 final int hStartPadding = getPaddingLeft();
760 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761
762 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
763 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
764 }
765
Adam Cohene3e27a82011-04-15 12:07:39 -0700766 /**
767 * Given a cell coordinate, return the point that represents the upper left corner of that cell
768 *
769 * @param cellX X coordinate of the cell
770 * @param cellY Y coordinate of the cell
771 *
772 * @param result Array of 2 ints to hold the x and y coordinate of the point
773 */
774 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700775 final int hStartPadding = getPaddingLeft();
776 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700777
778 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
779 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
780 }
781
Romain Guy84f296c2009-11-04 15:00:44 -0800782 int getCellWidth() {
783 return mCellWidth;
784 }
785
786 int getCellHeight() {
787 return mCellHeight;
788 }
789
Adam Cohend4844c32011-02-18 19:25:06 -0800790 int getWidthGap() {
791 return mWidthGap;
792 }
793
794 int getHeightGap() {
795 return mHeightGap;
796 }
797
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700798 Rect getContentRect(Rect r) {
799 if (r == null) {
800 r = new Rect();
801 }
802 int left = getPaddingLeft();
803 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700804 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
805 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700806 r.set(left, top, right, bottom);
807 return r;
808 }
809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 @Override
811 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
812 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700815 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
816
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
818 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700819
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
821 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
822 }
823
Adam Cohend22015c2010-07-26 22:02:18 -0700824 int numWidthGaps = mCountX - 1;
825 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826
Winson Chungece7f5b2010-10-22 14:54:12 -0700827 if (mWidthGap < 0 || mHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700828 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
829 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
830 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
831 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
832 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
833 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
834 int remainingHSpace = hFreeSpace - (numWidthGaps * mWidthGap);
835 int remainingVSpace = vFreeSpace - (numHeightGaps * mHeightGap);
836 mCellWidth = mOriginalCellWidth + remainingHSpace / mCountX;
837 mCellHeight = mOriginalCellHeight + remainingVSpace / mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838
Winson Chung4b825dcd2011-06-19 12:41:22 -0700839 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Winson Chungece7f5b2010-10-22 14:54:12 -0700840 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700841
Michael Jurka8c920dd2011-01-20 14:16:56 -0800842 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
843 int newWidth = widthSpecSize;
844 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700845 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700846 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700847 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700848 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700849 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700850 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700851 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800852
853 int count = getChildCount();
854 for (int i = 0; i < count; i++) {
855 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700856 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
857 mPaddingRight, MeasureSpec.EXACTLY);
858 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
859 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800860 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
861 }
862 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 }
864
865 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700866 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800869 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700870 child.layout(mPaddingLeft, mPaddingTop,
871 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 }
873 }
874
875 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700876 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
877 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700878 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800879 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700880 }
881
882 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800884 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886
887 @Override
888 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800889 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 }
891
Michael Jurka5f1c5092010-09-03 14:15:02 -0700892 public float getBackgroundAlpha() {
893 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700894 }
895
Michael Jurka742574b2011-02-02 23:51:01 -0800896 public void setFastBackgroundAlpha(float alpha) {
897 mBackgroundAlpha = alpha;
898 }
899
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700900 public void setBackgroundAlphaMultiplier(float multiplier) {
901 mBackgroundAlphaMultiplier = multiplier;
902 }
903
Adam Cohenddb82192010-11-10 16:32:54 -0800904 public float getBackgroundAlphaMultiplier() {
905 return mBackgroundAlphaMultiplier;
906 }
907
Michael Jurka5f1c5092010-09-03 14:15:02 -0700908 public void setBackgroundAlpha(float alpha) {
909 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700910 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700911 }
912
Michael Jurka5f1c5092010-09-03 14:15:02 -0700913 // Need to return true to let the view system know we know how to handle alpha-- this is
914 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
915 // versions
916 @Override
917 protected boolean onSetAlpha(int alpha) {
918 return true;
919 }
920
921 public void setAlpha(float alpha) {
922 setChildrenAlpha(alpha);
923 super.setAlpha(alpha);
924 }
925
Michael Jurka742574b2011-02-02 23:51:01 -0800926 public void setFastAlpha(float alpha) {
927 setFastChildrenAlpha(alpha);
928 super.setFastAlpha(alpha);
929 }
930
Michael Jurkadee05892010-07-27 10:01:56 -0700931 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700932 final int childCount = getChildCount();
933 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700934 getChildAt(i).setAlpha(alpha);
935 }
936 }
937
Michael Jurka742574b2011-02-02 23:51:01 -0800938 private void setFastChildrenAlpha(float alpha) {
939 final int childCount = getChildCount();
940 for (int i = 0; i < childCount; i++) {
941 getChildAt(i).setFastAlpha(alpha);
942 }
943 }
944
Patrick Dubroy440c3602010-07-13 17:50:32 -0700945 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800946 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700947 }
948
Adam Cohen76fc0852011-06-17 13:26:23 -0700949 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
950 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700951 CellLayoutChildren clc = getChildrenLayout();
952 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
953 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
954 final ItemInfo info = (ItemInfo) child.getTag();
955
956 // We cancel any existing animations
957 if (mReorderAnimators.containsKey(lp)) {
958 mReorderAnimators.get(lp).cancel();
959 mReorderAnimators.remove(lp);
960 }
961
962 int oldX = lp.x;
963 int oldY = lp.y;
964 mOccupied[lp.cellX][lp.cellY] = false;
965 mOccupied[cellX][cellY] = true;
966
967 lp.isLockedToGrid = true;
968 lp.cellX = info.cellX = cellX;
969 lp.cellY = info.cellY = cellY;
970 clc.setupLp(lp);
971 lp.isLockedToGrid = false;
972 int newX = lp.x;
973 int newY = lp.y;
974
Adam Cohen76fc0852011-06-17 13:26:23 -0700975 lp.x = oldX;
976 lp.y = oldY;
977 child.requestLayout();
978
Adam Cohenbfbfd262011-06-13 16:55:12 -0700979 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
980 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
981 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
982 oa.setDuration(duration);
983 mReorderAnimators.put(lp, oa);
984 oa.addUpdateListener(new AnimatorUpdateListener() {
985 public void onAnimationUpdate(ValueAnimator animation) {
986 child.requestLayout();
987 }
988 });
989 oa.addListener(new AnimatorListenerAdapter() {
990 boolean cancelled = false;
991 public void onAnimationEnd(Animator animation) {
992 // If the animation was cancelled, it means that another animation
993 // has interrupted this one, and we don't want to lock the item into
994 // place just yet.
995 if (!cancelled) {
996 lp.isLockedToGrid = true;
997 }
998 if (mReorderAnimators.containsKey(lp)) {
999 mReorderAnimators.remove(lp);
1000 }
1001 }
1002 public void onAnimationCancel(Animator animation) {
1003 cancelled = true;
1004 }
1005 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001006 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001007 oa.start();
1008 return true;
1009 }
1010 return false;
1011 }
1012
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001013 /**
1014 * Estimate where the top left cell of the dragged item will land if it is dropped.
1015 *
1016 * @param originX The X value of the top left corner of the item
1017 * @param originY The Y value of the top left corner of the item
1018 * @param spanX The number of horizontal cells that the item spans
1019 * @param spanY The number of vertical cells that the item spans
1020 * @param result The estimated drop cell X and Y.
1021 */
1022 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001023 final int countX = mCountX;
1024 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001025
Michael Jurkaa63c4522010-08-19 13:52:27 -07001026 // pointToCellRounded takes the top left of a cell but will pad that with
1027 // cellWidth/2 and cellHeight/2 when finding the matching cell
1028 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001029
1030 // If the item isn't fully on this screen, snap to the edges
1031 int rightOverhang = result[0] + spanX - countX;
1032 if (rightOverhang > 0) {
1033 result[0] -= rightOverhang; // Snap to right
1034 }
1035 result[0] = Math.max(0, result[0]); // Snap to left
1036 int bottomOverhang = result[1] + spanY - countY;
1037 if (bottomOverhang > 0) {
1038 result[1] -= bottomOverhang; // Snap to bottom
1039 }
1040 result[1] = Math.max(0, result[1]); // Snap to top
1041 }
1042
Joe Onorato4be866d2010-10-10 11:26:02 -07001043 void visualizeDropLocation(
1044 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1045
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001046 final int oldDragCellX = mDragCell[0];
1047 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001048 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001049 if (v != null) {
1050 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1051 } else {
1052 mDragCenter.set(originX, originY);
1053 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001054
Adam Cohen2801caf2011-05-13 20:57:39 -07001055 if (dragOutline == null && v == null) {
1056 if (mCrosshairsDrawable != null) {
1057 invalidate();
1058 }
1059 return;
1060 }
1061
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001062 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001063 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001064 final int[] topLeft = mTmpPoint;
1065 cellToPoint(nearest[0], nearest[1], topLeft);
1066
Joe Onorato4be866d2010-10-10 11:26:02 -07001067 int left = topLeft[0];
1068 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001069
Winson Chunga9abd0e2010-10-27 17:18:37 -07001070 if (v != null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001071 // When drawing the drag outline, it did not account for margin offsets
1072 // added by the view's parent.
1073 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1074 left += lp.leftMargin;
1075 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001076
Adam Cohen99e8b402011-03-25 19:23:43 -07001077 // Offsets due to the size difference between the View and the dragOutline.
1078 // There is a size difference to account for the outer blur, which may lie
1079 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001080 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1081 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001082 } else {
1083 // Center the drag outline in the cell
Winson Chung4b576be2011-04-27 17:40:20 -07001084 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1085 - dragOutline.getWidth()) / 2;
1086 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1087 - dragOutline.getHeight()) / 2;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001088 }
Winson Chung150fbab2010-09-29 17:14:26 -07001089
Joe Onorato4be866d2010-10-10 11:26:02 -07001090 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001091 mDragOutlineAnims[oldIndex].animateOut();
1092 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001093
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001094 mDragOutlines[mDragOutlineCurrent].set(left, top);
1095 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1096 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001097 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001098
1099 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1100 if (mCrosshairsDrawable != null) {
1101 invalidate();
1102 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001103 }
1104
Adam Cohene0310962011-04-18 16:15:31 -07001105 public void clearDragOutlines() {
1106 final int oldIndex = mDragOutlineCurrent;
1107 mDragOutlineAnims[oldIndex].animateOut();
1108 mDragCell[0] = -1;
1109 mDragCell[1] = -1;
1110 }
1111
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001113 * Find a vacant area that will fit the given bounds nearest the requested
1114 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001115 *
Romain Guy51afc022009-05-04 18:03:43 -07001116 * @param pixelX The X location at which you want to search for a vacant area.
1117 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001118 * @param spanX Horizontal span of the object.
1119 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001120 * @param result Array in which to place the result, or null (in which case a new array will
1121 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001122 * @return The X, Y cell of a vacant area that can contain this object,
1123 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001125 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001126 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1127 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001128 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001129
Michael Jurka6a1435d2010-09-27 17:35:12 -07001130 /**
1131 * Find a vacant area that will fit the given bounds nearest the requested
1132 * cell location. Uses Euclidean distance to score multiple vacant areas.
1133 *
1134 * @param pixelX The X location at which you want to search for a vacant area.
1135 * @param pixelY The Y location at which you want to search for a vacant area.
1136 * @param spanX Horizontal span of the object.
1137 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001138 * @param ignoreOccupied If true, the result can be an occupied cell
1139 * @param result Array in which to place the result, or null (in which case a new array will
1140 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001141 * @return The X, Y cell of a vacant area that can contain this object,
1142 * nearest the requested location.
1143 */
Adam Cohendf035382011-04-11 17:22:04 -07001144 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1145 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001146 // mark space take by ignoreView as available (method checks if ignoreView is null)
1147 markCellsAsUnoccupiedForView(ignoreView);
1148
Adam Cohene3e27a82011-04-15 12:07:39 -07001149 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1150 // to the center of the item, but we are searching based on the top-left cell, so
1151 // we translate the point over to correspond to the top-left.
1152 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1153 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1154
Jeff Sharkey70864282009-04-07 21:08:40 -07001155 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001156 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001157 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001158
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001159 final int countX = mCountX;
1160 final int countY = mCountY;
1161 final boolean[][] occupied = mOccupied;
1162
Winson Chungbbc60d82010-11-11 16:34:41 -08001163 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001164 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001165 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001166 if (ignoreOccupied) {
1167 for (int i = 0; i < spanX; i++) {
1168 for (int j = 0; j < spanY; j++) {
1169 if (occupied[x + i][y + j]) {
1170 // small optimization: we can skip to after the column we
1171 // just found an occupied cell
1172 x += i;
1173 continue inner;
1174 }
Michael Jurkac28de512010-08-13 11:27:44 -07001175 }
1176 }
1177 }
Winson Chung0be025d2011-05-23 17:45:09 -07001178 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001179 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180
Michael Jurkac28de512010-08-13 11:27:44 -07001181 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1182 + Math.pow(cellXY[1] - pixelY, 2));
1183 if (distance <= bestDistance) {
1184 bestDistance = distance;
1185 bestXY[0] = x;
1186 bestXY[1] = y;
1187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 }
1189 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001190 // re-mark space taken by ignoreView as occupied
1191 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192
Adam Cohenc0dcf592011-06-01 15:30:43 -07001193 // Return -1, -1 if no suitable location found
1194 if (bestDistance == Double.MAX_VALUE) {
1195 bestXY[0] = -1;
1196 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001197 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001198 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001200
Adam Cohendf035382011-04-11 17:22:04 -07001201 /**
1202 * Find a vacant area that will fit the given bounds nearest the requested
1203 * cell location. Uses Euclidean distance to score multiple vacant areas.
1204 *
1205 * @param pixelX The X location at which you want to search for a vacant area.
1206 * @param pixelY The Y location at which you want to search for a vacant area.
1207 * @param spanX Horizontal span of the object.
1208 * @param spanY Vertical span of the object.
1209 * @param ignoreView Considers space occupied by this view as unoccupied
1210 * @param result Previously returned value to possibly recycle.
1211 * @return The X, Y cell of a vacant area that can contain this object,
1212 * nearest the requested location.
1213 */
1214 int[] findNearestVacantArea(
1215 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1216 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1217 }
1218
1219 /**
1220 * Find a starting cell position that will fit the given bounds nearest the requested
1221 * cell location. Uses Euclidean distance to score multiple vacant areas.
1222 *
1223 * @param pixelX The X location at which you want to search for a vacant area.
1224 * @param pixelY The Y location at which you want to search for a vacant area.
1225 * @param spanX Horizontal span of the object.
1226 * @param spanY Vertical span of the object.
1227 * @param ignoreView Considers space occupied by this view as unoccupied
1228 * @param result Previously returned value to possibly recycle.
1229 * @return The X, Y cell of a vacant area that can contain this object,
1230 * nearest the requested location.
1231 */
1232 int[] findNearestArea(
1233 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1234 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1235 }
1236
Michael Jurka0280c3b2010-09-17 15:00:07 -07001237 boolean existsEmptyCell() {
1238 return findCellForSpan(null, 1, 1);
1239 }
1240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001242 * Finds the upper-left coordinate of the first rectangle in the grid that can
1243 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1244 * then this method will only return coordinates for rectangles that contain the cell
1245 * (intersectX, intersectY)
1246 *
1247 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1248 * can be found.
1249 * @param spanX The horizontal span of the cell we want to find.
1250 * @param spanY The vertical span of the cell we want to find.
1251 *
1252 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001253 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001254 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1255 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1256 }
1257
1258 /**
1259 * Like above, but ignores any cells occupied by the item "ignoreView"
1260 *
1261 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1262 * can be found.
1263 * @param spanX The horizontal span of the cell we want to find.
1264 * @param spanY The vertical span of the cell we want to find.
1265 * @param ignoreView The home screen item we should treat as not occupying any space
1266 * @return
1267 */
1268 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1269 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1270 }
1271
1272 /**
1273 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1274 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1275 *
1276 * @param spanX The horizontal span of the cell we want to find.
1277 * @param spanY The vertical span of the cell we want to find.
1278 * @param ignoreView The home screen item we should treat as not occupying any space
1279 * @param intersectX The X coordinate of the cell that we should try to overlap
1280 * @param intersectX The Y coordinate of the cell that we should try to overlap
1281 *
1282 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1283 */
1284 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1285 int intersectX, int intersectY) {
1286 return findCellForSpanThatIntersectsIgnoring(
1287 cellXY, spanX, spanY, intersectX, intersectY, null);
1288 }
1289
1290 /**
1291 * The superset of the above two methods
1292 */
1293 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1294 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001295 // mark space take by ignoreView as available (method checks if ignoreView is null)
1296 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001297
Michael Jurka28750fb2010-09-24 17:43:49 -07001298 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001299 while (true) {
1300 int startX = 0;
1301 if (intersectX >= 0) {
1302 startX = Math.max(startX, intersectX - (spanX - 1));
1303 }
1304 int endX = mCountX - (spanX - 1);
1305 if (intersectX >= 0) {
1306 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1307 }
1308 int startY = 0;
1309 if (intersectY >= 0) {
1310 startY = Math.max(startY, intersectY - (spanY - 1));
1311 }
1312 int endY = mCountY - (spanY - 1);
1313 if (intersectY >= 0) {
1314 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1315 }
1316
Winson Chungbbc60d82010-11-11 16:34:41 -08001317 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001318 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001319 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001320 for (int i = 0; i < spanX; i++) {
1321 for (int j = 0; j < spanY; j++) {
1322 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001323 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001324 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001325 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001326 continue inner;
1327 }
1328 }
1329 }
1330 if (cellXY != null) {
1331 cellXY[0] = x;
1332 cellXY[1] = y;
1333 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001334 foundCell = true;
1335 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001336 }
1337 }
1338 if (intersectX == -1 && intersectY == -1) {
1339 break;
1340 } else {
1341 // if we failed to find anything, try again but without any requirements of
1342 // intersecting
1343 intersectX = -1;
1344 intersectY = -1;
1345 continue;
1346 }
1347 }
1348
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001349 // re-mark space taken by ignoreView as occupied
1350 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001351 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001352 }
1353
1354 /**
1355 * Called when drag has left this CellLayout or has been completed (successfully or not)
1356 */
1357 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001358 // This can actually be called when we aren't in a drag, e.g. when adding a new
1359 // item to this layout via the customize drawer.
1360 // Guard against that case.
1361 if (mDragging) {
1362 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001363
Joe Onorato4be866d2010-10-10 11:26:02 -07001364 // Fade out the drag indicators
1365 if (mCrosshairsAnimator != null) {
1366 mCrosshairsAnimator.animateOut();
1367 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001368 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001369
1370 // Invalidate the drag data
1371 mDragCell[0] = -1;
1372 mDragCell[1] = -1;
1373 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1374 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1375
Michael Jurka33945b22010-12-21 18:19:38 -08001376 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001377 }
1378
1379 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001380 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001381 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001382 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 *
1384 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001386 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001387 if (child != null) {
1388 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001389 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001390 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001391 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
1394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 /**
1396 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001397 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 * @param child The child that is being dragged
1399 */
1400 void onDragChild(View child) {
1401 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1402 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001403 }
1404
1405 /**
1406 * A drag event has begun over this layout.
1407 * It may have begun over this layout (in which case onDragChild is called first),
1408 * or it may have begun on another layout.
1409 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001410 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001411 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001412 // Fade in the drag indicators
1413 if (mCrosshairsAnimator != null) {
1414 mCrosshairsAnimator.animateIn();
1415 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001416 }
1417 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001419
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001422 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 * @param cellX X coordinate of upper left corner expressed as a cell position
1424 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001425 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001427 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001429 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 final int cellWidth = mCellWidth;
1431 final int cellHeight = mCellHeight;
1432 final int widthGap = mWidthGap;
1433 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001434
Winson Chung4b825dcd2011-06-19 12:41:22 -07001435 final int hStartPadding = getPaddingLeft();
1436 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001437
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1439 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1440
1441 int x = hStartPadding + cellX * (cellWidth + widthGap);
1442 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001443
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001444 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001446
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001448 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001450 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 * @param width Width in pixels
1452 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001453 * @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 -08001454 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001455 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001456 return rectToCell(getResources(), width, height, result);
1457 }
1458
1459 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 // Always assume we're working with the smallest span to make sure we
1461 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001462 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1463 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 // Always round up to next largest cell
1467 int spanX = (width + smallerSize) / smallerSize;
1468 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001469
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001470 if (result == null) {
1471 return new int[] { spanX, spanY };
1472 }
1473 result[0] = spanX;
1474 result[1] = spanY;
1475 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001478 public int[] cellSpansToSize(int hSpans, int vSpans) {
1479 int[] size = new int[2];
1480 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1481 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1482 return size;
1483 }
1484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001486 * Calculate the grid spans needed to fit given item
1487 */
1488 public void calculateSpans(ItemInfo info) {
1489 final int minWidth;
1490 final int minHeight;
1491
1492 if (info instanceof LauncherAppWidgetInfo) {
1493 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1494 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1495 } else if (info instanceof PendingAddWidgetInfo) {
1496 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1497 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1498 } else {
1499 // It's not a widget, so it must be 1x1
1500 info.spanX = info.spanY = 1;
1501 return;
1502 }
1503 int[] spans = rectToCell(minWidth, minHeight, null);
1504 info.spanX = spans[0];
1505 info.spanY = spans[1];
1506 }
1507
1508 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 * Find the first vacant cell, if there is one.
1510 *
1511 * @param vacant Holds the x and y coordinate of the vacant cell
1512 * @param spanX Horizontal cell span.
1513 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001514 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 * @return True if a vacant cell was found
1516 */
1517 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518
Michael Jurka0280c3b2010-09-17 15:00:07 -07001519 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 }
1521
1522 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1523 int xCount, int yCount, boolean[][] occupied) {
1524
Adam Cohen2801caf2011-05-13 20:57:39 -07001525 for (int y = 0; y < yCount; y++) {
1526 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 boolean available = !occupied[x][y];
1528out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1529 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1530 available = available && !occupied[i][j];
1531 if (!available) break out;
1532 }
1533 }
1534
1535 if (available) {
1536 vacant[0] = x;
1537 vacant[1] = y;
1538 return true;
1539 }
1540 }
1541 }
1542
1543 return false;
1544 }
1545
Michael Jurka0280c3b2010-09-17 15:00:07 -07001546 private void clearOccupiedCells() {
1547 for (int x = 0; x < mCountX; x++) {
1548 for (int y = 0; y < mCountY; y++) {
1549 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
1551 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001552 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553
Adam Cohen1b607ed2011-03-03 17:26:50 -08001554 /**
1555 * Given a view, determines how much that view can be expanded in all directions, in terms of
1556 * whether or not there are other items occupying adjacent cells. Used by the
1557 * AppWidgetResizeFrame to determine how the widget can be resized.
1558 */
Adam Cohend4844c32011-02-18 19:25:06 -08001559 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001560 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001561 boolean flag;
1562
Adam Cohen1b607ed2011-03-03 17:26:50 -08001563 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001564 for (int x = lp.cellX - 1; x >= 0; x--) {
1565 flag = false;
1566 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1567 if (mOccupied[x][y]) flag = true;
1568 }
1569 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001570 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001571 }
1572
Adam Cohen1b607ed2011-03-03 17:26:50 -08001573 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001574 for (int y = lp.cellY - 1; y >= 0; y--) {
1575 flag = false;
1576 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1577 if (mOccupied[x][y]) flag = true;
1578 }
1579 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001580 expandability[AppWidgetResizeFrame.TOP]++;
1581 }
Adam Cohend4844c32011-02-18 19:25:06 -08001582
Adam Cohen1b607ed2011-03-03 17:26:50 -08001583 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001584 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1585 flag = false;
1586 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1587 if (mOccupied[x][y]) flag = true;
1588 }
1589 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001590 expandability[AppWidgetResizeFrame.RIGHT]++;
1591 }
Adam Cohend4844c32011-02-18 19:25:06 -08001592
Adam Cohen1b607ed2011-03-03 17:26:50 -08001593 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001594 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1595 flag = false;
1596 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1597 if (mOccupied[x][y]) flag = true;
1598 }
1599 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001600 expandability[AppWidgetResizeFrame.BOTTOM]++;
1601 }
Adam Cohend4844c32011-02-18 19:25:06 -08001602 }
1603
Michael Jurka0280c3b2010-09-17 15:00:07 -07001604 public void onMove(View view, int newCellX, int newCellY) {
1605 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1606 markCellsAsUnoccupiedForView(view);
1607 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1608 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609
Adam Cohend4844c32011-02-18 19:25:06 -08001610 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001611 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001612 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1613 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1614 }
1615
Adam Cohend4844c32011-02-18 19:25:06 -08001616 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001617 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001618 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1619 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1620 }
1621
1622 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1623 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1624 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1625 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 }
1627 }
1628 }
1629
Adam Cohen2801caf2011-05-13 20:57:39 -07001630 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001631 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001632 (Math.max((mCountX - 1), 0) * mWidthGap);
1633 }
1634
1635 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001636 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001637 (Math.max((mCountY - 1), 0) * mHeightGap);
1638 }
1639
Michael Jurka66d72172011-04-12 16:29:25 -07001640 public boolean isOccupied(int x, int y) {
1641 if (x < mCountX && y < mCountY) {
1642 return mOccupied[x][y];
1643 } else {
1644 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1645 }
1646 }
1647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 @Override
1649 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1650 return new CellLayout.LayoutParams(getContext(), attrs);
1651 }
1652
1653 @Override
1654 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1655 return p instanceof CellLayout.LayoutParams;
1656 }
1657
1658 @Override
1659 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1660 return new CellLayout.LayoutParams(p);
1661 }
1662
Winson Chungaafa03c2010-06-11 17:34:16 -07001663 public static class CellLayoutAnimationController extends LayoutAnimationController {
1664 public CellLayoutAnimationController(Animation animation, float delay) {
1665 super(animation, delay);
1666 }
1667
1668 @Override
1669 protected long getDelayForView(View view) {
1670 return (int) (Math.random() * 150);
1671 }
1672 }
1673
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1675 /**
1676 * Horizontal location of the item in the grid.
1677 */
1678 @ViewDebug.ExportedProperty
1679 public int cellX;
1680
1681 /**
1682 * Vertical location of the item in the grid.
1683 */
1684 @ViewDebug.ExportedProperty
1685 public int cellY;
1686
1687 /**
1688 * Number of cells spanned horizontally by the item.
1689 */
1690 @ViewDebug.ExportedProperty
1691 public int cellHSpan;
1692
1693 /**
1694 * Number of cells spanned vertically by the item.
1695 */
1696 @ViewDebug.ExportedProperty
1697 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001698
Adam Cohen1b607ed2011-03-03 17:26:50 -08001699 /**
1700 * Indicates whether the item will set its x, y, width and height parameters freely,
1701 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1702 */
Adam Cohend4844c32011-02-18 19:25:06 -08001703 public boolean isLockedToGrid = true;
1704
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 /**
1706 * Is this item currently being dragged
1707 */
1708 public boolean isDragging;
1709
1710 // X coordinate of the view in the layout.
1711 @ViewDebug.ExportedProperty
1712 int x;
1713 // Y coordinate of the view in the layout.
1714 @ViewDebug.ExportedProperty
1715 int y;
1716
Romain Guy84f296c2009-11-04 15:00:44 -08001717 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001718
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 public LayoutParams(Context c, AttributeSet attrs) {
1720 super(c, attrs);
1721 cellHSpan = 1;
1722 cellVSpan = 1;
1723 }
1724
1725 public LayoutParams(ViewGroup.LayoutParams source) {
1726 super(source);
1727 cellHSpan = 1;
1728 cellVSpan = 1;
1729 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001730
1731 public LayoutParams(LayoutParams source) {
1732 super(source);
1733 this.cellX = source.cellX;
1734 this.cellY = source.cellY;
1735 this.cellHSpan = source.cellHSpan;
1736 this.cellVSpan = source.cellVSpan;
1737 }
1738
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001740 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 this.cellX = cellX;
1742 this.cellY = cellY;
1743 this.cellHSpan = cellHSpan;
1744 this.cellVSpan = cellVSpan;
1745 }
1746
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001747 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001748 if (isLockedToGrid) {
1749 final int myCellHSpan = cellHSpan;
1750 final int myCellVSpan = cellVSpan;
1751 final int myCellX = cellX;
1752 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001753
Adam Cohend4844c32011-02-18 19:25:06 -08001754 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1755 leftMargin - rightMargin;
1756 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1757 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001758 x = myCellX * (cellWidth + widthGap) + leftMargin;
1759 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001760 }
1761 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001762
Winson Chungaafa03c2010-06-11 17:34:16 -07001763 public String toString() {
1764 return "(" + this.cellX + ", " + this.cellY + ")";
1765 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001766
1767 public void setWidth(int width) {
1768 this.width = width;
1769 }
1770
1771 public int getWidth() {
1772 return width;
1773 }
1774
1775 public void setHeight(int height) {
1776 this.height = height;
1777 }
1778
1779 public int getHeight() {
1780 return height;
1781 }
1782
1783 public void setX(int x) {
1784 this.x = x;
1785 }
1786
1787 public int getX() {
1788 return x;
1789 }
1790
1791 public void setY(int y) {
1792 this.y = y;
1793 }
1794
1795 public int getY() {
1796 return y;
1797 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 }
1799
Michael Jurka0280c3b2010-09-17 15:00:07 -07001800 // This class stores info for two purposes:
1801 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1802 // its spanX, spanY, and the screen it is on
1803 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1804 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1805 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001806 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001808 int cellX = -1;
1809 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 int spanX;
1811 int spanY;
1812 int screen;
1813 boolean valid;
1814
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 @Override
1816 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001817 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1818 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 }
1820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821}