blob: 54fdcc53cddfffd1bdd28ca46f1b87ed15f462fb [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
Winson Chungb8c69f32011-10-19 21:36:08 -0700221 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700222 for (int i = 0; i < mDragOutlines.length; i++) {
223 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700224 }
225
226 // When dragging things around the home screens, we show a green outline of
227 // where the item will land. The outlines gradually fade out, leaving a trail
228 // behind the drag path.
229 // Set up all the animations that are used to implement this fading.
230 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700231 final float fromAlphaValue = 0;
232 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700233
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700234 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700235
236 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700237 final InterruptibleInOutAnimator anim =
238 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700239 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700240 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700241 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700242 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700243 final Bitmap outline = (Bitmap)anim.getTag();
244
245 // If an animation is started and then stopped very quickly, we can still
246 // get spurious updates we've cleared the tag. Guard against this.
247 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700248 if (false) {
249 Object val = animation.getAnimatedValue();
250 Log.d(TAG, "anim " + thisIndex + " update: " + val +
251 ", isStopped " + anim.isStopped());
252 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700253 // Try to prevent it from continuing to run
254 animation.cancel();
255 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700256 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700257 final int left = mDragOutlines[thisIndex].x;
258 final int top = mDragOutlines[thisIndex].y;
259 CellLayout.this.invalidate(left, top,
260 left + outline.getWidth(), top + outline.getHeight());
261 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700262 }
263 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700264 // The animation holds a reference to the drag outline bitmap as long is it's
265 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700266 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700267 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700269 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700270 anim.setTag(null);
271 }
272 }
273 });
274 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700275 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700276
Michael Jurka18014792010-10-14 09:01:34 -0700277 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700278 mForegroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800279 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700280 setHoverScale(1.0f);
281 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800282
Michael Jurka8c920dd2011-01-20 14:16:56 -0800283 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700284 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800285 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700286 }
287
Michael Jurkaf6440da2011-04-05 14:50:34 -0700288 static int widthInPortrait(Resources r, int numCells) {
289 // We use this method from Workspace to figure out how many rows/columns Launcher should
290 // have. We ignore the left/right padding on CellLayout because it turns out in our design
291 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700292 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700293 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
294 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700295
Winson Chung4b825dcd2011-06-19 12:41:22 -0700296 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700297 }
298
Michael Jurkaf6440da2011-04-05 14:50:34 -0700299 static int heightInLandscape(Resources r, int numCells) {
300 // We use this method from Workspace to figure out how many rows/columns Launcher should
301 // have. We ignore the left/right padding on CellLayout because it turns out in our design
302 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700303 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700304 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
305 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700306
Winson Chung4b825dcd2011-06-19 12:41:22 -0700307 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700308 }
309
Adam Cohen2801caf2011-05-13 20:57:39 -0700310 public void enableHardwareLayers() {
Adam Cohen7ef91832011-08-25 14:06:02 -0700311 mChildren.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700312 }
313
314 public void setGridSize(int x, int y) {
315 mCountX = x;
316 mCountY = y;
317 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700318 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700319 }
320
Patrick Dubroy96864c32011-03-10 17:17:23 -0800321 private void invalidateBubbleTextView(BubbleTextView icon) {
322 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700323 invalidate(icon.getLeft() + getPaddingLeft() - padding,
324 icon.getTop() + getPaddingTop() - padding,
325 icon.getRight() + getPaddingLeft() + padding,
326 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800327 }
328
Adam Cohenb5ba0972011-09-07 18:02:31 -0700329 void setOverScrollAmount(float r, boolean left) {
330 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
331 mOverScrollForegroundDrawable = mOverScrollLeft;
332 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
333 mOverScrollForegroundDrawable = mOverScrollRight;
334 }
335
336 mForegroundAlpha = (int) Math.round((r * 255));
337 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
338 invalidate();
339 }
340
Patrick Dubroy96864c32011-03-10 17:17:23 -0800341 void setPressedOrFocusedIcon(BubbleTextView icon) {
342 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
343 // requires an expanded clip rect (due to the glow's blur radius)
344 BubbleTextView oldIcon = mPressedOrFocusedIcon;
345 mPressedOrFocusedIcon = icon;
346 if (oldIcon != null) {
347 invalidateBubbleTextView(oldIcon);
348 }
349 if (mPressedOrFocusedIcon != null) {
350 invalidateBubbleTextView(mPressedOrFocusedIcon);
351 }
352 }
353
Winson Chung6e314082011-01-27 16:46:51 -0800354 public CellLayoutChildren getChildrenLayout() {
355 if (getChildCount() > 0) {
356 return (CellLayoutChildren) getChildAt(0);
357 }
358 return null;
359 }
360
Michael Jurka33945b22010-12-21 18:19:38 -0800361 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
362 if (mIsDefaultDropTarget != isDefaultDropTarget) {
363 mIsDefaultDropTarget = isDefaultDropTarget;
364 invalidate();
365 }
366 }
367
Michael Jurka33945b22010-12-21 18:19:38 -0800368 void setIsDragOccuring(boolean isDragOccuring) {
369 if (mIsDragOccuring != isDragOccuring) {
370 mIsDragOccuring = isDragOccuring;
371 invalidate();
372 }
373 }
374
375 void setIsDragOverlapping(boolean isDragOverlapping) {
376 if (mIsDragOverlapping != isDragOverlapping) {
377 mIsDragOverlapping = isDragOverlapping;
378 invalidate();
379 }
380 }
381
382 boolean getIsDragOverlapping() {
383 return mIsDragOverlapping;
384 }
385
386 private void updateGlowRect() {
387 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700388 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
389 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800390 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700391 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
392 invalidate();
393 }
394
395 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800396 if (scaleFactor != mGlowBackgroundScale) {
397 mGlowBackgroundScale = scaleFactor;
398 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800399 if (getParent() != null) {
400 ((View) getParent()).invalidate();
401 }
Michael Jurka18014792010-10-14 09:01:34 -0700402 }
403 }
404
405 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800406 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700407 }
408
409 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800410 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700411 }
412
413 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800414 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700415 invalidate();
416 }
417
418 void animateDrop() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700419 Resources res = getResources();
420 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
421 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
422 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
423 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
424 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
425 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
Michael Jurka18014792010-10-14 09:01:34 -0700426
Winson Chungb26f3d62011-06-02 10:49:29 -0700427 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
428 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDuration));
Michael Jurka18014792010-10-14 09:01:34 -0700429
Winson Chungb26f3d62011-06-02 10:49:29 -0700430 AnimatorSet bouncer = new AnimatorSet();
431 bouncer.play(scaleUp).before(scaleDown);
432 bouncer.play(scaleUp).with(alphaFadeOut);
433 bouncer.addListener(new AnimatorListenerAdapter() {
434 @Override
435 public void onAnimationStart(Animator animation) {
436 setIsDragOverlapping(true);
437 }
438 @Override
439 public void onAnimationEnd(Animator animation) {
440 setIsDragOverlapping(false);
441 setHoverScale(1.0f);
442 setHoverAlpha(1.0f);
443 }
444 });
445 bouncer.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 }
447
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700448 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700449 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700450 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
451 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
452 // When we're small, we are either drawn normally or in the "accepts drops" state (during
453 // a drag). However, we also drag the mini hover background *over* one of those two
454 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700455 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700456 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800457 boolean mini = getScaleX() < 0.5f;
458
459 if (mIsDragOverlapping) {
460 // In the mini case, we draw the active_glow bg *over* the active background
461 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
462 } else if (mIsDragOccuring && mAcceptsDrops) {
463 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800464 } else if (mIsDefaultDropTarget && mini) {
465 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700466 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800467 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700468 }
Michael Jurka33945b22010-12-21 18:19:38 -0800469
470 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
471 bg.setBounds(mBackgroundRect);
472 bg.draw(canvas);
473
474 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700475 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800476 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700477 // If the hover background's scale is greater than 1, we'll be drawing outside
478 // the bounds of this CellLayout. Get around that by temporarily increasing the
479 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800480 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700481 Rect clipRect = canvas.getClipBounds();
482 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
483 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
484 canvas.save(Canvas.CLIP_SAVE_FLAG);
485 canvas.clipRect(-marginX, -marginY,
486 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
487 modifiedClipRect = true;
488 }
489
Michael Jurka33945b22010-12-21 18:19:38 -0800490 mActiveGlowBackgroundMini.setAlpha(
491 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
492 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
493 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700494 if (modifiedClipRect) {
495 canvas.restore();
496 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700497 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700498 }
Romain Guya6abce82009-11-10 02:54:41 -0800499
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700500 if (mCrosshairsVisibility > 0.0f) {
501 final int countX = mCountX;
502 final int countY = mCountY;
503
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700504 final float MAX_ALPHA = 0.4f;
505 final int MAX_VISIBLE_DISTANCE = 600;
506 final float DISTANCE_MULTIPLIER = 0.002f;
507
508 final Drawable d = mCrosshairsDrawable;
509 final int width = d.getIntrinsicWidth();
510 final int height = d.getIntrinsicHeight();
511
Winson Chung4b825dcd2011-06-19 12:41:22 -0700512 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700513 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700514 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700515 for (int row = 0; row <= countY; row++) {
516 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
517 float dist = mTmpPointF.length();
518 // Crosshairs further from the drag point are more faint
519 float alpha = Math.min(MAX_ALPHA,
520 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
521 if (alpha > 0.0f) {
522 d.setBounds(x, y, x + width, y + height);
523 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
524 d.draw(canvas);
525 }
526 y += mCellHeight + mHeightGap;
527 }
528 x += mCellWidth + mWidthGap;
529 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700530 }
Winson Chung150fbab2010-09-29 17:14:26 -0700531
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700532 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700533 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700534 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700535 if (alpha > 0) {
536 final Point p = mDragOutlines[i];
537 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700538 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700539 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700540 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700541 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800542
543 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
544 // requires an expanded clip rect (due to the glow's blur radius)
545 if (mPressedOrFocusedIcon != null) {
546 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
547 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
548 if (b != null) {
549 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700550 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
551 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800552 null);
553 }
554 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700555
556 // The folder outer / inner ring image(s)
557 for (int i = 0; i < mFolderOuterRings.size(); i++) {
558 FolderRingAnimator fra = mFolderOuterRings.get(i);
559
560 // Draw outer ring
561 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
562 int width = (int) fra.getOuterRingSize();
563 int height = width;
564 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
565
566 int centerX = mTempLocation[0] + mCellWidth / 2;
567 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
568
569 canvas.save();
570 canvas.translate(centerX - width / 2, centerY - height / 2);
571 d.setBounds(0, 0, width, height);
572 d.draw(canvas);
573 canvas.restore();
574
575 // Draw inner ring
576 d = FolderRingAnimator.sSharedInnerRingDrawable;
577 width = (int) fra.getInnerRingSize();
578 height = width;
579 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
580
581 centerX = mTempLocation[0] + mCellWidth / 2;
582 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
583 canvas.save();
584 canvas.translate(centerX - width / 2, centerY - width / 2);
585 d.setBounds(0, 0, width, height);
586 d.draw(canvas);
587 canvas.restore();
588 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700589
590 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
591 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
592 int width = d.getIntrinsicWidth();
593 int height = d.getIntrinsicHeight();
594
595 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
596 int centerX = mTempLocation[0] + mCellWidth / 2;
597 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
598
599 canvas.save();
600 canvas.translate(centerX - width / 2, centerY - width / 2);
601 d.setBounds(0, 0, width, height);
602 d.draw(canvas);
603 canvas.restore();
604 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700605 }
606
Adam Cohenb5ba0972011-09-07 18:02:31 -0700607 @Override
608 protected void dispatchDraw(Canvas canvas) {
609 super.dispatchDraw(canvas);
610 if (mForegroundAlpha > 0) {
611 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
612 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
613 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
614 mOverScrollForegroundDrawable.draw(canvas);
615 p.setXfermode(null);
616 }
617 }
618
Adam Cohen69ce2e52011-07-03 19:25:21 -0700619 public void showFolderAccept(FolderRingAnimator fra) {
620 mFolderOuterRings.add(fra);
621 }
622
623 public void hideFolderAccept(FolderRingAnimator fra) {
624 if (mFolderOuterRings.contains(fra)) {
625 mFolderOuterRings.remove(fra);
626 }
627 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700628 }
629
Adam Cohenc51934b2011-07-26 21:07:43 -0700630 public void setFolderLeaveBehindCell(int x, int y) {
631 mFolderLeaveBehindCell[0] = x;
632 mFolderLeaveBehindCell[1] = y;
633 invalidate();
634 }
635
636 public void clearFolderLeaveBehind() {
637 mFolderLeaveBehindCell[0] = -1;
638 mFolderLeaveBehindCell[1] = -1;
639 invalidate();
640 }
641
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700642 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700643 public boolean shouldDelayChildPressedState() {
644 return false;
645 }
646
647 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700648 public void cancelLongPress() {
649 super.cancelLongPress();
650
651 // Cancel long press for all children
652 final int count = getChildCount();
653 for (int i = 0; i < count; i++) {
654 final View child = getChildAt(i);
655 child.cancelLongPress();
656 }
657 }
658
Michael Jurkadee05892010-07-27 10:01:56 -0700659 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
660 mInterceptTouchListener = listener;
661 }
662
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700664 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
666
667 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700668 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 }
670
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700671 public boolean addViewToCellLayout(
672 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700673 final LayoutParams lp = params;
674
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 // Generate an id for each view, this assumes we have at most 256x256 cells
676 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700677 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700678 // If the horizontal or vertical span is set to -1, it is taken to
679 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700680 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
681 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682
Winson Chungaafa03c2010-06-11 17:34:16 -0700683 child.setId(childId);
684
Michael Jurka8c920dd2011-01-20 14:16:56 -0800685 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700686
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700687 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700688
Winson Chungaafa03c2010-06-11 17:34:16 -0700689 return true;
690 }
691 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700693
Michael Jurkabea15192010-11-17 12:33:46 -0800694 public void setAcceptsDrops(boolean acceptsDrops) {
695 if (mAcceptsDrops != acceptsDrops) {
696 mAcceptsDrops = acceptsDrops;
697 invalidate();
698 }
699 }
700
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700702 public void removeAllViews() {
703 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800704 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700705 }
706
707 @Override
708 public void removeAllViewsInLayout() {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700709 if (mChildren.getChildCount() > 0) {
710 clearOccupiedCells();
711 mChildren.removeAllViewsInLayout();
712 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700713 }
714
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700715 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800716 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700717 }
718
Michael Jurka0280c3b2010-09-17 15:00:07 -0700719 @Override
720 public void removeView(View view) {
721 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800722 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700723 }
724
725 @Override
726 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800727 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
728 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700729 }
730
731 @Override
732 public void removeViewInLayout(View view) {
733 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800734 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700735 }
736
737 @Override
738 public void removeViews(int start, int count) {
739 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800740 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700741 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800742 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700743 }
744
745 @Override
746 public void removeViewsInLayout(int start, int count) {
747 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800748 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700749 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800750 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700751 }
752
Michael Jurka8c920dd2011-01-20 14:16:56 -0800753 public void drawChildren(Canvas canvas) {
754 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 }
756
Michael Jurkaabded662011-03-04 12:06:57 -0800757 void buildChildrenLayer() {
758 mChildren.buildLayer();
759 }
760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 @Override
762 protected void onAttachedToWindow() {
763 super.onAttachedToWindow();
764 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
765 }
766
Michael Jurkaaf442092010-06-10 17:01:57 -0700767 public void setTagToCellInfoForPoint(int touchX, int touchY) {
768 final CellInfo cellInfo = mCellInfo;
769 final Rect frame = mRect;
770 final int x = touchX + mScrollX;
771 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800772 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700773
774 boolean found = false;
775 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800776 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800777 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700778
Adam Cohen1b607ed2011-03-03 17:26:50 -0800779 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
780 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700781 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700782
783 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
784 // offset that by this CellLayout's padding to test an (x,y) point that is relative
785 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700786 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700787
Michael Jurkaaf442092010-06-10 17:01:57 -0700788 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700789 cellInfo.cell = child;
790 cellInfo.cellX = lp.cellX;
791 cellInfo.cellY = lp.cellY;
792 cellInfo.spanX = lp.cellHSpan;
793 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700794 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700795 break;
796 }
797 }
798 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700799
Michael Jurkad771c962011-08-09 15:00:48 -0700800 mLastDownOnOccupiedCell = found;
801
Michael Jurkaaf442092010-06-10 17:01:57 -0700802 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700803 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700804 pointToCellExact(x, y, cellXY);
805
Michael Jurkaaf442092010-06-10 17:01:57 -0700806 cellInfo.cell = null;
807 cellInfo.cellX = cellXY[0];
808 cellInfo.cellY = cellXY[1];
809 cellInfo.spanX = 1;
810 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700811 }
812 setTag(cellInfo);
813 }
814
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 @Override
816 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700817 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
818 // even in the case where we return early. Not clearing here was causing bugs whereby on
819 // long-press we'd end up picking up an item from a previous drag operation.
820 final int action = ev.getAction();
821
822 if (action == MotionEvent.ACTION_DOWN) {
823 clearTagCellInfo();
824 }
825
Michael Jurkadee05892010-07-27 10:01:56 -0700826 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
827 return true;
828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829
830 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700831 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 return false;
834 }
835
Adam Cohenc1997fd2011-08-15 18:26:39 -0700836 private void clearTagCellInfo() {
837 final CellInfo cellInfo = mCellInfo;
838 cellInfo.cell = null;
839 cellInfo.cellX = -1;
840 cellInfo.cellY = -1;
841 cellInfo.spanX = 0;
842 cellInfo.spanY = 0;
843 setTag(cellInfo);
844 }
845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700847 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 }
849
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700850 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700851 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 * @param x X coordinate of the point
853 * @param y Y coordinate of the point
854 * @param result Array of 2 ints to hold the x and y coordinate of the cell
855 */
856 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700857 final int hStartPadding = getPaddingLeft();
858 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859
860 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
861 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
862
Adam Cohend22015c2010-07-26 22:02:18 -0700863 final int xAxis = mCountX;
864 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865
866 if (result[0] < 0) result[0] = 0;
867 if (result[0] >= xAxis) result[0] = xAxis - 1;
868 if (result[1] < 0) result[1] = 0;
869 if (result[1] >= yAxis) result[1] = yAxis - 1;
870 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700871
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 /**
873 * Given a point, return the cell that most closely encloses that point
874 * @param x X coordinate of the point
875 * @param y Y coordinate of the point
876 * @param result Array of 2 ints to hold the x and y coordinate of the cell
877 */
878 void pointToCellRounded(int x, int y, int[] result) {
879 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
880 }
881
882 /**
883 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700884 *
885 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700887 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 * @param result Array of 2 ints to hold the x and y coordinate of the point
889 */
890 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700891 final int hStartPadding = getPaddingLeft();
892 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893
894 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
895 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
896 }
897
Adam Cohene3e27a82011-04-15 12:07:39 -0700898 /**
899 * Given a cell coordinate, return the point that represents the upper left corner of that cell
900 *
901 * @param cellX X coordinate of the cell
902 * @param cellY Y coordinate of the cell
903 *
904 * @param result Array of 2 ints to hold the x and y coordinate of the point
905 */
906 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700907 final int hStartPadding = getPaddingLeft();
908 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700909
910 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
911 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
912 }
913
Romain Guy84f296c2009-11-04 15:00:44 -0800914 int getCellWidth() {
915 return mCellWidth;
916 }
917
918 int getCellHeight() {
919 return mCellHeight;
920 }
921
Adam Cohend4844c32011-02-18 19:25:06 -0800922 int getWidthGap() {
923 return mWidthGap;
924 }
925
926 int getHeightGap() {
927 return mHeightGap;
928 }
929
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700930 Rect getContentRect(Rect r) {
931 if (r == null) {
932 r = new Rect();
933 }
934 int left = getPaddingLeft();
935 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700936 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
937 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700938 r.set(left, top, right, bottom);
939 return r;
940 }
941
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 @Override
943 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
944 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700945
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700947 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
948
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
950 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700951
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
953 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
954 }
955
Adam Cohend22015c2010-07-26 22:02:18 -0700956 int numWidthGaps = mCountX - 1;
957 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958
Adam Cohen234c4cd2011-07-17 21:03:04 -0700959 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700960 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
961 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
962 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
963 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
964 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
965 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700966 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700967 } else {
968 mWidthGap = mOriginalWidthGap;
969 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700970 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700971
Michael Jurka8c920dd2011-01-20 14:16:56 -0800972 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
973 int newWidth = widthSpecSize;
974 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700975 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700976 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700977 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700978 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700979 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700980 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700981 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800982
983 int count = getChildCount();
984 for (int i = 0; i < count; i++) {
985 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700986 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
987 mPaddingRight, MeasureSpec.EXACTLY);
988 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
989 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800990 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
991 }
992 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
994
995 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700996 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800999 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -07001000 child.layout(mPaddingLeft, mPaddingTop,
1001 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 }
1003 }
1004
1005 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001006 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1007 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001008 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001009 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
1010 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurka33945b22010-12-21 18:19:38 -08001011 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -07001012 }
1013
1014 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001016 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 }
1018
1019 @Override
1020 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001021 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
1023
Michael Jurka5f1c5092010-09-03 14:15:02 -07001024 public float getBackgroundAlpha() {
1025 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001026 }
1027
Michael Jurka742574b2011-02-02 23:51:01 -08001028 public void setFastBackgroundAlpha(float alpha) {
1029 mBackgroundAlpha = alpha;
1030 }
1031
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001032 public void setBackgroundAlphaMultiplier(float multiplier) {
1033 mBackgroundAlphaMultiplier = multiplier;
1034 }
1035
Adam Cohenddb82192010-11-10 16:32:54 -08001036 public float getBackgroundAlphaMultiplier() {
1037 return mBackgroundAlphaMultiplier;
1038 }
1039
Michael Jurka5f1c5092010-09-03 14:15:02 -07001040 public void setBackgroundAlpha(float alpha) {
1041 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -07001042 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -07001043 }
1044
Michael Jurka5f1c5092010-09-03 14:15:02 -07001045 // Need to return true to let the view system know we know how to handle alpha-- this is
1046 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
1047 // versions
1048 @Override
1049 protected boolean onSetAlpha(int alpha) {
1050 return true;
1051 }
1052
1053 public void setAlpha(float alpha) {
1054 setChildrenAlpha(alpha);
1055 super.setAlpha(alpha);
1056 }
1057
Michael Jurka742574b2011-02-02 23:51:01 -08001058 public void setFastAlpha(float alpha) {
1059 setFastChildrenAlpha(alpha);
1060 super.setFastAlpha(alpha);
1061 }
1062
Michael Jurkadee05892010-07-27 10:01:56 -07001063 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001064 final int childCount = getChildCount();
1065 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001066 getChildAt(i).setAlpha(alpha);
1067 }
1068 }
1069
Michael Jurka742574b2011-02-02 23:51:01 -08001070 private void setFastChildrenAlpha(float alpha) {
1071 final int childCount = getChildCount();
1072 for (int i = 0; i < childCount; i++) {
1073 getChildAt(i).setFastAlpha(alpha);
1074 }
1075 }
1076
Patrick Dubroy440c3602010-07-13 17:50:32 -07001077 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001078 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001079 }
1080
Adam Cohen76fc0852011-06-17 13:26:23 -07001081 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
1082 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001083 CellLayoutChildren clc = getChildrenLayout();
1084 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
1085 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1086 final ItemInfo info = (ItemInfo) child.getTag();
1087
1088 // We cancel any existing animations
1089 if (mReorderAnimators.containsKey(lp)) {
1090 mReorderAnimators.get(lp).cancel();
1091 mReorderAnimators.remove(lp);
1092 }
1093
1094 int oldX = lp.x;
1095 int oldY = lp.y;
1096 mOccupied[lp.cellX][lp.cellY] = false;
1097 mOccupied[cellX][cellY] = true;
1098
1099 lp.isLockedToGrid = true;
1100 lp.cellX = info.cellX = cellX;
1101 lp.cellY = info.cellY = cellY;
1102 clc.setupLp(lp);
1103 lp.isLockedToGrid = false;
1104 int newX = lp.x;
1105 int newY = lp.y;
1106
Adam Cohen76fc0852011-06-17 13:26:23 -07001107 lp.x = oldX;
1108 lp.y = oldY;
1109 child.requestLayout();
1110
Adam Cohenbfbfd262011-06-13 16:55:12 -07001111 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
1112 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
1113 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
1114 oa.setDuration(duration);
1115 mReorderAnimators.put(lp, oa);
1116 oa.addUpdateListener(new AnimatorUpdateListener() {
1117 public void onAnimationUpdate(ValueAnimator animation) {
1118 child.requestLayout();
1119 }
1120 });
1121 oa.addListener(new AnimatorListenerAdapter() {
1122 boolean cancelled = false;
1123 public void onAnimationEnd(Animator animation) {
1124 // If the animation was cancelled, it means that another animation
1125 // has interrupted this one, and we don't want to lock the item into
1126 // place just yet.
1127 if (!cancelled) {
1128 lp.isLockedToGrid = true;
1129 }
1130 if (mReorderAnimators.containsKey(lp)) {
1131 mReorderAnimators.remove(lp);
1132 }
1133 }
1134 public void onAnimationCancel(Animator animation) {
1135 cancelled = true;
1136 }
1137 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001138 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001139 oa.start();
1140 return true;
1141 }
1142 return false;
1143 }
1144
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001145 /**
1146 * Estimate where the top left cell of the dragged item will land if it is dropped.
1147 *
1148 * @param originX The X value of the top left corner of the item
1149 * @param originY The Y value of the top left corner of the item
1150 * @param spanX The number of horizontal cells that the item spans
1151 * @param spanY The number of vertical cells that the item spans
1152 * @param result The estimated drop cell X and Y.
1153 */
1154 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001155 final int countX = mCountX;
1156 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001157
Michael Jurkaa63c4522010-08-19 13:52:27 -07001158 // pointToCellRounded takes the top left of a cell but will pad that with
1159 // cellWidth/2 and cellHeight/2 when finding the matching cell
1160 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001161
1162 // If the item isn't fully on this screen, snap to the edges
1163 int rightOverhang = result[0] + spanX - countX;
1164 if (rightOverhang > 0) {
1165 result[0] -= rightOverhang; // Snap to right
1166 }
1167 result[0] = Math.max(0, result[0]); // Snap to left
1168 int bottomOverhang = result[1] + spanY - countY;
1169 if (bottomOverhang > 0) {
1170 result[1] -= bottomOverhang; // Snap to bottom
1171 }
1172 result[1] = Math.max(0, result[1]); // Snap to top
1173 }
1174
Winson Chungb8c69f32011-10-19 21:36:08 -07001175 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY,
1176 int spanX, int spanY, Point dragOffset, Rect dragRegion) {
Joe Onorato4be866d2010-10-10 11:26:02 -07001177
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001178 final int oldDragCellX = mDragCell[0];
1179 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001180 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chungb8c69f32011-10-19 21:36:08 -07001181 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001182 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1183 } else {
1184 mDragCenter.set(originX, originY);
1185 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001186
Adam Cohen2801caf2011-05-13 20:57:39 -07001187 if (dragOutline == null && v == null) {
1188 if (mCrosshairsDrawable != null) {
1189 invalidate();
1190 }
1191 return;
1192 }
1193
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001194 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001195 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001196 final int[] topLeft = mTmpPoint;
1197 cellToPoint(nearest[0], nearest[1], topLeft);
1198
Joe Onorato4be866d2010-10-10 11:26:02 -07001199 int left = topLeft[0];
1200 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001201
Winson Chungb8c69f32011-10-19 21:36:08 -07001202 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001203 // When drawing the drag outline, it did not account for margin offsets
1204 // added by the view's parent.
1205 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1206 left += lp.leftMargin;
1207 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001208
Adam Cohen99e8b402011-03-25 19:23:43 -07001209 // Offsets due to the size difference between the View and the dragOutline.
1210 // There is a size difference to account for the outer blur, which may lie
1211 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001212 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001213 // We center about the x axis
1214 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1215 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001216 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001217 if (dragOffset != null && dragRegion != null) {
1218 // Center the drag region *horizontally* in the cell and apply a drag
1219 // outline offset
1220 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1221 - dragRegion.width()) / 2;
1222 top += dragOffset.y;
1223 } else {
1224 // Center the drag outline in the cell
1225 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1226 - dragOutline.getWidth()) / 2;
1227 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1228 - dragOutline.getHeight()) / 2;
1229 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001230 }
Winson Chung150fbab2010-09-29 17:14:26 -07001231
Joe Onorato4be866d2010-10-10 11:26:02 -07001232 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001233 mDragOutlineAnims[oldIndex].animateOut();
1234 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001235
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001236 mDragOutlines[mDragOutlineCurrent].set(left, top);
1237 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1238 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001239 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001240
1241 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1242 if (mCrosshairsDrawable != null) {
1243 invalidate();
1244 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001245 }
1246
Adam Cohene0310962011-04-18 16:15:31 -07001247 public void clearDragOutlines() {
1248 final int oldIndex = mDragOutlineCurrent;
1249 mDragOutlineAnims[oldIndex].animateOut();
1250 mDragCell[0] = -1;
1251 mDragCell[1] = -1;
1252 }
1253
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001255 * Find a vacant area that will fit the given bounds nearest the requested
1256 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001257 *
Romain Guy51afc022009-05-04 18:03:43 -07001258 * @param pixelX The X location at which you want to search for a vacant area.
1259 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001260 * @param spanX Horizontal span of the object.
1261 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001262 * @param result Array in which to place the result, or null (in which case a new array will
1263 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001264 * @return The X, Y cell of a vacant area that can contain this object,
1265 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001267 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001268 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1269 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001270 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001271
Michael Jurka6a1435d2010-09-27 17:35:12 -07001272 /**
1273 * Find a vacant area that will fit the given bounds nearest the requested
1274 * cell location. Uses Euclidean distance to score multiple vacant areas.
1275 *
1276 * @param pixelX The X location at which you want to search for a vacant area.
1277 * @param pixelY The Y location at which you want to search for a vacant area.
1278 * @param spanX Horizontal span of the object.
1279 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001280 * @param ignoreOccupied If true, the result can be an occupied cell
1281 * @param result Array in which to place the result, or null (in which case a new array will
1282 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001283 * @return The X, Y cell of a vacant area that can contain this object,
1284 * nearest the requested location.
1285 */
Adam Cohendf035382011-04-11 17:22:04 -07001286 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1287 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001288 // mark space take by ignoreView as available (method checks if ignoreView is null)
1289 markCellsAsUnoccupiedForView(ignoreView);
1290
Adam Cohene3e27a82011-04-15 12:07:39 -07001291 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1292 // to the center of the item, but we are searching based on the top-left cell, so
1293 // we translate the point over to correspond to the top-left.
1294 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1295 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1296
Jeff Sharkey70864282009-04-07 21:08:40 -07001297 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001298 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001299 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001300
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001301 final int countX = mCountX;
1302 final int countY = mCountY;
1303 final boolean[][] occupied = mOccupied;
1304
Winson Chungbbc60d82010-11-11 16:34:41 -08001305 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001306 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001307 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001308 if (ignoreOccupied) {
1309 for (int i = 0; i < spanX; i++) {
1310 for (int j = 0; j < spanY; j++) {
1311 if (occupied[x + i][y + j]) {
1312 // small optimization: we can skip to after the column we
1313 // just found an occupied cell
1314 x += i;
1315 continue inner;
1316 }
Michael Jurkac28de512010-08-13 11:27:44 -07001317 }
1318 }
1319 }
Winson Chung0be025d2011-05-23 17:45:09 -07001320 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001321 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322
Michael Jurkac28de512010-08-13 11:27:44 -07001323 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1324 + Math.pow(cellXY[1] - pixelY, 2));
1325 if (distance <= bestDistance) {
1326 bestDistance = distance;
1327 bestXY[0] = x;
1328 bestXY[1] = y;
1329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
1331 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001332 // re-mark space taken by ignoreView as occupied
1333 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334
Adam Cohenc0dcf592011-06-01 15:30:43 -07001335 // Return -1, -1 if no suitable location found
1336 if (bestDistance == Double.MAX_VALUE) {
1337 bestXY[0] = -1;
1338 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001339 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001340 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001342
Adam Cohendf035382011-04-11 17:22:04 -07001343 /**
1344 * Find a vacant area that will fit the given bounds nearest the requested
1345 * cell location. Uses Euclidean distance to score multiple vacant areas.
1346 *
1347 * @param pixelX The X location at which you want to search for a vacant area.
1348 * @param pixelY The Y location at which you want to search for a vacant area.
1349 * @param spanX Horizontal span of the object.
1350 * @param spanY Vertical span of the object.
1351 * @param ignoreView Considers space occupied by this view as unoccupied
1352 * @param result Previously returned value to possibly recycle.
1353 * @return The X, Y cell of a vacant area that can contain this object,
1354 * nearest the requested location.
1355 */
1356 int[] findNearestVacantArea(
1357 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1358 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1359 }
1360
1361 /**
1362 * Find a starting cell position that will fit the given bounds nearest the requested
1363 * cell location. Uses Euclidean distance to score multiple vacant areas.
1364 *
1365 * @param pixelX The X location at which you want to search for a vacant area.
1366 * @param pixelY The Y location at which you want to search for a vacant area.
1367 * @param spanX Horizontal span of the object.
1368 * @param spanY Vertical span of the object.
1369 * @param ignoreView Considers space occupied by this view as unoccupied
1370 * @param result Previously returned value to possibly recycle.
1371 * @return The X, Y cell of a vacant area that can contain this object,
1372 * nearest the requested location.
1373 */
1374 int[] findNearestArea(
1375 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1376 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1377 }
1378
Michael Jurka0280c3b2010-09-17 15:00:07 -07001379 boolean existsEmptyCell() {
1380 return findCellForSpan(null, 1, 1);
1381 }
1382
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001384 * Finds the upper-left coordinate of the first rectangle in the grid that can
1385 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1386 * then this method will only return coordinates for rectangles that contain the cell
1387 * (intersectX, intersectY)
1388 *
1389 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1390 * can be found.
1391 * @param spanX The horizontal span of the cell we want to find.
1392 * @param spanY The vertical span of the cell we want to find.
1393 *
1394 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001395 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001396 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1397 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1398 }
1399
1400 /**
1401 * Like above, but ignores any cells occupied by the item "ignoreView"
1402 *
1403 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1404 * can be found.
1405 * @param spanX The horizontal span of the cell we want to find.
1406 * @param spanY The vertical span of the cell we want to find.
1407 * @param ignoreView The home screen item we should treat as not occupying any space
1408 * @return
1409 */
1410 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1411 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1412 }
1413
1414 /**
1415 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1416 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1417 *
1418 * @param spanX The horizontal span of the cell we want to find.
1419 * @param spanY The vertical span of the cell we want to find.
1420 * @param ignoreView The home screen item we should treat as not occupying any space
1421 * @param intersectX The X coordinate of the cell that we should try to overlap
1422 * @param intersectX The Y coordinate of the cell that we should try to overlap
1423 *
1424 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1425 */
1426 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1427 int intersectX, int intersectY) {
1428 return findCellForSpanThatIntersectsIgnoring(
1429 cellXY, spanX, spanY, intersectX, intersectY, null);
1430 }
1431
1432 /**
1433 * The superset of the above two methods
1434 */
1435 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1436 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001437 // mark space take by ignoreView as available (method checks if ignoreView is null)
1438 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001439
Michael Jurka28750fb2010-09-24 17:43:49 -07001440 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001441 while (true) {
1442 int startX = 0;
1443 if (intersectX >= 0) {
1444 startX = Math.max(startX, intersectX - (spanX - 1));
1445 }
1446 int endX = mCountX - (spanX - 1);
1447 if (intersectX >= 0) {
1448 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1449 }
1450 int startY = 0;
1451 if (intersectY >= 0) {
1452 startY = Math.max(startY, intersectY - (spanY - 1));
1453 }
1454 int endY = mCountY - (spanY - 1);
1455 if (intersectY >= 0) {
1456 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1457 }
1458
Winson Chungbbc60d82010-11-11 16:34:41 -08001459 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001460 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001461 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001462 for (int i = 0; i < spanX; i++) {
1463 for (int j = 0; j < spanY; j++) {
1464 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001465 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001466 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001467 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001468 continue inner;
1469 }
1470 }
1471 }
1472 if (cellXY != null) {
1473 cellXY[0] = x;
1474 cellXY[1] = y;
1475 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001476 foundCell = true;
1477 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001478 }
1479 }
1480 if (intersectX == -1 && intersectY == -1) {
1481 break;
1482 } else {
1483 // if we failed to find anything, try again but without any requirements of
1484 // intersecting
1485 intersectX = -1;
1486 intersectY = -1;
1487 continue;
1488 }
1489 }
1490
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001491 // re-mark space taken by ignoreView as occupied
1492 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001493 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001494 }
1495
1496 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001497 * A drag event has begun over this layout.
1498 * It may have begun over this layout (in which case onDragChild is called first),
1499 * or it may have begun on another layout.
1500 */
1501 void onDragEnter() {
1502 if (!mDragging) {
1503 // Fade in the drag indicators
1504 if (mCrosshairsAnimator != null) {
1505 mCrosshairsAnimator.animateIn();
1506 }
1507 }
1508 mDragging = true;
1509 }
1510
1511 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001512 * Called when drag has left this CellLayout or has been completed (successfully or not)
1513 */
1514 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001515 // This can actually be called when we aren't in a drag, e.g. when adding a new
1516 // item to this layout via the customize drawer.
1517 // Guard against that case.
1518 if (mDragging) {
1519 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001520
Joe Onorato4be866d2010-10-10 11:26:02 -07001521 // Fade out the drag indicators
1522 if (mCrosshairsAnimator != null) {
1523 mCrosshairsAnimator.animateOut();
1524 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001525 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001526
1527 // Invalidate the drag data
1528 mDragCell[0] = -1;
1529 mDragCell[1] = -1;
1530 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1531 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1532
Michael Jurka33945b22010-12-21 18:19:38 -08001533 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001534 }
1535
1536 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001537 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001538 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001539 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 *
1541 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001543 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001544 if (child != null) {
1545 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001546 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001547 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
1550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001553 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 * @param cellX X coordinate of upper left corner expressed as a cell position
1555 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001556 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001558 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001560 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 final int cellWidth = mCellWidth;
1562 final int cellHeight = mCellHeight;
1563 final int widthGap = mWidthGap;
1564 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001565
Winson Chung4b825dcd2011-06-19 12:41:22 -07001566 final int hStartPadding = getPaddingLeft();
1567 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001568
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1570 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1571
1572 int x = hStartPadding + cellX * (cellWidth + widthGap);
1573 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001574
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001575 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001579 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001581 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 * @param width Width in pixels
1583 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001584 * @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 -08001585 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001586 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001587 return rectToCell(getResources(), width, height, result);
1588 }
1589
1590 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 // Always assume we're working with the smallest span to make sure we
1592 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001593 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1594 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001596
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07001598 int spanX = (int) Math.ceil(width / (float) smallerSize);
1599 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04001600
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001601 if (result == null) {
1602 return new int[] { spanX, spanY };
1603 }
1604 result[0] = spanX;
1605 result[1] = spanY;
1606 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 }
1608
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001609 public int[] cellSpansToSize(int hSpans, int vSpans) {
1610 int[] size = new int[2];
1611 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1612 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1613 return size;
1614 }
1615
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001617 * Calculate the grid spans needed to fit given item
1618 */
1619 public void calculateSpans(ItemInfo info) {
1620 final int minWidth;
1621 final int minHeight;
1622
1623 if (info instanceof LauncherAppWidgetInfo) {
1624 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1625 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1626 } else if (info instanceof PendingAddWidgetInfo) {
1627 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1628 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1629 } else {
1630 // It's not a widget, so it must be 1x1
1631 info.spanX = info.spanY = 1;
1632 return;
1633 }
1634 int[] spans = rectToCell(minWidth, minHeight, null);
1635 info.spanX = spans[0];
1636 info.spanY = spans[1];
1637 }
1638
1639 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 * Find the first vacant cell, if there is one.
1641 *
1642 * @param vacant Holds the x and y coordinate of the vacant cell
1643 * @param spanX Horizontal cell span.
1644 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001645 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 * @return True if a vacant cell was found
1647 */
1648 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649
Michael Jurka0280c3b2010-09-17 15:00:07 -07001650 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 }
1652
1653 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1654 int xCount, int yCount, boolean[][] occupied) {
1655
Adam Cohen2801caf2011-05-13 20:57:39 -07001656 for (int y = 0; y < yCount; y++) {
1657 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 boolean available = !occupied[x][y];
1659out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1660 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1661 available = available && !occupied[i][j];
1662 if (!available) break out;
1663 }
1664 }
1665
1666 if (available) {
1667 vacant[0] = x;
1668 vacant[1] = y;
1669 return true;
1670 }
1671 }
1672 }
1673
1674 return false;
1675 }
1676
Michael Jurka0280c3b2010-09-17 15:00:07 -07001677 private void clearOccupiedCells() {
1678 for (int x = 0; x < mCountX; x++) {
1679 for (int y = 0; y < mCountY; y++) {
1680 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 }
1682 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684
Adam Cohen1b607ed2011-03-03 17:26:50 -08001685 /**
1686 * Given a view, determines how much that view can be expanded in all directions, in terms of
1687 * whether or not there are other items occupying adjacent cells. Used by the
1688 * AppWidgetResizeFrame to determine how the widget can be resized.
1689 */
Adam Cohend4844c32011-02-18 19:25:06 -08001690 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001691 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001692 boolean flag;
1693
Adam Cohen1b607ed2011-03-03 17:26:50 -08001694 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001695 for (int x = lp.cellX - 1; x >= 0; x--) {
1696 flag = false;
1697 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1698 if (mOccupied[x][y]) flag = true;
1699 }
1700 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001701 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001702 }
1703
Adam Cohen1b607ed2011-03-03 17:26:50 -08001704 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001705 for (int y = lp.cellY - 1; y >= 0; y--) {
1706 flag = false;
1707 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1708 if (mOccupied[x][y]) flag = true;
1709 }
1710 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001711 expandability[AppWidgetResizeFrame.TOP]++;
1712 }
Adam Cohend4844c32011-02-18 19:25:06 -08001713
Adam Cohen1b607ed2011-03-03 17:26:50 -08001714 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001715 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1716 flag = false;
1717 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1718 if (mOccupied[x][y]) flag = true;
1719 }
1720 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001721 expandability[AppWidgetResizeFrame.RIGHT]++;
1722 }
Adam Cohend4844c32011-02-18 19:25:06 -08001723
Adam Cohen1b607ed2011-03-03 17:26:50 -08001724 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001725 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1726 flag = false;
1727 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1728 if (mOccupied[x][y]) flag = true;
1729 }
1730 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001731 expandability[AppWidgetResizeFrame.BOTTOM]++;
1732 }
Adam Cohend4844c32011-02-18 19:25:06 -08001733 }
1734
Michael Jurka0280c3b2010-09-17 15:00:07 -07001735 public void onMove(View view, int newCellX, int newCellY) {
1736 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1737 markCellsAsUnoccupiedForView(view);
1738 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740
Adam Cohend4844c32011-02-18 19:25:06 -08001741 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001742 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001743 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1744 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1745 }
1746
Adam Cohend4844c32011-02-18 19:25:06 -08001747 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001748 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001749 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1750 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1751 }
1752
1753 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1754 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1755 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1756 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 }
1758 }
1759 }
1760
Adam Cohen2801caf2011-05-13 20:57:39 -07001761 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001762 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001763 (Math.max((mCountX - 1), 0) * mWidthGap);
1764 }
1765
1766 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001767 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001768 (Math.max((mCountY - 1), 0) * mHeightGap);
1769 }
1770
Michael Jurka66d72172011-04-12 16:29:25 -07001771 public boolean isOccupied(int x, int y) {
1772 if (x < mCountX && y < mCountY) {
1773 return mOccupied[x][y];
1774 } else {
1775 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1776 }
1777 }
1778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 @Override
1780 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1781 return new CellLayout.LayoutParams(getContext(), attrs);
1782 }
1783
1784 @Override
1785 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1786 return p instanceof CellLayout.LayoutParams;
1787 }
1788
1789 @Override
1790 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1791 return new CellLayout.LayoutParams(p);
1792 }
1793
Winson Chungaafa03c2010-06-11 17:34:16 -07001794 public static class CellLayoutAnimationController extends LayoutAnimationController {
1795 public CellLayoutAnimationController(Animation animation, float delay) {
1796 super(animation, delay);
1797 }
1798
1799 @Override
1800 protected long getDelayForView(View view) {
1801 return (int) (Math.random() * 150);
1802 }
1803 }
1804
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1806 /**
1807 * Horizontal location of the item in the grid.
1808 */
1809 @ViewDebug.ExportedProperty
1810 public int cellX;
1811
1812 /**
1813 * Vertical location of the item in the grid.
1814 */
1815 @ViewDebug.ExportedProperty
1816 public int cellY;
1817
1818 /**
1819 * Number of cells spanned horizontally by the item.
1820 */
1821 @ViewDebug.ExportedProperty
1822 public int cellHSpan;
1823
1824 /**
1825 * Number of cells spanned vertically by the item.
1826 */
1827 @ViewDebug.ExportedProperty
1828 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001829
Adam Cohen1b607ed2011-03-03 17:26:50 -08001830 /**
1831 * Indicates whether the item will set its x, y, width and height parameters freely,
1832 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1833 */
Adam Cohend4844c32011-02-18 19:25:06 -08001834 public boolean isLockedToGrid = true;
1835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 // X coordinate of the view in the layout.
1837 @ViewDebug.ExportedProperty
1838 int x;
1839 // Y coordinate of the view in the layout.
1840 @ViewDebug.ExportedProperty
1841 int y;
1842
Romain Guy84f296c2009-11-04 15:00:44 -08001843 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 public LayoutParams(Context c, AttributeSet attrs) {
1846 super(c, attrs);
1847 cellHSpan = 1;
1848 cellVSpan = 1;
1849 }
1850
1851 public LayoutParams(ViewGroup.LayoutParams source) {
1852 super(source);
1853 cellHSpan = 1;
1854 cellVSpan = 1;
1855 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001856
1857 public LayoutParams(LayoutParams source) {
1858 super(source);
1859 this.cellX = source.cellX;
1860 this.cellY = source.cellY;
1861 this.cellHSpan = source.cellHSpan;
1862 this.cellVSpan = source.cellVSpan;
1863 }
1864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001866 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 this.cellX = cellX;
1868 this.cellY = cellY;
1869 this.cellHSpan = cellHSpan;
1870 this.cellVSpan = cellVSpan;
1871 }
1872
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001873 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001874 if (isLockedToGrid) {
1875 final int myCellHSpan = cellHSpan;
1876 final int myCellVSpan = cellVSpan;
1877 final int myCellX = cellX;
1878 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001879
Adam Cohend4844c32011-02-18 19:25:06 -08001880 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1881 leftMargin - rightMargin;
1882 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1883 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001884 x = myCellX * (cellWidth + widthGap) + leftMargin;
1885 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001886 }
1887 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001888
Winson Chungaafa03c2010-06-11 17:34:16 -07001889 public String toString() {
1890 return "(" + this.cellX + ", " + this.cellY + ")";
1891 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001892
1893 public void setWidth(int width) {
1894 this.width = width;
1895 }
1896
1897 public int getWidth() {
1898 return width;
1899 }
1900
1901 public void setHeight(int height) {
1902 this.height = height;
1903 }
1904
1905 public int getHeight() {
1906 return height;
1907 }
1908
1909 public void setX(int x) {
1910 this.x = x;
1911 }
1912
1913 public int getX() {
1914 return x;
1915 }
1916
1917 public void setY(int y) {
1918 this.y = y;
1919 }
1920
1921 public int getY() {
1922 return y;
1923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 }
1925
Michael Jurka0280c3b2010-09-17 15:00:07 -07001926 // This class stores info for two purposes:
1927 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1928 // its spanX, spanY, and the screen it is on
1929 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1930 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1931 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001932 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001934 int cellX = -1;
1935 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 int spanX;
1937 int spanY;
1938 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001939 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 @Override
1942 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001943 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1944 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
1946 }
Michael Jurkad771c962011-08-09 15:00:48 -07001947
1948 public boolean lastDownOnOccupiedCell() {
1949 return mLastDownOnOccupiedCell;
1950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951}