blob: ae22507734ec0685647d66dac63f2cff5c8c8b19 [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;
84
Michael Jurkadee05892010-07-27 10:01:56 -070085 private OnTouchListener mInterceptTouchListener;
86
Adam Cohen69ce2e52011-07-03 19:25:21 -070087 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
88
Michael Jurka5f1c5092010-09-03 14:15:02 -070089 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070090 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070091
Michael Jurka33945b22010-12-21 18:19:38 -080092 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080093 private Drawable mActiveBackground;
94 private Drawable mActiveGlowBackground;
95 private Drawable mNormalBackgroundMini;
96 private Drawable mNormalGlowBackgroundMini;
97 private Drawable mActiveBackgroundMini;
98 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -070099 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -0800100 private Rect mGlowBackgroundRect;
101 private float mGlowBackgroundScale;
102 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700103
Adam Cohendf035382011-04-11 17:22:04 -0700104 private boolean mAcceptsDrops = true;
Michael Jurka33945b22010-12-21 18:19:38 -0800105 // If we're actively dragging something over this screen, mIsDragOverlapping is true
106 private boolean mIsDragOverlapping = false;
107 private boolean mIsDragOccuring = false;
108 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700109 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700110
Winson Chung150fbab2010-09-29 17:14:26 -0700111 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700112 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Winson Chung63257c12011-05-05 17:06:13 -0700113 private Point[] mDragOutlines = new Point[4];
Chet Haase472b2812010-10-14 07:02:04 -0700114 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700115 private InterruptibleInOutAnimator[] mDragOutlineAnims =
116 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700117
118 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700119 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700120 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700121
Patrick Dubroy96864c32011-03-10 17:17:23 -0800122 private BubbleTextView mPressedOrFocusedIcon;
123
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700124 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700125 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700126 private float mCrosshairsVisibility = 0.0f;
127
Adam Cohenbfbfd262011-06-13 16:55:12 -0700128 private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new
129 HashMap<CellLayout.LayoutParams, ObjectAnimator>();
130
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700131 // When a drag operation is in progress, holds the nearest cell to the touch point
132 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Joe Onorato4be866d2010-10-10 11:26:02 -0700134 private boolean mDragging = false;
135
Patrick Dubroyce34a972010-10-19 10:34:32 -0700136 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800137 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 public CellLayout(Context context) {
140 this(context, null);
141 }
142
143 public CellLayout(Context context, AttributeSet attrs) {
144 this(context, attrs, 0);
145 }
146
147 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
148 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700149
150 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
151 // the user where a dragged item will land when dropped.
152 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
155
Winson Chung4b825dcd2011-06-19 12:41:22 -0700156 mOriginalCellWidth =
157 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
158 mOriginalCellHeight =
159 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700160 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
161 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700162 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700163 mCountX = LauncherModel.getCellCountX();
164 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700165 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
167 a.recycle();
168
169 setAlwaysDrawnWithCacheEnabled(false);
170
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700171 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700172
Winson Chung967289b2011-06-30 18:09:30 -0700173 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
174 mActiveBackground = res.getDrawable(R.drawable.homescreen_green_normal_holo);
175 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_green_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800176
Winson Chungb26f3d62011-06-02 10:49:29 -0700177 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
178 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
179 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
180 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
Michael Jurka33945b22010-12-21 18:19:38 -0800181
Winson Chungb26f3d62011-06-02 10:49:29 -0700182 mNormalBackground.setFilterBitmap(true);
183 mActiveBackground.setFilterBitmap(true);
184 mActiveGlowBackground.setFilterBitmap(true);
185 mNormalBackgroundMini.setFilterBitmap(true);
186 mNormalGlowBackgroundMini.setFilterBitmap(true);
187 mActiveBackgroundMini.setFilterBitmap(true);
188 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700189
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700190 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700191
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700192 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700193 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700194
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700195 // Set up the animation for fading the crosshairs in and out
196 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700197 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700198 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700199 public void onAnimationUpdate(ValueAnimator animation) {
200 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700201 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700202 }
203 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700204 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700205
Joe Onorato4be866d2010-10-10 11:26:02 -0700206 for (int i = 0; i < mDragOutlines.length; i++) {
207 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700208 }
209
210 // When dragging things around the home screens, we show a green outline of
211 // where the item will land. The outlines gradually fade out, leaving a trail
212 // behind the drag path.
213 // Set up all the animations that are used to implement this fading.
214 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700215 final float fromAlphaValue = 0;
216 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700217
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700218 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700219
220 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 final InterruptibleInOutAnimator anim =
222 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700223 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700224 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700225 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700226 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700227 final Bitmap outline = (Bitmap)anim.getTag();
228
229 // If an animation is started and then stopped very quickly, we can still
230 // get spurious updates we've cleared the tag. Guard against this.
231 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700232 if (false) {
233 Object val = animation.getAnimatedValue();
234 Log.d(TAG, "anim " + thisIndex + " update: " + val +
235 ", isStopped " + anim.isStopped());
236 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700237 // Try to prevent it from continuing to run
238 animation.cancel();
239 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700240 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700241 final int left = mDragOutlines[thisIndex].x;
242 final int top = mDragOutlines[thisIndex].y;
243 CellLayout.this.invalidate(left, top,
244 left + outline.getWidth(), top + outline.getHeight());
245 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700246 }
247 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700248 // The animation holds a reference to the drag outline bitmap as long is it's
249 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700250 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700251 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700252 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700253 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700254 anim.setTag(null);
255 }
256 }
257 });
258 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700259 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700260
Michael Jurka18014792010-10-14 09:01:34 -0700261 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800262 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700263 setHoverScale(1.0f);
264 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800265
Michael Jurka8c920dd2011-01-20 14:16:56 -0800266 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700267 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800268 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700269 }
270
Michael Jurkaf6440da2011-04-05 14:50:34 -0700271 static int widthInPortrait(Resources r, int numCells) {
272 // We use this method from Workspace to figure out how many rows/columns Launcher should
273 // have. We ignore the left/right padding on CellLayout because it turns out in our design
274 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700275 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700276 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
277 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700278
Winson Chung4b825dcd2011-06-19 12:41:22 -0700279 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700280 }
281
Michael Jurkaf6440da2011-04-05 14:50:34 -0700282 static int heightInLandscape(Resources r, int numCells) {
283 // We use this method from Workspace to figure out how many rows/columns Launcher should
284 // have. We ignore the left/right padding on CellLayout because it turns out in our design
285 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700286 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700287 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
288 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700289
Winson Chung4b825dcd2011-06-19 12:41:22 -0700290 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700291 }
292
Adam Cohen2801caf2011-05-13 20:57:39 -0700293 public void enableHardwareLayers() {
294 mChildren.enableHardwareLayers();
295 }
296
297 public void setGridSize(int x, int y) {
298 mCountX = x;
299 mCountY = y;
300 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700301 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700302 }
303
Patrick Dubroy96864c32011-03-10 17:17:23 -0800304 private void invalidateBubbleTextView(BubbleTextView icon) {
305 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700306 invalidate(icon.getLeft() + getPaddingLeft() - padding,
307 icon.getTop() + getPaddingTop() - padding,
308 icon.getRight() + getPaddingLeft() + padding,
309 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800310 }
311
312 void setPressedOrFocusedIcon(BubbleTextView icon) {
313 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
314 // requires an expanded clip rect (due to the glow's blur radius)
315 BubbleTextView oldIcon = mPressedOrFocusedIcon;
316 mPressedOrFocusedIcon = icon;
317 if (oldIcon != null) {
318 invalidateBubbleTextView(oldIcon);
319 }
320 if (mPressedOrFocusedIcon != null) {
321 invalidateBubbleTextView(mPressedOrFocusedIcon);
322 }
323 }
324
Winson Chung6e314082011-01-27 16:46:51 -0800325 public CellLayoutChildren getChildrenLayout() {
326 if (getChildCount() > 0) {
327 return (CellLayoutChildren) getChildAt(0);
328 }
329 return null;
330 }
331
Michael Jurka33945b22010-12-21 18:19:38 -0800332 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
333 if (mIsDefaultDropTarget != isDefaultDropTarget) {
334 mIsDefaultDropTarget = isDefaultDropTarget;
335 invalidate();
336 }
337 }
338
Michael Jurka33945b22010-12-21 18:19:38 -0800339 void setIsDragOccuring(boolean isDragOccuring) {
340 if (mIsDragOccuring != isDragOccuring) {
341 mIsDragOccuring = isDragOccuring;
342 invalidate();
343 }
344 }
345
346 void setIsDragOverlapping(boolean isDragOverlapping) {
347 if (mIsDragOverlapping != isDragOverlapping) {
348 mIsDragOverlapping = isDragOverlapping;
349 invalidate();
350 }
351 }
352
353 boolean getIsDragOverlapping() {
354 return mIsDragOverlapping;
355 }
356
357 private void updateGlowRect() {
358 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700359 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
360 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800361 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700362 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
363 invalidate();
364 }
365
366 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800367 if (scaleFactor != mGlowBackgroundScale) {
368 mGlowBackgroundScale = scaleFactor;
369 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800370 if (getParent() != null) {
371 ((View) getParent()).invalidate();
372 }
Michael Jurka18014792010-10-14 09:01:34 -0700373 }
374 }
375
376 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800377 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700378 }
379
380 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800381 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700382 }
383
384 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800385 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700386 invalidate();
387 }
388
389 void animateDrop() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700390 Resources res = getResources();
391 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
392 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
393 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
394 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
395 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
396 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
Michael Jurka18014792010-10-14 09:01:34 -0700397
Winson Chungb26f3d62011-06-02 10:49:29 -0700398 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
399 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDuration));
Michael Jurka18014792010-10-14 09:01:34 -0700400
Winson Chungb26f3d62011-06-02 10:49:29 -0700401 AnimatorSet bouncer = new AnimatorSet();
402 bouncer.play(scaleUp).before(scaleDown);
403 bouncer.play(scaleUp).with(alphaFadeOut);
404 bouncer.addListener(new AnimatorListenerAdapter() {
405 @Override
406 public void onAnimationStart(Animator animation) {
407 setIsDragOverlapping(true);
408 }
409 @Override
410 public void onAnimationEnd(Animator animation) {
411 setIsDragOverlapping(false);
412 setHoverScale(1.0f);
413 setHoverAlpha(1.0f);
414 }
415 });
416 bouncer.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 }
418
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700419 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700420 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700421 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
422 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
423 // When we're small, we are either drawn normally or in the "accepts drops" state (during
424 // a drag). However, we also drag the mini hover background *over* one of those two
425 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700426 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700427 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800428 boolean mini = getScaleX() < 0.5f;
429
430 if (mIsDragOverlapping) {
431 // In the mini case, we draw the active_glow bg *over* the active background
432 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
433 } else if (mIsDragOccuring && mAcceptsDrops) {
434 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800435 } else if (mIsDefaultDropTarget && mini) {
436 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700437 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800438 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700439 }
Michael Jurka33945b22010-12-21 18:19:38 -0800440
441 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
442 bg.setBounds(mBackgroundRect);
443 bg.draw(canvas);
444
445 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700446 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800447 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700448 // If the hover background's scale is greater than 1, we'll be drawing outside
449 // the bounds of this CellLayout. Get around that by temporarily increasing the
450 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800451 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700452 Rect clipRect = canvas.getClipBounds();
453 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
454 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
455 canvas.save(Canvas.CLIP_SAVE_FLAG);
456 canvas.clipRect(-marginX, -marginY,
457 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
458 modifiedClipRect = true;
459 }
460
Michael Jurka33945b22010-12-21 18:19:38 -0800461 mActiveGlowBackgroundMini.setAlpha(
462 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
463 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
464 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700465 if (modifiedClipRect) {
466 canvas.restore();
467 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700468 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700469 }
Romain Guya6abce82009-11-10 02:54:41 -0800470
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700471 if (mCrosshairsVisibility > 0.0f) {
472 final int countX = mCountX;
473 final int countY = mCountY;
474
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700475 final float MAX_ALPHA = 0.4f;
476 final int MAX_VISIBLE_DISTANCE = 600;
477 final float DISTANCE_MULTIPLIER = 0.002f;
478
479 final Drawable d = mCrosshairsDrawable;
480 final int width = d.getIntrinsicWidth();
481 final int height = d.getIntrinsicHeight();
482
Winson Chung4b825dcd2011-06-19 12:41:22 -0700483 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700484 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700485 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700486 for (int row = 0; row <= countY; row++) {
487 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
488 float dist = mTmpPointF.length();
489 // Crosshairs further from the drag point are more faint
490 float alpha = Math.min(MAX_ALPHA,
491 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
492 if (alpha > 0.0f) {
493 d.setBounds(x, y, x + width, y + height);
494 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
495 d.draw(canvas);
496 }
497 y += mCellHeight + mHeightGap;
498 }
499 x += mCellWidth + mWidthGap;
500 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700501 }
Winson Chung150fbab2010-09-29 17:14:26 -0700502
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700503 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700504 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700505 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700506 if (alpha > 0) {
507 final Point p = mDragOutlines[i];
508 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700509 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700510 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700511 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700512 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800513
514 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
515 // requires an expanded clip rect (due to the glow's blur radius)
516 if (mPressedOrFocusedIcon != null) {
517 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
518 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
519 if (b != null) {
520 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700521 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
522 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800523 null);
524 }
525 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700526
527 // The folder outer / inner ring image(s)
528 for (int i = 0; i < mFolderOuterRings.size(); i++) {
529 FolderRingAnimator fra = mFolderOuterRings.get(i);
530
531 // Draw outer ring
532 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
533 int width = (int) fra.getOuterRingSize();
534 int height = width;
535 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
536
537 int centerX = mTempLocation[0] + mCellWidth / 2;
538 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
539
540 canvas.save();
541 canvas.translate(centerX - width / 2, centerY - height / 2);
542 d.setBounds(0, 0, width, height);
543 d.draw(canvas);
544 canvas.restore();
545
546 // Draw inner ring
547 d = FolderRingAnimator.sSharedInnerRingDrawable;
548 width = (int) fra.getInnerRingSize();
549 height = width;
550 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
551
552 centerX = mTempLocation[0] + mCellWidth / 2;
553 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
554 canvas.save();
555 canvas.translate(centerX - width / 2, centerY - width / 2);
556 d.setBounds(0, 0, width, height);
557 d.draw(canvas);
558 canvas.restore();
559 }
560 }
561
562 public void showFolderAccept(FolderRingAnimator fra) {
563 mFolderOuterRings.add(fra);
564 }
565
566 public void hideFolderAccept(FolderRingAnimator fra) {
567 if (mFolderOuterRings.contains(fra)) {
568 mFolderOuterRings.remove(fra);
569 }
570 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700571 }
572
573 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700574 public void cancelLongPress() {
575 super.cancelLongPress();
576
577 // Cancel long press for all children
578 final int count = getChildCount();
579 for (int i = 0; i < count; i++) {
580 final View child = getChildAt(i);
581 child.cancelLongPress();
582 }
583 }
584
Michael Jurkadee05892010-07-27 10:01:56 -0700585 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
586 mInterceptTouchListener = listener;
587 }
588
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700590 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 }
592
593 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700594 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 }
596
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700597 public boolean addViewToCellLayout(
598 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700599 final LayoutParams lp = params;
600
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 // Generate an id for each view, this assumes we have at most 256x256 cells
602 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700603 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700604 // If the horizontal or vertical span is set to -1, it is taken to
605 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700606 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
607 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608
Winson Chungaafa03c2010-06-11 17:34:16 -0700609 child.setId(childId);
610
Michael Jurka8c920dd2011-01-20 14:16:56 -0800611 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700612
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700613 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700614
Winson Chungaafa03c2010-06-11 17:34:16 -0700615 return true;
616 }
617 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700619
Michael Jurkabea15192010-11-17 12:33:46 -0800620 public void setAcceptsDrops(boolean acceptsDrops) {
621 if (mAcceptsDrops != acceptsDrops) {
622 mAcceptsDrops = acceptsDrops;
623 invalidate();
624 }
625 }
626
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700628 public void removeAllViews() {
629 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800630 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700631 }
632
633 @Override
634 public void removeAllViewsInLayout() {
635 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800636 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700637 }
638
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700639 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800640 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700641 }
642
Michael Jurka0280c3b2010-09-17 15:00:07 -0700643 @Override
644 public void removeView(View view) {
645 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800646 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700647 }
648
649 @Override
650 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800651 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
652 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700653 }
654
655 @Override
656 public void removeViewInLayout(View view) {
657 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800658 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700659 }
660
661 @Override
662 public void removeViews(int start, int count) {
663 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800664 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700665 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800666 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700667 }
668
669 @Override
670 public void removeViewsInLayout(int start, int count) {
671 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800672 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700673 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800674 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700675 }
676
Michael Jurka8c920dd2011-01-20 14:16:56 -0800677 public void drawChildren(Canvas canvas) {
678 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
680
Michael Jurkaabded662011-03-04 12:06:57 -0800681 void buildChildrenLayer() {
682 mChildren.buildLayer();
683 }
684
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 @Override
686 protected void onAttachedToWindow() {
687 super.onAttachedToWindow();
688 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
689 }
690
Michael Jurkaaf442092010-06-10 17:01:57 -0700691 public void setTagToCellInfoForPoint(int touchX, int touchY) {
692 final CellInfo cellInfo = mCellInfo;
693 final Rect frame = mRect;
694 final int x = touchX + mScrollX;
695 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800696 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700697
698 boolean found = false;
699 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800700 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800701 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700702
Adam Cohen1b607ed2011-03-03 17:26:50 -0800703 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
704 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700705 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700706
707 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
708 // offset that by this CellLayout's padding to test an (x,y) point that is relative
709 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700710 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700711
Michael Jurkaaf442092010-06-10 17:01:57 -0700712 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700713 cellInfo.cell = child;
714 cellInfo.cellX = lp.cellX;
715 cellInfo.cellY = lp.cellY;
716 cellInfo.spanX = lp.cellHSpan;
717 cellInfo.spanY = lp.cellVSpan;
718 cellInfo.valid = true;
719 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700720 break;
721 }
722 }
723 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700724
Michael Jurkaaf442092010-06-10 17:01:57 -0700725 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700726 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700727 pointToCellExact(x, y, cellXY);
728
Michael Jurkaaf442092010-06-10 17:01:57 -0700729 cellInfo.cell = null;
730 cellInfo.cellX = cellXY[0];
731 cellInfo.cellY = cellXY[1];
732 cellInfo.spanX = 1;
733 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700734 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
735 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700736 }
737 setTag(cellInfo);
738 }
739
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 @Override
741 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700742 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
743 return true;
744 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 final int action = ev.getAction();
746 final CellInfo cellInfo = mCellInfo;
747
748 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700749 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 } else if (action == MotionEvent.ACTION_UP) {
751 cellInfo.cell = null;
752 cellInfo.cellX = -1;
753 cellInfo.cellY = -1;
754 cellInfo.spanX = 0;
755 cellInfo.spanY = 0;
756 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 setTag(cellInfo);
758 }
759
760 return false;
761 }
762
763 @Override
764 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700765 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 }
767
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700768 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700769 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 * @param x X coordinate of the point
771 * @param y Y coordinate of the point
772 * @param result Array of 2 ints to hold the x and y coordinate of the cell
773 */
774 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700775 final int hStartPadding = getPaddingLeft();
776 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777
778 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
779 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
780
Adam Cohend22015c2010-07-26 22:02:18 -0700781 final int xAxis = mCountX;
782 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783
784 if (result[0] < 0) result[0] = 0;
785 if (result[0] >= xAxis) result[0] = xAxis - 1;
786 if (result[1] < 0) result[1] = 0;
787 if (result[1] >= yAxis) result[1] = yAxis - 1;
788 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700789
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 /**
791 * Given a point, return the cell that most closely encloses that point
792 * @param x X coordinate of the point
793 * @param y Y coordinate of the point
794 * @param result Array of 2 ints to hold the x and y coordinate of the cell
795 */
796 void pointToCellRounded(int x, int y, int[] result) {
797 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
798 }
799
800 /**
801 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700802 *
803 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700805 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 * @param result Array of 2 ints to hold the x and y coordinate of the point
807 */
808 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700809 final int hStartPadding = getPaddingLeft();
810 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811
812 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
813 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
814 }
815
Adam Cohene3e27a82011-04-15 12:07:39 -0700816 /**
817 * Given a cell coordinate, return the point that represents the upper left corner of that cell
818 *
819 * @param cellX X coordinate of the cell
820 * @param cellY Y coordinate of the cell
821 *
822 * @param result Array of 2 ints to hold the x and y coordinate of the point
823 */
824 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700825 final int hStartPadding = getPaddingLeft();
826 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700827
828 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
829 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
830 }
831
Romain Guy84f296c2009-11-04 15:00:44 -0800832 int getCellWidth() {
833 return mCellWidth;
834 }
835
836 int getCellHeight() {
837 return mCellHeight;
838 }
839
Adam Cohend4844c32011-02-18 19:25:06 -0800840 int getWidthGap() {
841 return mWidthGap;
842 }
843
844 int getHeightGap() {
845 return mHeightGap;
846 }
847
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700848 Rect getContentRect(Rect r) {
849 if (r == null) {
850 r = new Rect();
851 }
852 int left = getPaddingLeft();
853 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700854 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
855 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700856 r.set(left, top, right, bottom);
857 return r;
858 }
859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 @Override
861 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
862 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700863
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700865 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
866
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
868 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700869
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
871 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
872 }
873
Adam Cohend22015c2010-07-26 22:02:18 -0700874 int numWidthGaps = mCountX - 1;
875 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876
Adam Cohen234c4cd2011-07-17 21:03:04 -0700877 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700878 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
879 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
880 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
881 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
882 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
883 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
884 int remainingHSpace = hFreeSpace - (numWidthGaps * mWidthGap);
885 int remainingVSpace = vFreeSpace - (numHeightGaps * mHeightGap);
886 mCellWidth = mOriginalCellWidth + remainingHSpace / mCountX;
887 mCellHeight = mOriginalCellHeight + remainingVSpace / mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888
Winson Chung4b825dcd2011-06-19 12:41:22 -0700889 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700890 } else {
891 mWidthGap = mOriginalWidthGap;
892 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700893 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700894
Michael Jurka8c920dd2011-01-20 14:16:56 -0800895 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
896 int newWidth = widthSpecSize;
897 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700898 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700899 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700900 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700901 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700902 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700903 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700904 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800905
906 int count = getChildCount();
907 for (int i = 0; i < count; i++) {
908 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700909 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
910 mPaddingRight, MeasureSpec.EXACTLY);
911 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
912 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800913 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
914 }
915 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 }
917
918 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700919 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800922 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700923 child.layout(mPaddingLeft, mPaddingTop,
924 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 }
926 }
927
928 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700929 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
930 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700931 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800932 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700933 }
934
935 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800937 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
940 @Override
941 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800942 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 }
944
Michael Jurka5f1c5092010-09-03 14:15:02 -0700945 public float getBackgroundAlpha() {
946 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700947 }
948
Michael Jurka742574b2011-02-02 23:51:01 -0800949 public void setFastBackgroundAlpha(float alpha) {
950 mBackgroundAlpha = alpha;
951 }
952
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700953 public void setBackgroundAlphaMultiplier(float multiplier) {
954 mBackgroundAlphaMultiplier = multiplier;
955 }
956
Adam Cohenddb82192010-11-10 16:32:54 -0800957 public float getBackgroundAlphaMultiplier() {
958 return mBackgroundAlphaMultiplier;
959 }
960
Michael Jurka5f1c5092010-09-03 14:15:02 -0700961 public void setBackgroundAlpha(float alpha) {
962 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700963 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700964 }
965
Michael Jurka5f1c5092010-09-03 14:15:02 -0700966 // Need to return true to let the view system know we know how to handle alpha-- this is
967 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
968 // versions
969 @Override
970 protected boolean onSetAlpha(int alpha) {
971 return true;
972 }
973
974 public void setAlpha(float alpha) {
975 setChildrenAlpha(alpha);
976 super.setAlpha(alpha);
977 }
978
Michael Jurka742574b2011-02-02 23:51:01 -0800979 public void setFastAlpha(float alpha) {
980 setFastChildrenAlpha(alpha);
981 super.setFastAlpha(alpha);
982 }
983
Michael Jurkadee05892010-07-27 10:01:56 -0700984 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700985 final int childCount = getChildCount();
986 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700987 getChildAt(i).setAlpha(alpha);
988 }
989 }
990
Michael Jurka742574b2011-02-02 23:51:01 -0800991 private void setFastChildrenAlpha(float alpha) {
992 final int childCount = getChildCount();
993 for (int i = 0; i < childCount; i++) {
994 getChildAt(i).setFastAlpha(alpha);
995 }
996 }
997
Patrick Dubroy440c3602010-07-13 17:50:32 -0700998 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800999 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001000 }
1001
Adam Cohen76fc0852011-06-17 13:26:23 -07001002 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
1003 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001004 CellLayoutChildren clc = getChildrenLayout();
1005 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
1006 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1007 final ItemInfo info = (ItemInfo) child.getTag();
1008
1009 // We cancel any existing animations
1010 if (mReorderAnimators.containsKey(lp)) {
1011 mReorderAnimators.get(lp).cancel();
1012 mReorderAnimators.remove(lp);
1013 }
1014
1015 int oldX = lp.x;
1016 int oldY = lp.y;
1017 mOccupied[lp.cellX][lp.cellY] = false;
1018 mOccupied[cellX][cellY] = true;
1019
1020 lp.isLockedToGrid = true;
1021 lp.cellX = info.cellX = cellX;
1022 lp.cellY = info.cellY = cellY;
1023 clc.setupLp(lp);
1024 lp.isLockedToGrid = false;
1025 int newX = lp.x;
1026 int newY = lp.y;
1027
Adam Cohen76fc0852011-06-17 13:26:23 -07001028 lp.x = oldX;
1029 lp.y = oldY;
1030 child.requestLayout();
1031
Adam Cohenbfbfd262011-06-13 16:55:12 -07001032 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
1033 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
1034 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
1035 oa.setDuration(duration);
1036 mReorderAnimators.put(lp, oa);
1037 oa.addUpdateListener(new AnimatorUpdateListener() {
1038 public void onAnimationUpdate(ValueAnimator animation) {
1039 child.requestLayout();
1040 }
1041 });
1042 oa.addListener(new AnimatorListenerAdapter() {
1043 boolean cancelled = false;
1044 public void onAnimationEnd(Animator animation) {
1045 // If the animation was cancelled, it means that another animation
1046 // has interrupted this one, and we don't want to lock the item into
1047 // place just yet.
1048 if (!cancelled) {
1049 lp.isLockedToGrid = true;
1050 }
1051 if (mReorderAnimators.containsKey(lp)) {
1052 mReorderAnimators.remove(lp);
1053 }
1054 }
1055 public void onAnimationCancel(Animator animation) {
1056 cancelled = true;
1057 }
1058 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001059 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001060 oa.start();
1061 return true;
1062 }
1063 return false;
1064 }
1065
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001066 /**
1067 * Estimate where the top left cell of the dragged item will land if it is dropped.
1068 *
1069 * @param originX The X value of the top left corner of the item
1070 * @param originY The Y value of the top left corner of the item
1071 * @param spanX The number of horizontal cells that the item spans
1072 * @param spanY The number of vertical cells that the item spans
1073 * @param result The estimated drop cell X and Y.
1074 */
1075 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001076 final int countX = mCountX;
1077 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001078
Michael Jurkaa63c4522010-08-19 13:52:27 -07001079 // pointToCellRounded takes the top left of a cell but will pad that with
1080 // cellWidth/2 and cellHeight/2 when finding the matching cell
1081 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001082
1083 // If the item isn't fully on this screen, snap to the edges
1084 int rightOverhang = result[0] + spanX - countX;
1085 if (rightOverhang > 0) {
1086 result[0] -= rightOverhang; // Snap to right
1087 }
1088 result[0] = Math.max(0, result[0]); // Snap to left
1089 int bottomOverhang = result[1] + spanY - countY;
1090 if (bottomOverhang > 0) {
1091 result[1] -= bottomOverhang; // Snap to bottom
1092 }
1093 result[1] = Math.max(0, result[1]); // Snap to top
1094 }
1095
Joe Onorato4be866d2010-10-10 11:26:02 -07001096 void visualizeDropLocation(
1097 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1098
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001099 final int oldDragCellX = mDragCell[0];
1100 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001101 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001102 if (v != null) {
1103 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1104 } else {
1105 mDragCenter.set(originX, originY);
1106 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001107
Adam Cohen2801caf2011-05-13 20:57:39 -07001108 if (dragOutline == null && v == null) {
1109 if (mCrosshairsDrawable != null) {
1110 invalidate();
1111 }
1112 return;
1113 }
1114
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001115 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001116 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001117 final int[] topLeft = mTmpPoint;
1118 cellToPoint(nearest[0], nearest[1], topLeft);
1119
Joe Onorato4be866d2010-10-10 11:26:02 -07001120 int left = topLeft[0];
1121 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001122
Winson Chunga9abd0e2010-10-27 17:18:37 -07001123 if (v != null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001124 // When drawing the drag outline, it did not account for margin offsets
1125 // added by the view's parent.
1126 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1127 left += lp.leftMargin;
1128 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001129
Adam Cohen99e8b402011-03-25 19:23:43 -07001130 // Offsets due to the size difference between the View and the dragOutline.
1131 // There is a size difference to account for the outer blur, which may lie
1132 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001133 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1134 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001135 } else {
1136 // Center the drag outline in the cell
Winson Chung4b576be2011-04-27 17:40:20 -07001137 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1138 - dragOutline.getWidth()) / 2;
1139 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1140 - dragOutline.getHeight()) / 2;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001141 }
Winson Chung150fbab2010-09-29 17:14:26 -07001142
Joe Onorato4be866d2010-10-10 11:26:02 -07001143 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001144 mDragOutlineAnims[oldIndex].animateOut();
1145 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001146
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001147 mDragOutlines[mDragOutlineCurrent].set(left, top);
1148 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1149 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001150 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001151
1152 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1153 if (mCrosshairsDrawable != null) {
1154 invalidate();
1155 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001156 }
1157
Adam Cohene0310962011-04-18 16:15:31 -07001158 public void clearDragOutlines() {
1159 final int oldIndex = mDragOutlineCurrent;
1160 mDragOutlineAnims[oldIndex].animateOut();
1161 mDragCell[0] = -1;
1162 mDragCell[1] = -1;
1163 }
1164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001166 * Find a vacant area that will fit the given bounds nearest the requested
1167 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001168 *
Romain Guy51afc022009-05-04 18:03:43 -07001169 * @param pixelX The X location at which you want to search for a vacant area.
1170 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001171 * @param spanX Horizontal span of the object.
1172 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001173 * @param result Array in which to place the result, or null (in which case a new array will
1174 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001175 * @return The X, Y cell of a vacant area that can contain this object,
1176 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001178 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001179 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1180 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001181 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001182
Michael Jurka6a1435d2010-09-27 17:35:12 -07001183 /**
1184 * Find a vacant area that will fit the given bounds nearest the requested
1185 * cell location. Uses Euclidean distance to score multiple vacant areas.
1186 *
1187 * @param pixelX The X location at which you want to search for a vacant area.
1188 * @param pixelY The Y location at which you want to search for a vacant area.
1189 * @param spanX Horizontal span of the object.
1190 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001191 * @param ignoreOccupied If true, the result can be an occupied cell
1192 * @param result Array in which to place the result, or null (in which case a new array will
1193 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001194 * @return The X, Y cell of a vacant area that can contain this object,
1195 * nearest the requested location.
1196 */
Adam Cohendf035382011-04-11 17:22:04 -07001197 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1198 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001199 // mark space take by ignoreView as available (method checks if ignoreView is null)
1200 markCellsAsUnoccupiedForView(ignoreView);
1201
Adam Cohene3e27a82011-04-15 12:07:39 -07001202 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1203 // to the center of the item, but we are searching based on the top-left cell, so
1204 // we translate the point over to correspond to the top-left.
1205 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1206 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1207
Jeff Sharkey70864282009-04-07 21:08:40 -07001208 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001209 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001210 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001211
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001212 final int countX = mCountX;
1213 final int countY = mCountY;
1214 final boolean[][] occupied = mOccupied;
1215
Winson Chungbbc60d82010-11-11 16:34:41 -08001216 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001217 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001218 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001219 if (ignoreOccupied) {
1220 for (int i = 0; i < spanX; i++) {
1221 for (int j = 0; j < spanY; j++) {
1222 if (occupied[x + i][y + j]) {
1223 // small optimization: we can skip to after the column we
1224 // just found an occupied cell
1225 x += i;
1226 continue inner;
1227 }
Michael Jurkac28de512010-08-13 11:27:44 -07001228 }
1229 }
1230 }
Winson Chung0be025d2011-05-23 17:45:09 -07001231 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001232 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233
Michael Jurkac28de512010-08-13 11:27:44 -07001234 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1235 + Math.pow(cellXY[1] - pixelY, 2));
1236 if (distance <= bestDistance) {
1237 bestDistance = distance;
1238 bestXY[0] = x;
1239 bestXY[1] = y;
1240 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 }
1242 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001243 // re-mark space taken by ignoreView as occupied
1244 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245
Adam Cohenc0dcf592011-06-01 15:30:43 -07001246 // Return -1, -1 if no suitable location found
1247 if (bestDistance == Double.MAX_VALUE) {
1248 bestXY[0] = -1;
1249 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001250 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001251 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001253
Adam Cohendf035382011-04-11 17:22:04 -07001254 /**
1255 * Find a vacant area that will fit the given bounds nearest the requested
1256 * cell location. Uses Euclidean distance to score multiple vacant areas.
1257 *
1258 * @param pixelX The X location at which you want to search for a vacant area.
1259 * @param pixelY The Y location at which you want to search for a vacant area.
1260 * @param spanX Horizontal span of the object.
1261 * @param spanY Vertical span of the object.
1262 * @param ignoreView Considers space occupied by this view as unoccupied
1263 * @param result Previously returned value to possibly recycle.
1264 * @return The X, Y cell of a vacant area that can contain this object,
1265 * nearest the requested location.
1266 */
1267 int[] findNearestVacantArea(
1268 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1269 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1270 }
1271
1272 /**
1273 * Find a starting cell position that will fit the given bounds nearest the requested
1274 * cell location. Uses Euclidean distance to score multiple vacant areas.
1275 *
1276 * @param pixelX The X location at which you want to search for a vacant area.
1277 * @param pixelY The Y location at which you want to search for a vacant area.
1278 * @param spanX Horizontal span of the object.
1279 * @param spanY Vertical span of the object.
1280 * @param ignoreView Considers space occupied by this view as unoccupied
1281 * @param result Previously returned value to possibly recycle.
1282 * @return The X, Y cell of a vacant area that can contain this object,
1283 * nearest the requested location.
1284 */
1285 int[] findNearestArea(
1286 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1287 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1288 }
1289
Michael Jurka0280c3b2010-09-17 15:00:07 -07001290 boolean existsEmptyCell() {
1291 return findCellForSpan(null, 1, 1);
1292 }
1293
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001295 * Finds the upper-left coordinate of the first rectangle in the grid that can
1296 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1297 * then this method will only return coordinates for rectangles that contain the cell
1298 * (intersectX, intersectY)
1299 *
1300 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1301 * can be found.
1302 * @param spanX The horizontal span of the cell we want to find.
1303 * @param spanY The vertical span of the cell we want to find.
1304 *
1305 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001306 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001307 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1308 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1309 }
1310
1311 /**
1312 * Like above, but ignores any cells occupied by the item "ignoreView"
1313 *
1314 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1315 * can be found.
1316 * @param spanX The horizontal span of the cell we want to find.
1317 * @param spanY The vertical span of the cell we want to find.
1318 * @param ignoreView The home screen item we should treat as not occupying any space
1319 * @return
1320 */
1321 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1322 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1323 }
1324
1325 /**
1326 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1327 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1328 *
1329 * @param spanX The horizontal span of the cell we want to find.
1330 * @param spanY The vertical span of the cell we want to find.
1331 * @param ignoreView The home screen item we should treat as not occupying any space
1332 * @param intersectX The X coordinate of the cell that we should try to overlap
1333 * @param intersectX The Y coordinate of the cell that we should try to overlap
1334 *
1335 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1336 */
1337 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1338 int intersectX, int intersectY) {
1339 return findCellForSpanThatIntersectsIgnoring(
1340 cellXY, spanX, spanY, intersectX, intersectY, null);
1341 }
1342
1343 /**
1344 * The superset of the above two methods
1345 */
1346 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1347 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001348 // mark space take by ignoreView as available (method checks if ignoreView is null)
1349 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001350
Michael Jurka28750fb2010-09-24 17:43:49 -07001351 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001352 while (true) {
1353 int startX = 0;
1354 if (intersectX >= 0) {
1355 startX = Math.max(startX, intersectX - (spanX - 1));
1356 }
1357 int endX = mCountX - (spanX - 1);
1358 if (intersectX >= 0) {
1359 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1360 }
1361 int startY = 0;
1362 if (intersectY >= 0) {
1363 startY = Math.max(startY, intersectY - (spanY - 1));
1364 }
1365 int endY = mCountY - (spanY - 1);
1366 if (intersectY >= 0) {
1367 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1368 }
1369
Winson Chungbbc60d82010-11-11 16:34:41 -08001370 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001371 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001372 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001373 for (int i = 0; i < spanX; i++) {
1374 for (int j = 0; j < spanY; j++) {
1375 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001376 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001377 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001378 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001379 continue inner;
1380 }
1381 }
1382 }
1383 if (cellXY != null) {
1384 cellXY[0] = x;
1385 cellXY[1] = y;
1386 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001387 foundCell = true;
1388 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001389 }
1390 }
1391 if (intersectX == -1 && intersectY == -1) {
1392 break;
1393 } else {
1394 // if we failed to find anything, try again but without any requirements of
1395 // intersecting
1396 intersectX = -1;
1397 intersectY = -1;
1398 continue;
1399 }
1400 }
1401
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001402 // re-mark space taken by ignoreView as occupied
1403 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001404 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001405 }
1406
1407 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001408 * A drag event has begun over this layout.
1409 * It may have begun over this layout (in which case onDragChild is called first),
1410 * or it may have begun on another layout.
1411 */
1412 void onDragEnter() {
1413 if (!mDragging) {
1414 // Fade in the drag indicators
1415 if (mCrosshairsAnimator != null) {
1416 mCrosshairsAnimator.animateIn();
1417 }
1418 }
1419 mDragging = true;
1420 }
1421
1422 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001423 * Called when drag has left this CellLayout or has been completed (successfully or not)
1424 */
1425 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001426 // This can actually be called when we aren't in a drag, e.g. when adding a new
1427 // item to this layout via the customize drawer.
1428 // Guard against that case.
1429 if (mDragging) {
1430 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001431
Joe Onorato4be866d2010-10-10 11:26:02 -07001432 // Fade out the drag indicators
1433 if (mCrosshairsAnimator != null) {
1434 mCrosshairsAnimator.animateOut();
1435 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001436 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001437
1438 // Invalidate the drag data
1439 mDragCell[0] = -1;
1440 mDragCell[1] = -1;
1441 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1442 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1443
Michael Jurka33945b22010-12-21 18:19:38 -08001444 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001445 }
1446
1447 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001448 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001449 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001450 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 *
1452 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001454 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001455 if (child != null) {
1456 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001457 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001458 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001459 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 }
1462
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 /**
1464 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001465 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 * @param child The child that is being dragged
1467 */
1468 void onDragChild(View child) {
1469 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1470 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001471 }
1472
1473 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001475 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 * @param cellX X coordinate of upper left corner expressed as a cell position
1477 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001478 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001480 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001482 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 final int cellWidth = mCellWidth;
1484 final int cellHeight = mCellHeight;
1485 final int widthGap = mWidthGap;
1486 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001487
Winson Chung4b825dcd2011-06-19 12:41:22 -07001488 final int hStartPadding = getPaddingLeft();
1489 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001490
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1492 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1493
1494 int x = hStartPadding + cellX * (cellWidth + widthGap);
1495 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001496
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001497 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001499
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001501 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001503 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 * @param width Width in pixels
1505 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001506 * @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 -08001507 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001508 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001509 return rectToCell(getResources(), width, height, result);
1510 }
1511
1512 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 // Always assume we're working with the smallest span to make sure we
1514 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001515 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1516 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001518
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 // Always round up to next largest cell
1520 int spanX = (width + smallerSize) / smallerSize;
1521 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001522
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001523 if (result == null) {
1524 return new int[] { spanX, spanY };
1525 }
1526 result[0] = spanX;
1527 result[1] = spanY;
1528 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 }
1530
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001531 public int[] cellSpansToSize(int hSpans, int vSpans) {
1532 int[] size = new int[2];
1533 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1534 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1535 return size;
1536 }
1537
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001539 * Calculate the grid spans needed to fit given item
1540 */
1541 public void calculateSpans(ItemInfo info) {
1542 final int minWidth;
1543 final int minHeight;
1544
1545 if (info instanceof LauncherAppWidgetInfo) {
1546 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1547 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1548 } else if (info instanceof PendingAddWidgetInfo) {
1549 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1550 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1551 } else {
1552 // It's not a widget, so it must be 1x1
1553 info.spanX = info.spanY = 1;
1554 return;
1555 }
1556 int[] spans = rectToCell(minWidth, minHeight, null);
1557 info.spanX = spans[0];
1558 info.spanY = spans[1];
1559 }
1560
1561 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 * Find the first vacant cell, if there is one.
1563 *
1564 * @param vacant Holds the x and y coordinate of the vacant cell
1565 * @param spanX Horizontal cell span.
1566 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001567 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 * @return True if a vacant cell was found
1569 */
1570 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571
Michael Jurka0280c3b2010-09-17 15:00:07 -07001572 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
1574
1575 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1576 int xCount, int yCount, boolean[][] occupied) {
1577
Adam Cohen2801caf2011-05-13 20:57:39 -07001578 for (int y = 0; y < yCount; y++) {
1579 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 boolean available = !occupied[x][y];
1581out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1582 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1583 available = available && !occupied[i][j];
1584 if (!available) break out;
1585 }
1586 }
1587
1588 if (available) {
1589 vacant[0] = x;
1590 vacant[1] = y;
1591 return true;
1592 }
1593 }
1594 }
1595
1596 return false;
1597 }
1598
Michael Jurka0280c3b2010-09-17 15:00:07 -07001599 private void clearOccupiedCells() {
1600 for (int x = 0; x < mCountX; x++) {
1601 for (int y = 0; y < mCountY; y++) {
1602 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 }
1604 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001605 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606
Adam Cohen1b607ed2011-03-03 17:26:50 -08001607 /**
1608 * Given a view, determines how much that view can be expanded in all directions, in terms of
1609 * whether or not there are other items occupying adjacent cells. Used by the
1610 * AppWidgetResizeFrame to determine how the widget can be resized.
1611 */
Adam Cohend4844c32011-02-18 19:25:06 -08001612 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001613 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001614 boolean flag;
1615
Adam Cohen1b607ed2011-03-03 17:26:50 -08001616 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001617 for (int x = lp.cellX - 1; x >= 0; x--) {
1618 flag = false;
1619 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1620 if (mOccupied[x][y]) flag = true;
1621 }
1622 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001623 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001624 }
1625
Adam Cohen1b607ed2011-03-03 17:26:50 -08001626 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001627 for (int y = lp.cellY - 1; y >= 0; y--) {
1628 flag = false;
1629 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1630 if (mOccupied[x][y]) flag = true;
1631 }
1632 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001633 expandability[AppWidgetResizeFrame.TOP]++;
1634 }
Adam Cohend4844c32011-02-18 19:25:06 -08001635
Adam Cohen1b607ed2011-03-03 17:26:50 -08001636 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001637 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1638 flag = false;
1639 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1640 if (mOccupied[x][y]) flag = true;
1641 }
1642 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001643 expandability[AppWidgetResizeFrame.RIGHT]++;
1644 }
Adam Cohend4844c32011-02-18 19:25:06 -08001645
Adam Cohen1b607ed2011-03-03 17:26:50 -08001646 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001647 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1648 flag = false;
1649 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1650 if (mOccupied[x][y]) flag = true;
1651 }
1652 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001653 expandability[AppWidgetResizeFrame.BOTTOM]++;
1654 }
Adam Cohend4844c32011-02-18 19:25:06 -08001655 }
1656
Michael Jurka0280c3b2010-09-17 15:00:07 -07001657 public void onMove(View view, int newCellX, int newCellY) {
1658 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1659 markCellsAsUnoccupiedForView(view);
1660 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1661 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662
Adam Cohend4844c32011-02-18 19:25:06 -08001663 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001664 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001665 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1666 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1667 }
1668
Adam Cohend4844c32011-02-18 19:25:06 -08001669 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001670 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001671 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1672 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1673 }
1674
1675 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1676 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1677 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1678 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
1680 }
1681 }
1682
Adam Cohen2801caf2011-05-13 20:57:39 -07001683 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001684 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001685 (Math.max((mCountX - 1), 0) * mWidthGap);
1686 }
1687
1688 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001689 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001690 (Math.max((mCountY - 1), 0) * mHeightGap);
1691 }
1692
Michael Jurka66d72172011-04-12 16:29:25 -07001693 public boolean isOccupied(int x, int y) {
1694 if (x < mCountX && y < mCountY) {
1695 return mOccupied[x][y];
1696 } else {
1697 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1698 }
1699 }
1700
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 @Override
1702 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1703 return new CellLayout.LayoutParams(getContext(), attrs);
1704 }
1705
1706 @Override
1707 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1708 return p instanceof CellLayout.LayoutParams;
1709 }
1710
1711 @Override
1712 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1713 return new CellLayout.LayoutParams(p);
1714 }
1715
Winson Chungaafa03c2010-06-11 17:34:16 -07001716 public static class CellLayoutAnimationController extends LayoutAnimationController {
1717 public CellLayoutAnimationController(Animation animation, float delay) {
1718 super(animation, delay);
1719 }
1720
1721 @Override
1722 protected long getDelayForView(View view) {
1723 return (int) (Math.random() * 150);
1724 }
1725 }
1726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1728 /**
1729 * Horizontal location of the item in the grid.
1730 */
1731 @ViewDebug.ExportedProperty
1732 public int cellX;
1733
1734 /**
1735 * Vertical location of the item in the grid.
1736 */
1737 @ViewDebug.ExportedProperty
1738 public int cellY;
1739
1740 /**
1741 * Number of cells spanned horizontally by the item.
1742 */
1743 @ViewDebug.ExportedProperty
1744 public int cellHSpan;
1745
1746 /**
1747 * Number of cells spanned vertically by the item.
1748 */
1749 @ViewDebug.ExportedProperty
1750 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001751
Adam Cohen1b607ed2011-03-03 17:26:50 -08001752 /**
1753 * Indicates whether the item will set its x, y, width and height parameters freely,
1754 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1755 */
Adam Cohend4844c32011-02-18 19:25:06 -08001756 public boolean isLockedToGrid = true;
1757
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 /**
1759 * Is this item currently being dragged
1760 */
1761 public boolean isDragging;
1762
1763 // X coordinate of the view in the layout.
1764 @ViewDebug.ExportedProperty
1765 int x;
1766 // Y coordinate of the view in the layout.
1767 @ViewDebug.ExportedProperty
1768 int y;
1769
Romain Guy84f296c2009-11-04 15:00:44 -08001770 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001771
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 public LayoutParams(Context c, AttributeSet attrs) {
1773 super(c, attrs);
1774 cellHSpan = 1;
1775 cellVSpan = 1;
1776 }
1777
1778 public LayoutParams(ViewGroup.LayoutParams source) {
1779 super(source);
1780 cellHSpan = 1;
1781 cellVSpan = 1;
1782 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001783
1784 public LayoutParams(LayoutParams source) {
1785 super(source);
1786 this.cellX = source.cellX;
1787 this.cellY = source.cellY;
1788 this.cellHSpan = source.cellHSpan;
1789 this.cellVSpan = source.cellVSpan;
1790 }
1791
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001793 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 this.cellX = cellX;
1795 this.cellY = cellY;
1796 this.cellHSpan = cellHSpan;
1797 this.cellVSpan = cellVSpan;
1798 }
1799
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001800 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001801 if (isLockedToGrid) {
1802 final int myCellHSpan = cellHSpan;
1803 final int myCellVSpan = cellVSpan;
1804 final int myCellX = cellX;
1805 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001806
Adam Cohend4844c32011-02-18 19:25:06 -08001807 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1808 leftMargin - rightMargin;
1809 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1810 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001811 x = myCellX * (cellWidth + widthGap) + leftMargin;
1812 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001813 }
1814 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001815
Winson Chungaafa03c2010-06-11 17:34:16 -07001816 public String toString() {
1817 return "(" + this.cellX + ", " + this.cellY + ")";
1818 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001819
1820 public void setWidth(int width) {
1821 this.width = width;
1822 }
1823
1824 public int getWidth() {
1825 return width;
1826 }
1827
1828 public void setHeight(int height) {
1829 this.height = height;
1830 }
1831
1832 public int getHeight() {
1833 return height;
1834 }
1835
1836 public void setX(int x) {
1837 this.x = x;
1838 }
1839
1840 public int getX() {
1841 return x;
1842 }
1843
1844 public void setY(int y) {
1845 this.y = y;
1846 }
1847
1848 public int getY() {
1849 return y;
1850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 }
1852
Michael Jurka0280c3b2010-09-17 15:00:07 -07001853 // This class stores info for two purposes:
1854 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1855 // its spanX, spanY, and the screen it is on
1856 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1857 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1858 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001859 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001861 int cellX = -1;
1862 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 int spanX;
1864 int spanY;
1865 int screen;
1866 boolean valid;
1867
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 @Override
1869 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001870 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1871 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 }
1873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874}