blob: a6c2c5f75bf2ba254bb392d9c6bea07d271171ce [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;
141
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 public CellLayout(Context context) {
143 this(context, null);
144 }
145
146 public CellLayout(Context context, AttributeSet attrs) {
147 this(context, attrs, 0);
148 }
149
150 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
151 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700152
153 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
154 // the user where a dragged item will land when dropped.
155 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
158
Winson Chung4b825dcd2011-06-19 12:41:22 -0700159 mOriginalCellWidth =
160 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
161 mOriginalCellHeight =
162 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700163 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
164 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700165 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700166 mCountX = LauncherModel.getCellCountX();
167 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700168 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
170 a.recycle();
171
172 setAlwaysDrawnWithCacheEnabled(false);
173
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700174 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700175
Winson Chung967289b2011-06-30 18:09:30 -0700176 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700177 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800178
Adam Cohenb5ba0972011-09-07 18:02:31 -0700179 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
180 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
181 mForegroundPadding =
182 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800183
Winson Chungb26f3d62011-06-02 10:49:29 -0700184 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700185 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700186
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800187 mBubbleScalePercent = res.getInteger(R.integer.app_icon_hotseat_scale_percent);
188
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700189 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700190
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700191 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700192 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700193
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700194 // Set up the animation for fading the crosshairs in and out
195 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700196 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700197 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700198 public void onAnimationUpdate(ValueAnimator animation) {
199 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700200 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700201 }
202 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700203 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700204
Winson Chungb8c69f32011-10-19 21:36:08 -0700205 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700206 for (int i = 0; i < mDragOutlines.length; i++) {
207 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700208 }
209
210 // When dragging things around the home screens, we show a green outline of
211 // where the item will land. The outlines gradually fade out, leaving a trail
212 // behind the drag path.
213 // Set up all the animations that are used to implement this fading.
214 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700215 final float fromAlphaValue = 0;
216 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700217
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700218 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700219
220 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 final InterruptibleInOutAnimator anim =
222 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700223 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700224 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700225 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700226 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700227 final Bitmap outline = (Bitmap)anim.getTag();
228
229 // If an animation is started and then stopped very quickly, we can still
230 // get spurious updates we've cleared the tag. Guard against this.
231 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700232 if (false) {
233 Object val = animation.getAnimatedValue();
234 Log.d(TAG, "anim " + thisIndex + " update: " + val +
235 ", isStopped " + anim.isStopped());
236 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700237 // Try to prevent it from continuing to run
238 animation.cancel();
239 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700240 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700241 final int left = mDragOutlines[thisIndex].x;
242 final int top = mDragOutlines[thisIndex].y;
243 CellLayout.this.invalidate(left, top,
244 left + outline.getWidth(), top + outline.getHeight());
245 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700246 }
247 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700248 // The animation holds a reference to the drag outline bitmap as long is it's
249 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700250 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700251 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700252 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700253 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700254 anim.setTag(null);
255 }
256 }
257 });
258 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700259 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700260
Michael Jurka18014792010-10-14 09:01:34 -0700261 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700262 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800263
Michael Jurka8c920dd2011-01-20 14:16:56 -0800264 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700265 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800266 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700267 }
268
Michael Jurkaf6440da2011-04-05 14:50:34 -0700269 static int widthInPortrait(Resources r, int numCells) {
270 // We use this method from Workspace to figure out how many rows/columns Launcher should
271 // have. We ignore the left/right padding on CellLayout because it turns out in our design
272 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700273 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700274 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
275 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700276
Winson Chung4b825dcd2011-06-19 12:41:22 -0700277 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700278 }
279
Michael Jurkaf6440da2011-04-05 14:50:34 -0700280 static int heightInLandscape(Resources r, int numCells) {
281 // We use this method from Workspace to figure out how many rows/columns Launcher should
282 // have. We ignore the left/right padding on CellLayout because it turns out in our design
283 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700284 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700285 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
286 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700287
Winson Chung4b825dcd2011-06-19 12:41:22 -0700288 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700289 }
290
Adam Cohen2801caf2011-05-13 20:57:39 -0700291 public void enableHardwareLayers() {
Adam Cohen7ef91832011-08-25 14:06:02 -0700292 mChildren.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700293 }
294
295 public void setGridSize(int x, int y) {
296 mCountX = x;
297 mCountY = y;
298 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700299 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700300 }
301
Patrick Dubroy96864c32011-03-10 17:17:23 -0800302 private void invalidateBubbleTextView(BubbleTextView icon) {
303 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700304 invalidate(icon.getLeft() + getPaddingLeft() - padding,
305 icon.getTop() + getPaddingTop() - padding,
306 icon.getRight() + getPaddingLeft() + padding,
307 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800308 }
309
Adam Cohenb5ba0972011-09-07 18:02:31 -0700310 void setOverScrollAmount(float r, boolean left) {
311 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
312 mOverScrollForegroundDrawable = mOverScrollLeft;
313 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
314 mOverScrollForegroundDrawable = mOverScrollRight;
315 }
316
317 mForegroundAlpha = (int) Math.round((r * 255));
318 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
319 invalidate();
320 }
321
Patrick Dubroy96864c32011-03-10 17:17:23 -0800322 void setPressedOrFocusedIcon(BubbleTextView icon) {
323 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
324 // requires an expanded clip rect (due to the glow's blur radius)
325 BubbleTextView oldIcon = mPressedOrFocusedIcon;
326 mPressedOrFocusedIcon = icon;
327 if (oldIcon != null) {
328 invalidateBubbleTextView(oldIcon);
329 }
330 if (mPressedOrFocusedIcon != null) {
331 invalidateBubbleTextView(mPressedOrFocusedIcon);
332 }
333 }
334
Winson Chung6e314082011-01-27 16:46:51 -0800335 public CellLayoutChildren getChildrenLayout() {
336 if (getChildCount() > 0) {
337 return (CellLayoutChildren) getChildAt(0);
338 }
339 return null;
340 }
341
Michael Jurka33945b22010-12-21 18:19:38 -0800342 void setIsDragOverlapping(boolean isDragOverlapping) {
343 if (mIsDragOverlapping != isDragOverlapping) {
344 mIsDragOverlapping = isDragOverlapping;
345 invalidate();
346 }
347 }
348
349 boolean getIsDragOverlapping() {
350 return mIsDragOverlapping;
351 }
352
Adam Cohenebea84d2011-11-09 17:20:41 -0800353 protected void setOverscrollTransformsDirty(boolean dirty) {
354 mScrollingTransformsDirty = dirty;
355 }
356
357 protected void resetOverscrollTransforms() {
358 if (mScrollingTransformsDirty) {
359 setOverscrollTransformsDirty(false);
360 setTranslationX(0);
361 setRotationY(0);
362 // It doesn't matter if we pass true or false here, the important thing is that we
363 // pass 0, which results in the overscroll drawable not being drawn any more.
364 setOverScrollAmount(0, false);
365 setPivotX(getMeasuredWidth() / 2);
366 setPivotY(getMeasuredHeight() / 2);
367 }
368 }
369
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700370 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700371 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700372 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
373 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
374 // When we're small, we are either drawn normally or in the "accepts drops" state (during
375 // a drag). However, we also drag the mini hover background *over* one of those two
376 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700377 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700378 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800379
380 if (mIsDragOverlapping) {
381 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700382 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700383 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700384 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700385 }
Michael Jurka33945b22010-12-21 18:19:38 -0800386
387 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
388 bg.setBounds(mBackgroundRect);
389 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700390 }
Romain Guya6abce82009-11-10 02:54:41 -0800391
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700392 if (mCrosshairsVisibility > 0.0f) {
393 final int countX = mCountX;
394 final int countY = mCountY;
395
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700396 final float MAX_ALPHA = 0.4f;
397 final int MAX_VISIBLE_DISTANCE = 600;
398 final float DISTANCE_MULTIPLIER = 0.002f;
399
400 final Drawable d = mCrosshairsDrawable;
401 final int width = d.getIntrinsicWidth();
402 final int height = d.getIntrinsicHeight();
403
Winson Chung4b825dcd2011-06-19 12:41:22 -0700404 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700405 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700406 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700407 for (int row = 0; row <= countY; row++) {
408 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
409 float dist = mTmpPointF.length();
410 // Crosshairs further from the drag point are more faint
411 float alpha = Math.min(MAX_ALPHA,
412 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
413 if (alpha > 0.0f) {
414 d.setBounds(x, y, x + width, y + height);
415 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
416 d.draw(canvas);
417 }
418 y += mCellHeight + mHeightGap;
419 }
420 x += mCellWidth + mWidthGap;
421 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700422 }
Winson Chung150fbab2010-09-29 17:14:26 -0700423
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700424 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700425 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700426 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700427 if (alpha > 0) {
428 final Point p = mDragOutlines[i];
429 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700430 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700431 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700432 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700433 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800434
435 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
436 // requires an expanded clip rect (due to the glow's blur radius)
437 if (mPressedOrFocusedIcon != null) {
438 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
439 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
440 if (b != null) {
441 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700442 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
443 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800444 null);
445 }
446 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700447
448 // The folder outer / inner ring image(s)
449 for (int i = 0; i < mFolderOuterRings.size(); i++) {
450 FolderRingAnimator fra = mFolderOuterRings.get(i);
451
452 // Draw outer ring
453 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
454 int width = (int) fra.getOuterRingSize();
455 int height = width;
456 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
457
458 int centerX = mTempLocation[0] + mCellWidth / 2;
459 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
460
461 canvas.save();
462 canvas.translate(centerX - width / 2, centerY - height / 2);
463 d.setBounds(0, 0, width, height);
464 d.draw(canvas);
465 canvas.restore();
466
467 // Draw inner ring
468 d = FolderRingAnimator.sSharedInnerRingDrawable;
469 width = (int) fra.getInnerRingSize();
470 height = width;
471 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
472
473 centerX = mTempLocation[0] + mCellWidth / 2;
474 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
475 canvas.save();
476 canvas.translate(centerX - width / 2, centerY - width / 2);
477 d.setBounds(0, 0, width, height);
478 d.draw(canvas);
479 canvas.restore();
480 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700481
482 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
483 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
484 int width = d.getIntrinsicWidth();
485 int height = d.getIntrinsicHeight();
486
487 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
488 int centerX = mTempLocation[0] + mCellWidth / 2;
489 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
490
491 canvas.save();
492 canvas.translate(centerX - width / 2, centerY - width / 2);
493 d.setBounds(0, 0, width, height);
494 d.draw(canvas);
495 canvas.restore();
496 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700497 }
498
Adam Cohenb5ba0972011-09-07 18:02:31 -0700499 @Override
500 protected void dispatchDraw(Canvas canvas) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800501 // Debug drawing for hit space
502 if (false) {
503 final Rect frame = mRect;
504 for (int i = mChildren.getChildCount() - 1; i >= 0; i--) {
505 final View child = mChildren.getChildAt(i);
506 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
507
508 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
509 lp.isLockedToGrid) {
510 child.getHitRect(frame);
511 frame.offset(mPaddingLeft, mPaddingTop);
512
513 Paint p = new Paint();
514 p.setColor(Color.GREEN);
515 canvas.drawRect(frame, p);
516 }
517 }
518 }
519
Adam Cohenb5ba0972011-09-07 18:02:31 -0700520 super.dispatchDraw(canvas);
521 if (mForegroundAlpha > 0) {
522 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
523 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
524 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
525 mOverScrollForegroundDrawable.draw(canvas);
526 p.setXfermode(null);
527 }
528 }
529
Adam Cohen69ce2e52011-07-03 19:25:21 -0700530 public void showFolderAccept(FolderRingAnimator fra) {
531 mFolderOuterRings.add(fra);
532 }
533
534 public void hideFolderAccept(FolderRingAnimator fra) {
535 if (mFolderOuterRings.contains(fra)) {
536 mFolderOuterRings.remove(fra);
537 }
538 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700539 }
540
Adam Cohenc51934b2011-07-26 21:07:43 -0700541 public void setFolderLeaveBehindCell(int x, int y) {
542 mFolderLeaveBehindCell[0] = x;
543 mFolderLeaveBehindCell[1] = y;
544 invalidate();
545 }
546
547 public void clearFolderLeaveBehind() {
548 mFolderLeaveBehindCell[0] = -1;
549 mFolderLeaveBehindCell[1] = -1;
550 invalidate();
551 }
552
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700553 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700554 public boolean shouldDelayChildPressedState() {
555 return false;
556 }
557
558 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700559 public void cancelLongPress() {
560 super.cancelLongPress();
561
562 // Cancel long press for all children
563 final int count = getChildCount();
564 for (int i = 0; i < count; i++) {
565 final View child = getChildAt(i);
566 child.cancelLongPress();
567 }
568 }
569
Michael Jurkadee05892010-07-27 10:01:56 -0700570 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
571 mInterceptTouchListener = listener;
572 }
573
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700575 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 }
577
578 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700579 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 }
581
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800582 public void setIsHotseat(boolean isHotseat) {
583 mIsHotseat = isHotseat;
584 }
585
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700586 public boolean addViewToCellLayout(
587 View child, int index, int childId, LayoutParams params, boolean markCells) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800588 return addViewToCellLayout(child, index, childId, params, markCells, false);
589 }
590
591 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
592 boolean markCells, boolean allApps) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700593 final LayoutParams lp = params;
594
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800595 // Hotseat icons - scale down and remove text
596 // Don't scale the all apps button
597 // scale percent set to -1 means do not scale
598 // Only scale BubbleTextViews
599 if (child instanceof BubbleTextView) {
600 BubbleTextView bubbleChild = (BubbleTextView) child;
601
602 if (mIsHotseat && !allApps && mBubbleScalePercent >= 0) {
603 // If we haven't measured the child yet, do it now
604 // (this happens if we're being dropped from all-apps
605 if ((bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) {
606 getChildrenLayout().measureChild(bubbleChild);
607 }
608 int measuredWidth = bubbleChild.getMeasuredWidth();
609 int measuredHeight = bubbleChild.getMeasuredHeight();
610
611 float bubbleScale = mBubbleScalePercent / 100f;
612 bubbleChild.setPivotX(0);
613 bubbleChild.setPivotY(0);
614 bubbleChild.setScaleX(bubbleScale);
615 bubbleChild.setScaleY(bubbleScale);
616 bubbleChild.setTranslationX(measuredWidth * (1 - bubbleScale) / 2);
617 bubbleChild.setTranslationY(measuredHeight * (1 - bubbleScale) / 2);
618
619 bubbleChild.setTextColor(android.R.color.transparent);
620 } else {
621 bubbleChild.setScaleX(1f);
622 bubbleChild.setScaleY(1f);
623 bubbleChild.setTranslationX(0f);
624 bubbleChild.setTranslationY(0f);
625
626 bubbleChild.setTextColor(
627 getResources().getColor(R.color.workspace_icon_text_color));
628 }
629 }
630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 // Generate an id for each view, this assumes we have at most 256x256 cells
632 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700633 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700634 // If the horizontal or vertical span is set to -1, it is taken to
635 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700636 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
637 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638
Winson Chungaafa03c2010-06-11 17:34:16 -0700639 child.setId(childId);
640
Michael Jurka8c920dd2011-01-20 14:16:56 -0800641 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700642
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700643 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644
Winson Chungaafa03c2010-06-11 17:34:16 -0700645 return true;
646 }
647 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700651 public void removeAllViews() {
652 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800653 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654 }
655
656 @Override
657 public void removeAllViewsInLayout() {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700658 if (mChildren.getChildCount() > 0) {
659 clearOccupiedCells();
660 mChildren.removeAllViewsInLayout();
661 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 }
663
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700664 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800665 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700666 }
667
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 @Override
669 public void removeView(View view) {
670 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800671 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700672 }
673
674 @Override
675 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800676 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
677 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 }
679
680 @Override
681 public void removeViewInLayout(View view) {
682 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800683 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
685
686 @Override
687 public void removeViews(int start, int count) {
688 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800689 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800691 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700692 }
693
694 @Override
695 public void removeViewsInLayout(int start, int count) {
696 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800697 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700698 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800699 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700700 }
701
Michael Jurka8c920dd2011-01-20 14:16:56 -0800702 public void drawChildren(Canvas canvas) {
703 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 }
705
Michael Jurkaabded662011-03-04 12:06:57 -0800706 void buildChildrenLayer() {
707 mChildren.buildLayer();
708 }
709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 @Override
711 protected void onAttachedToWindow() {
712 super.onAttachedToWindow();
713 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
714 }
715
Michael Jurkaaf442092010-06-10 17:01:57 -0700716 public void setTagToCellInfoForPoint(int touchX, int touchY) {
717 final CellInfo cellInfo = mCellInfo;
718 final Rect frame = mRect;
719 final int x = touchX + mScrollX;
720 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800721 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700722
723 boolean found = false;
724 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800725 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800726 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700727
Adam Cohen1b607ed2011-03-03 17:26:50 -0800728 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
729 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700730 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700731
732 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
733 // offset that by this CellLayout's padding to test an (x,y) point that is relative
734 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700735 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700736
Michael Jurkaaf442092010-06-10 17:01:57 -0700737 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700738 cellInfo.cell = child;
739 cellInfo.cellX = lp.cellX;
740 cellInfo.cellY = lp.cellY;
741 cellInfo.spanX = lp.cellHSpan;
742 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700743 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700744 break;
745 }
746 }
747 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700748
Michael Jurkad771c962011-08-09 15:00:48 -0700749 mLastDownOnOccupiedCell = found;
750
Michael Jurkaaf442092010-06-10 17:01:57 -0700751 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700752 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700753 pointToCellExact(x, y, cellXY);
754
Michael Jurkaaf442092010-06-10 17:01:57 -0700755 cellInfo.cell = null;
756 cellInfo.cellX = cellXY[0];
757 cellInfo.cellY = cellXY[1];
758 cellInfo.spanX = 1;
759 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700760 }
761 setTag(cellInfo);
762 }
763
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 @Override
765 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700766 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
767 // even in the case where we return early. Not clearing here was causing bugs whereby on
768 // long-press we'd end up picking up an item from a previous drag operation.
769 final int action = ev.getAction();
770
771 if (action == MotionEvent.ACTION_DOWN) {
772 clearTagCellInfo();
773 }
774
Michael Jurkadee05892010-07-27 10:01:56 -0700775 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
776 return true;
777 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778
779 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700780 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 return false;
783 }
784
Adam Cohenc1997fd2011-08-15 18:26:39 -0700785 private void clearTagCellInfo() {
786 final CellInfo cellInfo = mCellInfo;
787 cellInfo.cell = null;
788 cellInfo.cellX = -1;
789 cellInfo.cellY = -1;
790 cellInfo.spanX = 0;
791 cellInfo.spanY = 0;
792 setTag(cellInfo);
793 }
794
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700796 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 }
798
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700799 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700800 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 * @param x X coordinate of the point
802 * @param y Y coordinate of the point
803 * @param result Array of 2 ints to hold the x and y coordinate of the cell
804 */
805 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700806 final int hStartPadding = getPaddingLeft();
807 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808
809 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
810 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
811
Adam Cohend22015c2010-07-26 22:02:18 -0700812 final int xAxis = mCountX;
813 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814
815 if (result[0] < 0) result[0] = 0;
816 if (result[0] >= xAxis) result[0] = xAxis - 1;
817 if (result[1] < 0) result[1] = 0;
818 if (result[1] >= yAxis) result[1] = yAxis - 1;
819 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700820
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 /**
822 * Given a point, return the cell that most closely encloses that point
823 * @param x X coordinate of the point
824 * @param y Y coordinate of the point
825 * @param result Array of 2 ints to hold the x and y coordinate of the cell
826 */
827 void pointToCellRounded(int x, int y, int[] result) {
828 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
829 }
830
831 /**
832 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700833 *
834 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700836 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 * @param result Array of 2 ints to hold the x and y coordinate of the point
838 */
839 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700840 final int hStartPadding = getPaddingLeft();
841 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842
843 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
844 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
845 }
846
Adam Cohene3e27a82011-04-15 12:07:39 -0700847 /**
848 * Given a cell coordinate, return the point that represents the upper left corner of that cell
849 *
850 * @param cellX X coordinate of the cell
851 * @param cellY Y coordinate of the cell
852 *
853 * @param result Array of 2 ints to hold the x and y coordinate of the point
854 */
855 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700856 final int hStartPadding = getPaddingLeft();
857 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700858
859 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
860 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
861 }
862
Romain Guy84f296c2009-11-04 15:00:44 -0800863 int getCellWidth() {
864 return mCellWidth;
865 }
866
867 int getCellHeight() {
868 return mCellHeight;
869 }
870
Adam Cohend4844c32011-02-18 19:25:06 -0800871 int getWidthGap() {
872 return mWidthGap;
873 }
874
875 int getHeightGap() {
876 return mHeightGap;
877 }
878
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700879 Rect getContentRect(Rect r) {
880 if (r == null) {
881 r = new Rect();
882 }
883 int left = getPaddingLeft();
884 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700885 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
886 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700887 r.set(left, top, right, bottom);
888 return r;
889 }
890
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 @Override
892 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
893 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700894
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700896 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
897
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
899 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700900
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
902 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
903 }
904
Adam Cohend22015c2010-07-26 22:02:18 -0700905 int numWidthGaps = mCountX - 1;
906 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907
Adam Cohen234c4cd2011-07-17 21:03:04 -0700908 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700909 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
910 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
911 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
912 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
913 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
914 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700915 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700916 } else {
917 mWidthGap = mOriginalWidthGap;
918 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700919 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700920
Michael Jurka8c920dd2011-01-20 14:16:56 -0800921 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
922 int newWidth = widthSpecSize;
923 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700924 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700925 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700926 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700927 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700928 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700929 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700930 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800931
932 int count = getChildCount();
933 for (int i = 0; i < count; i++) {
934 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700935 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
936 mPaddingRight, MeasureSpec.EXACTLY);
937 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
938 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800939 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
940 }
941 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943
944 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700945 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800948 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700949 child.layout(mPaddingLeft, mPaddingTop,
950 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
952 }
953
954 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700955 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
956 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700957 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700958 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
959 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -0700960 }
961
962 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800964 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 }
966
967 @Override
968 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800969 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 }
971
Michael Jurka5f1c5092010-09-03 14:15:02 -0700972 public float getBackgroundAlpha() {
973 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700974 }
975
Michael Jurka742574b2011-02-02 23:51:01 -0800976 public void setFastBackgroundAlpha(float alpha) {
977 mBackgroundAlpha = alpha;
978 }
979
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700980 public void setBackgroundAlphaMultiplier(float multiplier) {
981 mBackgroundAlphaMultiplier = multiplier;
982 }
983
Adam Cohenddb82192010-11-10 16:32:54 -0800984 public float getBackgroundAlphaMultiplier() {
985 return mBackgroundAlphaMultiplier;
986 }
987
Michael Jurka5f1c5092010-09-03 14:15:02 -0700988 public void setBackgroundAlpha(float alpha) {
989 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700990 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700991 }
992
Michael Jurka5f1c5092010-09-03 14:15:02 -0700993 // Need to return true to let the view system know we know how to handle alpha-- this is
994 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
995 // versions
996 @Override
997 protected boolean onSetAlpha(int alpha) {
998 return true;
999 }
1000
1001 public void setAlpha(float alpha) {
1002 setChildrenAlpha(alpha);
1003 super.setAlpha(alpha);
1004 }
1005
Michael Jurka742574b2011-02-02 23:51:01 -08001006 public void setFastAlpha(float alpha) {
1007 setFastChildrenAlpha(alpha);
1008 super.setFastAlpha(alpha);
1009 }
1010
Michael Jurkadee05892010-07-27 10:01:56 -07001011 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001012 final int childCount = getChildCount();
1013 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001014 getChildAt(i).setAlpha(alpha);
1015 }
1016 }
1017
Michael Jurka742574b2011-02-02 23:51:01 -08001018 private void setFastChildrenAlpha(float alpha) {
1019 final int childCount = getChildCount();
1020 for (int i = 0; i < childCount; i++) {
1021 getChildAt(i).setFastAlpha(alpha);
1022 }
1023 }
1024
Patrick Dubroy440c3602010-07-13 17:50:32 -07001025 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001026 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001027 }
1028
Adam Cohen76fc0852011-06-17 13:26:23 -07001029 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
1030 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001031 CellLayoutChildren clc = getChildrenLayout();
1032 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
1033 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1034 final ItemInfo info = (ItemInfo) child.getTag();
1035
1036 // We cancel any existing animations
1037 if (mReorderAnimators.containsKey(lp)) {
1038 mReorderAnimators.get(lp).cancel();
1039 mReorderAnimators.remove(lp);
1040 }
1041
1042 int oldX = lp.x;
1043 int oldY = lp.y;
1044 mOccupied[lp.cellX][lp.cellY] = false;
1045 mOccupied[cellX][cellY] = true;
1046
1047 lp.isLockedToGrid = true;
1048 lp.cellX = info.cellX = cellX;
1049 lp.cellY = info.cellY = cellY;
1050 clc.setupLp(lp);
1051 lp.isLockedToGrid = false;
1052 int newX = lp.x;
1053 int newY = lp.y;
1054
Adam Cohen76fc0852011-06-17 13:26:23 -07001055 lp.x = oldX;
1056 lp.y = oldY;
1057 child.requestLayout();
1058
Adam Cohenbfbfd262011-06-13 16:55:12 -07001059 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
1060 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
1061 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
1062 oa.setDuration(duration);
1063 mReorderAnimators.put(lp, oa);
1064 oa.addUpdateListener(new AnimatorUpdateListener() {
1065 public void onAnimationUpdate(ValueAnimator animation) {
1066 child.requestLayout();
1067 }
1068 });
1069 oa.addListener(new AnimatorListenerAdapter() {
1070 boolean cancelled = false;
1071 public void onAnimationEnd(Animator animation) {
1072 // If the animation was cancelled, it means that another animation
1073 // has interrupted this one, and we don't want to lock the item into
1074 // place just yet.
1075 if (!cancelled) {
1076 lp.isLockedToGrid = true;
1077 }
1078 if (mReorderAnimators.containsKey(lp)) {
1079 mReorderAnimators.remove(lp);
1080 }
1081 }
1082 public void onAnimationCancel(Animator animation) {
1083 cancelled = true;
1084 }
1085 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001086 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001087 oa.start();
1088 return true;
1089 }
1090 return false;
1091 }
1092
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001093 /**
1094 * Estimate where the top left cell of the dragged item will land if it is dropped.
1095 *
1096 * @param originX The X value of the top left corner of the item
1097 * @param originY The Y value of the top left corner of the item
1098 * @param spanX The number of horizontal cells that the item spans
1099 * @param spanY The number of vertical cells that the item spans
1100 * @param result The estimated drop cell X and Y.
1101 */
1102 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001103 final int countX = mCountX;
1104 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001105
Michael Jurkaa63c4522010-08-19 13:52:27 -07001106 // pointToCellRounded takes the top left of a cell but will pad that with
1107 // cellWidth/2 and cellHeight/2 when finding the matching cell
1108 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001109
1110 // If the item isn't fully on this screen, snap to the edges
1111 int rightOverhang = result[0] + spanX - countX;
1112 if (rightOverhang > 0) {
1113 result[0] -= rightOverhang; // Snap to right
1114 }
1115 result[0] = Math.max(0, result[0]); // Snap to left
1116 int bottomOverhang = result[1] + spanY - countY;
1117 if (bottomOverhang > 0) {
1118 result[1] -= bottomOverhang; // Snap to bottom
1119 }
1120 result[1] = Math.max(0, result[1]); // Snap to top
1121 }
1122
Winson Chungb8c69f32011-10-19 21:36:08 -07001123 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY,
1124 int spanX, int spanY, Point dragOffset, Rect dragRegion) {
Joe Onorato4be866d2010-10-10 11:26:02 -07001125
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001126 final int oldDragCellX = mDragCell[0];
1127 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001128 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chungb8c69f32011-10-19 21:36:08 -07001129 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001130 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1131 } else {
1132 mDragCenter.set(originX, originY);
1133 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001134
Adam Cohen2801caf2011-05-13 20:57:39 -07001135 if (dragOutline == null && v == null) {
1136 if (mCrosshairsDrawable != null) {
1137 invalidate();
1138 }
1139 return;
1140 }
1141
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001142 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001143 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001144 final int[] topLeft = mTmpPoint;
1145 cellToPoint(nearest[0], nearest[1], topLeft);
1146
Joe Onorato4be866d2010-10-10 11:26:02 -07001147 int left = topLeft[0];
1148 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001149
Winson Chungb8c69f32011-10-19 21:36:08 -07001150 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001151 // When drawing the drag outline, it did not account for margin offsets
1152 // added by the view's parent.
1153 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1154 left += lp.leftMargin;
1155 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001156
Adam Cohen99e8b402011-03-25 19:23:43 -07001157 // Offsets due to the size difference between the View and the dragOutline.
1158 // There is a size difference to account for the outer blur, which may lie
1159 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001160 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001161 // We center about the x axis
1162 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1163 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001164 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001165 if (dragOffset != null && dragRegion != null) {
1166 // Center the drag region *horizontally* in the cell and apply a drag
1167 // outline offset
1168 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1169 - dragRegion.width()) / 2;
1170 top += dragOffset.y;
1171 } else {
1172 // Center the drag outline in the cell
1173 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1174 - dragOutline.getWidth()) / 2;
1175 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1176 - dragOutline.getHeight()) / 2;
1177 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001178 }
Winson Chung150fbab2010-09-29 17:14:26 -07001179
Joe Onorato4be866d2010-10-10 11:26:02 -07001180 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001181 mDragOutlineAnims[oldIndex].animateOut();
1182 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001183
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001184 mDragOutlines[mDragOutlineCurrent].set(left, top);
1185 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1186 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001187 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001188
1189 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1190 if (mCrosshairsDrawable != null) {
1191 invalidate();
1192 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001193 }
1194
Adam Cohene0310962011-04-18 16:15:31 -07001195 public void clearDragOutlines() {
1196 final int oldIndex = mDragOutlineCurrent;
1197 mDragOutlineAnims[oldIndex].animateOut();
1198 mDragCell[0] = -1;
1199 mDragCell[1] = -1;
1200 }
1201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001203 * Find a vacant area that will fit the given bounds nearest the requested
1204 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001205 *
Romain Guy51afc022009-05-04 18:03:43 -07001206 * @param pixelX The X location at which you want to search for a vacant area.
1207 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001208 * @param spanX Horizontal span of the object.
1209 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001210 * @param result Array in which to place the result, or null (in which case a new array will
1211 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001212 * @return The X, Y cell of a vacant area that can contain this object,
1213 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001215 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001216 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1217 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001218 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001219
Michael Jurka6a1435d2010-09-27 17:35:12 -07001220 /**
1221 * Find a vacant area that will fit the given bounds nearest the requested
1222 * cell location. Uses Euclidean distance to score multiple vacant areas.
1223 *
1224 * @param pixelX The X location at which you want to search for a vacant area.
1225 * @param pixelY The Y location at which you want to search for a vacant area.
1226 * @param spanX Horizontal span of the object.
1227 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001228 * @param ignoreOccupied If true, the result can be an occupied cell
1229 * @param result Array in which to place the result, or null (in which case a new array will
1230 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001231 * @return The X, Y cell of a vacant area that can contain this object,
1232 * nearest the requested location.
1233 */
Adam Cohendf035382011-04-11 17:22:04 -07001234 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1235 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001236 // mark space take by ignoreView as available (method checks if ignoreView is null)
1237 markCellsAsUnoccupiedForView(ignoreView);
1238
Adam Cohene3e27a82011-04-15 12:07:39 -07001239 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1240 // to the center of the item, but we are searching based on the top-left cell, so
1241 // we translate the point over to correspond to the top-left.
1242 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1243 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1244
Jeff Sharkey70864282009-04-07 21:08:40 -07001245 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001246 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001247 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001248
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001249 final int countX = mCountX;
1250 final int countY = mCountY;
1251 final boolean[][] occupied = mOccupied;
1252
Winson Chungbbc60d82010-11-11 16:34:41 -08001253 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001254 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001255 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001256 if (ignoreOccupied) {
1257 for (int i = 0; i < spanX; i++) {
1258 for (int j = 0; j < spanY; j++) {
1259 if (occupied[x + i][y + j]) {
1260 // small optimization: we can skip to after the column we
1261 // just found an occupied cell
1262 x += i;
1263 continue inner;
1264 }
Michael Jurkac28de512010-08-13 11:27:44 -07001265 }
1266 }
1267 }
Winson Chung0be025d2011-05-23 17:45:09 -07001268 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001269 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270
Michael Jurkac28de512010-08-13 11:27:44 -07001271 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1272 + Math.pow(cellXY[1] - pixelY, 2));
1273 if (distance <= bestDistance) {
1274 bestDistance = distance;
1275 bestXY[0] = x;
1276 bestXY[1] = y;
1277 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 }
1279 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001280 // re-mark space taken by ignoreView as occupied
1281 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282
Adam Cohenc0dcf592011-06-01 15:30:43 -07001283 // Return -1, -1 if no suitable location found
1284 if (bestDistance == Double.MAX_VALUE) {
1285 bestXY[0] = -1;
1286 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001287 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001288 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001290
Adam Cohendf035382011-04-11 17:22:04 -07001291 /**
1292 * Find a vacant area that will fit the given bounds nearest the requested
1293 * cell location. Uses Euclidean distance to score multiple vacant areas.
1294 *
1295 * @param pixelX The X location at which you want to search for a vacant area.
1296 * @param pixelY The Y location at which you want to search for a vacant area.
1297 * @param spanX Horizontal span of the object.
1298 * @param spanY Vertical span of the object.
1299 * @param ignoreView Considers space occupied by this view as unoccupied
1300 * @param result Previously returned value to possibly recycle.
1301 * @return The X, Y cell of a vacant area that can contain this object,
1302 * nearest the requested location.
1303 */
1304 int[] findNearestVacantArea(
1305 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1306 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1307 }
1308
1309 /**
1310 * Find a starting cell position that will fit the given bounds nearest the requested
1311 * cell location. Uses Euclidean distance to score multiple vacant areas.
1312 *
1313 * @param pixelX The X location at which you want to search for a vacant area.
1314 * @param pixelY The Y location at which you want to search for a vacant area.
1315 * @param spanX Horizontal span of the object.
1316 * @param spanY Vertical span of the object.
1317 * @param ignoreView Considers space occupied by this view as unoccupied
1318 * @param result Previously returned value to possibly recycle.
1319 * @return The X, Y cell of a vacant area that can contain this object,
1320 * nearest the requested location.
1321 */
1322 int[] findNearestArea(
1323 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1324 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1325 }
1326
Michael Jurka0280c3b2010-09-17 15:00:07 -07001327 boolean existsEmptyCell() {
1328 return findCellForSpan(null, 1, 1);
1329 }
1330
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001332 * Finds the upper-left coordinate of the first rectangle in the grid that can
1333 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1334 * then this method will only return coordinates for rectangles that contain the cell
1335 * (intersectX, intersectY)
1336 *
1337 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1338 * can be found.
1339 * @param spanX The horizontal span of the cell we want to find.
1340 * @param spanY The vertical span of the cell we want to find.
1341 *
1342 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001343 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001344 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1345 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1346 }
1347
1348 /**
1349 * Like above, but ignores any cells occupied by the item "ignoreView"
1350 *
1351 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1352 * can be found.
1353 * @param spanX The horizontal span of the cell we want to find.
1354 * @param spanY The vertical span of the cell we want to find.
1355 * @param ignoreView The home screen item we should treat as not occupying any space
1356 * @return
1357 */
1358 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1359 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1360 }
1361
1362 /**
1363 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1364 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1365 *
1366 * @param spanX The horizontal span of the cell we want to find.
1367 * @param spanY The vertical span of the cell we want to find.
1368 * @param ignoreView The home screen item we should treat as not occupying any space
1369 * @param intersectX The X coordinate of the cell that we should try to overlap
1370 * @param intersectX The Y coordinate of the cell that we should try to overlap
1371 *
1372 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1373 */
1374 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1375 int intersectX, int intersectY) {
1376 return findCellForSpanThatIntersectsIgnoring(
1377 cellXY, spanX, spanY, intersectX, intersectY, null);
1378 }
1379
1380 /**
1381 * The superset of the above two methods
1382 */
1383 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1384 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001385 // mark space take by ignoreView as available (method checks if ignoreView is null)
1386 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001387
Michael Jurka28750fb2010-09-24 17:43:49 -07001388 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001389 while (true) {
1390 int startX = 0;
1391 if (intersectX >= 0) {
1392 startX = Math.max(startX, intersectX - (spanX - 1));
1393 }
1394 int endX = mCountX - (spanX - 1);
1395 if (intersectX >= 0) {
1396 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1397 }
1398 int startY = 0;
1399 if (intersectY >= 0) {
1400 startY = Math.max(startY, intersectY - (spanY - 1));
1401 }
1402 int endY = mCountY - (spanY - 1);
1403 if (intersectY >= 0) {
1404 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1405 }
1406
Winson Chungbbc60d82010-11-11 16:34:41 -08001407 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001408 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001409 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001410 for (int i = 0; i < spanX; i++) {
1411 for (int j = 0; j < spanY; j++) {
1412 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001413 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001414 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001415 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001416 continue inner;
1417 }
1418 }
1419 }
1420 if (cellXY != null) {
1421 cellXY[0] = x;
1422 cellXY[1] = y;
1423 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001424 foundCell = true;
1425 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001426 }
1427 }
1428 if (intersectX == -1 && intersectY == -1) {
1429 break;
1430 } else {
1431 // if we failed to find anything, try again but without any requirements of
1432 // intersecting
1433 intersectX = -1;
1434 intersectY = -1;
1435 continue;
1436 }
1437 }
1438
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001439 // re-mark space taken by ignoreView as occupied
1440 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001441 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001442 }
1443
1444 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001445 * A drag event has begun over this layout.
1446 * It may have begun over this layout (in which case onDragChild is called first),
1447 * or it may have begun on another layout.
1448 */
1449 void onDragEnter() {
1450 if (!mDragging) {
1451 // Fade in the drag indicators
1452 if (mCrosshairsAnimator != null) {
1453 mCrosshairsAnimator.animateIn();
1454 }
1455 }
1456 mDragging = true;
1457 }
1458
1459 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001460 * Called when drag has left this CellLayout or has been completed (successfully or not)
1461 */
1462 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001463 // This can actually be called when we aren't in a drag, e.g. when adding a new
1464 // item to this layout via the customize drawer.
1465 // Guard against that case.
1466 if (mDragging) {
1467 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001468
Joe Onorato4be866d2010-10-10 11:26:02 -07001469 // Fade out the drag indicators
1470 if (mCrosshairsAnimator != null) {
1471 mCrosshairsAnimator.animateOut();
1472 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001473 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001474
1475 // Invalidate the drag data
1476 mDragCell[0] = -1;
1477 mDragCell[1] = -1;
1478 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1479 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1480
Michael Jurka33945b22010-12-21 18:19:38 -08001481 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001482 }
1483
1484 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001485 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001486 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001487 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 *
1489 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001491 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001492 if (child != null) {
1493 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001494 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001495 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001496 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 }
1498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001501 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 * @param cellX X coordinate of upper left corner expressed as a cell position
1503 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001504 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001506 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001508 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 final int cellWidth = mCellWidth;
1510 final int cellHeight = mCellHeight;
1511 final int widthGap = mWidthGap;
1512 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001513
Winson Chung4b825dcd2011-06-19 12:41:22 -07001514 final int hStartPadding = getPaddingLeft();
1515 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001516
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1518 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1519
1520 int x = hStartPadding + cellX * (cellWidth + widthGap);
1521 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001522
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001523 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001525
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001527 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001529 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 * @param width Width in pixels
1531 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001532 * @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 -08001533 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001534 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001535 return rectToCell(getResources(), width, height, result);
1536 }
1537
1538 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 // Always assume we're working with the smallest span to make sure we
1540 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001541 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1542 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001544
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07001546 int spanX = (int) Math.ceil(width / (float) smallerSize);
1547 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04001548
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001549 if (result == null) {
1550 return new int[] { spanX, spanY };
1551 }
1552 result[0] = spanX;
1553 result[1] = spanY;
1554 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
1556
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001557 public int[] cellSpansToSize(int hSpans, int vSpans) {
1558 int[] size = new int[2];
1559 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1560 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1561 return size;
1562 }
1563
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001565 * Calculate the grid spans needed to fit given item
1566 */
1567 public void calculateSpans(ItemInfo info) {
1568 final int minWidth;
1569 final int minHeight;
1570
1571 if (info instanceof LauncherAppWidgetInfo) {
1572 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1573 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1574 } else if (info instanceof PendingAddWidgetInfo) {
1575 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1576 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1577 } else {
1578 // It's not a widget, so it must be 1x1
1579 info.spanX = info.spanY = 1;
1580 return;
1581 }
1582 int[] spans = rectToCell(minWidth, minHeight, null);
1583 info.spanX = spans[0];
1584 info.spanY = spans[1];
1585 }
1586
1587 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 * Find the first vacant cell, if there is one.
1589 *
1590 * @param vacant Holds the x and y coordinate of the vacant cell
1591 * @param spanX Horizontal cell span.
1592 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001593 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 * @return True if a vacant cell was found
1595 */
1596 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597
Michael Jurka0280c3b2010-09-17 15:00:07 -07001598 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
1600
1601 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1602 int xCount, int yCount, boolean[][] occupied) {
1603
Adam Cohen2801caf2011-05-13 20:57:39 -07001604 for (int y = 0; y < yCount; y++) {
1605 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 boolean available = !occupied[x][y];
1607out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1608 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1609 available = available && !occupied[i][j];
1610 if (!available) break out;
1611 }
1612 }
1613
1614 if (available) {
1615 vacant[0] = x;
1616 vacant[1] = y;
1617 return true;
1618 }
1619 }
1620 }
1621
1622 return false;
1623 }
1624
Michael Jurka0280c3b2010-09-17 15:00:07 -07001625 private void clearOccupiedCells() {
1626 for (int x = 0; x < mCountX; x++) {
1627 for (int y = 0; y < mCountY; y++) {
1628 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 }
1630 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001631 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632
Adam Cohen1b607ed2011-03-03 17:26:50 -08001633 /**
1634 * Given a view, determines how much that view can be expanded in all directions, in terms of
1635 * whether or not there are other items occupying adjacent cells. Used by the
1636 * AppWidgetResizeFrame to determine how the widget can be resized.
1637 */
Adam Cohend4844c32011-02-18 19:25:06 -08001638 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001639 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001640 boolean flag;
1641
Adam Cohen1b607ed2011-03-03 17:26:50 -08001642 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001643 for (int x = lp.cellX - 1; x >= 0; x--) {
1644 flag = false;
1645 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1646 if (mOccupied[x][y]) flag = true;
1647 }
1648 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001649 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001650 }
1651
Adam Cohen1b607ed2011-03-03 17:26:50 -08001652 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001653 for (int y = lp.cellY - 1; y >= 0; y--) {
1654 flag = false;
1655 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1656 if (mOccupied[x][y]) flag = true;
1657 }
1658 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001659 expandability[AppWidgetResizeFrame.TOP]++;
1660 }
Adam Cohend4844c32011-02-18 19:25:06 -08001661
Adam Cohen1b607ed2011-03-03 17:26:50 -08001662 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001663 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1664 flag = false;
1665 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1666 if (mOccupied[x][y]) flag = true;
1667 }
1668 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001669 expandability[AppWidgetResizeFrame.RIGHT]++;
1670 }
Adam Cohend4844c32011-02-18 19:25:06 -08001671
Adam Cohen1b607ed2011-03-03 17:26:50 -08001672 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001673 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1674 flag = false;
1675 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1676 if (mOccupied[x][y]) flag = true;
1677 }
1678 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001679 expandability[AppWidgetResizeFrame.BOTTOM]++;
1680 }
Adam Cohend4844c32011-02-18 19:25:06 -08001681 }
1682
Michael Jurka0280c3b2010-09-17 15:00:07 -07001683 public void onMove(View view, int newCellX, int newCellY) {
1684 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1685 markCellsAsUnoccupiedForView(view);
1686 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1687 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688
Adam Cohend4844c32011-02-18 19:25:06 -08001689 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001690 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001691 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1692 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1693 }
1694
Adam Cohend4844c32011-02-18 19:25:06 -08001695 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001696 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001697 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1698 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1699 }
1700
1701 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1702 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1703 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1704 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 }
1706 }
1707 }
1708
Adam Cohen2801caf2011-05-13 20:57:39 -07001709 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001710 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001711 (Math.max((mCountX - 1), 0) * mWidthGap);
1712 }
1713
1714 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001715 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001716 (Math.max((mCountY - 1), 0) * mHeightGap);
1717 }
1718
Michael Jurka66d72172011-04-12 16:29:25 -07001719 public boolean isOccupied(int x, int y) {
1720 if (x < mCountX && y < mCountY) {
1721 return mOccupied[x][y];
1722 } else {
1723 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1724 }
1725 }
1726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 @Override
1728 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1729 return new CellLayout.LayoutParams(getContext(), attrs);
1730 }
1731
1732 @Override
1733 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1734 return p instanceof CellLayout.LayoutParams;
1735 }
1736
1737 @Override
1738 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1739 return new CellLayout.LayoutParams(p);
1740 }
1741
Winson Chungaafa03c2010-06-11 17:34:16 -07001742 public static class CellLayoutAnimationController extends LayoutAnimationController {
1743 public CellLayoutAnimationController(Animation animation, float delay) {
1744 super(animation, delay);
1745 }
1746
1747 @Override
1748 protected long getDelayForView(View view) {
1749 return (int) (Math.random() * 150);
1750 }
1751 }
1752
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1754 /**
1755 * Horizontal location of the item in the grid.
1756 */
1757 @ViewDebug.ExportedProperty
1758 public int cellX;
1759
1760 /**
1761 * Vertical location of the item in the grid.
1762 */
1763 @ViewDebug.ExportedProperty
1764 public int cellY;
1765
1766 /**
1767 * Number of cells spanned horizontally by the item.
1768 */
1769 @ViewDebug.ExportedProperty
1770 public int cellHSpan;
1771
1772 /**
1773 * Number of cells spanned vertically by the item.
1774 */
1775 @ViewDebug.ExportedProperty
1776 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001777
Adam Cohen1b607ed2011-03-03 17:26:50 -08001778 /**
1779 * Indicates whether the item will set its x, y, width and height parameters freely,
1780 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1781 */
Adam Cohend4844c32011-02-18 19:25:06 -08001782 public boolean isLockedToGrid = true;
1783
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 // X coordinate of the view in the layout.
1785 @ViewDebug.ExportedProperty
1786 int x;
1787 // Y coordinate of the view in the layout.
1788 @ViewDebug.ExportedProperty
1789 int y;
1790
Romain Guy84f296c2009-11-04 15:00:44 -08001791 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001792
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 public LayoutParams(Context c, AttributeSet attrs) {
1794 super(c, attrs);
1795 cellHSpan = 1;
1796 cellVSpan = 1;
1797 }
1798
1799 public LayoutParams(ViewGroup.LayoutParams source) {
1800 super(source);
1801 cellHSpan = 1;
1802 cellVSpan = 1;
1803 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001804
1805 public LayoutParams(LayoutParams source) {
1806 super(source);
1807 this.cellX = source.cellX;
1808 this.cellY = source.cellY;
1809 this.cellHSpan = source.cellHSpan;
1810 this.cellVSpan = source.cellVSpan;
1811 }
1812
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001814 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 this.cellX = cellX;
1816 this.cellY = cellY;
1817 this.cellHSpan = cellHSpan;
1818 this.cellVSpan = cellVSpan;
1819 }
1820
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001821 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001822 if (isLockedToGrid) {
1823 final int myCellHSpan = cellHSpan;
1824 final int myCellVSpan = cellVSpan;
1825 final int myCellX = cellX;
1826 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001827
Adam Cohend4844c32011-02-18 19:25:06 -08001828 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1829 leftMargin - rightMargin;
1830 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1831 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001832 x = myCellX * (cellWidth + widthGap) + leftMargin;
1833 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001834 }
1835 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001836
Winson Chungaafa03c2010-06-11 17:34:16 -07001837 public String toString() {
1838 return "(" + this.cellX + ", " + this.cellY + ")";
1839 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001840
1841 public void setWidth(int width) {
1842 this.width = width;
1843 }
1844
1845 public int getWidth() {
1846 return width;
1847 }
1848
1849 public void setHeight(int height) {
1850 this.height = height;
1851 }
1852
1853 public int getHeight() {
1854 return height;
1855 }
1856
1857 public void setX(int x) {
1858 this.x = x;
1859 }
1860
1861 public int getX() {
1862 return x;
1863 }
1864
1865 public void setY(int y) {
1866 this.y = y;
1867 }
1868
1869 public int getY() {
1870 return y;
1871 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 }
1873
Michael Jurka0280c3b2010-09-17 15:00:07 -07001874 // This class stores info for two purposes:
1875 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1876 // its spanX, spanY, and the screen it is on
1877 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1878 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1879 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001880 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001882 int cellX = -1;
1883 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 int spanX;
1885 int spanY;
1886 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001887 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 @Override
1890 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001891 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1892 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 }
1894 }
Michael Jurkad771c962011-08-09 15:00:48 -07001895
1896 public boolean lastDownOnOccupiedCell() {
1897 return mLastDownOnOccupiedCell;
1898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899}