blob: 08dc866eddfcc9d4735070568a8dede000260901 [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.ObjectAnimator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070022import android.animation.PropertyValuesHolder;
Chet Haase00397b12010-10-07 11:13:10 -070023import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070024import android.animation.ValueAnimator;
25import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040027import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070029import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080031import android.graphics.Color;
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;
Adam Cohenb5ba0972011-09-07 18:02:31 -070035import android.graphics.PorterDuff;
36import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.graphics.Rect;
38import android.graphics.RectF;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070039import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070040import android.graphics.drawable.NinePatchDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070042import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Adam Cohen66396872011-04-15 17:50:36 -070051import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070052import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070053
Adam Cohen69ce2e52011-07-03 19:25:21 -070054import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070056import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -070057
Michael Jurkabdb5c532011-02-01 15:05:06 -080058public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070059 static final String TAG = "CellLayout";
60
Winson Chung4b825dcd2011-06-19 12:41:22 -070061 private int mOriginalCellWidth;
62 private int mOriginalCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 private int mCellWidth;
64 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070065
Adam Cohend22015c2010-07-26 22:02:18 -070066 private int mCountX;
67 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
Adam Cohen234c4cd2011-07-17 21:03:04 -070069 private int mOriginalWidthGap;
70 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 private int mWidthGap;
72 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070073 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080074 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
76 private final Rect mRect = new Rect();
77 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070078
Patrick Dubroyde7658b2010-09-27 11:15:43 -070079 // These are temporary variables to prevent having to allocate a new object just to
80 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070081 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070082 private final int[] mTmpPoint = new int[2];
83 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070084 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070085
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 boolean[][] mOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070087 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Michael Jurkadee05892010-07-27 10:01:56 -070089 private OnTouchListener mInterceptTouchListener;
90
Adam Cohen69ce2e52011-07-03 19:25:21 -070091 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070092 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070093
Adam Cohenb5ba0972011-09-07 18:02:31 -070094 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070095 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070096 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070097
Michael Jurka33945b22010-12-21 18:19:38 -080098 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080099 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700100 private Drawable mOverScrollForegroundDrawable;
101 private Drawable mOverScrollLeft;
102 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700103 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700104 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700105 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700106
Michael Jurka33945b22010-12-21 18:19:38 -0800107 // If we're actively dragging something over this screen, mIsDragOverlapping is true
108 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700109 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700110
Winson Chung150fbab2010-09-29 17:14:26 -0700111 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700112 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Winson Chung63257c12011-05-05 17:06:13 -0700113 private Point[] mDragOutlines = new Point[4];
Chet Haase472b2812010-10-14 07:02:04 -0700114 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700115 private InterruptibleInOutAnimator[] mDragOutlineAnims =
116 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700117
118 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700119 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700120 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700121
Patrick Dubroy96864c32011-03-10 17:17:23 -0800122 private BubbleTextView mPressedOrFocusedIcon;
123
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700124 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700125 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700126 private float mCrosshairsVisibility = 0.0f;
127
Adam Cohenbfbfd262011-06-13 16:55:12 -0700128 private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new
129 HashMap<CellLayout.LayoutParams, ObjectAnimator>();
130
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700131 // When a drag operation is in progress, holds the nearest cell to the touch point
132 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Joe Onorato4be866d2010-10-10 11:26:02 -0700134 private boolean mDragging = false;
135
Patrick Dubroyce34a972010-10-19 10:34:32 -0700136 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800137 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700138
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800139 private boolean mIsHotseat = false;
140 private final int mBubbleScalePercent;
Andrew Flynnbc239a12012-03-06 11:39:49 -0800141 private final int mBubbleHotseatScalePercent;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 public CellLayout(Context context) {
144 this(context, null);
145 }
146
147 public CellLayout(Context context, AttributeSet attrs) {
148 this(context, attrs, 0);
149 }
150
151 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
152 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700153
154 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
155 // the user where a dragged item will land when dropped.
156 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
159
Winson Chung4b825dcd2011-06-19 12:41:22 -0700160 mOriginalCellWidth =
161 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
162 mOriginalCellHeight =
163 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700164 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
165 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700166 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700167 mCountX = LauncherModel.getCellCountX();
168 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700169 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
171 a.recycle();
172
173 setAlwaysDrawnWithCacheEnabled(false);
174
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700175 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700176
Winson Chung967289b2011-06-30 18:09:30 -0700177 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700178 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800179
Adam Cohenb5ba0972011-09-07 18:02:31 -0700180 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
181 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
182 mForegroundPadding =
183 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800184
Winson Chungb26f3d62011-06-02 10:49:29 -0700185 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700186 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700187
Andrew Flynnbc239a12012-03-06 11:39:49 -0800188 mBubbleScalePercent = res.getInteger(R.integer.app_icon_scale_percent);
189 mBubbleHotseatScalePercent = res.getInteger(R.integer.app_icon_hotseat_scale_percent);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800190
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700191 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700192
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700193 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700194 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700195
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700196 // Set up the animation for fading the crosshairs in and out
197 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700198 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700199 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700200 public void onAnimationUpdate(ValueAnimator animation) {
201 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700202 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700203 }
204 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700205 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700206
Winson Chungb8c69f32011-10-19 21:36:08 -0700207 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700208 for (int i = 0; i < mDragOutlines.length; i++) {
209 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700210 }
211
212 // When dragging things around the home screens, we show a green outline of
213 // where the item will land. The outlines gradually fade out, leaving a trail
214 // behind the drag path.
215 // Set up all the animations that are used to implement this fading.
216 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700217 final float fromAlphaValue = 0;
218 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700219
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700220 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700221
222 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700223 final InterruptibleInOutAnimator anim =
224 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700225 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700226 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700227 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700228 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700229 final Bitmap outline = (Bitmap)anim.getTag();
230
231 // If an animation is started and then stopped very quickly, we can still
232 // get spurious updates we've cleared the tag. Guard against this.
233 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700234 if (false) {
235 Object val = animation.getAnimatedValue();
236 Log.d(TAG, "anim " + thisIndex + " update: " + val +
237 ", isStopped " + anim.isStopped());
238 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700239 // Try to prevent it from continuing to run
240 animation.cancel();
241 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700242 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700243 final int left = mDragOutlines[thisIndex].x;
244 final int top = mDragOutlines[thisIndex].y;
245 CellLayout.this.invalidate(left, top,
246 left + outline.getWidth(), top + outline.getHeight());
247 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700248 }
249 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700250 // The animation holds a reference to the drag outline bitmap as long is it's
251 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700252 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700253 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700254 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700255 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700256 anim.setTag(null);
257 }
258 }
259 });
260 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700261 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700262
Michael Jurka18014792010-10-14 09:01:34 -0700263 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700264 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800265
Michael Jurka8c920dd2011-01-20 14:16:56 -0800266 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700267 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800268 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700269 }
270
Michael Jurkaf6440da2011-04-05 14:50:34 -0700271 static int widthInPortrait(Resources r, int numCells) {
272 // We use this method from Workspace to figure out how many rows/columns Launcher should
273 // have. We ignore the left/right padding on CellLayout because it turns out in our design
274 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700275 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700276 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
277 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700278
Winson Chung4b825dcd2011-06-19 12:41:22 -0700279 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700280 }
281
Michael Jurkaf6440da2011-04-05 14:50:34 -0700282 static int heightInLandscape(Resources r, int numCells) {
283 // We use this method from Workspace to figure out how many rows/columns Launcher should
284 // have. We ignore the left/right padding on CellLayout because it turns out in our design
285 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700286 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700287 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
288 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700289
Winson Chung4b825dcd2011-06-19 12:41:22 -0700290 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700291 }
292
Adam Cohen2801caf2011-05-13 20:57:39 -0700293 public void enableHardwareLayers() {
Adam Cohen7ef91832011-08-25 14:06:02 -0700294 mChildren.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700295 }
296
297 public void setGridSize(int x, int y) {
298 mCountX = x;
299 mCountY = y;
300 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700301 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700302 }
303
Patrick Dubroy96864c32011-03-10 17:17:23 -0800304 private void invalidateBubbleTextView(BubbleTextView icon) {
305 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700306 invalidate(icon.getLeft() + getPaddingLeft() - padding,
307 icon.getTop() + getPaddingTop() - padding,
308 icon.getRight() + getPaddingLeft() + padding,
309 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800310 }
311
Adam Cohenb5ba0972011-09-07 18:02:31 -0700312 void setOverScrollAmount(float r, boolean left) {
313 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
314 mOverScrollForegroundDrawable = mOverScrollLeft;
315 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
316 mOverScrollForegroundDrawable = mOverScrollRight;
317 }
318
319 mForegroundAlpha = (int) Math.round((r * 255));
320 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
321 invalidate();
322 }
323
Patrick Dubroy96864c32011-03-10 17:17:23 -0800324 void setPressedOrFocusedIcon(BubbleTextView icon) {
325 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
326 // requires an expanded clip rect (due to the glow's blur radius)
327 BubbleTextView oldIcon = mPressedOrFocusedIcon;
328 mPressedOrFocusedIcon = icon;
329 if (oldIcon != null) {
330 invalidateBubbleTextView(oldIcon);
331 }
332 if (mPressedOrFocusedIcon != null) {
333 invalidateBubbleTextView(mPressedOrFocusedIcon);
334 }
335 }
336
Winson Chung6e314082011-01-27 16:46:51 -0800337 public CellLayoutChildren getChildrenLayout() {
338 if (getChildCount() > 0) {
339 return (CellLayoutChildren) getChildAt(0);
340 }
341 return null;
342 }
343
Michael Jurka33945b22010-12-21 18:19:38 -0800344 void setIsDragOverlapping(boolean isDragOverlapping) {
345 if (mIsDragOverlapping != isDragOverlapping) {
346 mIsDragOverlapping = isDragOverlapping;
347 invalidate();
348 }
349 }
350
351 boolean getIsDragOverlapping() {
352 return mIsDragOverlapping;
353 }
354
Adam Cohenebea84d2011-11-09 17:20:41 -0800355 protected void setOverscrollTransformsDirty(boolean dirty) {
356 mScrollingTransformsDirty = dirty;
357 }
358
359 protected void resetOverscrollTransforms() {
360 if (mScrollingTransformsDirty) {
361 setOverscrollTransformsDirty(false);
362 setTranslationX(0);
363 setRotationY(0);
364 // It doesn't matter if we pass true or false here, the important thing is that we
365 // pass 0, which results in the overscroll drawable not being drawn any more.
366 setOverScrollAmount(0, false);
367 setPivotX(getMeasuredWidth() / 2);
368 setPivotY(getMeasuredHeight() / 2);
369 }
370 }
371
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700372 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700373 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700374 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
375 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
376 // When we're small, we are either drawn normally or in the "accepts drops" state (during
377 // a drag). However, we also drag the mini hover background *over* one of those two
378 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700379 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700380 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800381
382 if (mIsDragOverlapping) {
383 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700384 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700385 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700386 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700387 }
Michael Jurka33945b22010-12-21 18:19:38 -0800388
389 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
390 bg.setBounds(mBackgroundRect);
391 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700392 }
Romain Guya6abce82009-11-10 02:54:41 -0800393
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700394 if (mCrosshairsVisibility > 0.0f) {
395 final int countX = mCountX;
396 final int countY = mCountY;
397
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700398 final float MAX_ALPHA = 0.4f;
399 final int MAX_VISIBLE_DISTANCE = 600;
400 final float DISTANCE_MULTIPLIER = 0.002f;
401
402 final Drawable d = mCrosshairsDrawable;
403 final int width = d.getIntrinsicWidth();
404 final int height = d.getIntrinsicHeight();
405
Winson Chung4b825dcd2011-06-19 12:41:22 -0700406 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700407 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700408 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700409 for (int row = 0; row <= countY; row++) {
410 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
411 float dist = mTmpPointF.length();
412 // Crosshairs further from the drag point are more faint
413 float alpha = Math.min(MAX_ALPHA,
414 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
415 if (alpha > 0.0f) {
416 d.setBounds(x, y, x + width, y + height);
417 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
418 d.draw(canvas);
419 }
420 y += mCellHeight + mHeightGap;
421 }
422 x += mCellWidth + mWidthGap;
423 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700424 }
Winson Chung150fbab2010-09-29 17:14:26 -0700425
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700426 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700427 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700428 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700429 if (alpha > 0) {
430 final Point p = mDragOutlines[i];
431 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700432 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700433 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700434 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700435 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800436
437 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
438 // requires an expanded clip rect (due to the glow's blur radius)
439 if (mPressedOrFocusedIcon != null) {
440 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
441 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
442 if (b != null) {
443 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700444 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
445 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800446 null);
447 }
448 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700449
450 // The folder outer / inner ring image(s)
451 for (int i = 0; i < mFolderOuterRings.size(); i++) {
452 FolderRingAnimator fra = mFolderOuterRings.get(i);
453
454 // Draw outer ring
455 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
456 int width = (int) fra.getOuterRingSize();
457 int height = width;
458 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
459
460 int centerX = mTempLocation[0] + mCellWidth / 2;
461 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
462
463 canvas.save();
464 canvas.translate(centerX - width / 2, centerY - height / 2);
465 d.setBounds(0, 0, width, height);
466 d.draw(canvas);
467 canvas.restore();
468
469 // Draw inner ring
470 d = FolderRingAnimator.sSharedInnerRingDrawable;
471 width = (int) fra.getInnerRingSize();
472 height = width;
473 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
474
475 centerX = mTempLocation[0] + mCellWidth / 2;
476 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
477 canvas.save();
478 canvas.translate(centerX - width / 2, centerY - width / 2);
479 d.setBounds(0, 0, width, height);
480 d.draw(canvas);
481 canvas.restore();
482 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700483
484 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
485 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
486 int width = d.getIntrinsicWidth();
487 int height = d.getIntrinsicHeight();
488
489 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
490 int centerX = mTempLocation[0] + mCellWidth / 2;
491 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
492
493 canvas.save();
494 canvas.translate(centerX - width / 2, centerY - width / 2);
495 d.setBounds(0, 0, width, height);
496 d.draw(canvas);
497 canvas.restore();
498 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700499 }
500
Adam Cohenb5ba0972011-09-07 18:02:31 -0700501 @Override
502 protected void dispatchDraw(Canvas canvas) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800503 // Debug drawing for hit space
504 if (false) {
505 final Rect frame = mRect;
506 for (int i = mChildren.getChildCount() - 1; i >= 0; i--) {
507 final View child = mChildren.getChildAt(i);
508 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
509
510 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
511 lp.isLockedToGrid) {
512 child.getHitRect(frame);
513 frame.offset(mPaddingLeft, mPaddingTop);
514
515 Paint p = new Paint();
516 p.setColor(Color.GREEN);
517 canvas.drawRect(frame, p);
518 }
519 }
520 }
521
Adam Cohenb5ba0972011-09-07 18:02:31 -0700522 super.dispatchDraw(canvas);
523 if (mForegroundAlpha > 0) {
524 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
525 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
526 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
527 mOverScrollForegroundDrawable.draw(canvas);
528 p.setXfermode(null);
529 }
530 }
531
Adam Cohen69ce2e52011-07-03 19:25:21 -0700532 public void showFolderAccept(FolderRingAnimator fra) {
533 mFolderOuterRings.add(fra);
534 }
535
536 public void hideFolderAccept(FolderRingAnimator fra) {
537 if (mFolderOuterRings.contains(fra)) {
538 mFolderOuterRings.remove(fra);
539 }
540 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700541 }
542
Adam Cohenc51934b2011-07-26 21:07:43 -0700543 public void setFolderLeaveBehindCell(int x, int y) {
544 mFolderLeaveBehindCell[0] = x;
545 mFolderLeaveBehindCell[1] = y;
546 invalidate();
547 }
548
549 public void clearFolderLeaveBehind() {
550 mFolderLeaveBehindCell[0] = -1;
551 mFolderLeaveBehindCell[1] = -1;
552 invalidate();
553 }
554
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700555 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700556 public boolean shouldDelayChildPressedState() {
557 return false;
558 }
559
560 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700561 public void cancelLongPress() {
562 super.cancelLongPress();
563
564 // Cancel long press for all children
565 final int count = getChildCount();
566 for (int i = 0; i < count; i++) {
567 final View child = getChildAt(i);
568 child.cancelLongPress();
569 }
570 }
571
Michael Jurkadee05892010-07-27 10:01:56 -0700572 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
573 mInterceptTouchListener = listener;
574 }
575
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700577 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 }
579
580 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700581 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 }
583
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800584 public void setIsHotseat(boolean isHotseat) {
585 mIsHotseat = isHotseat;
586 }
587
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700588 public boolean addViewToCellLayout(
589 View child, int index, int childId, LayoutParams params, boolean markCells) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800590 return addViewToCellLayout(child, index, childId, params, markCells, false);
591 }
592
Andrew Flynnbc239a12012-03-06 11:39:49 -0800593 private void scaleChild(BubbleTextView bubbleChild, float pivot, int scalePercent) {
594 // If we haven't measured the child yet, do it now
595 // (this happens if we're being dropped from all-apps
596 if (bubbleChild.getLayoutParams() instanceof LayoutParams &&
597 (bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) {
598 getChildrenLayout().measureChild(bubbleChild);
599 }
600 int measuredWidth = bubbleChild.getMeasuredWidth();
601 int measuredHeight = bubbleChild.getMeasuredHeight();
602
603 float scale = scalePercent / 100f;
604 bubbleChild.setPivotX(pivot);
605 bubbleChild.setPivotY(pivot);
606 bubbleChild.setScaleX(scale);
607 bubbleChild.setScaleY(scale);
608 bubbleChild.setTranslationX(measuredWidth * (1 - scale) / 2);
609 bubbleChild.setTranslationY(measuredHeight * (1 - scale) / 2);
610 }
611
612 private void resetChild(BubbleTextView bubbleChild) {
613 bubbleChild.setScaleX(1f);
614 bubbleChild.setScaleY(1f);
615 bubbleChild.setTranslationX(0f);
616 bubbleChild.setTranslationY(0f);
617
618 bubbleChild.setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
619 }
620
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800621 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
622 boolean markCells, boolean allApps) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700623 final LayoutParams lp = params;
624
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800625 // Hotseat icons - scale down and remove text
626 // Don't scale the all apps button
627 // scale percent set to -1 means do not scale
628 // Only scale BubbleTextViews
629 if (child instanceof BubbleTextView) {
630 BubbleTextView bubbleChild = (BubbleTextView) child;
631
Andrew Flynnbc239a12012-03-06 11:39:49 -0800632 // Start the child with 100% scale and visible text
633 resetChild(bubbleChild);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800634
Andrew Flynnbc239a12012-03-06 11:39:49 -0800635 if (mIsHotseat && !allApps && mBubbleHotseatScalePercent >= 0) {
636 // Scale/make transparent for a hotseat
637 scaleChild(bubbleChild, 0f, mBubbleHotseatScalePercent);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800638
Andrew Flynnbc239a12012-03-06 11:39:49 -0800639 bubbleChild.setTextColor(getResources().getColor(android.R.color.transparent));
640 } else if (mBubbleScalePercent >= 0) {
641 // Else possibly still scale it if we need to for smaller icons
642 scaleChild(bubbleChild, 0f, mBubbleScalePercent);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800643 }
644 }
645
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 // Generate an id for each view, this assumes we have at most 256x256 cells
647 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700648 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700649 // If the horizontal or vertical span is set to -1, it is taken to
650 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700651 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
652 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653
Winson Chungaafa03c2010-06-11 17:34:16 -0700654 child.setId(childId);
655
Michael Jurka8c920dd2011-01-20 14:16:56 -0800656 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700657
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700658 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700659
Winson Chungaafa03c2010-06-11 17:34:16 -0700660 return true;
661 }
662 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 public void removeAllViews() {
667 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800668 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700669 }
670
671 @Override
672 public void removeAllViewsInLayout() {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700673 if (mChildren.getChildCount() > 0) {
674 clearOccupiedCells();
675 mChildren.removeAllViewsInLayout();
676 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700677 }
678
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700679 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800680 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700681 }
682
Michael Jurka0280c3b2010-09-17 15:00:07 -0700683 @Override
684 public void removeView(View view) {
685 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800686 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700687 }
688
689 @Override
690 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800691 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
692 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700693 }
694
695 @Override
696 public void removeViewInLayout(View view) {
697 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800698 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700699 }
700
701 @Override
702 public void removeViews(int start, int count) {
703 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800704 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700705 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800706 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700707 }
708
709 @Override
710 public void removeViewsInLayout(int start, int count) {
711 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800712 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700713 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800714 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700715 }
716
Michael Jurka8c920dd2011-01-20 14:16:56 -0800717 public void drawChildren(Canvas canvas) {
718 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 }
720
Michael Jurkaabded662011-03-04 12:06:57 -0800721 void buildChildrenLayer() {
722 mChildren.buildLayer();
723 }
724
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 @Override
726 protected void onAttachedToWindow() {
727 super.onAttachedToWindow();
728 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
729 }
730
Michael Jurkaaf442092010-06-10 17:01:57 -0700731 public void setTagToCellInfoForPoint(int touchX, int touchY) {
732 final CellInfo cellInfo = mCellInfo;
733 final Rect frame = mRect;
734 final int x = touchX + mScrollX;
735 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800736 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700737
738 boolean found = false;
739 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800740 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800741 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700742
Adam Cohen1b607ed2011-03-03 17:26:50 -0800743 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
744 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700745 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700746
747 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
748 // offset that by this CellLayout's padding to test an (x,y) point that is relative
749 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700750 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700751
Michael Jurkaaf442092010-06-10 17:01:57 -0700752 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700753 cellInfo.cell = child;
754 cellInfo.cellX = lp.cellX;
755 cellInfo.cellY = lp.cellY;
756 cellInfo.spanX = lp.cellHSpan;
757 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700758 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700759 break;
760 }
761 }
762 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700763
Michael Jurkad771c962011-08-09 15:00:48 -0700764 mLastDownOnOccupiedCell = found;
765
Michael Jurkaaf442092010-06-10 17:01:57 -0700766 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700767 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700768 pointToCellExact(x, y, cellXY);
769
Michael Jurkaaf442092010-06-10 17:01:57 -0700770 cellInfo.cell = null;
771 cellInfo.cellX = cellXY[0];
772 cellInfo.cellY = cellXY[1];
773 cellInfo.spanX = 1;
774 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700775 }
776 setTag(cellInfo);
777 }
778
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 @Override
780 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700781 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
782 // even in the case where we return early. Not clearing here was causing bugs whereby on
783 // long-press we'd end up picking up an item from a previous drag operation.
784 final int action = ev.getAction();
785
786 if (action == MotionEvent.ACTION_DOWN) {
787 clearTagCellInfo();
788 }
789
Michael Jurkadee05892010-07-27 10:01:56 -0700790 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
791 return true;
792 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793
794 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700795 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 return false;
798 }
799
Adam Cohenc1997fd2011-08-15 18:26:39 -0700800 private void clearTagCellInfo() {
801 final CellInfo cellInfo = mCellInfo;
802 cellInfo.cell = null;
803 cellInfo.cellX = -1;
804 cellInfo.cellY = -1;
805 cellInfo.spanX = 0;
806 cellInfo.spanY = 0;
807 setTag(cellInfo);
808 }
809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700811 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
813
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700814 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700815 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 * @param x X coordinate of the point
817 * @param y Y coordinate of the point
818 * @param result Array of 2 ints to hold the x and y coordinate of the cell
819 */
820 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700821 final int hStartPadding = getPaddingLeft();
822 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823
824 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
825 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
826
Adam Cohend22015c2010-07-26 22:02:18 -0700827 final int xAxis = mCountX;
828 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829
830 if (result[0] < 0) result[0] = 0;
831 if (result[0] >= xAxis) result[0] = xAxis - 1;
832 if (result[1] < 0) result[1] = 0;
833 if (result[1] >= yAxis) result[1] = yAxis - 1;
834 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700835
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 /**
837 * Given a point, return the cell that most closely encloses that point
838 * @param x X coordinate of the point
839 * @param y Y coordinate of the point
840 * @param result Array of 2 ints to hold the x and y coordinate of the cell
841 */
842 void pointToCellRounded(int x, int y, int[] result) {
843 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
844 }
845
846 /**
847 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700848 *
849 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700851 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 * @param result Array of 2 ints to hold the x and y coordinate of the point
853 */
854 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700855 final int hStartPadding = getPaddingLeft();
856 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857
858 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
859 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
860 }
861
Adam Cohene3e27a82011-04-15 12:07:39 -0700862 /**
863 * Given a cell coordinate, return the point that represents the upper left corner of that cell
864 *
865 * @param cellX X coordinate of the cell
866 * @param cellY Y coordinate of the cell
867 *
868 * @param result Array of 2 ints to hold the x and y coordinate of the point
869 */
870 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700871 final int hStartPadding = getPaddingLeft();
872 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700873
874 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
875 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
876 }
877
Romain Guy84f296c2009-11-04 15:00:44 -0800878 int getCellWidth() {
879 return mCellWidth;
880 }
881
882 int getCellHeight() {
883 return mCellHeight;
884 }
885
Adam Cohend4844c32011-02-18 19:25:06 -0800886 int getWidthGap() {
887 return mWidthGap;
888 }
889
890 int getHeightGap() {
891 return mHeightGap;
892 }
893
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700894 Rect getContentRect(Rect r) {
895 if (r == null) {
896 r = new Rect();
897 }
898 int left = getPaddingLeft();
899 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700900 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
901 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700902 r.set(left, top, right, bottom);
903 return r;
904 }
905
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 @Override
907 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
908 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700909
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700911 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
912
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
914 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700915
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
917 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
918 }
919
Adam Cohend22015c2010-07-26 22:02:18 -0700920 int numWidthGaps = mCountX - 1;
921 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922
Adam Cohen234c4cd2011-07-17 21:03:04 -0700923 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700924 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
925 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
926 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
927 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
928 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
929 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700930 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700931 } else {
932 mWidthGap = mOriginalWidthGap;
933 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700934 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700935
Michael Jurka8c920dd2011-01-20 14:16:56 -0800936 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
937 int newWidth = widthSpecSize;
938 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700939 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700940 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700941 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700942 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700943 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700944 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700945 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800946
947 int count = getChildCount();
948 for (int i = 0; i < count; i++) {
949 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700950 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
951 mPaddingRight, MeasureSpec.EXACTLY);
952 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
953 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800954 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
955 }
956 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 }
958
959 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700960 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800963 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700964 child.layout(mPaddingLeft, mPaddingTop,
965 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 }
967 }
968
969 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700970 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
971 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700972 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700973 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
974 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -0700975 }
976
977 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800979 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 }
981
982 @Override
983 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800984 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 }
986
Michael Jurka5f1c5092010-09-03 14:15:02 -0700987 public float getBackgroundAlpha() {
988 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700989 }
990
Michael Jurka742574b2011-02-02 23:51:01 -0800991 public void setFastBackgroundAlpha(float alpha) {
992 mBackgroundAlpha = alpha;
993 }
994
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700995 public void setBackgroundAlphaMultiplier(float multiplier) {
996 mBackgroundAlphaMultiplier = multiplier;
997 }
998
Adam Cohenddb82192010-11-10 16:32:54 -0800999 public float getBackgroundAlphaMultiplier() {
1000 return mBackgroundAlphaMultiplier;
1001 }
1002
Michael Jurka5f1c5092010-09-03 14:15:02 -07001003 public void setBackgroundAlpha(float alpha) {
1004 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -07001005 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -07001006 }
1007
Michael Jurka5f1c5092010-09-03 14:15:02 -07001008 // Need to return true to let the view system know we know how to handle alpha-- this is
1009 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
1010 // versions
1011 @Override
1012 protected boolean onSetAlpha(int alpha) {
1013 return true;
1014 }
1015
1016 public void setAlpha(float alpha) {
1017 setChildrenAlpha(alpha);
1018 super.setAlpha(alpha);
1019 }
1020
Michael Jurka742574b2011-02-02 23:51:01 -08001021 public void setFastAlpha(float alpha) {
1022 setFastChildrenAlpha(alpha);
1023 super.setFastAlpha(alpha);
1024 }
1025
Michael Jurkadee05892010-07-27 10:01:56 -07001026 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001027 final int childCount = getChildCount();
1028 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001029 getChildAt(i).setAlpha(alpha);
1030 }
1031 }
1032
Michael Jurka742574b2011-02-02 23:51:01 -08001033 private void setFastChildrenAlpha(float alpha) {
1034 final int childCount = getChildCount();
1035 for (int i = 0; i < childCount; i++) {
1036 getChildAt(i).setFastAlpha(alpha);
1037 }
1038 }
1039
Patrick Dubroy440c3602010-07-13 17:50:32 -07001040 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001041 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001042 }
1043
Adam Cohen76fc0852011-06-17 13:26:23 -07001044 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
1045 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001046 CellLayoutChildren clc = getChildrenLayout();
1047 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
1048 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1049 final ItemInfo info = (ItemInfo) child.getTag();
1050
1051 // We cancel any existing animations
1052 if (mReorderAnimators.containsKey(lp)) {
1053 mReorderAnimators.get(lp).cancel();
1054 mReorderAnimators.remove(lp);
1055 }
1056
1057 int oldX = lp.x;
1058 int oldY = lp.y;
1059 mOccupied[lp.cellX][lp.cellY] = false;
1060 mOccupied[cellX][cellY] = true;
1061
1062 lp.isLockedToGrid = true;
1063 lp.cellX = info.cellX = cellX;
1064 lp.cellY = info.cellY = cellY;
1065 clc.setupLp(lp);
1066 lp.isLockedToGrid = false;
1067 int newX = lp.x;
1068 int newY = lp.y;
1069
Adam Cohen76fc0852011-06-17 13:26:23 -07001070 lp.x = oldX;
1071 lp.y = oldY;
1072 child.requestLayout();
1073
Adam Cohenbfbfd262011-06-13 16:55:12 -07001074 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
1075 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
1076 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
1077 oa.setDuration(duration);
1078 mReorderAnimators.put(lp, oa);
1079 oa.addUpdateListener(new AnimatorUpdateListener() {
1080 public void onAnimationUpdate(ValueAnimator animation) {
1081 child.requestLayout();
1082 }
1083 });
1084 oa.addListener(new AnimatorListenerAdapter() {
1085 boolean cancelled = false;
1086 public void onAnimationEnd(Animator animation) {
1087 // If the animation was cancelled, it means that another animation
1088 // has interrupted this one, and we don't want to lock the item into
1089 // place just yet.
1090 if (!cancelled) {
1091 lp.isLockedToGrid = true;
1092 }
1093 if (mReorderAnimators.containsKey(lp)) {
1094 mReorderAnimators.remove(lp);
1095 }
1096 }
1097 public void onAnimationCancel(Animator animation) {
1098 cancelled = true;
1099 }
1100 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001101 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001102 oa.start();
1103 return true;
1104 }
1105 return false;
1106 }
1107
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001108 /**
1109 * Estimate where the top left cell of the dragged item will land if it is dropped.
1110 *
1111 * @param originX The X value of the top left corner of the item
1112 * @param originY The Y value of the top left corner of the item
1113 * @param spanX The number of horizontal cells that the item spans
1114 * @param spanY The number of vertical cells that the item spans
1115 * @param result The estimated drop cell X and Y.
1116 */
1117 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001118 final int countX = mCountX;
1119 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001120
Michael Jurkaa63c4522010-08-19 13:52:27 -07001121 // pointToCellRounded takes the top left of a cell but will pad that with
1122 // cellWidth/2 and cellHeight/2 when finding the matching cell
1123 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001124
1125 // If the item isn't fully on this screen, snap to the edges
1126 int rightOverhang = result[0] + spanX - countX;
1127 if (rightOverhang > 0) {
1128 result[0] -= rightOverhang; // Snap to right
1129 }
1130 result[0] = Math.max(0, result[0]); // Snap to left
1131 int bottomOverhang = result[1] + spanY - countY;
1132 if (bottomOverhang > 0) {
1133 result[1] -= bottomOverhang; // Snap to bottom
1134 }
1135 result[1] = Math.max(0, result[1]); // Snap to top
1136 }
1137
Winson Chungb8c69f32011-10-19 21:36:08 -07001138 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY,
1139 int spanX, int spanY, Point dragOffset, Rect dragRegion) {
Joe Onorato4be866d2010-10-10 11:26:02 -07001140
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001141 final int oldDragCellX = mDragCell[0];
1142 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001143 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chungb8c69f32011-10-19 21:36:08 -07001144 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001145 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1146 } else {
1147 mDragCenter.set(originX, originY);
1148 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001149
Adam Cohen2801caf2011-05-13 20:57:39 -07001150 if (dragOutline == null && v == null) {
1151 if (mCrosshairsDrawable != null) {
1152 invalidate();
1153 }
1154 return;
1155 }
1156
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001157 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001158 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001159 final int[] topLeft = mTmpPoint;
1160 cellToPoint(nearest[0], nearest[1], topLeft);
1161
Joe Onorato4be866d2010-10-10 11:26:02 -07001162 int left = topLeft[0];
1163 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001164
Winson Chungb8c69f32011-10-19 21:36:08 -07001165 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001166 // When drawing the drag outline, it did not account for margin offsets
1167 // added by the view's parent.
1168 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1169 left += lp.leftMargin;
1170 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001171
Adam Cohen99e8b402011-03-25 19:23:43 -07001172 // Offsets due to the size difference between the View and the dragOutline.
1173 // There is a size difference to account for the outer blur, which may lie
1174 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001175 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001176 // We center about the x axis
1177 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1178 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001179 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001180 if (dragOffset != null && dragRegion != null) {
1181 // Center the drag region *horizontally* in the cell and apply a drag
1182 // outline offset
1183 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1184 - dragRegion.width()) / 2;
1185 top += dragOffset.y;
1186 } else {
1187 // Center the drag outline in the cell
1188 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1189 - dragOutline.getWidth()) / 2;
1190 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1191 - dragOutline.getHeight()) / 2;
1192 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001193 }
Winson Chung150fbab2010-09-29 17:14:26 -07001194
Joe Onorato4be866d2010-10-10 11:26:02 -07001195 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001196 mDragOutlineAnims[oldIndex].animateOut();
1197 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001198
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001199 mDragOutlines[mDragOutlineCurrent].set(left, top);
1200 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1201 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001202 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001203
1204 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1205 if (mCrosshairsDrawable != null) {
1206 invalidate();
1207 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001208 }
1209
Adam Cohene0310962011-04-18 16:15:31 -07001210 public void clearDragOutlines() {
1211 final int oldIndex = mDragOutlineCurrent;
1212 mDragOutlineAnims[oldIndex].animateOut();
1213 mDragCell[0] = -1;
1214 mDragCell[1] = -1;
1215 }
1216
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001218 * Find a vacant area that will fit the given bounds nearest the requested
1219 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001220 *
Romain Guy51afc022009-05-04 18:03:43 -07001221 * @param pixelX The X location at which you want to search for a vacant area.
1222 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001223 * @param spanX Horizontal span of the object.
1224 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001225 * @param result Array in which to place the result, or null (in which case a new array will
1226 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001227 * @return The X, Y cell of a vacant area that can contain this object,
1228 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001230 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001231 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1232 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001233 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001234
Michael Jurka6a1435d2010-09-27 17:35:12 -07001235 /**
1236 * Find a vacant area that will fit the given bounds nearest the requested
1237 * cell location. Uses Euclidean distance to score multiple vacant areas.
1238 *
1239 * @param pixelX The X location at which you want to search for a vacant area.
1240 * @param pixelY The Y location at which you want to search for a vacant area.
1241 * @param spanX Horizontal span of the object.
1242 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001243 * @param ignoreOccupied If true, the result can be an occupied cell
1244 * @param result Array in which to place the result, or null (in which case a new array will
1245 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001246 * @return The X, Y cell of a vacant area that can contain this object,
1247 * nearest the requested location.
1248 */
Adam Cohendf035382011-04-11 17:22:04 -07001249 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1250 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001251 // mark space take by ignoreView as available (method checks if ignoreView is null)
1252 markCellsAsUnoccupiedForView(ignoreView);
1253
Adam Cohene3e27a82011-04-15 12:07:39 -07001254 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1255 // to the center of the item, but we are searching based on the top-left cell, so
1256 // we translate the point over to correspond to the top-left.
1257 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1258 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1259
Jeff Sharkey70864282009-04-07 21:08:40 -07001260 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001261 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001262 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001263
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001264 final int countX = mCountX;
1265 final int countY = mCountY;
1266 final boolean[][] occupied = mOccupied;
1267
Winson Chungbbc60d82010-11-11 16:34:41 -08001268 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001269 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001270 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001271 if (ignoreOccupied) {
1272 for (int i = 0; i < spanX; i++) {
1273 for (int j = 0; j < spanY; j++) {
1274 if (occupied[x + i][y + j]) {
1275 // small optimization: we can skip to after the column we
1276 // just found an occupied cell
1277 x += i;
1278 continue inner;
1279 }
Michael Jurkac28de512010-08-13 11:27:44 -07001280 }
1281 }
1282 }
Winson Chung0be025d2011-05-23 17:45:09 -07001283 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001284 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285
Michael Jurkac28de512010-08-13 11:27:44 -07001286 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1287 + Math.pow(cellXY[1] - pixelY, 2));
1288 if (distance <= bestDistance) {
1289 bestDistance = distance;
1290 bestXY[0] = x;
1291 bestXY[1] = y;
1292 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 }
1294 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001295 // re-mark space taken by ignoreView as occupied
1296 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297
Adam Cohenc0dcf592011-06-01 15:30:43 -07001298 // Return -1, -1 if no suitable location found
1299 if (bestDistance == Double.MAX_VALUE) {
1300 bestXY[0] = -1;
1301 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001302 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001303 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001305
Adam Cohendf035382011-04-11 17:22:04 -07001306 /**
1307 * Find a vacant area that will fit the given bounds nearest the requested
1308 * cell location. Uses Euclidean distance to score multiple vacant areas.
1309 *
1310 * @param pixelX The X location at which you want to search for a vacant area.
1311 * @param pixelY The Y location at which you want to search for a vacant area.
1312 * @param spanX Horizontal span of the object.
1313 * @param spanY Vertical span of the object.
1314 * @param ignoreView Considers space occupied by this view as unoccupied
1315 * @param result Previously returned value to possibly recycle.
1316 * @return The X, Y cell of a vacant area that can contain this object,
1317 * nearest the requested location.
1318 */
1319 int[] findNearestVacantArea(
1320 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1321 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1322 }
1323
1324 /**
1325 * Find a starting cell position that will fit the given bounds nearest the requested
1326 * cell location. Uses Euclidean distance to score multiple vacant areas.
1327 *
1328 * @param pixelX The X location at which you want to search for a vacant area.
1329 * @param pixelY The Y location at which you want to search for a vacant area.
1330 * @param spanX Horizontal span of the object.
1331 * @param spanY Vertical span of the object.
1332 * @param ignoreView Considers space occupied by this view as unoccupied
1333 * @param result Previously returned value to possibly recycle.
1334 * @return The X, Y cell of a vacant area that can contain this object,
1335 * nearest the requested location.
1336 */
1337 int[] findNearestArea(
1338 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1339 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1340 }
1341
Michael Jurka0280c3b2010-09-17 15:00:07 -07001342 boolean existsEmptyCell() {
1343 return findCellForSpan(null, 1, 1);
1344 }
1345
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001347 * Finds the upper-left coordinate of the first rectangle in the grid that can
1348 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1349 * then this method will only return coordinates for rectangles that contain the cell
1350 * (intersectX, intersectY)
1351 *
1352 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1353 * can be found.
1354 * @param spanX The horizontal span of the cell we want to find.
1355 * @param spanY The vertical span of the cell we want to find.
1356 *
1357 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001358 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001359 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1360 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1361 }
1362
1363 /**
1364 * Like above, but ignores any cells occupied by the item "ignoreView"
1365 *
1366 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1367 * can be found.
1368 * @param spanX The horizontal span of the cell we want to find.
1369 * @param spanY The vertical span of the cell we want to find.
1370 * @param ignoreView The home screen item we should treat as not occupying any space
1371 * @return
1372 */
1373 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1374 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1375 }
1376
1377 /**
1378 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1379 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1380 *
1381 * @param spanX The horizontal span of the cell we want to find.
1382 * @param spanY The vertical span of the cell we want to find.
1383 * @param ignoreView The home screen item we should treat as not occupying any space
1384 * @param intersectX The X coordinate of the cell that we should try to overlap
1385 * @param intersectX The Y coordinate of the cell that we should try to overlap
1386 *
1387 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1388 */
1389 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1390 int intersectX, int intersectY) {
1391 return findCellForSpanThatIntersectsIgnoring(
1392 cellXY, spanX, spanY, intersectX, intersectY, null);
1393 }
1394
1395 /**
1396 * The superset of the above two methods
1397 */
1398 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1399 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001400 // mark space take by ignoreView as available (method checks if ignoreView is null)
1401 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001402
Michael Jurka28750fb2010-09-24 17:43:49 -07001403 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001404 while (true) {
1405 int startX = 0;
1406 if (intersectX >= 0) {
1407 startX = Math.max(startX, intersectX - (spanX - 1));
1408 }
1409 int endX = mCountX - (spanX - 1);
1410 if (intersectX >= 0) {
1411 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1412 }
1413 int startY = 0;
1414 if (intersectY >= 0) {
1415 startY = Math.max(startY, intersectY - (spanY - 1));
1416 }
1417 int endY = mCountY - (spanY - 1);
1418 if (intersectY >= 0) {
1419 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1420 }
1421
Winson Chungbbc60d82010-11-11 16:34:41 -08001422 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001423 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001424 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001425 for (int i = 0; i < spanX; i++) {
1426 for (int j = 0; j < spanY; j++) {
1427 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001428 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001429 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001430 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001431 continue inner;
1432 }
1433 }
1434 }
1435 if (cellXY != null) {
1436 cellXY[0] = x;
1437 cellXY[1] = y;
1438 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001439 foundCell = true;
1440 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001441 }
1442 }
1443 if (intersectX == -1 && intersectY == -1) {
1444 break;
1445 } else {
1446 // if we failed to find anything, try again but without any requirements of
1447 // intersecting
1448 intersectX = -1;
1449 intersectY = -1;
1450 continue;
1451 }
1452 }
1453
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001454 // re-mark space taken by ignoreView as occupied
1455 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001456 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001457 }
1458
1459 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001460 * A drag event has begun over this layout.
1461 * It may have begun over this layout (in which case onDragChild is called first),
1462 * or it may have begun on another layout.
1463 */
1464 void onDragEnter() {
1465 if (!mDragging) {
1466 // Fade in the drag indicators
1467 if (mCrosshairsAnimator != null) {
1468 mCrosshairsAnimator.animateIn();
1469 }
1470 }
1471 mDragging = true;
1472 }
1473
1474 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001475 * Called when drag has left this CellLayout or has been completed (successfully or not)
1476 */
1477 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001478 // This can actually be called when we aren't in a drag, e.g. when adding a new
1479 // item to this layout via the customize drawer.
1480 // Guard against that case.
1481 if (mDragging) {
1482 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001483
Joe Onorato4be866d2010-10-10 11:26:02 -07001484 // Fade out the drag indicators
1485 if (mCrosshairsAnimator != null) {
1486 mCrosshairsAnimator.animateOut();
1487 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001488 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001489
1490 // Invalidate the drag data
1491 mDragCell[0] = -1;
1492 mDragCell[1] = -1;
1493 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1494 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1495
Michael Jurka33945b22010-12-21 18:19:38 -08001496 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001497 }
1498
1499 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001500 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001501 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001502 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 *
1504 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001506 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001507 if (child != null) {
1508 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001509 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001510 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001511 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 }
1513
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001516 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 * @param cellX X coordinate of upper left corner expressed as a cell position
1518 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001519 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001521 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001523 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 final int cellWidth = mCellWidth;
1525 final int cellHeight = mCellHeight;
1526 final int widthGap = mWidthGap;
1527 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001528
Winson Chung4b825dcd2011-06-19 12:41:22 -07001529 final int hStartPadding = getPaddingLeft();
1530 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001531
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1533 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1534
1535 int x = hStartPadding + cellX * (cellWidth + widthGap);
1536 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001537
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001538 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001540
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001542 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001544 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 * @param width Width in pixels
1546 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001547 * @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 -08001548 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001549 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001550 return rectToCell(getResources(), width, height, result);
1551 }
1552
1553 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 // Always assume we're working with the smallest span to make sure we
1555 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001556 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1557 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07001561 int spanX = (int) Math.ceil(width / (float) smallerSize);
1562 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04001563
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001564 if (result == null) {
1565 return new int[] { spanX, spanY };
1566 }
1567 result[0] = spanX;
1568 result[1] = spanY;
1569 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
1571
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001572 public int[] cellSpansToSize(int hSpans, int vSpans) {
1573 int[] size = new int[2];
1574 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1575 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1576 return size;
1577 }
1578
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001580 * Calculate the grid spans needed to fit given item
1581 */
1582 public void calculateSpans(ItemInfo info) {
1583 final int minWidth;
1584 final int minHeight;
1585
1586 if (info instanceof LauncherAppWidgetInfo) {
1587 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1588 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1589 } else if (info instanceof PendingAddWidgetInfo) {
1590 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1591 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1592 } else {
1593 // It's not a widget, so it must be 1x1
1594 info.spanX = info.spanY = 1;
1595 return;
1596 }
1597 int[] spans = rectToCell(minWidth, minHeight, null);
1598 info.spanX = spans[0];
1599 info.spanY = spans[1];
1600 }
1601
1602 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 * Find the first vacant cell, if there is one.
1604 *
1605 * @param vacant Holds the x and y coordinate of the vacant cell
1606 * @param spanX Horizontal cell span.
1607 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001608 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 * @return True if a vacant cell was found
1610 */
1611 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612
Michael Jurka0280c3b2010-09-17 15:00:07 -07001613 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 }
1615
1616 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1617 int xCount, int yCount, boolean[][] occupied) {
1618
Adam Cohen2801caf2011-05-13 20:57:39 -07001619 for (int y = 0; y < yCount; y++) {
1620 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 boolean available = !occupied[x][y];
1622out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1623 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1624 available = available && !occupied[i][j];
1625 if (!available) break out;
1626 }
1627 }
1628
1629 if (available) {
1630 vacant[0] = x;
1631 vacant[1] = y;
1632 return true;
1633 }
1634 }
1635 }
1636
1637 return false;
1638 }
1639
Michael Jurka0280c3b2010-09-17 15:00:07 -07001640 private void clearOccupiedCells() {
1641 for (int x = 0; x < mCountX; x++) {
1642 for (int y = 0; y < mCountY; y++) {
1643 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 }
1645 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001646 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647
Adam Cohen1b607ed2011-03-03 17:26:50 -08001648 /**
1649 * Given a view, determines how much that view can be expanded in all directions, in terms of
1650 * whether or not there are other items occupying adjacent cells. Used by the
1651 * AppWidgetResizeFrame to determine how the widget can be resized.
1652 */
Adam Cohend4844c32011-02-18 19:25:06 -08001653 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001654 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001655 boolean flag;
1656
Adam Cohen1b607ed2011-03-03 17:26:50 -08001657 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001658 for (int x = lp.cellX - 1; x >= 0; x--) {
1659 flag = false;
1660 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1661 if (mOccupied[x][y]) flag = true;
1662 }
1663 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001664 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001665 }
1666
Adam Cohen1b607ed2011-03-03 17:26:50 -08001667 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001668 for (int y = lp.cellY - 1; y >= 0; y--) {
1669 flag = false;
1670 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1671 if (mOccupied[x][y]) flag = true;
1672 }
1673 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001674 expandability[AppWidgetResizeFrame.TOP]++;
1675 }
Adam Cohend4844c32011-02-18 19:25:06 -08001676
Adam Cohen1b607ed2011-03-03 17:26:50 -08001677 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001678 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1679 flag = false;
1680 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1681 if (mOccupied[x][y]) flag = true;
1682 }
1683 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001684 expandability[AppWidgetResizeFrame.RIGHT]++;
1685 }
Adam Cohend4844c32011-02-18 19:25:06 -08001686
Adam Cohen1b607ed2011-03-03 17:26:50 -08001687 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001688 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1689 flag = false;
1690 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1691 if (mOccupied[x][y]) flag = true;
1692 }
1693 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001694 expandability[AppWidgetResizeFrame.BOTTOM]++;
1695 }
Adam Cohend4844c32011-02-18 19:25:06 -08001696 }
1697
Michael Jurka0280c3b2010-09-17 15:00:07 -07001698 public void onMove(View view, int newCellX, int newCellY) {
1699 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1700 markCellsAsUnoccupiedForView(view);
1701 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1702 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703
Adam Cohend4844c32011-02-18 19:25:06 -08001704 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001705 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001706 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1707 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1708 }
1709
Adam Cohend4844c32011-02-18 19:25:06 -08001710 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001711 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001712 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1713 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1714 }
1715
1716 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1717 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1718 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1719 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 }
1721 }
1722 }
1723
Adam Cohen2801caf2011-05-13 20:57:39 -07001724 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001725 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001726 (Math.max((mCountX - 1), 0) * mWidthGap);
1727 }
1728
1729 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001730 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001731 (Math.max((mCountY - 1), 0) * mHeightGap);
1732 }
1733
Michael Jurka66d72172011-04-12 16:29:25 -07001734 public boolean isOccupied(int x, int y) {
1735 if (x < mCountX && y < mCountY) {
1736 return mOccupied[x][y];
1737 } else {
1738 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1739 }
1740 }
1741
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 @Override
1743 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1744 return new CellLayout.LayoutParams(getContext(), attrs);
1745 }
1746
1747 @Override
1748 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1749 return p instanceof CellLayout.LayoutParams;
1750 }
1751
1752 @Override
1753 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1754 return new CellLayout.LayoutParams(p);
1755 }
1756
Winson Chungaafa03c2010-06-11 17:34:16 -07001757 public static class CellLayoutAnimationController extends LayoutAnimationController {
1758 public CellLayoutAnimationController(Animation animation, float delay) {
1759 super(animation, delay);
1760 }
1761
1762 @Override
1763 protected long getDelayForView(View view) {
1764 return (int) (Math.random() * 150);
1765 }
1766 }
1767
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1769 /**
1770 * Horizontal location of the item in the grid.
1771 */
1772 @ViewDebug.ExportedProperty
1773 public int cellX;
1774
1775 /**
1776 * Vertical location of the item in the grid.
1777 */
1778 @ViewDebug.ExportedProperty
1779 public int cellY;
1780
1781 /**
1782 * Number of cells spanned horizontally by the item.
1783 */
1784 @ViewDebug.ExportedProperty
1785 public int cellHSpan;
1786
1787 /**
1788 * Number of cells spanned vertically by the item.
1789 */
1790 @ViewDebug.ExportedProperty
1791 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001792
Adam Cohen1b607ed2011-03-03 17:26:50 -08001793 /**
1794 * Indicates whether the item will set its x, y, width and height parameters freely,
1795 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1796 */
Adam Cohend4844c32011-02-18 19:25:06 -08001797 public boolean isLockedToGrid = true;
1798
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 // X coordinate of the view in the layout.
1800 @ViewDebug.ExportedProperty
1801 int x;
1802 // Y coordinate of the view in the layout.
1803 @ViewDebug.ExportedProperty
1804 int y;
1805
Romain Guy84f296c2009-11-04 15:00:44 -08001806 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001807
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 public LayoutParams(Context c, AttributeSet attrs) {
1809 super(c, attrs);
1810 cellHSpan = 1;
1811 cellVSpan = 1;
1812 }
1813
1814 public LayoutParams(ViewGroup.LayoutParams source) {
1815 super(source);
1816 cellHSpan = 1;
1817 cellVSpan = 1;
1818 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001819
1820 public LayoutParams(LayoutParams source) {
1821 super(source);
1822 this.cellX = source.cellX;
1823 this.cellY = source.cellY;
1824 this.cellHSpan = source.cellHSpan;
1825 this.cellVSpan = source.cellVSpan;
1826 }
1827
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001829 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 this.cellX = cellX;
1831 this.cellY = cellY;
1832 this.cellHSpan = cellHSpan;
1833 this.cellVSpan = cellVSpan;
1834 }
1835
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001836 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001837 if (isLockedToGrid) {
1838 final int myCellHSpan = cellHSpan;
1839 final int myCellVSpan = cellVSpan;
1840 final int myCellX = cellX;
1841 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001842
Adam Cohend4844c32011-02-18 19:25:06 -08001843 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1844 leftMargin - rightMargin;
1845 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1846 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001847 x = myCellX * (cellWidth + widthGap) + leftMargin;
1848 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001849 }
1850 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001851
Winson Chungaafa03c2010-06-11 17:34:16 -07001852 public String toString() {
1853 return "(" + this.cellX + ", " + this.cellY + ")";
1854 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001855
1856 public void setWidth(int width) {
1857 this.width = width;
1858 }
1859
1860 public int getWidth() {
1861 return width;
1862 }
1863
1864 public void setHeight(int height) {
1865 this.height = height;
1866 }
1867
1868 public int getHeight() {
1869 return height;
1870 }
1871
1872 public void setX(int x) {
1873 this.x = x;
1874 }
1875
1876 public int getX() {
1877 return x;
1878 }
1879
1880 public void setY(int y) {
1881 this.y = y;
1882 }
1883
1884 public int getY() {
1885 return y;
1886 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 }
1888
Michael Jurka0280c3b2010-09-17 15:00:07 -07001889 // This class stores info for two purposes:
1890 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1891 // its spanX, spanY, and the screen it is on
1892 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1893 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1894 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001895 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001897 int cellX = -1;
1898 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 int spanX;
1900 int spanY;
1901 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001902 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 @Override
1905 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001906 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1907 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
1909 }
Michael Jurkad771c962011-08-09 15:00:48 -07001910
1911 public boolean lastDownOnOccupiedCell() {
1912 return mLastDownOnOccupiedCell;
1913 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914}