blob: 1aaba6db9c044f61269137e25c8dc6fd0a5dfa89 [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;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070038import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070039import android.graphics.drawable.NinePatchDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.MotionEvent;
43import android.view.View;
44import android.view.ViewDebug;
45import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070047import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
Adam Cohen66396872011-04-15 17:50:36 -070050import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070051import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070052
Adam Cohen69ce2e52011-07-03 19:25:21 -070053import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070054import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070055import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080056import java.util.Stack;
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.
Adam Cohend41fbf52012-02-16 23:53:59 -0800113 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700114 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700115 private InterruptibleInOutAnimator[] mDragOutlineAnims =
116 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700117
118 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700119 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700120 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700121
Patrick Dubroy96864c32011-03-10 17:17:23 -0800122 private BubbleTextView mPressedOrFocusedIcon;
123
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700124 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700125 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700126 private float mCrosshairsVisibility = 0.0f;
127
Adam Cohenbfbfd262011-06-13 16:55:12 -0700128 private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new
129 HashMap<CellLayout.LayoutParams, ObjectAnimator>();
130
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700131 // When a drag operation is in progress, holds the nearest cell to the touch point
132 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Joe Onorato4be866d2010-10-10 11:26:02 -0700134 private boolean mDragging = false;
135
Patrick Dubroyce34a972010-10-19 10:34:32 -0700136 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800137 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700138
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800139 private boolean mIsHotseat = false;
140 private final int mBubbleScalePercent;
Andrew Flynnbc239a12012-03-06 11:39:49 -0800141 private final int mBubbleHotseatScalePercent;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 public CellLayout(Context context) {
144 this(context, null);
145 }
146
147 public CellLayout(Context context, AttributeSet attrs) {
148 this(context, attrs, 0);
149 }
150
151 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
152 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700153
154 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
155 // the user where a dragged item will land when dropped.
156 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
159
Winson Chung4b825dcd2011-06-19 12:41:22 -0700160 mOriginalCellWidth =
161 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
162 mOriginalCellHeight =
163 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700164 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
165 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700166 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700167 mCountX = LauncherModel.getCellCountX();
168 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700169 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
171 a.recycle();
172
173 setAlwaysDrawnWithCacheEnabled(false);
174
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700175 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700176
Winson Chung967289b2011-06-30 18:09:30 -0700177 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700178 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800179
Adam Cohenb5ba0972011-09-07 18:02:31 -0700180 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
181 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
182 mForegroundPadding =
183 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800184
Winson Chungb26f3d62011-06-02 10:49:29 -0700185 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700186 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700187
Andrew Flynnbc239a12012-03-06 11:39:49 -0800188 mBubbleScalePercent = res.getInteger(R.integer.app_icon_scale_percent);
189 mBubbleHotseatScalePercent = res.getInteger(R.integer.app_icon_hotseat_scale_percent);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800190
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700191 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700192
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700193 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700194 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700195
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700196 // Set up the animation for fading the crosshairs in and out
197 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700198 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700199 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700200 public void onAnimationUpdate(ValueAnimator animation) {
201 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700202 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700203 }
204 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700205 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700206
Winson Chungb8c69f32011-10-19 21:36:08 -0700207 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700208 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800209 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700210 }
211
212 // When dragging things around the home screens, we show a green outline of
213 // where the item will land. The outlines gradually fade out, leaving a trail
214 // behind the drag path.
215 // Set up all the animations that are used to implement this fading.
216 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700217 final float fromAlphaValue = 0;
218 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700219
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700220 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700221
222 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700223 final InterruptibleInOutAnimator anim =
224 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700225 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700226 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700227 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700228 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700229 final Bitmap outline = (Bitmap)anim.getTag();
230
231 // If an animation is started and then stopped very quickly, we can still
232 // get spurious updates we've cleared the tag. Guard against this.
233 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700234 if (false) {
235 Object val = animation.getAnimatedValue();
236 Log.d(TAG, "anim " + thisIndex + " update: " + val +
237 ", isStopped " + anim.isStopped());
238 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700239 // Try to prevent it from continuing to run
240 animation.cancel();
241 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700242 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800243 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700244 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700245 }
246 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700247 // The animation holds a reference to the drag outline bitmap as long is it's
248 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700249 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700250 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700251 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700252 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700253 anim.setTag(null);
254 }
255 }
256 });
257 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700258 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700259
Michael Jurka18014792010-10-14 09:01:34 -0700260 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700261 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800262
Michael Jurka8c920dd2011-01-20 14:16:56 -0800263 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700264 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800265 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700266 }
267
Michael Jurkaf6440da2011-04-05 14:50:34 -0700268 static int widthInPortrait(Resources r, int numCells) {
269 // We use this method from Workspace to figure out how many rows/columns Launcher should
270 // have. We ignore the left/right padding on CellLayout because it turns out in our design
271 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700272 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700273 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
274 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700275
Winson Chung4b825dcd2011-06-19 12:41:22 -0700276 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700277 }
278
Michael Jurkaf6440da2011-04-05 14:50:34 -0700279 static int heightInLandscape(Resources r, int numCells) {
280 // We use this method from Workspace to figure out how many rows/columns Launcher should
281 // have. We ignore the left/right padding on CellLayout because it turns out in our design
282 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700283 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700284 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
285 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700286
Winson Chung4b825dcd2011-06-19 12:41:22 -0700287 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700288 }
289
Adam Cohen2801caf2011-05-13 20:57:39 -0700290 public void enableHardwareLayers() {
Adam Cohen7ef91832011-08-25 14:06:02 -0700291 mChildren.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700292 }
293
294 public void setGridSize(int x, int y) {
295 mCountX = x;
296 mCountY = y;
297 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700298 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700299 }
300
Patrick Dubroy96864c32011-03-10 17:17:23 -0800301 private void invalidateBubbleTextView(BubbleTextView icon) {
302 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700303 invalidate(icon.getLeft() + getPaddingLeft() - padding,
304 icon.getTop() + getPaddingTop() - padding,
305 icon.getRight() + getPaddingLeft() + padding,
306 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800307 }
308
Adam Cohenb5ba0972011-09-07 18:02:31 -0700309 void setOverScrollAmount(float r, boolean left) {
310 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
311 mOverScrollForegroundDrawable = mOverScrollLeft;
312 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
313 mOverScrollForegroundDrawable = mOverScrollRight;
314 }
315
316 mForegroundAlpha = (int) Math.round((r * 255));
317 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
318 invalidate();
319 }
320
Patrick Dubroy96864c32011-03-10 17:17:23 -0800321 void setPressedOrFocusedIcon(BubbleTextView icon) {
322 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
323 // requires an expanded clip rect (due to the glow's blur radius)
324 BubbleTextView oldIcon = mPressedOrFocusedIcon;
325 mPressedOrFocusedIcon = icon;
326 if (oldIcon != null) {
327 invalidateBubbleTextView(oldIcon);
328 }
329 if (mPressedOrFocusedIcon != null) {
330 invalidateBubbleTextView(mPressedOrFocusedIcon);
331 }
332 }
333
Winson Chung6e314082011-01-27 16:46:51 -0800334 public CellLayoutChildren getChildrenLayout() {
335 if (getChildCount() > 0) {
336 return (CellLayoutChildren) getChildAt(0);
337 }
338 return null;
339 }
340
Michael Jurka33945b22010-12-21 18:19:38 -0800341 void setIsDragOverlapping(boolean isDragOverlapping) {
342 if (mIsDragOverlapping != isDragOverlapping) {
343 mIsDragOverlapping = isDragOverlapping;
344 invalidate();
345 }
346 }
347
348 boolean getIsDragOverlapping() {
349 return mIsDragOverlapping;
350 }
351
Adam Cohenebea84d2011-11-09 17:20:41 -0800352 protected void setOverscrollTransformsDirty(boolean dirty) {
353 mScrollingTransformsDirty = dirty;
354 }
355
356 protected void resetOverscrollTransforms() {
357 if (mScrollingTransformsDirty) {
358 setOverscrollTransformsDirty(false);
359 setTranslationX(0);
360 setRotationY(0);
361 // It doesn't matter if we pass true or false here, the important thing is that we
362 // pass 0, which results in the overscroll drawable not being drawn any more.
363 setOverScrollAmount(0, false);
364 setPivotX(getMeasuredWidth() / 2);
365 setPivotY(getMeasuredHeight() / 2);
366 }
367 }
368
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700369 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700370 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700371 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
372 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
373 // When we're small, we are either drawn normally or in the "accepts drops" state (during
374 // a drag). However, we also drag the mini hover background *over* one of those two
375 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700376 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700377 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800378
379 if (mIsDragOverlapping) {
380 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700381 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700382 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700383 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700384 }
Michael Jurka33945b22010-12-21 18:19:38 -0800385
386 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
387 bg.setBounds(mBackgroundRect);
388 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700389 }
Romain Guya6abce82009-11-10 02:54:41 -0800390
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700391 if (mCrosshairsVisibility > 0.0f) {
392 final int countX = mCountX;
393 final int countY = mCountY;
394
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700395 final float MAX_ALPHA = 0.4f;
396 final int MAX_VISIBLE_DISTANCE = 600;
397 final float DISTANCE_MULTIPLIER = 0.002f;
398
399 final Drawable d = mCrosshairsDrawable;
400 final int width = d.getIntrinsicWidth();
401 final int height = d.getIntrinsicHeight();
402
Winson Chung4b825dcd2011-06-19 12:41:22 -0700403 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700404 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700405 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700406 for (int row = 0; row <= countY; row++) {
407 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
408 float dist = mTmpPointF.length();
409 // Crosshairs further from the drag point are more faint
410 float alpha = Math.min(MAX_ALPHA,
411 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
412 if (alpha > 0.0f) {
413 d.setBounds(x, y, x + width, y + height);
414 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
415 d.draw(canvas);
416 }
417 y += mCellHeight + mHeightGap;
418 }
419 x += mCellWidth + mWidthGap;
420 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700421 }
Winson Chung150fbab2010-09-29 17:14:26 -0700422
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700423 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700424 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700425 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700426 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800427 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700428 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700429 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800430 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700431 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700432 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800433
434 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
435 // requires an expanded clip rect (due to the glow's blur radius)
436 if (mPressedOrFocusedIcon != null) {
437 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
438 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
439 if (b != null) {
440 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700441 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
442 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800443 null);
444 }
445 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700446
447 // The folder outer / inner ring image(s)
448 for (int i = 0; i < mFolderOuterRings.size(); i++) {
449 FolderRingAnimator fra = mFolderOuterRings.get(i);
450
451 // Draw outer ring
452 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
453 int width = (int) fra.getOuterRingSize();
454 int height = width;
455 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
456
457 int centerX = mTempLocation[0] + mCellWidth / 2;
458 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
459
460 canvas.save();
461 canvas.translate(centerX - width / 2, centerY - height / 2);
462 d.setBounds(0, 0, width, height);
463 d.draw(canvas);
464 canvas.restore();
465
466 // Draw inner ring
467 d = FolderRingAnimator.sSharedInnerRingDrawable;
468 width = (int) fra.getInnerRingSize();
469 height = width;
470 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
471
472 centerX = mTempLocation[0] + mCellWidth / 2;
473 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
474 canvas.save();
475 canvas.translate(centerX - width / 2, centerY - width / 2);
476 d.setBounds(0, 0, width, height);
477 d.draw(canvas);
478 canvas.restore();
479 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700480
481 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
482 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
483 int width = d.getIntrinsicWidth();
484 int height = d.getIntrinsicHeight();
485
486 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
487 int centerX = mTempLocation[0] + mCellWidth / 2;
488 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
489
490 canvas.save();
491 canvas.translate(centerX - width / 2, centerY - width / 2);
492 d.setBounds(0, 0, width, height);
493 d.draw(canvas);
494 canvas.restore();
495 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700496 }
497
Adam Cohenb5ba0972011-09-07 18:02:31 -0700498 @Override
499 protected void dispatchDraw(Canvas canvas) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800500 // Debug drawing for hit space
501 if (false) {
502 final Rect frame = mRect;
503 for (int i = mChildren.getChildCount() - 1; i >= 0; i--) {
504 final View child = mChildren.getChildAt(i);
505 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
506
507 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
508 lp.isLockedToGrid) {
509 child.getHitRect(frame);
510 frame.offset(mPaddingLeft, mPaddingTop);
511
512 Paint p = new Paint();
513 p.setColor(Color.GREEN);
514 canvas.drawRect(frame, p);
515 }
516 }
517 }
518
Adam Cohenb5ba0972011-09-07 18:02:31 -0700519 super.dispatchDraw(canvas);
520 if (mForegroundAlpha > 0) {
521 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
522 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
523 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
524 mOverScrollForegroundDrawable.draw(canvas);
525 p.setXfermode(null);
526 }
527 }
528
Adam Cohen69ce2e52011-07-03 19:25:21 -0700529 public void showFolderAccept(FolderRingAnimator fra) {
530 mFolderOuterRings.add(fra);
531 }
532
533 public void hideFolderAccept(FolderRingAnimator fra) {
534 if (mFolderOuterRings.contains(fra)) {
535 mFolderOuterRings.remove(fra);
536 }
537 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700538 }
539
Adam Cohenc51934b2011-07-26 21:07:43 -0700540 public void setFolderLeaveBehindCell(int x, int y) {
541 mFolderLeaveBehindCell[0] = x;
542 mFolderLeaveBehindCell[1] = y;
543 invalidate();
544 }
545
546 public void clearFolderLeaveBehind() {
547 mFolderLeaveBehindCell[0] = -1;
548 mFolderLeaveBehindCell[1] = -1;
549 invalidate();
550 }
551
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700552 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700553 public boolean shouldDelayChildPressedState() {
554 return false;
555 }
556
557 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700558 public void cancelLongPress() {
559 super.cancelLongPress();
560
561 // Cancel long press for all children
562 final int count = getChildCount();
563 for (int i = 0; i < count; i++) {
564 final View child = getChildAt(i);
565 child.cancelLongPress();
566 }
567 }
568
Michael Jurkadee05892010-07-27 10:01:56 -0700569 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
570 mInterceptTouchListener = listener;
571 }
572
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700574 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 }
576
577 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700578 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 }
580
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800581 public void setIsHotseat(boolean isHotseat) {
582 mIsHotseat = isHotseat;
583 }
584
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700585 public boolean addViewToCellLayout(
586 View child, int index, int childId, LayoutParams params, boolean markCells) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800587 return addViewToCellLayout(child, index, childId, params, markCells, false);
588 }
589
Andrew Flynnbc239a12012-03-06 11:39:49 -0800590 private void scaleChild(BubbleTextView bubbleChild, float pivot, int scalePercent) {
591 // If we haven't measured the child yet, do it now
592 // (this happens if we're being dropped from all-apps
593 if (bubbleChild.getLayoutParams() instanceof LayoutParams &&
594 (bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) {
595 getChildrenLayout().measureChild(bubbleChild);
596 }
597 int measuredWidth = bubbleChild.getMeasuredWidth();
598 int measuredHeight = bubbleChild.getMeasuredHeight();
599
600 float scale = scalePercent / 100f;
601 bubbleChild.setPivotX(pivot);
602 bubbleChild.setPivotY(pivot);
603 bubbleChild.setScaleX(scale);
604 bubbleChild.setScaleY(scale);
605 bubbleChild.setTranslationX(measuredWidth * (1 - scale) / 2);
606 bubbleChild.setTranslationY(measuredHeight * (1 - scale) / 2);
607 }
608
609 private void resetChild(BubbleTextView bubbleChild) {
610 bubbleChild.setScaleX(1f);
611 bubbleChild.setScaleY(1f);
612 bubbleChild.setTranslationX(0f);
613 bubbleChild.setTranslationY(0f);
614
615 bubbleChild.setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
616 }
617
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800618 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
619 boolean markCells, boolean allApps) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700620 final LayoutParams lp = params;
621
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800622 // Hotseat icons - scale down and remove text
623 // Don't scale the all apps button
624 // scale percent set to -1 means do not scale
625 // Only scale BubbleTextViews
626 if (child instanceof BubbleTextView) {
627 BubbleTextView bubbleChild = (BubbleTextView) child;
628
Andrew Flynnbc239a12012-03-06 11:39:49 -0800629 // Start the child with 100% scale and visible text
630 resetChild(bubbleChild);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800631
Andrew Flynnbc239a12012-03-06 11:39:49 -0800632 if (mIsHotseat && !allApps && mBubbleHotseatScalePercent >= 0) {
633 // Scale/make transparent for a hotseat
634 scaleChild(bubbleChild, 0f, mBubbleHotseatScalePercent);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800635
Andrew Flynnbc239a12012-03-06 11:39:49 -0800636 bubbleChild.setTextColor(getResources().getColor(android.R.color.transparent));
637 } else if (mBubbleScalePercent >= 0) {
638 // Else possibly still scale it if we need to for smaller icons
639 scaleChild(bubbleChild, 0f, mBubbleScalePercent);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800640 }
641 }
642
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 // Generate an id for each view, this assumes we have at most 256x256 cells
644 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700645 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700646 // If the horizontal or vertical span is set to -1, it is taken to
647 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700648 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
649 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650
Winson Chungaafa03c2010-06-11 17:34:16 -0700651 child.setId(childId);
652
Michael Jurka8c920dd2011-01-20 14:16:56 -0800653 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700654
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700655 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656
Winson Chungaafa03c2010-06-11 17:34:16 -0700657 return true;
658 }
659 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700663 public void removeAllViews() {
664 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800665 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 }
667
668 @Override
669 public void removeAllViewsInLayout() {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700670 if (mChildren.getChildCount() > 0) {
671 clearOccupiedCells();
672 mChildren.removeAllViewsInLayout();
673 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 }
675
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700676 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800677 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700678 }
679
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 @Override
681 public void removeView(View view) {
682 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800683 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
685
686 @Override
687 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800688 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
689 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 }
691
692 @Override
693 public void removeViewInLayout(View view) {
694 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800695 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700696 }
697
698 @Override
699 public void removeViews(int start, int count) {
700 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800701 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700702 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800703 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700704 }
705
706 @Override
707 public void removeViewsInLayout(int start, int count) {
708 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800709 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700710 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800711 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700712 }
713
Michael Jurka8c920dd2011-01-20 14:16:56 -0800714 public void drawChildren(Canvas canvas) {
715 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 }
717
Michael Jurkaabded662011-03-04 12:06:57 -0800718 void buildChildrenLayer() {
719 mChildren.buildLayer();
720 }
721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 @Override
723 protected void onAttachedToWindow() {
724 super.onAttachedToWindow();
725 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
726 }
727
Michael Jurkaaf442092010-06-10 17:01:57 -0700728 public void setTagToCellInfoForPoint(int touchX, int touchY) {
729 final CellInfo cellInfo = mCellInfo;
730 final Rect frame = mRect;
731 final int x = touchX + mScrollX;
732 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800733 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700734
735 boolean found = false;
736 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800737 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800738 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700739
Adam Cohen1b607ed2011-03-03 17:26:50 -0800740 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
741 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700742 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700743
744 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
745 // offset that by this CellLayout's padding to test an (x,y) point that is relative
746 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700747 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700748
Michael Jurkaaf442092010-06-10 17:01:57 -0700749 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700750 cellInfo.cell = child;
751 cellInfo.cellX = lp.cellX;
752 cellInfo.cellY = lp.cellY;
753 cellInfo.spanX = lp.cellHSpan;
754 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700755 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700756 break;
757 }
758 }
759 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700760
Michael Jurkad771c962011-08-09 15:00:48 -0700761 mLastDownOnOccupiedCell = found;
762
Michael Jurkaaf442092010-06-10 17:01:57 -0700763 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700764 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700765 pointToCellExact(x, y, cellXY);
766
Michael Jurkaaf442092010-06-10 17:01:57 -0700767 cellInfo.cell = null;
768 cellInfo.cellX = cellXY[0];
769 cellInfo.cellY = cellXY[1];
770 cellInfo.spanX = 1;
771 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700772 }
773 setTag(cellInfo);
774 }
775
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 @Override
777 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700778 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
779 // even in the case where we return early. Not clearing here was causing bugs whereby on
780 // long-press we'd end up picking up an item from a previous drag operation.
781 final int action = ev.getAction();
782
783 if (action == MotionEvent.ACTION_DOWN) {
784 clearTagCellInfo();
785 }
786
Michael Jurkadee05892010-07-27 10:01:56 -0700787 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
788 return true;
789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790
791 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700792 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 return false;
795 }
796
Adam Cohenc1997fd2011-08-15 18:26:39 -0700797 private void clearTagCellInfo() {
798 final CellInfo cellInfo = mCellInfo;
799 cellInfo.cell = null;
800 cellInfo.cellX = -1;
801 cellInfo.cellY = -1;
802 cellInfo.spanX = 0;
803 cellInfo.spanY = 0;
804 setTag(cellInfo);
805 }
806
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700808 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 }
810
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700811 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700812 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 * @param x X coordinate of the point
814 * @param y Y coordinate of the point
815 * @param result Array of 2 ints to hold the x and y coordinate of the cell
816 */
817 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700818 final int hStartPadding = getPaddingLeft();
819 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820
821 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
822 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
823
Adam Cohend22015c2010-07-26 22:02:18 -0700824 final int xAxis = mCountX;
825 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826
827 if (result[0] < 0) result[0] = 0;
828 if (result[0] >= xAxis) result[0] = xAxis - 1;
829 if (result[1] < 0) result[1] = 0;
830 if (result[1] >= yAxis) result[1] = yAxis - 1;
831 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700832
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 /**
834 * Given a point, return the cell that most closely encloses that point
835 * @param x X coordinate of the point
836 * @param y Y coordinate of the point
837 * @param result Array of 2 ints to hold the x and y coordinate of the cell
838 */
839 void pointToCellRounded(int x, int y, int[] result) {
840 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
841 }
842
843 /**
844 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700845 *
846 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700848 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 * @param result Array of 2 ints to hold the x and y coordinate of the point
850 */
851 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700852 final int hStartPadding = getPaddingLeft();
853 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854
855 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
856 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
857 }
858
Adam Cohene3e27a82011-04-15 12:07:39 -0700859 /**
860 * Given a cell coordinate, return the point that represents the upper left corner of that cell
861 *
862 * @param cellX X coordinate of the cell
863 * @param cellY Y coordinate of the cell
864 *
865 * @param result Array of 2 ints to hold the x and y coordinate of the point
866 */
867 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700868 final int hStartPadding = getPaddingLeft();
869 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700870
871 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
872 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
873 }
874
Romain Guy84f296c2009-11-04 15:00:44 -0800875 int getCellWidth() {
876 return mCellWidth;
877 }
878
879 int getCellHeight() {
880 return mCellHeight;
881 }
882
Adam Cohend4844c32011-02-18 19:25:06 -0800883 int getWidthGap() {
884 return mWidthGap;
885 }
886
887 int getHeightGap() {
888 return mHeightGap;
889 }
890
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700891 Rect getContentRect(Rect r) {
892 if (r == null) {
893 r = new Rect();
894 }
895 int left = getPaddingLeft();
896 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700897 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
898 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700899 r.set(left, top, right, bottom);
900 return r;
901 }
902
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 @Override
904 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
905 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700906
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700908 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
909
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
911 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700912
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
914 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
915 }
916
Adam Cohend22015c2010-07-26 22:02:18 -0700917 int numWidthGaps = mCountX - 1;
918 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919
Adam Cohen234c4cd2011-07-17 21:03:04 -0700920 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700921 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
922 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
923 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
924 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
925 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
926 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700927 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700928 } else {
929 mWidthGap = mOriginalWidthGap;
930 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700931 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700932
Michael Jurka8c920dd2011-01-20 14:16:56 -0800933 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
934 int newWidth = widthSpecSize;
935 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700936 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700937 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700938 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700939 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700940 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700941 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700942 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800943
944 int count = getChildCount();
945 for (int i = 0; i < count; i++) {
946 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700947 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
948 mPaddingRight, MeasureSpec.EXACTLY);
949 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
950 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800951 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
952 }
953 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 }
955
956 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700957 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800960 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700961 child.layout(mPaddingLeft, mPaddingTop,
962 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 }
964 }
965
966 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700967 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
968 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700969 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700970 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
971 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -0700972 }
973
974 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800976 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 }
978
979 @Override
980 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800981 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 }
983
Michael Jurka5f1c5092010-09-03 14:15:02 -0700984 public float getBackgroundAlpha() {
985 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700986 }
987
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700988 public void setBackgroundAlphaMultiplier(float multiplier) {
989 mBackgroundAlphaMultiplier = multiplier;
990 }
991
Adam Cohenddb82192010-11-10 16:32:54 -0800992 public float getBackgroundAlphaMultiplier() {
993 return mBackgroundAlphaMultiplier;
994 }
995
Michael Jurka5f1c5092010-09-03 14:15:02 -0700996 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800997 if (mBackgroundAlpha != alpha) {
998 mBackgroundAlpha = alpha;
999 invalidate();
1000 }
Michael Jurkadee05892010-07-27 10:01:56 -07001001 }
1002
Michael Jurka5f1c5092010-09-03 14:15:02 -07001003 // Need to return true to let the view system know we know how to handle alpha-- this is
1004 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
1005 // versions
1006 @Override
1007 protected boolean onSetAlpha(int alpha) {
1008 return true;
1009 }
1010
Michael Jurkaafaa0502011-12-13 18:22:50 -08001011 @Override
Michael Jurka5f1c5092010-09-03 14:15:02 -07001012 public void setAlpha(float alpha) {
1013 setChildrenAlpha(alpha);
1014 super.setAlpha(alpha);
1015 }
1016
Michael Jurkadee05892010-07-27 10:01:56 -07001017 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001018 final int childCount = getChildCount();
1019 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001020 getChildAt(i).setAlpha(alpha);
1021 }
1022 }
1023
Patrick Dubroy440c3602010-07-13 17:50:32 -07001024 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001025 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001026 }
1027
Adam Cohen76fc0852011-06-17 13:26:23 -07001028 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
1029 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001030 CellLayoutChildren clc = getChildrenLayout();
1031 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
1032 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1033 final ItemInfo info = (ItemInfo) child.getTag();
1034
1035 // We cancel any existing animations
1036 if (mReorderAnimators.containsKey(lp)) {
1037 mReorderAnimators.get(lp).cancel();
1038 mReorderAnimators.remove(lp);
1039 }
1040
1041 int oldX = lp.x;
1042 int oldY = lp.y;
1043 mOccupied[lp.cellX][lp.cellY] = false;
1044 mOccupied[cellX][cellY] = true;
1045
1046 lp.isLockedToGrid = true;
1047 lp.cellX = info.cellX = cellX;
1048 lp.cellY = info.cellY = cellY;
1049 clc.setupLp(lp);
1050 lp.isLockedToGrid = false;
1051 int newX = lp.x;
1052 int newY = lp.y;
1053
Adam Cohen76fc0852011-06-17 13:26:23 -07001054 lp.x = oldX;
1055 lp.y = oldY;
1056 child.requestLayout();
1057
Adam Cohenbfbfd262011-06-13 16:55:12 -07001058 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
1059 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
1060 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
1061 oa.setDuration(duration);
1062 mReorderAnimators.put(lp, oa);
1063 oa.addUpdateListener(new AnimatorUpdateListener() {
1064 public void onAnimationUpdate(ValueAnimator animation) {
1065 child.requestLayout();
1066 }
1067 });
1068 oa.addListener(new AnimatorListenerAdapter() {
1069 boolean cancelled = false;
1070 public void onAnimationEnd(Animator animation) {
1071 // If the animation was cancelled, it means that another animation
1072 // has interrupted this one, and we don't want to lock the item into
1073 // place just yet.
1074 if (!cancelled) {
1075 lp.isLockedToGrid = true;
1076 }
1077 if (mReorderAnimators.containsKey(lp)) {
1078 mReorderAnimators.remove(lp);
1079 }
1080 }
1081 public void onAnimationCancel(Animator animation) {
1082 cancelled = true;
1083 }
1084 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001085 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001086 oa.start();
1087 return true;
1088 }
1089 return false;
1090 }
1091
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001092 /**
1093 * Estimate where the top left cell of the dragged item will land if it is dropped.
1094 *
1095 * @param originX The X value of the top left corner of the item
1096 * @param originY The Y value of the top left corner of the item
1097 * @param spanX The number of horizontal cells that the item spans
1098 * @param spanY The number of vertical cells that the item spans
1099 * @param result The estimated drop cell X and Y.
1100 */
1101 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001102 final int countX = mCountX;
1103 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001104
Michael Jurkaa63c4522010-08-19 13:52:27 -07001105 // pointToCellRounded takes the top left of a cell but will pad that with
1106 // cellWidth/2 and cellHeight/2 when finding the matching cell
1107 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001108
1109 // If the item isn't fully on this screen, snap to the edges
1110 int rightOverhang = result[0] + spanX - countX;
1111 if (rightOverhang > 0) {
1112 result[0] -= rightOverhang; // Snap to right
1113 }
1114 result[0] = Math.max(0, result[0]); // Snap to left
1115 int bottomOverhang = result[1] + spanY - countY;
1116 if (bottomOverhang > 0) {
1117 result[1] -= bottomOverhang; // Snap to bottom
1118 }
1119 result[1] = Math.max(0, result[1]); // Snap to top
1120 }
1121
Winson Chungb8c69f32011-10-19 21:36:08 -07001122 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY,
Adam Cohend41fbf52012-02-16 23:53:59 -08001123 int minSpanX, int minSpanY, int spanX, int spanY, Point dragOffset, Rect dragRegion) {
Joe Onorato4be866d2010-10-10 11:26:02 -07001124
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001125 final int oldDragCellX = mDragCell[0];
1126 final int oldDragCellY = mDragCell[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001127 int[] resultSpan = new int[2];
1128 final int[] nearest = findNearestVacantArea(originX, originY, minSpanX, minSpanY,
1129 spanX, spanY, v, mDragCell, resultSpan);
1130 boolean resize = spanX > resultSpan[0] || spanY > resultSpan[1];
1131 spanX = resultSpan[0];
1132 spanY = resultSpan[1];
Winson Chungb8c69f32011-10-19 21:36:08 -07001133 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001134 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1135 } else {
1136 mDragCenter.set(originX, originY);
1137 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001138
Adam Cohen2801caf2011-05-13 20:57:39 -07001139 if (dragOutline == null && v == null) {
1140 if (mCrosshairsDrawable != null) {
1141 invalidate();
1142 }
1143 return;
1144 }
1145
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001146 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001147 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001148 final int[] topLeft = mTmpPoint;
1149 cellToPoint(nearest[0], nearest[1], topLeft);
1150
Joe Onorato4be866d2010-10-10 11:26:02 -07001151 int left = topLeft[0];
1152 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001153
Winson Chungb8c69f32011-10-19 21:36:08 -07001154 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001155 // When drawing the drag outline, it did not account for margin offsets
1156 // added by the view's parent.
1157 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1158 left += lp.leftMargin;
1159 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001160
Adam Cohen99e8b402011-03-25 19:23:43 -07001161 // Offsets due to the size difference between the View and the dragOutline.
1162 // There is a size difference to account for the outer blur, which may lie
1163 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001164 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001165 // We center about the x axis
1166 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1167 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001168 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001169 if (dragOffset != null && dragRegion != null) {
1170 // Center the drag region *horizontally* in the cell and apply a drag
1171 // outline offset
1172 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1173 - dragRegion.width()) / 2;
1174 top += dragOffset.y;
1175 } else {
1176 // Center the drag outline in the cell
1177 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1178 - dragOutline.getWidth()) / 2;
1179 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1180 - dragOutline.getHeight()) / 2;
1181 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001182 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001183 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001184 mDragOutlineAnims[oldIndex].animateOut();
1185 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001186 Rect r = mDragOutlines[mDragOutlineCurrent];
1187 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1188 if (resize) {
1189 cellToRect(nearest[0], nearest[1], spanX, spanY, r);
1190 }
Winson Chung150fbab2010-09-29 17:14:26 -07001191
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001192 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1193 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001194 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001195
1196 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1197 if (mCrosshairsDrawable != null) {
1198 invalidate();
1199 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001200 }
1201
Adam Cohene0310962011-04-18 16:15:31 -07001202 public void clearDragOutlines() {
1203 final int oldIndex = mDragOutlineCurrent;
1204 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001205 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001206 }
1207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001209 * Find a vacant area that will fit the given bounds nearest the requested
1210 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001211 *
Romain Guy51afc022009-05-04 18:03:43 -07001212 * @param pixelX The X location at which you want to search for a vacant area.
1213 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001214 * @param spanX Horizontal span of the object.
1215 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001216 * @param result Array in which to place the result, or null (in which case a new array will
1217 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001218 * @return The X, Y cell of a vacant area that can contain this object,
1219 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001221 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1222 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001223 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001224 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001225
Michael Jurka6a1435d2010-09-27 17:35:12 -07001226 /**
1227 * Find a vacant area that will fit the given bounds nearest the requested
1228 * cell location. Uses Euclidean distance to score multiple vacant areas.
1229 *
1230 * @param pixelX The X location at which you want to search for a vacant area.
1231 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001232 * @param minSpanX The minimum horizontal span required
1233 * @param minSpanY The minimum vertical span required
1234 * @param spanX Horizontal span of the object.
1235 * @param spanY Vertical span of the object.
1236 * @param result Array in which to place the result, or null (in which case a new array will
1237 * be allocated)
1238 * @return The X, Y cell of a vacant area that can contain this object,
1239 * nearest the requested location.
1240 */
1241 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1242 int spanY, int[] result, int[] resultSpan) {
1243 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1244 result, resultSpan);
1245 }
1246
1247 /**
1248 * Find a vacant area that will fit the given bounds nearest the requested
1249 * cell location. Uses Euclidean distance to score multiple vacant areas.
1250 *
1251 * @param pixelX The X location at which you want to search for a vacant area.
1252 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001253 * @param spanX Horizontal span of the object.
1254 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001255 * @param ignoreOccupied If true, the result can be an occupied cell
1256 * @param result Array in which to place the result, or null (in which case a new array will
1257 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001258 * @return The X, Y cell of a vacant area that can contain this object,
1259 * nearest the requested location.
1260 */
Adam Cohendf035382011-04-11 17:22:04 -07001261 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1262 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001263 return findNearestArea(pixelX, pixelY, spanX, spanY,
1264 spanX, spanY, ignoreView, ignoreOccupied, result, null);
1265 }
1266
1267 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1268 private void lazyInitTempRectStack() {
1269 if (mTempRectStack.isEmpty()) {
1270 for (int i = 0; i < mCountX * mCountY; i++) {
1271 mTempRectStack.push(new Rect());
1272 }
1273 }
1274 }
1275 private void recycleTempRects(Stack<Rect> used) {
1276 while (!used.isEmpty()) {
1277 mTempRectStack.push(used.pop());
1278 }
1279 }
1280
1281 /**
1282 * Find a vacant area that will fit the given bounds nearest the requested
1283 * cell location. Uses Euclidean distance to score multiple vacant areas.
1284 *
1285 * @param pixelX The X location at which you want to search for a vacant area.
1286 * @param pixelY The Y location at which you want to search for a vacant area.
1287 * @param minSpanX The minimum horizontal span required
1288 * @param minSpanY The minimum vertical span required
1289 * @param spanX Horizontal span of the object.
1290 * @param spanY Vertical span of the object.
1291 * @param ignoreOccupied If true, the result can be an occupied cell
1292 * @param result Array in which to place the result, or null (in which case a new array will
1293 * be allocated)
1294 * @return The X, Y cell of a vacant area that can contain this object,
1295 * nearest the requested location.
1296 */
1297 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
1298 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan) {
1299 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001300 // mark space take by ignoreView as available (method checks if ignoreView is null)
1301 markCellsAsUnoccupiedForView(ignoreView);
1302
Adam Cohene3e27a82011-04-15 12:07:39 -07001303 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1304 // to the center of the item, but we are searching based on the top-left cell, so
1305 // we translate the point over to correspond to the top-left.
1306 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1307 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1308
Jeff Sharkey70864282009-04-07 21:08:40 -07001309 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001310 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001311 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001312 final Rect bestRect = new Rect(-1, -1, -1, -1);
1313 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001314
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001315 final int countX = mCountX;
1316 final int countY = mCountY;
1317 final boolean[][] occupied = mOccupied;
1318
Adam Cohend41fbf52012-02-16 23:53:59 -08001319 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1320 spanX < minSpanX || spanY < minSpanY) {
1321 return bestXY;
1322 }
1323
1324 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001325 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001326 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1327 int ySize = -1;
1328 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001329 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001330 // First, let's see if this thing fits anywhere
1331 for (int i = 0; i < minSpanX; i++) {
1332 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001333 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001334 continue inner;
1335 }
Michael Jurkac28de512010-08-13 11:27:44 -07001336 }
1337 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001338 xSize = minSpanX;
1339 ySize = minSpanY;
1340
1341 // We know that the item will fit at _some_ acceptable size, now let's see
1342 // how big we can make it. We'll alternate between incrementing x and y spans
1343 // until we hit a limit.
1344 boolean incX = true;
1345 boolean hitMaxX = xSize >= spanX;
1346 boolean hitMaxY = ySize >= spanY;
1347 while (!(hitMaxX && hitMaxY)) {
1348 if (incX && !hitMaxX) {
1349 for (int j = 0; j < ySize; j++) {
1350 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1351 // We can't move out horizontally
1352 hitMaxX = true;
1353 }
1354 }
1355 if (!hitMaxX) {
1356 xSize++;
1357 }
1358 } else if (!hitMaxY) {
1359 for (int i = 0; i < xSize; i++) {
1360 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1361 // We can't move out vertically
1362 hitMaxY = true;
1363 }
1364 }
1365 if (!hitMaxY) {
1366 ySize++;
1367 }
1368 }
1369 hitMaxX |= xSize >= spanX;
1370 hitMaxY |= ySize >= spanY;
1371 incX = !incX;
1372 }
1373 incX = true;
1374 hitMaxX = xSize >= spanX;
1375 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001376 }
Winson Chung0be025d2011-05-23 17:45:09 -07001377 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001378 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379
Adam Cohend41fbf52012-02-16 23:53:59 -08001380 // We verify that the current rect is not a sub-rect of any of our previous
1381 // candidates. In this case, the current rect is disqualified in favour of the
1382 // containing rect.
1383 Rect currentRect = mTempRectStack.pop();
1384 currentRect.set(x, y, x + xSize, y + ySize);
1385 boolean contained = false;
1386 for (Rect r : validRegions) {
1387 if (r.contains(currentRect)) {
1388 contained = true;
1389 break;
1390 }
1391 }
1392 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001393 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1394 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohend41fbf52012-02-16 23:53:59 -08001395 if ((distance <= bestDistance && !contained) ||
1396 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001397 bestDistance = distance;
1398 bestXY[0] = x;
1399 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001400 if (resultSpan != null) {
1401 resultSpan[0] = xSize;
1402 resultSpan[1] = ySize;
1403 }
1404 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 }
1407 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001408 // re-mark space taken by ignoreView as occupied
1409 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410
Adam Cohenc0dcf592011-06-01 15:30:43 -07001411 // Return -1, -1 if no suitable location found
1412 if (bestDistance == Double.MAX_VALUE) {
1413 bestXY[0] = -1;
1414 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001415 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001416 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001417 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001419
Adam Cohendf035382011-04-11 17:22:04 -07001420 /**
1421 * Find a vacant area that will fit the given bounds nearest the requested
1422 * cell location. Uses Euclidean distance to score multiple vacant areas.
1423 *
1424 * @param pixelX The X location at which you want to search for a vacant area.
1425 * @param pixelY The Y location at which you want to search for a vacant area.
1426 * @param spanX Horizontal span of the object.
1427 * @param spanY Vertical span of the object.
1428 * @param ignoreView Considers space occupied by this view as unoccupied
1429 * @param result Previously returned value to possibly recycle.
1430 * @return The X, Y cell of a vacant area that can contain this object,
1431 * nearest the requested location.
1432 */
1433 int[] findNearestVacantArea(
1434 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1435 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1436 }
1437
1438 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08001439 * Find a vacant area that will fit the given bounds nearest the requested
1440 * cell location. Uses Euclidean distance to score multiple vacant areas.
1441 *
1442 * @param pixelX The X location at which you want to search for a vacant area.
1443 * @param pixelY The Y location at which you want to search for a vacant area.
1444 * @param minSpanX The minimum horizontal span required
1445 * @param minSpanY The minimum vertical span required
1446 * @param spanX Horizontal span of the object.
1447 * @param spanY Vertical span of the object.
1448 * @param ignoreView Considers space occupied by this view as unoccupied
1449 * @param result Previously returned value to possibly recycle.
1450 * @return The X, Y cell of a vacant area that can contain this object,
1451 * nearest the requested location.
1452 */
1453 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
1454 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
1455 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY,
1456 spanX, spanY, ignoreView, true, result, resultSpan);
1457 }
1458
1459 /**
Adam Cohendf035382011-04-11 17:22:04 -07001460 * Find a starting cell position that will fit the given bounds nearest the requested
1461 * cell location. Uses Euclidean distance to score multiple vacant areas.
1462 *
1463 * @param pixelX The X location at which you want to search for a vacant area.
1464 * @param pixelY The Y location at which you want to search for a vacant area.
1465 * @param spanX Horizontal span of the object.
1466 * @param spanY Vertical span of the object.
1467 * @param ignoreView Considers space occupied by this view as unoccupied
1468 * @param result Previously returned value to possibly recycle.
1469 * @return The X, Y cell of a vacant area that can contain this object,
1470 * nearest the requested location.
1471 */
1472 int[] findNearestArea(
1473 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1474 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1475 }
1476
Michael Jurka0280c3b2010-09-17 15:00:07 -07001477 boolean existsEmptyCell() {
1478 return findCellForSpan(null, 1, 1);
1479 }
1480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001482 * Finds the upper-left coordinate of the first rectangle in the grid that can
1483 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1484 * then this method will only return coordinates for rectangles that contain the cell
1485 * (intersectX, intersectY)
1486 *
1487 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1488 * can be found.
1489 * @param spanX The horizontal span of the cell we want to find.
1490 * @param spanY The vertical span of the cell we want to find.
1491 *
1492 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001493 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001494 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1495 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1496 }
1497
1498 /**
1499 * Like above, but ignores any cells occupied by the item "ignoreView"
1500 *
1501 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1502 * can be found.
1503 * @param spanX The horizontal span of the cell we want to find.
1504 * @param spanY The vertical span of the cell we want to find.
1505 * @param ignoreView The home screen item we should treat as not occupying any space
1506 * @return
1507 */
1508 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1509 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1510 }
1511
1512 /**
1513 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1514 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1515 *
1516 * @param spanX The horizontal span of the cell we want to find.
1517 * @param spanY The vertical span of the cell we want to find.
1518 * @param ignoreView The home screen item we should treat as not occupying any space
1519 * @param intersectX The X coordinate of the cell that we should try to overlap
1520 * @param intersectX The Y coordinate of the cell that we should try to overlap
1521 *
1522 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1523 */
1524 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1525 int intersectX, int intersectY) {
1526 return findCellForSpanThatIntersectsIgnoring(
1527 cellXY, spanX, spanY, intersectX, intersectY, null);
1528 }
1529
1530 /**
1531 * The superset of the above two methods
1532 */
1533 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1534 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001535 // mark space take by ignoreView as available (method checks if ignoreView is null)
1536 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001537
Michael Jurka28750fb2010-09-24 17:43:49 -07001538 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001539 while (true) {
1540 int startX = 0;
1541 if (intersectX >= 0) {
1542 startX = Math.max(startX, intersectX - (spanX - 1));
1543 }
1544 int endX = mCountX - (spanX - 1);
1545 if (intersectX >= 0) {
1546 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1547 }
1548 int startY = 0;
1549 if (intersectY >= 0) {
1550 startY = Math.max(startY, intersectY - (spanY - 1));
1551 }
1552 int endY = mCountY - (spanY - 1);
1553 if (intersectY >= 0) {
1554 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1555 }
1556
Winson Chungbbc60d82010-11-11 16:34:41 -08001557 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001558 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001559 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001560 for (int i = 0; i < spanX; i++) {
1561 for (int j = 0; j < spanY; j++) {
1562 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001563 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001564 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001565 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001566 continue inner;
1567 }
1568 }
1569 }
1570 if (cellXY != null) {
1571 cellXY[0] = x;
1572 cellXY[1] = y;
1573 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001574 foundCell = true;
1575 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001576 }
1577 }
1578 if (intersectX == -1 && intersectY == -1) {
1579 break;
1580 } else {
1581 // if we failed to find anything, try again but without any requirements of
1582 // intersecting
1583 intersectX = -1;
1584 intersectY = -1;
1585 continue;
1586 }
1587 }
1588
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001589 // re-mark space taken by ignoreView as occupied
1590 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001591 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001592 }
1593
1594 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001595 * A drag event has begun over this layout.
1596 * It may have begun over this layout (in which case onDragChild is called first),
1597 * or it may have begun on another layout.
1598 */
1599 void onDragEnter() {
1600 if (!mDragging) {
1601 // Fade in the drag indicators
1602 if (mCrosshairsAnimator != null) {
1603 mCrosshairsAnimator.animateIn();
1604 }
1605 }
1606 mDragging = true;
1607 }
1608
1609 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001610 * Called when drag has left this CellLayout or has been completed (successfully or not)
1611 */
1612 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001613 // This can actually be called when we aren't in a drag, e.g. when adding a new
1614 // item to this layout via the customize drawer.
1615 // Guard against that case.
1616 if (mDragging) {
1617 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001618
Joe Onorato4be866d2010-10-10 11:26:02 -07001619 // Fade out the drag indicators
1620 if (mCrosshairsAnimator != null) {
1621 mCrosshairsAnimator.animateOut();
1622 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001623 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001624
1625 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08001626 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001627 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1628 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1629
Michael Jurka33945b22010-12-21 18:19:38 -08001630 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001631 }
1632
1633 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001634 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001635 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001636 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 *
1638 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001640 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001641 if (child != null) {
1642 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001643 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001644 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001645 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 }
1647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001650 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 * @param cellX X coordinate of upper left corner expressed as a cell position
1652 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001653 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001655 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001657 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 final int cellWidth = mCellWidth;
1659 final int cellHeight = mCellHeight;
1660 final int widthGap = mWidthGap;
1661 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001662
Winson Chung4b825dcd2011-06-19 12:41:22 -07001663 final int hStartPadding = getPaddingLeft();
1664 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1667 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1668
1669 int x = hStartPadding + cellX * (cellWidth + widthGap);
1670 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001671
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001672 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001674
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001676 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001678 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 * @param width Width in pixels
1680 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001681 * @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 -08001682 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001683 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001684 return rectToCell(getResources(), width, height, result);
1685 }
1686
1687 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 // Always assume we're working with the smallest span to make sure we
1689 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001690 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1691 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001693
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07001695 int spanX = (int) Math.ceil(width / (float) smallerSize);
1696 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04001697
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001698 if (result == null) {
1699 return new int[] { spanX, spanY };
1700 }
1701 result[0] = spanX;
1702 result[1] = spanY;
1703 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 }
1705
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001706 public int[] cellSpansToSize(int hSpans, int vSpans) {
1707 int[] size = new int[2];
1708 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1709 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1710 return size;
1711 }
1712
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001714 * Calculate the grid spans needed to fit given item
1715 */
1716 public void calculateSpans(ItemInfo info) {
1717 final int minWidth;
1718 final int minHeight;
1719
1720 if (info instanceof LauncherAppWidgetInfo) {
1721 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1722 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1723 } else if (info instanceof PendingAddWidgetInfo) {
1724 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1725 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1726 } else {
1727 // It's not a widget, so it must be 1x1
1728 info.spanX = info.spanY = 1;
1729 return;
1730 }
1731 int[] spans = rectToCell(minWidth, minHeight, null);
1732 info.spanX = spans[0];
1733 info.spanY = spans[1];
1734 }
1735
1736 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 * Find the first vacant cell, if there is one.
1738 *
1739 * @param vacant Holds the x and y coordinate of the vacant cell
1740 * @param spanX Horizontal cell span.
1741 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001742 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 * @return True if a vacant cell was found
1744 */
1745 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746
Michael Jurka0280c3b2010-09-17 15:00:07 -07001747 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 }
1749
1750 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1751 int xCount, int yCount, boolean[][] occupied) {
1752
Adam Cohen2801caf2011-05-13 20:57:39 -07001753 for (int y = 0; y < yCount; y++) {
1754 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 boolean available = !occupied[x][y];
1756out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1757 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1758 available = available && !occupied[i][j];
1759 if (!available) break out;
1760 }
1761 }
1762
1763 if (available) {
1764 vacant[0] = x;
1765 vacant[1] = y;
1766 return true;
1767 }
1768 }
1769 }
1770
1771 return false;
1772 }
1773
Michael Jurka0280c3b2010-09-17 15:00:07 -07001774 private void clearOccupiedCells() {
1775 for (int x = 0; x < mCountX; x++) {
1776 for (int y = 0; y < mCountY; y++) {
1777 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 }
1779 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001780 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781
Adam Cohen1b607ed2011-03-03 17:26:50 -08001782 /**
1783 * Given a view, determines how much that view can be expanded in all directions, in terms of
1784 * whether or not there are other items occupying adjacent cells. Used by the
1785 * AppWidgetResizeFrame to determine how the widget can be resized.
1786 */
Adam Cohend4844c32011-02-18 19:25:06 -08001787 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001788 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001789 boolean flag;
1790
Adam Cohen1b607ed2011-03-03 17:26:50 -08001791 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001792 for (int x = lp.cellX - 1; x >= 0; x--) {
1793 flag = false;
1794 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1795 if (mOccupied[x][y]) flag = true;
1796 }
1797 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001798 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001799 }
1800
Adam Cohen1b607ed2011-03-03 17:26:50 -08001801 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001802 for (int y = lp.cellY - 1; y >= 0; y--) {
1803 flag = false;
1804 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1805 if (mOccupied[x][y]) flag = true;
1806 }
1807 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001808 expandability[AppWidgetResizeFrame.TOP]++;
1809 }
Adam Cohend4844c32011-02-18 19:25:06 -08001810
Adam Cohen1b607ed2011-03-03 17:26:50 -08001811 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001812 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1813 flag = false;
1814 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1815 if (mOccupied[x][y]) flag = true;
1816 }
1817 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001818 expandability[AppWidgetResizeFrame.RIGHT]++;
1819 }
Adam Cohend4844c32011-02-18 19:25:06 -08001820
Adam Cohen1b607ed2011-03-03 17:26:50 -08001821 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001822 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1823 flag = false;
1824 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1825 if (mOccupied[x][y]) flag = true;
1826 }
1827 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001828 expandability[AppWidgetResizeFrame.BOTTOM]++;
1829 }
Adam Cohend4844c32011-02-18 19:25:06 -08001830 }
1831
Adam Cohend41fbf52012-02-16 23:53:59 -08001832 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001833 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1834 markCellsAsUnoccupiedForView(view);
Adam Cohend41fbf52012-02-16 23:53:59 -08001835 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001836 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837
Adam Cohend4844c32011-02-18 19:25:06 -08001838 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001839 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001840 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1841 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1842 }
1843
Adam Cohend4844c32011-02-18 19:25:06 -08001844 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001845 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001846 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1847 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1848 }
1849
1850 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1851 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1852 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1853 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 }
1855 }
1856 }
1857
Adam Cohen2801caf2011-05-13 20:57:39 -07001858 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001859 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001860 (Math.max((mCountX - 1), 0) * mWidthGap);
1861 }
1862
1863 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001864 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001865 (Math.max((mCountY - 1), 0) * mHeightGap);
1866 }
1867
Michael Jurka66d72172011-04-12 16:29:25 -07001868 public boolean isOccupied(int x, int y) {
1869 if (x < mCountX && y < mCountY) {
1870 return mOccupied[x][y];
1871 } else {
1872 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1873 }
1874 }
1875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 @Override
1877 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1878 return new CellLayout.LayoutParams(getContext(), attrs);
1879 }
1880
1881 @Override
1882 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1883 return p instanceof CellLayout.LayoutParams;
1884 }
1885
1886 @Override
1887 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1888 return new CellLayout.LayoutParams(p);
1889 }
1890
Winson Chungaafa03c2010-06-11 17:34:16 -07001891 public static class CellLayoutAnimationController extends LayoutAnimationController {
1892 public CellLayoutAnimationController(Animation animation, float delay) {
1893 super(animation, delay);
1894 }
1895
1896 @Override
1897 protected long getDelayForView(View view) {
1898 return (int) (Math.random() * 150);
1899 }
1900 }
1901
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1903 /**
1904 * Horizontal location of the item in the grid.
1905 */
1906 @ViewDebug.ExportedProperty
1907 public int cellX;
1908
1909 /**
1910 * Vertical location of the item in the grid.
1911 */
1912 @ViewDebug.ExportedProperty
1913 public int cellY;
1914
1915 /**
1916 * Number of cells spanned horizontally by the item.
1917 */
1918 @ViewDebug.ExportedProperty
1919 public int cellHSpan;
1920
1921 /**
1922 * Number of cells spanned vertically by the item.
1923 */
1924 @ViewDebug.ExportedProperty
1925 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001926
Adam Cohen1b607ed2011-03-03 17:26:50 -08001927 /**
1928 * Indicates whether the item will set its x, y, width and height parameters freely,
1929 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1930 */
Adam Cohend4844c32011-02-18 19:25:06 -08001931 public boolean isLockedToGrid = true;
1932
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 // X coordinate of the view in the layout.
1934 @ViewDebug.ExportedProperty
1935 int x;
1936 // Y coordinate of the view in the layout.
1937 @ViewDebug.ExportedProperty
1938 int y;
1939
Romain Guy84f296c2009-11-04 15:00:44 -08001940 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001941
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 public LayoutParams(Context c, AttributeSet attrs) {
1943 super(c, attrs);
1944 cellHSpan = 1;
1945 cellVSpan = 1;
1946 }
1947
1948 public LayoutParams(ViewGroup.LayoutParams source) {
1949 super(source);
1950 cellHSpan = 1;
1951 cellVSpan = 1;
1952 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001953
1954 public LayoutParams(LayoutParams source) {
1955 super(source);
1956 this.cellX = source.cellX;
1957 this.cellY = source.cellY;
1958 this.cellHSpan = source.cellHSpan;
1959 this.cellVSpan = source.cellVSpan;
1960 }
1961
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001963 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 this.cellX = cellX;
1965 this.cellY = cellY;
1966 this.cellHSpan = cellHSpan;
1967 this.cellVSpan = cellVSpan;
1968 }
1969
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001970 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001971 if (isLockedToGrid) {
1972 final int myCellHSpan = cellHSpan;
1973 final int myCellVSpan = cellVSpan;
1974 final int myCellX = cellX;
1975 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001976
Adam Cohend4844c32011-02-18 19:25:06 -08001977 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1978 leftMargin - rightMargin;
1979 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1980 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001981 x = myCellX * (cellWidth + widthGap) + leftMargin;
1982 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001983 }
1984 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001985
Winson Chungaafa03c2010-06-11 17:34:16 -07001986 public String toString() {
1987 return "(" + this.cellX + ", " + this.cellY + ")";
1988 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001989
1990 public void setWidth(int width) {
1991 this.width = width;
1992 }
1993
1994 public int getWidth() {
1995 return width;
1996 }
1997
1998 public void setHeight(int height) {
1999 this.height = height;
2000 }
2001
2002 public int getHeight() {
2003 return height;
2004 }
2005
2006 public void setX(int x) {
2007 this.x = x;
2008 }
2009
2010 public int getX() {
2011 return x;
2012 }
2013
2014 public void setY(int y) {
2015 this.y = y;
2016 }
2017
2018 public int getY() {
2019 return y;
2020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 }
2022
Michael Jurka0280c3b2010-09-17 15:00:07 -07002023 // This class stores info for two purposes:
2024 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2025 // its spanX, spanY, and the screen it is on
2026 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2027 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2028 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002029 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002031 int cellX = -1;
2032 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 int spanX;
2034 int spanY;
2035 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002036 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 @Override
2039 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002040 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2041 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 }
2043 }
Michael Jurkad771c962011-08-09 15:00:48 -07002044
2045 public boolean lastDownOnOccupiedCell() {
2046 return mLastDownOnOccupiedCell;
2047 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048}