blob: ad87fa6036275ec55ffccd5375a9cbf7787a194f [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;
Adam Cohenb5ba0972011-09-07 18:02:31 -070035import android.graphics.PorterDuff;
36import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.graphics.Rect;
38import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070039import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070040import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070041import android.graphics.drawable.NinePatchDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070043import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.view.MotionEvent;
45import android.view.View;
46import android.view.ViewDebug;
47import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070049import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051
Adam Cohen66396872011-04-15 17:50:36 -070052import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070053import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070054
Adam Cohen69ce2e52011-07-03 19:25:21 -070055import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070056import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070057import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -070058
Michael Jurkabdb5c532011-02-01 15:05:06 -080059public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070060 static final String TAG = "CellLayout";
61
Winson Chung4b825dcd2011-06-19 12:41:22 -070062 private int mOriginalCellWidth;
63 private int mOriginalCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 private int mCellWidth;
65 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070066
Adam Cohend22015c2010-07-26 22:02:18 -070067 private int mCountX;
68 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
Adam Cohen234c4cd2011-07-17 21:03:04 -070070 private int mOriginalWidthGap;
71 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 private int mWidthGap;
73 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070074 private int mMaxGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
76 private final Rect mRect = new Rect();
77 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070078
Patrick Dubroyde7658b2010-09-27 11:15:43 -070079 // These are temporary variables to prevent having to allocate a new object just to
80 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070081 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070082 private final int[] mTmpPoint = new int[2];
83 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070084 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070085
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 boolean[][] mOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070087 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Michael Jurkadee05892010-07-27 10:01:56 -070089 private OnTouchListener mInterceptTouchListener;
90
Adam Cohen69ce2e52011-07-03 19:25:21 -070091 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070092 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070093
Adam Cohenb5ba0972011-09-07 18:02:31 -070094 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070095 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070096 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070097
Michael Jurka33945b22010-12-21 18:19:38 -080098 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080099 private Drawable mActiveBackground;
100 private Drawable mActiveGlowBackground;
101 private Drawable mNormalBackgroundMini;
102 private Drawable mNormalGlowBackgroundMini;
103 private Drawable mActiveBackgroundMini;
104 private Drawable mActiveGlowBackgroundMini;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700105 private Drawable mOverScrollForegroundDrawable;
106 private Drawable mOverScrollLeft;
107 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700108 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700109 private Rect mForegroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -0800110 private Rect mGlowBackgroundRect;
111 private float mGlowBackgroundScale;
112 private float mGlowBackgroundAlpha;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700113 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700114
Adam Cohendf035382011-04-11 17:22:04 -0700115 private boolean mAcceptsDrops = true;
Michael Jurka33945b22010-12-21 18:19:38 -0800116 // If we're actively dragging something over this screen, mIsDragOverlapping is true
117 private boolean mIsDragOverlapping = false;
118 private boolean mIsDragOccuring = false;
119 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700120 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700121
Winson Chung150fbab2010-09-29 17:14:26 -0700122 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700123 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Winson Chung63257c12011-05-05 17:06:13 -0700124 private Point[] mDragOutlines = new Point[4];
Chet Haase472b2812010-10-14 07:02:04 -0700125 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700126 private InterruptibleInOutAnimator[] mDragOutlineAnims =
127 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700128
129 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700130 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700131 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700132
Patrick Dubroy96864c32011-03-10 17:17:23 -0800133 private BubbleTextView mPressedOrFocusedIcon;
134
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700135 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700136 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700137 private float mCrosshairsVisibility = 0.0f;
138
Adam Cohenbfbfd262011-06-13 16:55:12 -0700139 private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new
140 HashMap<CellLayout.LayoutParams, ObjectAnimator>();
141
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700142 // When a drag operation is in progress, holds the nearest cell to the touch point
143 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Joe Onorato4be866d2010-10-10 11:26:02 -0700145 private boolean mDragging = false;
146
Patrick Dubroyce34a972010-10-19 10:34:32 -0700147 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800148 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 public CellLayout(Context context) {
151 this(context, null);
152 }
153
154 public CellLayout(Context context, AttributeSet attrs) {
155 this(context, attrs, 0);
156 }
157
158 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
159 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700160
161 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
162 // the user where a dragged item will land when dropped.
163 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
166
Winson Chung4b825dcd2011-06-19 12:41:22 -0700167 mOriginalCellWidth =
168 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
169 mOriginalCellHeight =
170 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700171 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
172 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700173 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700174 mCountX = LauncherModel.getCellCountX();
175 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700176 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
178 a.recycle();
179
180 setAlwaysDrawnWithCacheEnabled(false);
181
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700182 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700183
Winson Chung967289b2011-06-30 18:09:30 -0700184 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700185 mActiveBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
186 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800187
Winson Chungb26f3d62011-06-02 10:49:29 -0700188 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
189 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
Winson Chungdea74b72011-09-13 18:06:43 -0700190 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
191 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700192 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
193 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
194 mForegroundPadding =
195 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800196
Winson Chungb26f3d62011-06-02 10:49:29 -0700197 mNormalBackground.setFilterBitmap(true);
198 mActiveBackground.setFilterBitmap(true);
199 mActiveGlowBackground.setFilterBitmap(true);
200 mNormalBackgroundMini.setFilterBitmap(true);
201 mNormalGlowBackgroundMini.setFilterBitmap(true);
202 mActiveBackgroundMini.setFilterBitmap(true);
203 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700204
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700205 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700206
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700207 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700208 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700209
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700210 // Set up the animation for fading the crosshairs in and out
211 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700212 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700213 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700214 public void onAnimationUpdate(ValueAnimator animation) {
215 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700216 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700217 }
218 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700219 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700220
Joe Onorato4be866d2010-10-10 11:26:02 -0700221 for (int i = 0; i < mDragOutlines.length; i++) {
222 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700223 }
224
225 // When dragging things around the home screens, we show a green outline of
226 // where the item will land. The outlines gradually fade out, leaving a trail
227 // behind the drag path.
228 // Set up all the animations that are used to implement this fading.
229 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700230 final float fromAlphaValue = 0;
231 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700232
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700233 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700234
235 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700236 final InterruptibleInOutAnimator anim =
237 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700238 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700239 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700240 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700241 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700242 final Bitmap outline = (Bitmap)anim.getTag();
243
244 // If an animation is started and then stopped very quickly, we can still
245 // get spurious updates we've cleared the tag. Guard against this.
246 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700247 if (false) {
248 Object val = animation.getAnimatedValue();
249 Log.d(TAG, "anim " + thisIndex + " update: " + val +
250 ", isStopped " + anim.isStopped());
251 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700252 // Try to prevent it from continuing to run
253 animation.cancel();
254 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700255 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700256 final int left = mDragOutlines[thisIndex].x;
257 final int top = mDragOutlines[thisIndex].y;
258 CellLayout.this.invalidate(left, top,
259 left + outline.getWidth(), top + outline.getHeight());
260 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700261 }
262 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700263 // The animation holds a reference to the drag outline bitmap as long is it's
264 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700265 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700266 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700267 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700268 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700269 anim.setTag(null);
270 }
271 }
272 });
273 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700274 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700275
Michael Jurka18014792010-10-14 09:01:34 -0700276 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700277 mForegroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800278 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700279 setHoverScale(1.0f);
280 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800281
Michael Jurka8c920dd2011-01-20 14:16:56 -0800282 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700283 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800284 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700285 }
286
Michael Jurkaf6440da2011-04-05 14:50:34 -0700287 static int widthInPortrait(Resources r, int numCells) {
288 // We use this method from Workspace to figure out how many rows/columns Launcher should
289 // have. We ignore the left/right padding on CellLayout because it turns out in our design
290 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700291 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700292 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
293 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700294
Winson Chung4b825dcd2011-06-19 12:41:22 -0700295 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700296 }
297
Michael Jurkaf6440da2011-04-05 14:50:34 -0700298 static int heightInLandscape(Resources r, int numCells) {
299 // We use this method from Workspace to figure out how many rows/columns Launcher should
300 // have. We ignore the left/right padding on CellLayout because it turns out in our design
301 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700302 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700303 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
304 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700305
Winson Chung4b825dcd2011-06-19 12:41:22 -0700306 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700307 }
308
Adam Cohen2801caf2011-05-13 20:57:39 -0700309 public void enableHardwareLayers() {
Adam Cohen7ef91832011-08-25 14:06:02 -0700310 mChildren.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700311 }
312
313 public void setGridSize(int x, int y) {
314 mCountX = x;
315 mCountY = y;
316 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700317 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700318 }
319
Patrick Dubroy96864c32011-03-10 17:17:23 -0800320 private void invalidateBubbleTextView(BubbleTextView icon) {
321 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700322 invalidate(icon.getLeft() + getPaddingLeft() - padding,
323 icon.getTop() + getPaddingTop() - padding,
324 icon.getRight() + getPaddingLeft() + padding,
325 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800326 }
327
Adam Cohenb5ba0972011-09-07 18:02:31 -0700328 void setOverScrollAmount(float r, boolean left) {
329 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
330 mOverScrollForegroundDrawable = mOverScrollLeft;
331 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
332 mOverScrollForegroundDrawable = mOverScrollRight;
333 }
334
335 mForegroundAlpha = (int) Math.round((r * 255));
336 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
337 invalidate();
338 }
339
Patrick Dubroy96864c32011-03-10 17:17:23 -0800340 void setPressedOrFocusedIcon(BubbleTextView icon) {
341 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
342 // requires an expanded clip rect (due to the glow's blur radius)
343 BubbleTextView oldIcon = mPressedOrFocusedIcon;
344 mPressedOrFocusedIcon = icon;
345 if (oldIcon != null) {
346 invalidateBubbleTextView(oldIcon);
347 }
348 if (mPressedOrFocusedIcon != null) {
349 invalidateBubbleTextView(mPressedOrFocusedIcon);
350 }
351 }
352
Winson Chung6e314082011-01-27 16:46:51 -0800353 public CellLayoutChildren getChildrenLayout() {
354 if (getChildCount() > 0) {
355 return (CellLayoutChildren) getChildAt(0);
356 }
357 return null;
358 }
359
Michael Jurka33945b22010-12-21 18:19:38 -0800360 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
361 if (mIsDefaultDropTarget != isDefaultDropTarget) {
362 mIsDefaultDropTarget = isDefaultDropTarget;
363 invalidate();
364 }
365 }
366
Michael Jurka33945b22010-12-21 18:19:38 -0800367 void setIsDragOccuring(boolean isDragOccuring) {
368 if (mIsDragOccuring != isDragOccuring) {
369 mIsDragOccuring = isDragOccuring;
370 invalidate();
371 }
372 }
373
374 void setIsDragOverlapping(boolean isDragOverlapping) {
375 if (mIsDragOverlapping != isDragOverlapping) {
376 mIsDragOverlapping = isDragOverlapping;
377 invalidate();
378 }
379 }
380
381 boolean getIsDragOverlapping() {
382 return mIsDragOverlapping;
383 }
384
385 private void updateGlowRect() {
386 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700387 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
388 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800389 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700390 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
391 invalidate();
392 }
393
394 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800395 if (scaleFactor != mGlowBackgroundScale) {
396 mGlowBackgroundScale = scaleFactor;
397 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800398 if (getParent() != null) {
399 ((View) getParent()).invalidate();
400 }
Michael Jurka18014792010-10-14 09:01:34 -0700401 }
402 }
403
404 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800405 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700406 }
407
408 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800409 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700410 }
411
412 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800413 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700414 invalidate();
415 }
416
417 void animateDrop() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700418 Resources res = getResources();
419 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
420 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
421 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
422 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
423 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
424 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
Michael Jurka18014792010-10-14 09:01:34 -0700425
Winson Chungb26f3d62011-06-02 10:49:29 -0700426 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
427 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDuration));
Michael Jurka18014792010-10-14 09:01:34 -0700428
Winson Chungb26f3d62011-06-02 10:49:29 -0700429 AnimatorSet bouncer = new AnimatorSet();
430 bouncer.play(scaleUp).before(scaleDown);
431 bouncer.play(scaleUp).with(alphaFadeOut);
432 bouncer.addListener(new AnimatorListenerAdapter() {
433 @Override
434 public void onAnimationStart(Animator animation) {
435 setIsDragOverlapping(true);
436 }
437 @Override
438 public void onAnimationEnd(Animator animation) {
439 setIsDragOverlapping(false);
440 setHoverScale(1.0f);
441 setHoverAlpha(1.0f);
442 }
443 });
444 bouncer.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 }
446
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700447 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700448 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700449 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
450 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
451 // When we're small, we are either drawn normally or in the "accepts drops" state (during
452 // a drag). However, we also drag the mini hover background *over* one of those two
453 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700454 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700455 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800456 boolean mini = getScaleX() < 0.5f;
457
458 if (mIsDragOverlapping) {
459 // In the mini case, we draw the active_glow bg *over* the active background
460 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
461 } else if (mIsDragOccuring && mAcceptsDrops) {
462 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800463 } else if (mIsDefaultDropTarget && mini) {
464 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700465 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800466 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700467 }
Michael Jurka33945b22010-12-21 18:19:38 -0800468
469 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
470 bg.setBounds(mBackgroundRect);
471 bg.draw(canvas);
472
473 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700474 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800475 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700476 // If the hover background's scale is greater than 1, we'll be drawing outside
477 // the bounds of this CellLayout. Get around that by temporarily increasing the
478 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800479 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700480 Rect clipRect = canvas.getClipBounds();
481 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
482 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
483 canvas.save(Canvas.CLIP_SAVE_FLAG);
484 canvas.clipRect(-marginX, -marginY,
485 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
486 modifiedClipRect = true;
487 }
488
Michael Jurka33945b22010-12-21 18:19:38 -0800489 mActiveGlowBackgroundMini.setAlpha(
490 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
491 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
492 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700493 if (modifiedClipRect) {
494 canvas.restore();
495 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700496 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700497 }
Romain Guya6abce82009-11-10 02:54:41 -0800498
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700499 if (mCrosshairsVisibility > 0.0f) {
500 final int countX = mCountX;
501 final int countY = mCountY;
502
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700503 final float MAX_ALPHA = 0.4f;
504 final int MAX_VISIBLE_DISTANCE = 600;
505 final float DISTANCE_MULTIPLIER = 0.002f;
506
507 final Drawable d = mCrosshairsDrawable;
508 final int width = d.getIntrinsicWidth();
509 final int height = d.getIntrinsicHeight();
510
Winson Chung4b825dcd2011-06-19 12:41:22 -0700511 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700512 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700513 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700514 for (int row = 0; row <= countY; row++) {
515 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
516 float dist = mTmpPointF.length();
517 // Crosshairs further from the drag point are more faint
518 float alpha = Math.min(MAX_ALPHA,
519 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
520 if (alpha > 0.0f) {
521 d.setBounds(x, y, x + width, y + height);
522 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
523 d.draw(canvas);
524 }
525 y += mCellHeight + mHeightGap;
526 }
527 x += mCellWidth + mWidthGap;
528 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700529 }
Winson Chung150fbab2010-09-29 17:14:26 -0700530
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700531 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700532 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700533 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700534 if (alpha > 0) {
535 final Point p = mDragOutlines[i];
536 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700537 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700538 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700539 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700540 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800541
542 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
543 // requires an expanded clip rect (due to the glow's blur radius)
544 if (mPressedOrFocusedIcon != null) {
545 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
546 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
547 if (b != null) {
548 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700549 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
550 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800551 null);
552 }
553 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700554
555 // The folder outer / inner ring image(s)
556 for (int i = 0; i < mFolderOuterRings.size(); i++) {
557 FolderRingAnimator fra = mFolderOuterRings.get(i);
558
559 // Draw outer ring
560 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
561 int width = (int) fra.getOuterRingSize();
562 int height = width;
563 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
564
565 int centerX = mTempLocation[0] + mCellWidth / 2;
566 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
567
568 canvas.save();
569 canvas.translate(centerX - width / 2, centerY - height / 2);
570 d.setBounds(0, 0, width, height);
571 d.draw(canvas);
572 canvas.restore();
573
574 // Draw inner ring
575 d = FolderRingAnimator.sSharedInnerRingDrawable;
576 width = (int) fra.getInnerRingSize();
577 height = width;
578 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
579
580 centerX = mTempLocation[0] + mCellWidth / 2;
581 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
582 canvas.save();
583 canvas.translate(centerX - width / 2, centerY - width / 2);
584 d.setBounds(0, 0, width, height);
585 d.draw(canvas);
586 canvas.restore();
587 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700588
589 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
590 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
591 int width = d.getIntrinsicWidth();
592 int height = d.getIntrinsicHeight();
593
594 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
595 int centerX = mTempLocation[0] + mCellWidth / 2;
596 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
597
598 canvas.save();
599 canvas.translate(centerX - width / 2, centerY - width / 2);
600 d.setBounds(0, 0, width, height);
601 d.draw(canvas);
602 canvas.restore();
603 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700604 }
605
Adam Cohenb5ba0972011-09-07 18:02:31 -0700606 @Override
607 protected void dispatchDraw(Canvas canvas) {
608 super.dispatchDraw(canvas);
609 if (mForegroundAlpha > 0) {
610 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
611 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
612 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
613 mOverScrollForegroundDrawable.draw(canvas);
614 p.setXfermode(null);
615 }
616 }
617
Adam Cohen69ce2e52011-07-03 19:25:21 -0700618 public void showFolderAccept(FolderRingAnimator fra) {
619 mFolderOuterRings.add(fra);
620 }
621
622 public void hideFolderAccept(FolderRingAnimator fra) {
623 if (mFolderOuterRings.contains(fra)) {
624 mFolderOuterRings.remove(fra);
625 }
626 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700627 }
628
Adam Cohenc51934b2011-07-26 21:07:43 -0700629 public void setFolderLeaveBehindCell(int x, int y) {
630 mFolderLeaveBehindCell[0] = x;
631 mFolderLeaveBehindCell[1] = y;
632 invalidate();
633 }
634
635 public void clearFolderLeaveBehind() {
636 mFolderLeaveBehindCell[0] = -1;
637 mFolderLeaveBehindCell[1] = -1;
638 invalidate();
639 }
640
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700641 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700642 public boolean shouldDelayChildPressedState() {
643 return false;
644 }
645
646 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700647 public void cancelLongPress() {
648 super.cancelLongPress();
649
650 // Cancel long press for all children
651 final int count = getChildCount();
652 for (int i = 0; i < count; i++) {
653 final View child = getChildAt(i);
654 child.cancelLongPress();
655 }
656 }
657
Michael Jurkadee05892010-07-27 10:01:56 -0700658 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
659 mInterceptTouchListener = listener;
660 }
661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700663 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
665
666 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700667 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
669
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700670 public boolean addViewToCellLayout(
671 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700672 final LayoutParams lp = params;
673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 // Generate an id for each view, this assumes we have at most 256x256 cells
675 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700676 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700677 // If the horizontal or vertical span is set to -1, it is taken to
678 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700679 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
680 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681
Winson Chungaafa03c2010-06-11 17:34:16 -0700682 child.setId(childId);
683
Michael Jurka8c920dd2011-01-20 14:16:56 -0800684 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700685
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700686 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700687
Winson Chungaafa03c2010-06-11 17:34:16 -0700688 return true;
689 }
690 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700692
Michael Jurkabea15192010-11-17 12:33:46 -0800693 public void setAcceptsDrops(boolean acceptsDrops) {
694 if (mAcceptsDrops != acceptsDrops) {
695 mAcceptsDrops = acceptsDrops;
696 invalidate();
697 }
698 }
699
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700701 public void removeAllViews() {
702 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800703 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700704 }
705
706 @Override
707 public void removeAllViewsInLayout() {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700708 if (mChildren.getChildCount() > 0) {
709 clearOccupiedCells();
710 mChildren.removeAllViewsInLayout();
711 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700712 }
713
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700714 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800715 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700716 }
717
Michael Jurka0280c3b2010-09-17 15:00:07 -0700718 @Override
719 public void removeView(View view) {
720 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800721 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700722 }
723
724 @Override
725 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800726 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
727 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700728 }
729
730 @Override
731 public void removeViewInLayout(View view) {
732 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800733 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700734 }
735
736 @Override
737 public void removeViews(int start, int count) {
738 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800739 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700740 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800741 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700742 }
743
744 @Override
745 public void removeViewsInLayout(int start, int count) {
746 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800747 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700748 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800749 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700750 }
751
Michael Jurka8c920dd2011-01-20 14:16:56 -0800752 public void drawChildren(Canvas canvas) {
753 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 }
755
Michael Jurkaabded662011-03-04 12:06:57 -0800756 void buildChildrenLayer() {
757 mChildren.buildLayer();
758 }
759
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 @Override
761 protected void onAttachedToWindow() {
762 super.onAttachedToWindow();
763 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
764 }
765
Michael Jurkaaf442092010-06-10 17:01:57 -0700766 public void setTagToCellInfoForPoint(int touchX, int touchY) {
767 final CellInfo cellInfo = mCellInfo;
768 final Rect frame = mRect;
769 final int x = touchX + mScrollX;
770 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800771 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700772
773 boolean found = false;
774 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800775 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800776 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700777
Adam Cohen1b607ed2011-03-03 17:26:50 -0800778 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
779 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700780 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700781
782 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
783 // offset that by this CellLayout's padding to test an (x,y) point that is relative
784 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700785 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700786
Michael Jurkaaf442092010-06-10 17:01:57 -0700787 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700788 cellInfo.cell = child;
789 cellInfo.cellX = lp.cellX;
790 cellInfo.cellY = lp.cellY;
791 cellInfo.spanX = lp.cellHSpan;
792 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700793 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700794 break;
795 }
796 }
797 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700798
Michael Jurkad771c962011-08-09 15:00:48 -0700799 mLastDownOnOccupiedCell = found;
800
Michael Jurkaaf442092010-06-10 17:01:57 -0700801 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700802 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700803 pointToCellExact(x, y, cellXY);
804
Michael Jurkaaf442092010-06-10 17:01:57 -0700805 cellInfo.cell = null;
806 cellInfo.cellX = cellXY[0];
807 cellInfo.cellY = cellXY[1];
808 cellInfo.spanX = 1;
809 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700810 }
811 setTag(cellInfo);
812 }
813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 @Override
815 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700816 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
817 // even in the case where we return early. Not clearing here was causing bugs whereby on
818 // long-press we'd end up picking up an item from a previous drag operation.
819 final int action = ev.getAction();
820
821 if (action == MotionEvent.ACTION_DOWN) {
822 clearTagCellInfo();
823 }
824
Michael Jurkadee05892010-07-27 10:01:56 -0700825 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
826 return true;
827 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828
829 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700830 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 return false;
833 }
834
Adam Cohenc1997fd2011-08-15 18:26:39 -0700835 private void clearTagCellInfo() {
836 final CellInfo cellInfo = mCellInfo;
837 cellInfo.cell = null;
838 cellInfo.cellX = -1;
839 cellInfo.cellY = -1;
840 cellInfo.spanX = 0;
841 cellInfo.spanY = 0;
842 setTag(cellInfo);
843 }
844
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700846 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 }
848
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700849 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700850 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 * @param x X coordinate of the point
852 * @param y Y coordinate of the point
853 * @param result Array of 2 ints to hold the x and y coordinate of the cell
854 */
855 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700856 final int hStartPadding = getPaddingLeft();
857 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858
859 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
860 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
861
Adam Cohend22015c2010-07-26 22:02:18 -0700862 final int xAxis = mCountX;
863 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864
865 if (result[0] < 0) result[0] = 0;
866 if (result[0] >= xAxis) result[0] = xAxis - 1;
867 if (result[1] < 0) result[1] = 0;
868 if (result[1] >= yAxis) result[1] = yAxis - 1;
869 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700870
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 /**
872 * Given a point, return the cell that most closely encloses that point
873 * @param x X coordinate of the point
874 * @param y Y coordinate of the point
875 * @param result Array of 2 ints to hold the x and y coordinate of the cell
876 */
877 void pointToCellRounded(int x, int y, int[] result) {
878 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
879 }
880
881 /**
882 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700883 *
884 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700886 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 * @param result Array of 2 ints to hold the x and y coordinate of the point
888 */
889 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700890 final int hStartPadding = getPaddingLeft();
891 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892
893 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
894 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
895 }
896
Adam Cohene3e27a82011-04-15 12:07:39 -0700897 /**
898 * Given a cell coordinate, return the point that represents the upper left corner of that cell
899 *
900 * @param cellX X coordinate of the cell
901 * @param cellY Y coordinate of the cell
902 *
903 * @param result Array of 2 ints to hold the x and y coordinate of the point
904 */
905 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700906 final int hStartPadding = getPaddingLeft();
907 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700908
909 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
910 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
911 }
912
Romain Guy84f296c2009-11-04 15:00:44 -0800913 int getCellWidth() {
914 return mCellWidth;
915 }
916
917 int getCellHeight() {
918 return mCellHeight;
919 }
920
Adam Cohend4844c32011-02-18 19:25:06 -0800921 int getWidthGap() {
922 return mWidthGap;
923 }
924
925 int getHeightGap() {
926 return mHeightGap;
927 }
928
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700929 Rect getContentRect(Rect r) {
930 if (r == null) {
931 r = new Rect();
932 }
933 int left = getPaddingLeft();
934 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700935 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
936 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700937 r.set(left, top, right, bottom);
938 return r;
939 }
940
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 @Override
942 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
943 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700944
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700946 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
947
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
949 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700950
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
952 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
953 }
954
Adam Cohend22015c2010-07-26 22:02:18 -0700955 int numWidthGaps = mCountX - 1;
956 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957
Adam Cohen234c4cd2011-07-17 21:03:04 -0700958 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700959 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
960 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
961 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
962 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
963 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
964 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700965 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700966 } else {
967 mWidthGap = mOriginalWidthGap;
968 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700969 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700970
Michael Jurka8c920dd2011-01-20 14:16:56 -0800971 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
972 int newWidth = widthSpecSize;
973 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700974 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700975 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700976 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700977 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700978 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700979 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700980 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800981
982 int count = getChildCount();
983 for (int i = 0; i < count; i++) {
984 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700985 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
986 mPaddingRight, MeasureSpec.EXACTLY);
987 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
988 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800989 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
990 }
991 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 }
993
994 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700995 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800998 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700999 child.layout(mPaddingLeft, mPaddingTop,
1000 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 }
1002 }
1003
1004 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001005 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1006 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001007 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001008 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
1009 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurka33945b22010-12-21 18:19:38 -08001010 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -07001011 }
1012
1013 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001015 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 }
1017
1018 @Override
1019 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001020 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 }
1022
Michael Jurka5f1c5092010-09-03 14:15:02 -07001023 public float getBackgroundAlpha() {
1024 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001025 }
1026
Michael Jurka742574b2011-02-02 23:51:01 -08001027 public void setFastBackgroundAlpha(float alpha) {
1028 mBackgroundAlpha = alpha;
1029 }
1030
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001031 public void setBackgroundAlphaMultiplier(float multiplier) {
1032 mBackgroundAlphaMultiplier = multiplier;
1033 }
1034
Adam Cohenddb82192010-11-10 16:32:54 -08001035 public float getBackgroundAlphaMultiplier() {
1036 return mBackgroundAlphaMultiplier;
1037 }
1038
Michael Jurka5f1c5092010-09-03 14:15:02 -07001039 public void setBackgroundAlpha(float alpha) {
1040 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -07001041 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -07001042 }
1043
Michael Jurka5f1c5092010-09-03 14:15:02 -07001044 // Need to return true to let the view system know we know how to handle alpha-- this is
1045 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
1046 // versions
1047 @Override
1048 protected boolean onSetAlpha(int alpha) {
1049 return true;
1050 }
1051
1052 public void setAlpha(float alpha) {
1053 setChildrenAlpha(alpha);
1054 super.setAlpha(alpha);
1055 }
1056
Michael Jurka742574b2011-02-02 23:51:01 -08001057 public void setFastAlpha(float alpha) {
1058 setFastChildrenAlpha(alpha);
1059 super.setFastAlpha(alpha);
1060 }
1061
Michael Jurkadee05892010-07-27 10:01:56 -07001062 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001063 final int childCount = getChildCount();
1064 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001065 getChildAt(i).setAlpha(alpha);
1066 }
1067 }
1068
Michael Jurka742574b2011-02-02 23:51:01 -08001069 private void setFastChildrenAlpha(float alpha) {
1070 final int childCount = getChildCount();
1071 for (int i = 0; i < childCount; i++) {
1072 getChildAt(i).setFastAlpha(alpha);
1073 }
1074 }
1075
Patrick Dubroy440c3602010-07-13 17:50:32 -07001076 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001077 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001078 }
1079
Adam Cohen76fc0852011-06-17 13:26:23 -07001080 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
1081 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001082 CellLayoutChildren clc = getChildrenLayout();
1083 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
1084 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1085 final ItemInfo info = (ItemInfo) child.getTag();
1086
1087 // We cancel any existing animations
1088 if (mReorderAnimators.containsKey(lp)) {
1089 mReorderAnimators.get(lp).cancel();
1090 mReorderAnimators.remove(lp);
1091 }
1092
1093 int oldX = lp.x;
1094 int oldY = lp.y;
1095 mOccupied[lp.cellX][lp.cellY] = false;
1096 mOccupied[cellX][cellY] = true;
1097
1098 lp.isLockedToGrid = true;
1099 lp.cellX = info.cellX = cellX;
1100 lp.cellY = info.cellY = cellY;
1101 clc.setupLp(lp);
1102 lp.isLockedToGrid = false;
1103 int newX = lp.x;
1104 int newY = lp.y;
1105
Adam Cohen76fc0852011-06-17 13:26:23 -07001106 lp.x = oldX;
1107 lp.y = oldY;
1108 child.requestLayout();
1109
Adam Cohenbfbfd262011-06-13 16:55:12 -07001110 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
1111 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
1112 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
1113 oa.setDuration(duration);
1114 mReorderAnimators.put(lp, oa);
1115 oa.addUpdateListener(new AnimatorUpdateListener() {
1116 public void onAnimationUpdate(ValueAnimator animation) {
1117 child.requestLayout();
1118 }
1119 });
1120 oa.addListener(new AnimatorListenerAdapter() {
1121 boolean cancelled = false;
1122 public void onAnimationEnd(Animator animation) {
1123 // If the animation was cancelled, it means that another animation
1124 // has interrupted this one, and we don't want to lock the item into
1125 // place just yet.
1126 if (!cancelled) {
1127 lp.isLockedToGrid = true;
1128 }
1129 if (mReorderAnimators.containsKey(lp)) {
1130 mReorderAnimators.remove(lp);
1131 }
1132 }
1133 public void onAnimationCancel(Animator animation) {
1134 cancelled = true;
1135 }
1136 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001137 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001138 oa.start();
1139 return true;
1140 }
1141 return false;
1142 }
1143
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001144 /**
1145 * Estimate where the top left cell of the dragged item will land if it is dropped.
1146 *
1147 * @param originX The X value of the top left corner of the item
1148 * @param originY The Y value of the top left corner of the item
1149 * @param spanX The number of horizontal cells that the item spans
1150 * @param spanY The number of vertical cells that the item spans
1151 * @param result The estimated drop cell X and Y.
1152 */
1153 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001154 final int countX = mCountX;
1155 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001156
Michael Jurkaa63c4522010-08-19 13:52:27 -07001157 // pointToCellRounded takes the top left of a cell but will pad that with
1158 // cellWidth/2 and cellHeight/2 when finding the matching cell
1159 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001160
1161 // If the item isn't fully on this screen, snap to the edges
1162 int rightOverhang = result[0] + spanX - countX;
1163 if (rightOverhang > 0) {
1164 result[0] -= rightOverhang; // Snap to right
1165 }
1166 result[0] = Math.max(0, result[0]); // Snap to left
1167 int bottomOverhang = result[1] + spanY - countY;
1168 if (bottomOverhang > 0) {
1169 result[1] -= bottomOverhang; // Snap to bottom
1170 }
1171 result[1] = Math.max(0, result[1]); // Snap to top
1172 }
1173
Joe Onorato4be866d2010-10-10 11:26:02 -07001174 void visualizeDropLocation(
1175 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1176
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001177 final int oldDragCellX = mDragCell[0];
1178 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001179 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001180 if (v != null) {
1181 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1182 } else {
1183 mDragCenter.set(originX, originY);
1184 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001185
Adam Cohen2801caf2011-05-13 20:57:39 -07001186 if (dragOutline == null && v == null) {
1187 if (mCrosshairsDrawable != null) {
1188 invalidate();
1189 }
1190 return;
1191 }
1192
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001193 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001194 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001195 final int[] topLeft = mTmpPoint;
1196 cellToPoint(nearest[0], nearest[1], topLeft);
1197
Joe Onorato4be866d2010-10-10 11:26:02 -07001198 int left = topLeft[0];
1199 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001200
Winson Chunga9abd0e2010-10-27 17:18:37 -07001201 if (v != null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001202 // When drawing the drag outline, it did not account for margin offsets
1203 // added by the view's parent.
1204 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1205 left += lp.leftMargin;
1206 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001207
Adam Cohen99e8b402011-03-25 19:23:43 -07001208 // Offsets due to the size difference between the View and the dragOutline.
1209 // There is a size difference to account for the outer blur, which may lie
1210 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001211 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001212 // We center about the x axis
1213 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1214 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001215 } else {
1216 // Center the drag outline in the cell
Winson Chung4b576be2011-04-27 17:40:20 -07001217 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1218 - dragOutline.getWidth()) / 2;
1219 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1220 - dragOutline.getHeight()) / 2;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001221 }
Winson Chung150fbab2010-09-29 17:14:26 -07001222
Joe Onorato4be866d2010-10-10 11:26:02 -07001223 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001224 mDragOutlineAnims[oldIndex].animateOut();
1225 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001226
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001227 mDragOutlines[mDragOutlineCurrent].set(left, top);
1228 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1229 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001230 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001231
1232 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1233 if (mCrosshairsDrawable != null) {
1234 invalidate();
1235 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001236 }
1237
Adam Cohene0310962011-04-18 16:15:31 -07001238 public void clearDragOutlines() {
1239 final int oldIndex = mDragOutlineCurrent;
1240 mDragOutlineAnims[oldIndex].animateOut();
1241 mDragCell[0] = -1;
1242 mDragCell[1] = -1;
1243 }
1244
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001246 * Find a vacant area that will fit the given bounds nearest the requested
1247 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001248 *
Romain Guy51afc022009-05-04 18:03:43 -07001249 * @param pixelX The X location at which you want to search for a vacant area.
1250 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001251 * @param spanX Horizontal span of the object.
1252 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001253 * @param result Array in which to place the result, or null (in which case a new array will
1254 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001255 * @return The X, Y cell of a vacant area that can contain this object,
1256 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001258 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001259 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1260 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001261 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001262
Michael Jurka6a1435d2010-09-27 17:35:12 -07001263 /**
1264 * Find a vacant area that will fit the given bounds nearest the requested
1265 * cell location. Uses Euclidean distance to score multiple vacant areas.
1266 *
1267 * @param pixelX The X location at which you want to search for a vacant area.
1268 * @param pixelY The Y location at which you want to search for a vacant area.
1269 * @param spanX Horizontal span of the object.
1270 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001271 * @param ignoreOccupied If true, the result can be an occupied cell
1272 * @param result Array in which to place the result, or null (in which case a new array will
1273 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001274 * @return The X, Y cell of a vacant area that can contain this object,
1275 * nearest the requested location.
1276 */
Adam Cohendf035382011-04-11 17:22:04 -07001277 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1278 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001279 // mark space take by ignoreView as available (method checks if ignoreView is null)
1280 markCellsAsUnoccupiedForView(ignoreView);
1281
Adam Cohene3e27a82011-04-15 12:07:39 -07001282 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1283 // to the center of the item, but we are searching based on the top-left cell, so
1284 // we translate the point over to correspond to the top-left.
1285 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1286 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1287
Jeff Sharkey70864282009-04-07 21:08:40 -07001288 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001289 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001290 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001291
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001292 final int countX = mCountX;
1293 final int countY = mCountY;
1294 final boolean[][] occupied = mOccupied;
1295
Winson Chungbbc60d82010-11-11 16:34:41 -08001296 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001297 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001298 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001299 if (ignoreOccupied) {
1300 for (int i = 0; i < spanX; i++) {
1301 for (int j = 0; j < spanY; j++) {
1302 if (occupied[x + i][y + j]) {
1303 // small optimization: we can skip to after the column we
1304 // just found an occupied cell
1305 x += i;
1306 continue inner;
1307 }
Michael Jurkac28de512010-08-13 11:27:44 -07001308 }
1309 }
1310 }
Winson Chung0be025d2011-05-23 17:45:09 -07001311 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001312 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313
Michael Jurkac28de512010-08-13 11:27:44 -07001314 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1315 + Math.pow(cellXY[1] - pixelY, 2));
1316 if (distance <= bestDistance) {
1317 bestDistance = distance;
1318 bestXY[0] = x;
1319 bestXY[1] = y;
1320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001323 // re-mark space taken by ignoreView as occupied
1324 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
Adam Cohenc0dcf592011-06-01 15:30:43 -07001326 // Return -1, -1 if no suitable location found
1327 if (bestDistance == Double.MAX_VALUE) {
1328 bestXY[0] = -1;
1329 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001330 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001331 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001333
Adam Cohendf035382011-04-11 17:22:04 -07001334 /**
1335 * Find a vacant area that will fit the given bounds nearest the requested
1336 * cell location. Uses Euclidean distance to score multiple vacant areas.
1337 *
1338 * @param pixelX The X location at which you want to search for a vacant area.
1339 * @param pixelY The Y location at which you want to search for a vacant area.
1340 * @param spanX Horizontal span of the object.
1341 * @param spanY Vertical span of the object.
1342 * @param ignoreView Considers space occupied by this view as unoccupied
1343 * @param result Previously returned value to possibly recycle.
1344 * @return The X, Y cell of a vacant area that can contain this object,
1345 * nearest the requested location.
1346 */
1347 int[] findNearestVacantArea(
1348 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1349 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1350 }
1351
1352 /**
1353 * Find a starting cell position that will fit the given bounds nearest the requested
1354 * cell location. Uses Euclidean distance to score multiple vacant areas.
1355 *
1356 * @param pixelX The X location at which you want to search for a vacant area.
1357 * @param pixelY The Y location at which you want to search for a vacant area.
1358 * @param spanX Horizontal span of the object.
1359 * @param spanY Vertical span of the object.
1360 * @param ignoreView Considers space occupied by this view as unoccupied
1361 * @param result Previously returned value to possibly recycle.
1362 * @return The X, Y cell of a vacant area that can contain this object,
1363 * nearest the requested location.
1364 */
1365 int[] findNearestArea(
1366 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1367 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1368 }
1369
Michael Jurka0280c3b2010-09-17 15:00:07 -07001370 boolean existsEmptyCell() {
1371 return findCellForSpan(null, 1, 1);
1372 }
1373
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001375 * Finds the upper-left coordinate of the first rectangle in the grid that can
1376 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1377 * then this method will only return coordinates for rectangles that contain the cell
1378 * (intersectX, intersectY)
1379 *
1380 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1381 * can be found.
1382 * @param spanX The horizontal span of the cell we want to find.
1383 * @param spanY The vertical span of the cell we want to find.
1384 *
1385 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001386 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001387 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1388 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1389 }
1390
1391 /**
1392 * Like above, but ignores any cells occupied by the item "ignoreView"
1393 *
1394 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1395 * can be found.
1396 * @param spanX The horizontal span of the cell we want to find.
1397 * @param spanY The vertical span of the cell we want to find.
1398 * @param ignoreView The home screen item we should treat as not occupying any space
1399 * @return
1400 */
1401 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1402 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1403 }
1404
1405 /**
1406 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1407 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1408 *
1409 * @param spanX The horizontal span of the cell we want to find.
1410 * @param spanY The vertical span of the cell we want to find.
1411 * @param ignoreView The home screen item we should treat as not occupying any space
1412 * @param intersectX The X coordinate of the cell that we should try to overlap
1413 * @param intersectX The Y coordinate of the cell that we should try to overlap
1414 *
1415 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1416 */
1417 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1418 int intersectX, int intersectY) {
1419 return findCellForSpanThatIntersectsIgnoring(
1420 cellXY, spanX, spanY, intersectX, intersectY, null);
1421 }
1422
1423 /**
1424 * The superset of the above two methods
1425 */
1426 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1427 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001428 // mark space take by ignoreView as available (method checks if ignoreView is null)
1429 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001430
Michael Jurka28750fb2010-09-24 17:43:49 -07001431 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001432 while (true) {
1433 int startX = 0;
1434 if (intersectX >= 0) {
1435 startX = Math.max(startX, intersectX - (spanX - 1));
1436 }
1437 int endX = mCountX - (spanX - 1);
1438 if (intersectX >= 0) {
1439 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1440 }
1441 int startY = 0;
1442 if (intersectY >= 0) {
1443 startY = Math.max(startY, intersectY - (spanY - 1));
1444 }
1445 int endY = mCountY - (spanY - 1);
1446 if (intersectY >= 0) {
1447 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1448 }
1449
Winson Chungbbc60d82010-11-11 16:34:41 -08001450 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001451 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001452 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001453 for (int i = 0; i < spanX; i++) {
1454 for (int j = 0; j < spanY; j++) {
1455 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001456 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001457 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001458 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001459 continue inner;
1460 }
1461 }
1462 }
1463 if (cellXY != null) {
1464 cellXY[0] = x;
1465 cellXY[1] = y;
1466 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001467 foundCell = true;
1468 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001469 }
1470 }
1471 if (intersectX == -1 && intersectY == -1) {
1472 break;
1473 } else {
1474 // if we failed to find anything, try again but without any requirements of
1475 // intersecting
1476 intersectX = -1;
1477 intersectY = -1;
1478 continue;
1479 }
1480 }
1481
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001482 // re-mark space taken by ignoreView as occupied
1483 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001484 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001485 }
1486
1487 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001488 * A drag event has begun over this layout.
1489 * It may have begun over this layout (in which case onDragChild is called first),
1490 * or it may have begun on another layout.
1491 */
1492 void onDragEnter() {
1493 if (!mDragging) {
1494 // Fade in the drag indicators
1495 if (mCrosshairsAnimator != null) {
1496 mCrosshairsAnimator.animateIn();
1497 }
1498 }
1499 mDragging = true;
1500 }
1501
1502 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001503 * Called when drag has left this CellLayout or has been completed (successfully or not)
1504 */
1505 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001506 // This can actually be called when we aren't in a drag, e.g. when adding a new
1507 // item to this layout via the customize drawer.
1508 // Guard against that case.
1509 if (mDragging) {
1510 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001511
Joe Onorato4be866d2010-10-10 11:26:02 -07001512 // Fade out the drag indicators
1513 if (mCrosshairsAnimator != null) {
1514 mCrosshairsAnimator.animateOut();
1515 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001516 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001517
1518 // Invalidate the drag data
1519 mDragCell[0] = -1;
1520 mDragCell[1] = -1;
1521 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1522 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1523
Michael Jurka33945b22010-12-21 18:19:38 -08001524 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001525 }
1526
1527 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001528 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001529 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001530 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 *
1532 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001534 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001535 if (child != null) {
1536 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001537 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001538 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001539 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
1541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001544 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 * @param cellX X coordinate of upper left corner expressed as a cell position
1546 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001547 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001549 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001551 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 final int cellWidth = mCellWidth;
1553 final int cellHeight = mCellHeight;
1554 final int widthGap = mWidthGap;
1555 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001556
Winson Chung4b825dcd2011-06-19 12:41:22 -07001557 final int hStartPadding = getPaddingLeft();
1558 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1561 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1562
1563 int x = hStartPadding + cellX * (cellWidth + widthGap);
1564 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001565
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001566 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001568
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001570 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001572 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 * @param width Width in pixels
1574 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001575 * @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 -08001576 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001577 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001578 return rectToCell(getResources(), width, height, result);
1579 }
1580
1581 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 // Always assume we're working with the smallest span to make sure we
1583 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001584 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1585 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001587
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07001589 int spanX = (int) Math.ceil(width / (float) smallerSize);
1590 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04001591
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001592 if (result == null) {
1593 return new int[] { spanX, spanY };
1594 }
1595 result[0] = spanX;
1596 result[1] = spanY;
1597 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 }
1599
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001600 public int[] cellSpansToSize(int hSpans, int vSpans) {
1601 int[] size = new int[2];
1602 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1603 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1604 return size;
1605 }
1606
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001608 * Calculate the grid spans needed to fit given item
1609 */
1610 public void calculateSpans(ItemInfo info) {
1611 final int minWidth;
1612 final int minHeight;
1613
1614 if (info instanceof LauncherAppWidgetInfo) {
1615 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1616 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1617 } else if (info instanceof PendingAddWidgetInfo) {
1618 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1619 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1620 } else {
1621 // It's not a widget, so it must be 1x1
1622 info.spanX = info.spanY = 1;
1623 return;
1624 }
1625 int[] spans = rectToCell(minWidth, minHeight, null);
1626 info.spanX = spans[0];
1627 info.spanY = spans[1];
1628 }
1629
1630 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 * Find the first vacant cell, if there is one.
1632 *
1633 * @param vacant Holds the x and y coordinate of the vacant cell
1634 * @param spanX Horizontal cell span.
1635 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001636 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 * @return True if a vacant cell was found
1638 */
1639 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640
Michael Jurka0280c3b2010-09-17 15:00:07 -07001641 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 }
1643
1644 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1645 int xCount, int yCount, boolean[][] occupied) {
1646
Adam Cohen2801caf2011-05-13 20:57:39 -07001647 for (int y = 0; y < yCount; y++) {
1648 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 boolean available = !occupied[x][y];
1650out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1651 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1652 available = available && !occupied[i][j];
1653 if (!available) break out;
1654 }
1655 }
1656
1657 if (available) {
1658 vacant[0] = x;
1659 vacant[1] = y;
1660 return true;
1661 }
1662 }
1663 }
1664
1665 return false;
1666 }
1667
Michael Jurka0280c3b2010-09-17 15:00:07 -07001668 private void clearOccupiedCells() {
1669 for (int x = 0; x < mCountX; x++) {
1670 for (int y = 0; y < mCountY; y++) {
1671 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 }
1673 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675
Adam Cohen1b607ed2011-03-03 17:26:50 -08001676 /**
1677 * Given a view, determines how much that view can be expanded in all directions, in terms of
1678 * whether or not there are other items occupying adjacent cells. Used by the
1679 * AppWidgetResizeFrame to determine how the widget can be resized.
1680 */
Adam Cohend4844c32011-02-18 19:25:06 -08001681 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001682 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001683 boolean flag;
1684
Adam Cohen1b607ed2011-03-03 17:26:50 -08001685 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001686 for (int x = lp.cellX - 1; x >= 0; x--) {
1687 flag = false;
1688 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1689 if (mOccupied[x][y]) flag = true;
1690 }
1691 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001692 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001693 }
1694
Adam Cohen1b607ed2011-03-03 17:26:50 -08001695 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001696 for (int y = lp.cellY - 1; y >= 0; y--) {
1697 flag = false;
1698 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1699 if (mOccupied[x][y]) flag = true;
1700 }
1701 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001702 expandability[AppWidgetResizeFrame.TOP]++;
1703 }
Adam Cohend4844c32011-02-18 19:25:06 -08001704
Adam Cohen1b607ed2011-03-03 17:26:50 -08001705 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001706 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1707 flag = false;
1708 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1709 if (mOccupied[x][y]) flag = true;
1710 }
1711 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001712 expandability[AppWidgetResizeFrame.RIGHT]++;
1713 }
Adam Cohend4844c32011-02-18 19:25:06 -08001714
Adam Cohen1b607ed2011-03-03 17:26:50 -08001715 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001716 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1717 flag = false;
1718 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1719 if (mOccupied[x][y]) flag = true;
1720 }
1721 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001722 expandability[AppWidgetResizeFrame.BOTTOM]++;
1723 }
Adam Cohend4844c32011-02-18 19:25:06 -08001724 }
1725
Michael Jurka0280c3b2010-09-17 15:00:07 -07001726 public void onMove(View view, int newCellX, int newCellY) {
1727 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1728 markCellsAsUnoccupiedForView(view);
1729 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1730 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731
Adam Cohend4844c32011-02-18 19:25:06 -08001732 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001733 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001734 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1735 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1736 }
1737
Adam Cohend4844c32011-02-18 19:25:06 -08001738 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001739 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001740 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1741 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1742 }
1743
1744 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1745 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1746 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1747 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 }
1749 }
1750 }
1751
Adam Cohen2801caf2011-05-13 20:57:39 -07001752 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001753 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001754 (Math.max((mCountX - 1), 0) * mWidthGap);
1755 }
1756
1757 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001758 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001759 (Math.max((mCountY - 1), 0) * mHeightGap);
1760 }
1761
Michael Jurka66d72172011-04-12 16:29:25 -07001762 public boolean isOccupied(int x, int y) {
1763 if (x < mCountX && y < mCountY) {
1764 return mOccupied[x][y];
1765 } else {
1766 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1767 }
1768 }
1769
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 @Override
1771 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1772 return new CellLayout.LayoutParams(getContext(), attrs);
1773 }
1774
1775 @Override
1776 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1777 return p instanceof CellLayout.LayoutParams;
1778 }
1779
1780 @Override
1781 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1782 return new CellLayout.LayoutParams(p);
1783 }
1784
Winson Chungaafa03c2010-06-11 17:34:16 -07001785 public static class CellLayoutAnimationController extends LayoutAnimationController {
1786 public CellLayoutAnimationController(Animation animation, float delay) {
1787 super(animation, delay);
1788 }
1789
1790 @Override
1791 protected long getDelayForView(View view) {
1792 return (int) (Math.random() * 150);
1793 }
1794 }
1795
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1797 /**
1798 * Horizontal location of the item in the grid.
1799 */
1800 @ViewDebug.ExportedProperty
1801 public int cellX;
1802
1803 /**
1804 * Vertical location of the item in the grid.
1805 */
1806 @ViewDebug.ExportedProperty
1807 public int cellY;
1808
1809 /**
1810 * Number of cells spanned horizontally by the item.
1811 */
1812 @ViewDebug.ExportedProperty
1813 public int cellHSpan;
1814
1815 /**
1816 * Number of cells spanned vertically by the item.
1817 */
1818 @ViewDebug.ExportedProperty
1819 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001820
Adam Cohen1b607ed2011-03-03 17:26:50 -08001821 /**
1822 * Indicates whether the item will set its x, y, width and height parameters freely,
1823 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1824 */
Adam Cohend4844c32011-02-18 19:25:06 -08001825 public boolean isLockedToGrid = true;
1826
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 // X coordinate of the view in the layout.
1828 @ViewDebug.ExportedProperty
1829 int x;
1830 // Y coordinate of the view in the layout.
1831 @ViewDebug.ExportedProperty
1832 int y;
1833
Romain Guy84f296c2009-11-04 15:00:44 -08001834 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 public LayoutParams(Context c, AttributeSet attrs) {
1837 super(c, attrs);
1838 cellHSpan = 1;
1839 cellVSpan = 1;
1840 }
1841
1842 public LayoutParams(ViewGroup.LayoutParams source) {
1843 super(source);
1844 cellHSpan = 1;
1845 cellVSpan = 1;
1846 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001847
1848 public LayoutParams(LayoutParams source) {
1849 super(source);
1850 this.cellX = source.cellX;
1851 this.cellY = source.cellY;
1852 this.cellHSpan = source.cellHSpan;
1853 this.cellVSpan = source.cellVSpan;
1854 }
1855
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001857 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 this.cellX = cellX;
1859 this.cellY = cellY;
1860 this.cellHSpan = cellHSpan;
1861 this.cellVSpan = cellVSpan;
1862 }
1863
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001864 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001865 if (isLockedToGrid) {
1866 final int myCellHSpan = cellHSpan;
1867 final int myCellVSpan = cellVSpan;
1868 final int myCellX = cellX;
1869 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001870
Adam Cohend4844c32011-02-18 19:25:06 -08001871 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1872 leftMargin - rightMargin;
1873 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1874 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001875 x = myCellX * (cellWidth + widthGap) + leftMargin;
1876 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001877 }
1878 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001879
Winson Chungaafa03c2010-06-11 17:34:16 -07001880 public String toString() {
1881 return "(" + this.cellX + ", " + this.cellY + ")";
1882 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001883
1884 public void setWidth(int width) {
1885 this.width = width;
1886 }
1887
1888 public int getWidth() {
1889 return width;
1890 }
1891
1892 public void setHeight(int height) {
1893 this.height = height;
1894 }
1895
1896 public int getHeight() {
1897 return height;
1898 }
1899
1900 public void setX(int x) {
1901 this.x = x;
1902 }
1903
1904 public int getX() {
1905 return x;
1906 }
1907
1908 public void setY(int y) {
1909 this.y = y;
1910 }
1911
1912 public int getY() {
1913 return y;
1914 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 }
1916
Michael Jurka0280c3b2010-09-17 15:00:07 -07001917 // This class stores info for two purposes:
1918 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1919 // its spanX, spanY, and the screen it is on
1920 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1921 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1922 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001923 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001925 int cellX = -1;
1926 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 int spanX;
1928 int spanY;
1929 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001930 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 @Override
1933 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001934 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1935 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
1937 }
Michael Jurkad771c962011-08-09 15:00:48 -07001938
1939 public boolean lastDownOnOccupiedCell() {
1940 return mLastDownOnOccupiedCell;
1941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942}