blob: 1841713f536c29015519eef60311fa01614997b6 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Michael Jurka18014792010-10-14 09:01:34 -070021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070023import android.animation.PropertyValuesHolder;
Chet Haase00397b12010-10-07 11:13:10 -070024import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070025import android.animation.ValueAnimator;
26import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040028import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070031import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070033import android.graphics.Point;
34import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Rect;
36import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070037import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070038import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.MotionEvent;
42import android.view.View;
43import android.view.ViewDebug;
44import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070046import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Adam Cohen66396872011-04-15 17:50:36 -070049import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070050import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070051
Adam Cohen69ce2e52011-07-03 19:25:21 -070052import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070054import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055
Michael Jurkabdb5c532011-02-01 15:05:06 -080056public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070057 static final String TAG = "CellLayout";
58
Winson Chung4b825dcd2011-06-19 12:41:22 -070059 private int mOriginalCellWidth;
60 private int mOriginalCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 private int mCellWidth;
62 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070063
Adam Cohend22015c2010-07-26 22:02:18 -070064 private int mCountX;
65 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
Adam Cohen234c4cd2011-07-17 21:03:04 -070067 private int mOriginalWidthGap;
68 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 private int mWidthGap;
70 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070071 private int mMaxGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
73 private final Rect mRect = new Rect();
74 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070075
Patrick Dubroyde7658b2010-09-27 11:15:43 -070076 // These are temporary variables to prevent having to allocate a new object just to
77 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070078 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070079 private final int[] mTmpPoint = new int[2];
80 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070081 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 boolean[][] mOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070084 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085
Michael Jurkadee05892010-07-27 10:01:56 -070086 private OnTouchListener mInterceptTouchListener;
87
Adam Cohen69ce2e52011-07-03 19:25:21 -070088 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070089 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070090
Michael Jurka5f1c5092010-09-03 14:15:02 -070091 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070092 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070093
Michael Jurka33945b22010-12-21 18:19:38 -080094 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080095 private Drawable mActiveBackground;
96 private Drawable mActiveGlowBackground;
97 private Drawable mNormalBackgroundMini;
98 private Drawable mNormalGlowBackgroundMini;
99 private Drawable mActiveBackgroundMini;
100 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -0700101 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -0800102 private Rect mGlowBackgroundRect;
103 private float mGlowBackgroundScale;
104 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700105
Adam Cohendf035382011-04-11 17:22:04 -0700106 private boolean mAcceptsDrops = true;
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;
109 private boolean mIsDragOccuring = false;
110 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700111 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700112
Winson Chung150fbab2010-09-29 17:14:26 -0700113 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700114 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Winson Chung63257c12011-05-05 17:06:13 -0700115 private Point[] mDragOutlines = new Point[4];
Chet Haase472b2812010-10-14 07:02:04 -0700116 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700117 private InterruptibleInOutAnimator[] mDragOutlineAnims =
118 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700119
120 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700121 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700122 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700123
Patrick Dubroy96864c32011-03-10 17:17:23 -0800124 private BubbleTextView mPressedOrFocusedIcon;
125
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700126 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700127 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700128 private float mCrosshairsVisibility = 0.0f;
129
Adam Cohenbfbfd262011-06-13 16:55:12 -0700130 private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new
131 HashMap<CellLayout.LayoutParams, ObjectAnimator>();
132
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700133 // When a drag operation is in progress, holds the nearest cell to the touch point
134 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Joe Onorato4be866d2010-10-10 11:26:02 -0700136 private boolean mDragging = false;
137
Patrick Dubroyce34a972010-10-19 10:34:32 -0700138 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800139 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700140
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 public CellLayout(Context context) {
142 this(context, null);
143 }
144
145 public CellLayout(Context context, AttributeSet attrs) {
146 this(context, attrs, 0);
147 }
148
149 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
150 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700151
152 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
153 // the user where a dragged item will land when dropped.
154 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
157
Winson Chung4b825dcd2011-06-19 12:41:22 -0700158 mOriginalCellWidth =
159 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
160 mOriginalCellHeight =
161 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700162 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
163 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700164 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700165 mCountX = LauncherModel.getCellCountX();
166 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700167 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
169 a.recycle();
170
171 setAlwaysDrawnWithCacheEnabled(false);
172
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700173 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700174
Winson Chung967289b2011-06-30 18:09:30 -0700175 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
176 mActiveBackground = res.getDrawable(R.drawable.homescreen_green_normal_holo);
177 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_green_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800178
Winson Chungb26f3d62011-06-02 10:49:29 -0700179 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
180 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
181 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
182 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
Michael Jurka33945b22010-12-21 18:19:38 -0800183
Winson Chungb26f3d62011-06-02 10:49:29 -0700184 mNormalBackground.setFilterBitmap(true);
185 mActiveBackground.setFilterBitmap(true);
186 mActiveGlowBackground.setFilterBitmap(true);
187 mNormalBackgroundMini.setFilterBitmap(true);
188 mNormalGlowBackgroundMini.setFilterBitmap(true);
189 mActiveBackgroundMini.setFilterBitmap(true);
190 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700191
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700192 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700193
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700194 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700195 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700196
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700197 // Set up the animation for fading the crosshairs in and out
198 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700199 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700200 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700201 public void onAnimationUpdate(ValueAnimator animation) {
202 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700203 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700204 }
205 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700206 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700207
Joe Onorato4be866d2010-10-10 11:26:02 -0700208 for (int i = 0; i < mDragOutlines.length; i++) {
209 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700210 }
211
212 // When dragging things around the home screens, we show a green outline of
213 // where the item will land. The outlines gradually fade out, leaving a trail
214 // behind the drag path.
215 // Set up all the animations that are used to implement this fading.
216 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700217 final float fromAlphaValue = 0;
218 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700219
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700220 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700221
222 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700223 final InterruptibleInOutAnimator anim =
224 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700225 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700226 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700227 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700228 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700229 final Bitmap outline = (Bitmap)anim.getTag();
230
231 // If an animation is started and then stopped very quickly, we can still
232 // get spurious updates we've cleared the tag. Guard against this.
233 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700234 if (false) {
235 Object val = animation.getAnimatedValue();
236 Log.d(TAG, "anim " + thisIndex + " update: " + val +
237 ", isStopped " + anim.isStopped());
238 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700239 // Try to prevent it from continuing to run
240 animation.cancel();
241 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700242 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700243 final int left = mDragOutlines[thisIndex].x;
244 final int top = mDragOutlines[thisIndex].y;
245 CellLayout.this.invalidate(left, top,
246 left + outline.getWidth(), top + outline.getHeight());
247 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700248 }
249 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700250 // The animation holds a reference to the drag outline bitmap as long is it's
251 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700252 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700253 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700254 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700255 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700256 anim.setTag(null);
257 }
258 }
259 });
260 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700261 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700262
Michael Jurka18014792010-10-14 09:01:34 -0700263 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800264 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700265 setHoverScale(1.0f);
266 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800267
Michael Jurka8c920dd2011-01-20 14:16:56 -0800268 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700269 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800270 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700271 }
272
Michael Jurkaf6440da2011-04-05 14:50:34 -0700273 static int widthInPortrait(Resources r, int numCells) {
274 // We use this method from Workspace to figure out how many rows/columns Launcher should
275 // have. We ignore the left/right padding on CellLayout because it turns out in our design
276 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700277 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700278 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
279 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700280
Winson Chung4b825dcd2011-06-19 12:41:22 -0700281 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700282 }
283
Michael Jurkaf6440da2011-04-05 14:50:34 -0700284 static int heightInLandscape(Resources r, int numCells) {
285 // We use this method from Workspace to figure out how many rows/columns Launcher should
286 // have. We ignore the left/right padding on CellLayout because it turns out in our design
287 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700288 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700289 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
290 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700291
Winson Chung4b825dcd2011-06-19 12:41:22 -0700292 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700293 }
294
Michael Jurkab8d0e1c2011-08-11 18:05:24 -0700295 @Override
296 public void setChildrenLayersEnabled(boolean enabled) {
297 // see "Hardware Layer Note" lower in the code
298 if (LauncherApplication.isScreenLarge()) {
299 super.setChildrenLayersEnabled(enabled);
300 } else {
301 mChildren.setChildrenLayersEnabled(enabled);
302 }
303 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700304 public void enableHardwareLayers() {
Michael Jurkab8d0e1c2011-08-11 18:05:24 -0700305 // see "Hardware Layer Note" lower in the code
306 if (LauncherApplication.isScreenLarge()) {
307 mChildren.enableHardwareLayers();
308 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700309 }
310
311 public void setGridSize(int x, int y) {
312 mCountX = x;
313 mCountY = y;
314 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700315 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700316 }
317
Patrick Dubroy96864c32011-03-10 17:17:23 -0800318 private void invalidateBubbleTextView(BubbleTextView icon) {
319 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700320 invalidate(icon.getLeft() + getPaddingLeft() - padding,
321 icon.getTop() + getPaddingTop() - padding,
322 icon.getRight() + getPaddingLeft() + padding,
323 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800324 }
325
326 void setPressedOrFocusedIcon(BubbleTextView icon) {
327 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
328 // requires an expanded clip rect (due to the glow's blur radius)
329 BubbleTextView oldIcon = mPressedOrFocusedIcon;
330 mPressedOrFocusedIcon = icon;
331 if (oldIcon != null) {
332 invalidateBubbleTextView(oldIcon);
333 }
334 if (mPressedOrFocusedIcon != null) {
335 invalidateBubbleTextView(mPressedOrFocusedIcon);
336 }
337 }
338
Winson Chung6e314082011-01-27 16:46:51 -0800339 public CellLayoutChildren getChildrenLayout() {
340 if (getChildCount() > 0) {
341 return (CellLayoutChildren) getChildAt(0);
342 }
343 return null;
344 }
345
Michael Jurka33945b22010-12-21 18:19:38 -0800346 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
347 if (mIsDefaultDropTarget != isDefaultDropTarget) {
348 mIsDefaultDropTarget = isDefaultDropTarget;
349 invalidate();
350 }
351 }
352
Michael Jurka33945b22010-12-21 18:19:38 -0800353 void setIsDragOccuring(boolean isDragOccuring) {
354 if (mIsDragOccuring != isDragOccuring) {
355 mIsDragOccuring = isDragOccuring;
356 invalidate();
357 }
358 }
359
360 void setIsDragOverlapping(boolean isDragOverlapping) {
361 if (mIsDragOverlapping != isDragOverlapping) {
362 mIsDragOverlapping = isDragOverlapping;
363 invalidate();
364 }
365 }
366
367 boolean getIsDragOverlapping() {
368 return mIsDragOverlapping;
369 }
370
371 private void updateGlowRect() {
372 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700373 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
374 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800375 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700376 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
377 invalidate();
378 }
379
380 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800381 if (scaleFactor != mGlowBackgroundScale) {
382 mGlowBackgroundScale = scaleFactor;
383 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800384 if (getParent() != null) {
385 ((View) getParent()).invalidate();
386 }
Michael Jurka18014792010-10-14 09:01:34 -0700387 }
388 }
389
390 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800391 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700392 }
393
394 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800395 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700396 }
397
398 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800399 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700400 invalidate();
401 }
402
403 void animateDrop() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700404 Resources res = getResources();
405 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
406 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
407 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
408 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
409 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
410 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
Michael Jurka18014792010-10-14 09:01:34 -0700411
Winson Chungb26f3d62011-06-02 10:49:29 -0700412 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
413 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDuration));
Michael Jurka18014792010-10-14 09:01:34 -0700414
Winson Chungb26f3d62011-06-02 10:49:29 -0700415 AnimatorSet bouncer = new AnimatorSet();
416 bouncer.play(scaleUp).before(scaleDown);
417 bouncer.play(scaleUp).with(alphaFadeOut);
418 bouncer.addListener(new AnimatorListenerAdapter() {
419 @Override
420 public void onAnimationStart(Animator animation) {
421 setIsDragOverlapping(true);
422 }
423 @Override
424 public void onAnimationEnd(Animator animation) {
425 setIsDragOverlapping(false);
426 setHoverScale(1.0f);
427 setHoverAlpha(1.0f);
428 }
429 });
430 bouncer.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 }
432
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700433 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700434 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700435 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
436 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
437 // When we're small, we are either drawn normally or in the "accepts drops" state (during
438 // a drag). However, we also drag the mini hover background *over* one of those two
439 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700440 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700441 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800442 boolean mini = getScaleX() < 0.5f;
443
444 if (mIsDragOverlapping) {
445 // In the mini case, we draw the active_glow bg *over* the active background
446 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
447 } else if (mIsDragOccuring && mAcceptsDrops) {
448 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800449 } else if (mIsDefaultDropTarget && mini) {
450 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700451 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800452 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700453 }
Michael Jurka33945b22010-12-21 18:19:38 -0800454
455 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
456 bg.setBounds(mBackgroundRect);
457 bg.draw(canvas);
458
459 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700460 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800461 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700462 // If the hover background's scale is greater than 1, we'll be drawing outside
463 // the bounds of this CellLayout. Get around that by temporarily increasing the
464 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800465 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700466 Rect clipRect = canvas.getClipBounds();
467 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
468 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
469 canvas.save(Canvas.CLIP_SAVE_FLAG);
470 canvas.clipRect(-marginX, -marginY,
471 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
472 modifiedClipRect = true;
473 }
474
Michael Jurka33945b22010-12-21 18:19:38 -0800475 mActiveGlowBackgroundMini.setAlpha(
476 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
477 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
478 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700479 if (modifiedClipRect) {
480 canvas.restore();
481 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700482 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700483 }
Romain Guya6abce82009-11-10 02:54:41 -0800484
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700485 if (mCrosshairsVisibility > 0.0f) {
486 final int countX = mCountX;
487 final int countY = mCountY;
488
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700489 final float MAX_ALPHA = 0.4f;
490 final int MAX_VISIBLE_DISTANCE = 600;
491 final float DISTANCE_MULTIPLIER = 0.002f;
492
493 final Drawable d = mCrosshairsDrawable;
494 final int width = d.getIntrinsicWidth();
495 final int height = d.getIntrinsicHeight();
496
Winson Chung4b825dcd2011-06-19 12:41:22 -0700497 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700498 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700499 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700500 for (int row = 0; row <= countY; row++) {
501 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
502 float dist = mTmpPointF.length();
503 // Crosshairs further from the drag point are more faint
504 float alpha = Math.min(MAX_ALPHA,
505 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
506 if (alpha > 0.0f) {
507 d.setBounds(x, y, x + width, y + height);
508 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
509 d.draw(canvas);
510 }
511 y += mCellHeight + mHeightGap;
512 }
513 x += mCellWidth + mWidthGap;
514 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700515 }
Winson Chung150fbab2010-09-29 17:14:26 -0700516
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700517 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700518 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700519 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700520 if (alpha > 0) {
521 final Point p = mDragOutlines[i];
522 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700523 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700524 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700525 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700526 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800527
528 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
529 // requires an expanded clip rect (due to the glow's blur radius)
530 if (mPressedOrFocusedIcon != null) {
531 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
532 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
533 if (b != null) {
534 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700535 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
536 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800537 null);
538 }
539 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700540
541 // The folder outer / inner ring image(s)
542 for (int i = 0; i < mFolderOuterRings.size(); i++) {
543 FolderRingAnimator fra = mFolderOuterRings.get(i);
544
545 // Draw outer ring
546 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
547 int width = (int) fra.getOuterRingSize();
548 int height = width;
549 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
550
551 int centerX = mTempLocation[0] + mCellWidth / 2;
552 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
553
554 canvas.save();
555 canvas.translate(centerX - width / 2, centerY - height / 2);
556 d.setBounds(0, 0, width, height);
557 d.draw(canvas);
558 canvas.restore();
559
560 // Draw inner ring
561 d = FolderRingAnimator.sSharedInnerRingDrawable;
562 width = (int) fra.getInnerRingSize();
563 height = width;
564 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
565
566 centerX = mTempLocation[0] + mCellWidth / 2;
567 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
568 canvas.save();
569 canvas.translate(centerX - width / 2, centerY - width / 2);
570 d.setBounds(0, 0, width, height);
571 d.draw(canvas);
572 canvas.restore();
573 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700574
575 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
576 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
577 int width = d.getIntrinsicWidth();
578 int height = d.getIntrinsicHeight();
579
580 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
581 int centerX = mTempLocation[0] + mCellWidth / 2;
582 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
583
584 canvas.save();
585 canvas.translate(centerX - width / 2, centerY - width / 2);
586 d.setBounds(0, 0, width, height);
587 d.draw(canvas);
588 canvas.restore();
589 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700590 }
591
592 public void showFolderAccept(FolderRingAnimator fra) {
593 mFolderOuterRings.add(fra);
594 }
595
596 public void hideFolderAccept(FolderRingAnimator fra) {
597 if (mFolderOuterRings.contains(fra)) {
598 mFolderOuterRings.remove(fra);
599 }
600 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700601 }
602
Adam Cohenc51934b2011-07-26 21:07:43 -0700603 public void setFolderLeaveBehindCell(int x, int y) {
604 mFolderLeaveBehindCell[0] = x;
605 mFolderLeaveBehindCell[1] = y;
606 invalidate();
607 }
608
609 public void clearFolderLeaveBehind() {
610 mFolderLeaveBehindCell[0] = -1;
611 mFolderLeaveBehindCell[1] = -1;
612 invalidate();
613 }
614
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700615 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700616 public void cancelLongPress() {
617 super.cancelLongPress();
618
619 // Cancel long press for all children
620 final int count = getChildCount();
621 for (int i = 0; i < count; i++) {
622 final View child = getChildAt(i);
623 child.cancelLongPress();
624 }
625 }
626
Michael Jurkadee05892010-07-27 10:01:56 -0700627 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
628 mInterceptTouchListener = listener;
629 }
630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700632 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 }
634
635 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700636 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
638
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700639 public boolean addViewToCellLayout(
640 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700641 final LayoutParams lp = params;
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 Jurkab8d0e1c2011-08-11 18:05:24 -0700653 if (!LauncherApplication.isScreenLarge()) {
654 // Hardware Layer Note:
655 // On phones, we set hardware layers on individual items
656 // On tablets, we set hardware layers on the entire mChildren view
657 // Setting the hardware layers on individual items only uses
658 // less memory but on tablet-size devices it has worse performance
659 // (a drop of ~6fps) whereas on phones the performance is the same
660 // with both approaches
661 child.setLayerType(LAYER_TYPE_HARDWARE, null);
662 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800663 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700664
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700665 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666
Winson Chungaafa03c2010-06-11 17:34:16 -0700667 return true;
668 }
669 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700671
Michael Jurkabea15192010-11-17 12:33:46 -0800672 public void setAcceptsDrops(boolean acceptsDrops) {
673 if (mAcceptsDrops != acceptsDrops) {
674 mAcceptsDrops = acceptsDrops;
675 invalidate();
676 }
677 }
678
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 public void removeAllViews() {
681 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800682 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700683 }
684
685 @Override
686 public void removeAllViewsInLayout() {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700687 if (mChildren.getChildCount() > 0) {
688 clearOccupiedCells();
689 mChildren.removeAllViewsInLayout();
690 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700691 }
692
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700693 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800694 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700695 }
696
Michael Jurka0280c3b2010-09-17 15:00:07 -0700697 @Override
698 public void removeView(View view) {
699 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800700 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700701 }
702
703 @Override
704 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800705 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
706 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700707 }
708
709 @Override
710 public void removeViewInLayout(View view) {
711 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800712 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700713 }
714
715 @Override
716 public void removeViews(int start, int count) {
717 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800718 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700719 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800720 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700721 }
722
723 @Override
724 public void removeViewsInLayout(int start, int count) {
725 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800726 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700727 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800728 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700729 }
730
Michael Jurka8c920dd2011-01-20 14:16:56 -0800731 public void drawChildren(Canvas canvas) {
732 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 }
734
Michael Jurkaabded662011-03-04 12:06:57 -0800735 void buildChildrenLayer() {
736 mChildren.buildLayer();
737 }
738
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 @Override
740 protected void onAttachedToWindow() {
741 super.onAttachedToWindow();
742 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
743 }
744
Michael Jurkaaf442092010-06-10 17:01:57 -0700745 public void setTagToCellInfoForPoint(int touchX, int touchY) {
746 final CellInfo cellInfo = mCellInfo;
747 final Rect frame = mRect;
748 final int x = touchX + mScrollX;
749 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800750 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700751
752 boolean found = false;
753 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800754 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800755 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700756
Adam Cohen1b607ed2011-03-03 17:26:50 -0800757 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
758 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700759 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700760
761 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
762 // offset that by this CellLayout's padding to test an (x,y) point that is relative
763 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700764 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700765
Michael Jurkaaf442092010-06-10 17:01:57 -0700766 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700767 cellInfo.cell = child;
768 cellInfo.cellX = lp.cellX;
769 cellInfo.cellY = lp.cellY;
770 cellInfo.spanX = lp.cellHSpan;
771 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700772 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700773 break;
774 }
775 }
776 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700777
Michael Jurkad771c962011-08-09 15:00:48 -0700778 mLastDownOnOccupiedCell = found;
779
Michael Jurkaaf442092010-06-10 17:01:57 -0700780 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700781 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700782 pointToCellExact(x, y, cellXY);
783
Michael Jurkaaf442092010-06-10 17:01:57 -0700784 cellInfo.cell = null;
785 cellInfo.cellX = cellXY[0];
786 cellInfo.cellY = cellXY[1];
787 cellInfo.spanX = 1;
788 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700789 }
790 setTag(cellInfo);
791 }
792
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 @Override
794 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700795 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
796 // even in the case where we return early. Not clearing here was causing bugs whereby on
797 // long-press we'd end up picking up an item from a previous drag operation.
798 final int action = ev.getAction();
799
800 if (action == MotionEvent.ACTION_DOWN) {
801 clearTagCellInfo();
802 }
803
Michael Jurkadee05892010-07-27 10:01:56 -0700804 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
805 return true;
806 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807
808 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700809 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 return false;
812 }
813
Adam Cohenc1997fd2011-08-15 18:26:39 -0700814 private void clearTagCellInfo() {
815 final CellInfo cellInfo = mCellInfo;
816 cellInfo.cell = null;
817 cellInfo.cellX = -1;
818 cellInfo.cellY = -1;
819 cellInfo.spanX = 0;
820 cellInfo.spanY = 0;
821 setTag(cellInfo);
822 }
823
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700825 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 }
827
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700828 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700829 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 * @param x X coordinate of the point
831 * @param y Y coordinate of the point
832 * @param result Array of 2 ints to hold the x and y coordinate of the cell
833 */
834 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700835 final int hStartPadding = getPaddingLeft();
836 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837
838 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
839 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
840
Adam Cohend22015c2010-07-26 22:02:18 -0700841 final int xAxis = mCountX;
842 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843
844 if (result[0] < 0) result[0] = 0;
845 if (result[0] >= xAxis) result[0] = xAxis - 1;
846 if (result[1] < 0) result[1] = 0;
847 if (result[1] >= yAxis) result[1] = yAxis - 1;
848 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700849
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 /**
851 * Given a point, return the cell that most closely encloses that point
852 * @param x X coordinate of the point
853 * @param y Y coordinate of the point
854 * @param result Array of 2 ints to hold the x and y coordinate of the cell
855 */
856 void pointToCellRounded(int x, int y, int[] result) {
857 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
858 }
859
860 /**
861 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700862 *
863 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700865 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 * @param result Array of 2 ints to hold the x and y coordinate of the point
867 */
868 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700869 final int hStartPadding = getPaddingLeft();
870 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871
872 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
873 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
874 }
875
Adam Cohene3e27a82011-04-15 12:07:39 -0700876 /**
877 * Given a cell coordinate, return the point that represents the upper left corner of that cell
878 *
879 * @param cellX X coordinate of the cell
880 * @param cellY Y coordinate of the cell
881 *
882 * @param result Array of 2 ints to hold the x and y coordinate of the point
883 */
884 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700885 final int hStartPadding = getPaddingLeft();
886 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700887
888 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
889 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
890 }
891
Romain Guy84f296c2009-11-04 15:00:44 -0800892 int getCellWidth() {
893 return mCellWidth;
894 }
895
896 int getCellHeight() {
897 return mCellHeight;
898 }
899
Adam Cohend4844c32011-02-18 19:25:06 -0800900 int getWidthGap() {
901 return mWidthGap;
902 }
903
904 int getHeightGap() {
905 return mHeightGap;
906 }
907
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700908 Rect getContentRect(Rect r) {
909 if (r == null) {
910 r = new Rect();
911 }
912 int left = getPaddingLeft();
913 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700914 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
915 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700916 r.set(left, top, right, bottom);
917 return r;
918 }
919
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 @Override
921 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
922 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700923
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700925 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
926
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
928 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700929
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
931 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
932 }
933
Adam Cohend22015c2010-07-26 22:02:18 -0700934 int numWidthGaps = mCountX - 1;
935 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936
Adam Cohen234c4cd2011-07-17 21:03:04 -0700937 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700938 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
939 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
940 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
941 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
942 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
943 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700944 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700945 } else {
946 mWidthGap = mOriginalWidthGap;
947 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700948 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700949
Michael Jurka8c920dd2011-01-20 14:16:56 -0800950 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
951 int newWidth = widthSpecSize;
952 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700953 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700954 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700955 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700956 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700957 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700958 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700959 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800960
961 int count = getChildCount();
962 for (int i = 0; i < count; i++) {
963 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700964 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
965 mPaddingRight, MeasureSpec.EXACTLY);
966 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
967 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800968 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
969 }
970 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 }
972
973 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700974 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800977 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700978 child.layout(mPaddingLeft, mPaddingTop,
979 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 }
981 }
982
983 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700984 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
985 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700986 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800987 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700988 }
989
990 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800992 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
994
995 @Override
996 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800997 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 }
999
Michael Jurka5f1c5092010-09-03 14:15:02 -07001000 public float getBackgroundAlpha() {
1001 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001002 }
1003
Michael Jurka742574b2011-02-02 23:51:01 -08001004 public void setFastBackgroundAlpha(float alpha) {
1005 mBackgroundAlpha = alpha;
1006 }
1007
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001008 public void setBackgroundAlphaMultiplier(float multiplier) {
1009 mBackgroundAlphaMultiplier = multiplier;
1010 }
1011
Adam Cohenddb82192010-11-10 16:32:54 -08001012 public float getBackgroundAlphaMultiplier() {
1013 return mBackgroundAlphaMultiplier;
1014 }
1015
Michael Jurka5f1c5092010-09-03 14:15:02 -07001016 public void setBackgroundAlpha(float alpha) {
1017 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -07001018 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -07001019 }
1020
Michael Jurka5f1c5092010-09-03 14:15:02 -07001021 // Need to return true to let the view system know we know how to handle alpha-- this is
1022 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
1023 // versions
1024 @Override
1025 protected boolean onSetAlpha(int alpha) {
1026 return true;
1027 }
1028
1029 public void setAlpha(float alpha) {
1030 setChildrenAlpha(alpha);
1031 super.setAlpha(alpha);
1032 }
1033
Michael Jurka742574b2011-02-02 23:51:01 -08001034 public void setFastAlpha(float alpha) {
1035 setFastChildrenAlpha(alpha);
1036 super.setFastAlpha(alpha);
1037 }
1038
Michael Jurkadee05892010-07-27 10:01:56 -07001039 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001040 final int childCount = getChildCount();
1041 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001042 getChildAt(i).setAlpha(alpha);
1043 }
1044 }
1045
Michael Jurka742574b2011-02-02 23:51:01 -08001046 private void setFastChildrenAlpha(float alpha) {
1047 final int childCount = getChildCount();
1048 for (int i = 0; i < childCount; i++) {
1049 getChildAt(i).setFastAlpha(alpha);
1050 }
1051 }
1052
Patrick Dubroy440c3602010-07-13 17:50:32 -07001053 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001054 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001055 }
1056
Adam Cohen76fc0852011-06-17 13:26:23 -07001057 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
1058 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001059 CellLayoutChildren clc = getChildrenLayout();
1060 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
1061 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1062 final ItemInfo info = (ItemInfo) child.getTag();
1063
1064 // We cancel any existing animations
1065 if (mReorderAnimators.containsKey(lp)) {
1066 mReorderAnimators.get(lp).cancel();
1067 mReorderAnimators.remove(lp);
1068 }
1069
1070 int oldX = lp.x;
1071 int oldY = lp.y;
1072 mOccupied[lp.cellX][lp.cellY] = false;
1073 mOccupied[cellX][cellY] = true;
1074
1075 lp.isLockedToGrid = true;
1076 lp.cellX = info.cellX = cellX;
1077 lp.cellY = info.cellY = cellY;
1078 clc.setupLp(lp);
1079 lp.isLockedToGrid = false;
1080 int newX = lp.x;
1081 int newY = lp.y;
1082
Adam Cohen76fc0852011-06-17 13:26:23 -07001083 lp.x = oldX;
1084 lp.y = oldY;
1085 child.requestLayout();
1086
Adam Cohenbfbfd262011-06-13 16:55:12 -07001087 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
1088 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
1089 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
1090 oa.setDuration(duration);
1091 mReorderAnimators.put(lp, oa);
1092 oa.addUpdateListener(new AnimatorUpdateListener() {
1093 public void onAnimationUpdate(ValueAnimator animation) {
1094 child.requestLayout();
1095 }
1096 });
1097 oa.addListener(new AnimatorListenerAdapter() {
1098 boolean cancelled = false;
1099 public void onAnimationEnd(Animator animation) {
1100 // If the animation was cancelled, it means that another animation
1101 // has interrupted this one, and we don't want to lock the item into
1102 // place just yet.
1103 if (!cancelled) {
1104 lp.isLockedToGrid = true;
1105 }
1106 if (mReorderAnimators.containsKey(lp)) {
1107 mReorderAnimators.remove(lp);
1108 }
1109 }
1110 public void onAnimationCancel(Animator animation) {
1111 cancelled = true;
1112 }
1113 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001114 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001115 oa.start();
1116 return true;
1117 }
1118 return false;
1119 }
1120
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001121 /**
1122 * Estimate where the top left cell of the dragged item will land if it is dropped.
1123 *
1124 * @param originX The X value of the top left corner of the item
1125 * @param originY The Y value of the top left corner of the item
1126 * @param spanX The number of horizontal cells that the item spans
1127 * @param spanY The number of vertical cells that the item spans
1128 * @param result The estimated drop cell X and Y.
1129 */
1130 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001131 final int countX = mCountX;
1132 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001133
Michael Jurkaa63c4522010-08-19 13:52:27 -07001134 // pointToCellRounded takes the top left of a cell but will pad that with
1135 // cellWidth/2 and cellHeight/2 when finding the matching cell
1136 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001137
1138 // If the item isn't fully on this screen, snap to the edges
1139 int rightOverhang = result[0] + spanX - countX;
1140 if (rightOverhang > 0) {
1141 result[0] -= rightOverhang; // Snap to right
1142 }
1143 result[0] = Math.max(0, result[0]); // Snap to left
1144 int bottomOverhang = result[1] + spanY - countY;
1145 if (bottomOverhang > 0) {
1146 result[1] -= bottomOverhang; // Snap to bottom
1147 }
1148 result[1] = Math.max(0, result[1]); // Snap to top
1149 }
1150
Joe Onorato4be866d2010-10-10 11:26:02 -07001151 void visualizeDropLocation(
1152 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1153
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001154 final int oldDragCellX = mDragCell[0];
1155 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001156 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001157 if (v != null) {
1158 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1159 } else {
1160 mDragCenter.set(originX, originY);
1161 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001162
Adam Cohen2801caf2011-05-13 20:57:39 -07001163 if (dragOutline == null && v == null) {
1164 if (mCrosshairsDrawable != null) {
1165 invalidate();
1166 }
1167 return;
1168 }
1169
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001170 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001171 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001172 final int[] topLeft = mTmpPoint;
1173 cellToPoint(nearest[0], nearest[1], topLeft);
1174
Joe Onorato4be866d2010-10-10 11:26:02 -07001175 int left = topLeft[0];
1176 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001177
Winson Chunga9abd0e2010-10-27 17:18:37 -07001178 if (v != null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001179 // When drawing the drag outline, it did not account for margin offsets
1180 // added by the view's parent.
1181 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1182 left += lp.leftMargin;
1183 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001184
Adam Cohen99e8b402011-03-25 19:23:43 -07001185 // Offsets due to the size difference between the View and the dragOutline.
1186 // There is a size difference to account for the outer blur, which may lie
1187 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001188 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1189 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001190 } else {
1191 // Center the drag outline in the cell
Winson Chung4b576be2011-04-27 17:40:20 -07001192 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1193 - dragOutline.getWidth()) / 2;
1194 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1195 - dragOutline.getHeight()) / 2;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001196 }
Winson Chung150fbab2010-09-29 17:14:26 -07001197
Joe Onorato4be866d2010-10-10 11:26:02 -07001198 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001199 mDragOutlineAnims[oldIndex].animateOut();
1200 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001201
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001202 mDragOutlines[mDragOutlineCurrent].set(left, top);
1203 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1204 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001205 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001206
1207 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1208 if (mCrosshairsDrawable != null) {
1209 invalidate();
1210 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001211 }
1212
Adam Cohene0310962011-04-18 16:15:31 -07001213 public void clearDragOutlines() {
1214 final int oldIndex = mDragOutlineCurrent;
1215 mDragOutlineAnims[oldIndex].animateOut();
1216 mDragCell[0] = -1;
1217 mDragCell[1] = -1;
1218 }
1219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001221 * Find a vacant area that will fit the given bounds nearest the requested
1222 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001223 *
Romain Guy51afc022009-05-04 18:03:43 -07001224 * @param pixelX The X location at which you want to search for a vacant area.
1225 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001226 * @param spanX Horizontal span of the object.
1227 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001228 * @param result Array in which to place the result, or null (in which case a new array will
1229 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001230 * @return The X, Y cell of a vacant area that can contain this object,
1231 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001233 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001234 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1235 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001236 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001237
Michael Jurka6a1435d2010-09-27 17:35:12 -07001238 /**
1239 * Find a vacant area that will fit the given bounds nearest the requested
1240 * cell location. Uses Euclidean distance to score multiple vacant areas.
1241 *
1242 * @param pixelX The X location at which you want to search for a vacant area.
1243 * @param pixelY The Y location at which you want to search for a vacant area.
1244 * @param spanX Horizontal span of the object.
1245 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001246 * @param ignoreOccupied If true, the result can be an occupied cell
1247 * @param result Array in which to place the result, or null (in which case a new array will
1248 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001249 * @return The X, Y cell of a vacant area that can contain this object,
1250 * nearest the requested location.
1251 */
Adam Cohendf035382011-04-11 17:22:04 -07001252 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1253 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001254 // mark space take by ignoreView as available (method checks if ignoreView is null)
1255 markCellsAsUnoccupiedForView(ignoreView);
1256
Adam Cohene3e27a82011-04-15 12:07:39 -07001257 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1258 // to the center of the item, but we are searching based on the top-left cell, so
1259 // we translate the point over to correspond to the top-left.
1260 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1261 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1262
Jeff Sharkey70864282009-04-07 21:08:40 -07001263 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001264 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001265 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001266
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001267 final int countX = mCountX;
1268 final int countY = mCountY;
1269 final boolean[][] occupied = mOccupied;
1270
Winson Chungbbc60d82010-11-11 16:34:41 -08001271 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001272 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001273 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001274 if (ignoreOccupied) {
1275 for (int i = 0; i < spanX; i++) {
1276 for (int j = 0; j < spanY; j++) {
1277 if (occupied[x + i][y + j]) {
1278 // small optimization: we can skip to after the column we
1279 // just found an occupied cell
1280 x += i;
1281 continue inner;
1282 }
Michael Jurkac28de512010-08-13 11:27:44 -07001283 }
1284 }
1285 }
Winson Chung0be025d2011-05-23 17:45:09 -07001286 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001287 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288
Michael Jurkac28de512010-08-13 11:27:44 -07001289 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1290 + Math.pow(cellXY[1] - pixelY, 2));
1291 if (distance <= bestDistance) {
1292 bestDistance = distance;
1293 bestXY[0] = x;
1294 bestXY[1] = y;
1295 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 }
1297 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001298 // re-mark space taken by ignoreView as occupied
1299 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300
Adam Cohenc0dcf592011-06-01 15:30:43 -07001301 // Return -1, -1 if no suitable location found
1302 if (bestDistance == Double.MAX_VALUE) {
1303 bestXY[0] = -1;
1304 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001305 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001306 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001308
Adam Cohendf035382011-04-11 17:22:04 -07001309 /**
1310 * Find a vacant area that will fit the given bounds nearest the requested
1311 * cell location. Uses Euclidean distance to score multiple vacant areas.
1312 *
1313 * @param pixelX The X location at which you want to search for a vacant area.
1314 * @param pixelY The Y location at which you want to search for a vacant area.
1315 * @param spanX Horizontal span of the object.
1316 * @param spanY Vertical span of the object.
1317 * @param ignoreView Considers space occupied by this view as unoccupied
1318 * @param result Previously returned value to possibly recycle.
1319 * @return The X, Y cell of a vacant area that can contain this object,
1320 * nearest the requested location.
1321 */
1322 int[] findNearestVacantArea(
1323 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1324 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1325 }
1326
1327 /**
1328 * Find a starting cell position that will fit the given bounds nearest the requested
1329 * cell location. Uses Euclidean distance to score multiple vacant areas.
1330 *
1331 * @param pixelX The X location at which you want to search for a vacant area.
1332 * @param pixelY The Y location at which you want to search for a vacant area.
1333 * @param spanX Horizontal span of the object.
1334 * @param spanY Vertical span of the object.
1335 * @param ignoreView Considers space occupied by this view as unoccupied
1336 * @param result Previously returned value to possibly recycle.
1337 * @return The X, Y cell of a vacant area that can contain this object,
1338 * nearest the requested location.
1339 */
1340 int[] findNearestArea(
1341 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1342 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1343 }
1344
Michael Jurka0280c3b2010-09-17 15:00:07 -07001345 boolean existsEmptyCell() {
1346 return findCellForSpan(null, 1, 1);
1347 }
1348
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001350 * Finds the upper-left coordinate of the first rectangle in the grid that can
1351 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1352 * then this method will only return coordinates for rectangles that contain the cell
1353 * (intersectX, intersectY)
1354 *
1355 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1356 * can be found.
1357 * @param spanX The horizontal span of the cell we want to find.
1358 * @param spanY The vertical span of the cell we want to find.
1359 *
1360 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001361 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001362 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1363 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1364 }
1365
1366 /**
1367 * Like above, but ignores any cells occupied by the item "ignoreView"
1368 *
1369 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1370 * can be found.
1371 * @param spanX The horizontal span of the cell we want to find.
1372 * @param spanY The vertical span of the cell we want to find.
1373 * @param ignoreView The home screen item we should treat as not occupying any space
1374 * @return
1375 */
1376 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1377 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1378 }
1379
1380 /**
1381 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1382 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1383 *
1384 * @param spanX The horizontal span of the cell we want to find.
1385 * @param spanY The vertical span of the cell we want to find.
1386 * @param ignoreView The home screen item we should treat as not occupying any space
1387 * @param intersectX The X coordinate of the cell that we should try to overlap
1388 * @param intersectX The Y coordinate of the cell that we should try to overlap
1389 *
1390 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1391 */
1392 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1393 int intersectX, int intersectY) {
1394 return findCellForSpanThatIntersectsIgnoring(
1395 cellXY, spanX, spanY, intersectX, intersectY, null);
1396 }
1397
1398 /**
1399 * The superset of the above two methods
1400 */
1401 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1402 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001403 // mark space take by ignoreView as available (method checks if ignoreView is null)
1404 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001405
Michael Jurka28750fb2010-09-24 17:43:49 -07001406 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001407 while (true) {
1408 int startX = 0;
1409 if (intersectX >= 0) {
1410 startX = Math.max(startX, intersectX - (spanX - 1));
1411 }
1412 int endX = mCountX - (spanX - 1);
1413 if (intersectX >= 0) {
1414 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1415 }
1416 int startY = 0;
1417 if (intersectY >= 0) {
1418 startY = Math.max(startY, intersectY - (spanY - 1));
1419 }
1420 int endY = mCountY - (spanY - 1);
1421 if (intersectY >= 0) {
1422 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1423 }
1424
Winson Chungbbc60d82010-11-11 16:34:41 -08001425 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001426 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001427 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001428 for (int i = 0; i < spanX; i++) {
1429 for (int j = 0; j < spanY; j++) {
1430 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001431 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001432 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001433 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001434 continue inner;
1435 }
1436 }
1437 }
1438 if (cellXY != null) {
1439 cellXY[0] = x;
1440 cellXY[1] = y;
1441 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001442 foundCell = true;
1443 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001444 }
1445 }
1446 if (intersectX == -1 && intersectY == -1) {
1447 break;
1448 } else {
1449 // if we failed to find anything, try again but without any requirements of
1450 // intersecting
1451 intersectX = -1;
1452 intersectY = -1;
1453 continue;
1454 }
1455 }
1456
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001457 // re-mark space taken by ignoreView as occupied
1458 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001459 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001460 }
1461
1462 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001463 * A drag event has begun over this layout.
1464 * It may have begun over this layout (in which case onDragChild is called first),
1465 * or it may have begun on another layout.
1466 */
1467 void onDragEnter() {
1468 if (!mDragging) {
1469 // Fade in the drag indicators
1470 if (mCrosshairsAnimator != null) {
1471 mCrosshairsAnimator.animateIn();
1472 }
1473 }
1474 mDragging = true;
1475 }
1476
1477 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001478 * Called when drag has left this CellLayout or has been completed (successfully or not)
1479 */
1480 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001481 // This can actually be called when we aren't in a drag, e.g. when adding a new
1482 // item to this layout via the customize drawer.
1483 // Guard against that case.
1484 if (mDragging) {
1485 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001486
Joe Onorato4be866d2010-10-10 11:26:02 -07001487 // Fade out the drag indicators
1488 if (mCrosshairsAnimator != null) {
1489 mCrosshairsAnimator.animateOut();
1490 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001491 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001492
1493 // Invalidate the drag data
1494 mDragCell[0] = -1;
1495 mDragCell[1] = -1;
1496 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1497 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1498
Michael Jurka33945b22010-12-21 18:19:38 -08001499 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001500 }
1501
1502 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001503 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001504 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001505 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 *
1507 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001509 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001510 if (child != null) {
1511 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001512 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001513 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001514 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
1516
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001519 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 * @param cellX X coordinate of upper left corner expressed as a cell position
1521 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001522 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001524 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001526 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 final int cellWidth = mCellWidth;
1528 final int cellHeight = mCellHeight;
1529 final int widthGap = mWidthGap;
1530 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001531
Winson Chung4b825dcd2011-06-19 12:41:22 -07001532 final int hStartPadding = getPaddingLeft();
1533 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001534
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1536 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1537
1538 int x = hStartPadding + cellX * (cellWidth + widthGap);
1539 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001540
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001541 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001543
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001545 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001547 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 * @param width Width in pixels
1549 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001550 * @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 -08001551 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001552 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001553 return rectToCell(getResources(), width, height, result);
1554 }
1555
1556 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 // Always assume we're working with the smallest span to make sure we
1558 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001559 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1560 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001562
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07001564 int spanX = (int) Math.ceil(width / (float) smallerSize);
1565 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04001566
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001567 if (result == null) {
1568 return new int[] { spanX, spanY };
1569 }
1570 result[0] = spanX;
1571 result[1] = spanY;
1572 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
1574
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001575 public int[] cellSpansToSize(int hSpans, int vSpans) {
1576 int[] size = new int[2];
1577 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1578 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1579 return size;
1580 }
1581
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001583 * Calculate the grid spans needed to fit given item
1584 */
1585 public void calculateSpans(ItemInfo info) {
1586 final int minWidth;
1587 final int minHeight;
1588
1589 if (info instanceof LauncherAppWidgetInfo) {
1590 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1591 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1592 } else if (info instanceof PendingAddWidgetInfo) {
1593 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1594 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1595 } else {
1596 // It's not a widget, so it must be 1x1
1597 info.spanX = info.spanY = 1;
1598 return;
1599 }
1600 int[] spans = rectToCell(minWidth, minHeight, null);
1601 info.spanX = spans[0];
1602 info.spanY = spans[1];
1603 }
1604
1605 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 * Find the first vacant cell, if there is one.
1607 *
1608 * @param vacant Holds the x and y coordinate of the vacant cell
1609 * @param spanX Horizontal cell span.
1610 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001611 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 * @return True if a vacant cell was found
1613 */
1614 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615
Michael Jurka0280c3b2010-09-17 15:00:07 -07001616 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 }
1618
1619 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1620 int xCount, int yCount, boolean[][] occupied) {
1621
Adam Cohen2801caf2011-05-13 20:57:39 -07001622 for (int y = 0; y < yCount; y++) {
1623 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 boolean available = !occupied[x][y];
1625out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1626 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1627 available = available && !occupied[i][j];
1628 if (!available) break out;
1629 }
1630 }
1631
1632 if (available) {
1633 vacant[0] = x;
1634 vacant[1] = y;
1635 return true;
1636 }
1637 }
1638 }
1639
1640 return false;
1641 }
1642
Michael Jurka0280c3b2010-09-17 15:00:07 -07001643 private void clearOccupiedCells() {
1644 for (int x = 0; x < mCountX; x++) {
1645 for (int y = 0; y < mCountY; y++) {
1646 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 }
1648 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001649 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650
Adam Cohen1b607ed2011-03-03 17:26:50 -08001651 /**
1652 * Given a view, determines how much that view can be expanded in all directions, in terms of
1653 * whether or not there are other items occupying adjacent cells. Used by the
1654 * AppWidgetResizeFrame to determine how the widget can be resized.
1655 */
Adam Cohend4844c32011-02-18 19:25:06 -08001656 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001657 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001658 boolean flag;
1659
Adam Cohen1b607ed2011-03-03 17:26:50 -08001660 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001661 for (int x = lp.cellX - 1; x >= 0; x--) {
1662 flag = false;
1663 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1664 if (mOccupied[x][y]) flag = true;
1665 }
1666 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001667 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001668 }
1669
Adam Cohen1b607ed2011-03-03 17:26:50 -08001670 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001671 for (int y = lp.cellY - 1; y >= 0; y--) {
1672 flag = false;
1673 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1674 if (mOccupied[x][y]) flag = true;
1675 }
1676 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001677 expandability[AppWidgetResizeFrame.TOP]++;
1678 }
Adam Cohend4844c32011-02-18 19:25:06 -08001679
Adam Cohen1b607ed2011-03-03 17:26:50 -08001680 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001681 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1682 flag = false;
1683 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1684 if (mOccupied[x][y]) flag = true;
1685 }
1686 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001687 expandability[AppWidgetResizeFrame.RIGHT]++;
1688 }
Adam Cohend4844c32011-02-18 19:25:06 -08001689
Adam Cohen1b607ed2011-03-03 17:26:50 -08001690 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001691 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1692 flag = false;
1693 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1694 if (mOccupied[x][y]) flag = true;
1695 }
1696 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001697 expandability[AppWidgetResizeFrame.BOTTOM]++;
1698 }
Adam Cohend4844c32011-02-18 19:25:06 -08001699 }
1700
Michael Jurka0280c3b2010-09-17 15:00:07 -07001701 public void onMove(View view, int newCellX, int newCellY) {
1702 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1703 markCellsAsUnoccupiedForView(view);
1704 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1705 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706
Adam Cohend4844c32011-02-18 19:25:06 -08001707 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001708 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001709 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1710 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1711 }
1712
Adam Cohend4844c32011-02-18 19:25:06 -08001713 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001714 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001715 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1716 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1717 }
1718
1719 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1720 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1721 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1722 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 }
1724 }
1725 }
1726
Adam Cohen2801caf2011-05-13 20:57:39 -07001727 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001728 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001729 (Math.max((mCountX - 1), 0) * mWidthGap);
1730 }
1731
1732 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001733 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001734 (Math.max((mCountY - 1), 0) * mHeightGap);
1735 }
1736
Michael Jurka66d72172011-04-12 16:29:25 -07001737 public boolean isOccupied(int x, int y) {
1738 if (x < mCountX && y < mCountY) {
1739 return mOccupied[x][y];
1740 } else {
1741 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1742 }
1743 }
1744
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 @Override
1746 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1747 return new CellLayout.LayoutParams(getContext(), attrs);
1748 }
1749
1750 @Override
1751 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1752 return p instanceof CellLayout.LayoutParams;
1753 }
1754
1755 @Override
1756 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1757 return new CellLayout.LayoutParams(p);
1758 }
1759
Winson Chungaafa03c2010-06-11 17:34:16 -07001760 public static class CellLayoutAnimationController extends LayoutAnimationController {
1761 public CellLayoutAnimationController(Animation animation, float delay) {
1762 super(animation, delay);
1763 }
1764
1765 @Override
1766 protected long getDelayForView(View view) {
1767 return (int) (Math.random() * 150);
1768 }
1769 }
1770
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1772 /**
1773 * Horizontal location of the item in the grid.
1774 */
1775 @ViewDebug.ExportedProperty
1776 public int cellX;
1777
1778 /**
1779 * Vertical location of the item in the grid.
1780 */
1781 @ViewDebug.ExportedProperty
1782 public int cellY;
1783
1784 /**
1785 * Number of cells spanned horizontally by the item.
1786 */
1787 @ViewDebug.ExportedProperty
1788 public int cellHSpan;
1789
1790 /**
1791 * Number of cells spanned vertically by the item.
1792 */
1793 @ViewDebug.ExportedProperty
1794 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001795
Adam Cohen1b607ed2011-03-03 17:26:50 -08001796 /**
1797 * Indicates whether the item will set its x, y, width and height parameters freely,
1798 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1799 */
Adam Cohend4844c32011-02-18 19:25:06 -08001800 public boolean isLockedToGrid = true;
1801
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 // X coordinate of the view in the layout.
1803 @ViewDebug.ExportedProperty
1804 int x;
1805 // Y coordinate of the view in the layout.
1806 @ViewDebug.ExportedProperty
1807 int y;
1808
Romain Guy84f296c2009-11-04 15:00:44 -08001809 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001810
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 public LayoutParams(Context c, AttributeSet attrs) {
1812 super(c, attrs);
1813 cellHSpan = 1;
1814 cellVSpan = 1;
1815 }
1816
1817 public LayoutParams(ViewGroup.LayoutParams source) {
1818 super(source);
1819 cellHSpan = 1;
1820 cellVSpan = 1;
1821 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001822
1823 public LayoutParams(LayoutParams source) {
1824 super(source);
1825 this.cellX = source.cellX;
1826 this.cellY = source.cellY;
1827 this.cellHSpan = source.cellHSpan;
1828 this.cellVSpan = source.cellVSpan;
1829 }
1830
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001832 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 this.cellX = cellX;
1834 this.cellY = cellY;
1835 this.cellHSpan = cellHSpan;
1836 this.cellVSpan = cellVSpan;
1837 }
1838
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001839 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001840 if (isLockedToGrid) {
1841 final int myCellHSpan = cellHSpan;
1842 final int myCellVSpan = cellVSpan;
1843 final int myCellX = cellX;
1844 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001845
Adam Cohend4844c32011-02-18 19:25:06 -08001846 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1847 leftMargin - rightMargin;
1848 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1849 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001850 x = myCellX * (cellWidth + widthGap) + leftMargin;
1851 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001852 }
1853 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001854
Winson Chungaafa03c2010-06-11 17:34:16 -07001855 public String toString() {
1856 return "(" + this.cellX + ", " + this.cellY + ")";
1857 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001858
1859 public void setWidth(int width) {
1860 this.width = width;
1861 }
1862
1863 public int getWidth() {
1864 return width;
1865 }
1866
1867 public void setHeight(int height) {
1868 this.height = height;
1869 }
1870
1871 public int getHeight() {
1872 return height;
1873 }
1874
1875 public void setX(int x) {
1876 this.x = x;
1877 }
1878
1879 public int getX() {
1880 return x;
1881 }
1882
1883 public void setY(int y) {
1884 this.y = y;
1885 }
1886
1887 public int getY() {
1888 return y;
1889 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 }
1891
Michael Jurka0280c3b2010-09-17 15:00:07 -07001892 // This class stores info for two purposes:
1893 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1894 // its spanX, spanY, and the screen it is on
1895 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1896 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1897 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001898 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001900 int cellX = -1;
1901 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 int spanX;
1903 int spanY;
1904 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001905 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 @Override
1908 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001909 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1910 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 }
1912 }
Michael Jurkad771c962011-08-09 15:00:48 -07001913
1914 public boolean lastDownOnOccupiedCell() {
1915 return mLastDownOnOccupiedCell;
1916 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917}