blob: 6e60280775c0ad05e4ce329f0f9d46e356ef4876 [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;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070039import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070040import android.graphics.drawable.NinePatchDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070042import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Adam Cohen66396872011-04-15 17:50:36 -070051import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070052import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070053
Adam Cohen69ce2e52011-07-03 19:25:21 -070054import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070056import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -070057
Michael Jurkabdb5c532011-02-01 15:05:06 -080058public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070059 static final String TAG = "CellLayout";
60
Winson Chung4b825dcd2011-06-19 12:41:22 -070061 private int mOriginalCellWidth;
62 private int mOriginalCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 private int mCellWidth;
64 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070065
Adam Cohend22015c2010-07-26 22:02:18 -070066 private int mCountX;
67 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
Adam Cohen234c4cd2011-07-17 21:03:04 -070069 private int mOriginalWidthGap;
70 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 private int mWidthGap;
72 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070073 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080074 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
76 private final Rect mRect = new Rect();
77 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070078
Patrick Dubroyde7658b2010-09-27 11:15:43 -070079 // These are temporary variables to prevent having to allocate a new object just to
80 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070081 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070082 private final int[] mTmpPoint = new int[2];
83 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070084 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070085
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 boolean[][] mOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070087 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Michael Jurkadee05892010-07-27 10:01:56 -070089 private OnTouchListener mInterceptTouchListener;
90
Adam Cohen69ce2e52011-07-03 19:25:21 -070091 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070092 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070093
Adam Cohenb5ba0972011-09-07 18:02:31 -070094 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070095 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070096 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070097
Michael Jurka33945b22010-12-21 18:19:38 -080098 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080099 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700100 private Drawable mOverScrollForegroundDrawable;
101 private Drawable mOverScrollLeft;
102 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700103 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700104 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700105 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700106
Michael Jurka33945b22010-12-21 18:19:38 -0800107 // If we're actively dragging something over this screen, mIsDragOverlapping is true
108 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700109 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700110
Winson Chung150fbab2010-09-29 17:14:26 -0700111 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700112 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Winson Chung63257c12011-05-05 17:06:13 -0700113 private Point[] mDragOutlines = new Point[4];
Chet Haase472b2812010-10-14 07:02:04 -0700114 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700115 private InterruptibleInOutAnimator[] mDragOutlineAnims =
116 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700117
118 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700119 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700120 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700121
Patrick Dubroy96864c32011-03-10 17:17:23 -0800122 private BubbleTextView mPressedOrFocusedIcon;
123
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700124 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700125 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700126 private float mCrosshairsVisibility = 0.0f;
127
Adam Cohenbfbfd262011-06-13 16:55:12 -0700128 private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new
129 HashMap<CellLayout.LayoutParams, ObjectAnimator>();
130
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700131 // When a drag operation is in progress, holds the nearest cell to the touch point
132 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Joe Onorato4be866d2010-10-10 11:26:02 -0700134 private boolean mDragging = false;
135
Patrick Dubroyce34a972010-10-19 10:34:32 -0700136 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800137 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 public CellLayout(Context context) {
140 this(context, null);
141 }
142
143 public CellLayout(Context context, AttributeSet attrs) {
144 this(context, attrs, 0);
145 }
146
147 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
148 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700149
150 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
151 // the user where a dragged item will land when dropped.
152 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
155
Winson Chung4b825dcd2011-06-19 12:41:22 -0700156 mOriginalCellWidth =
157 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
158 mOriginalCellHeight =
159 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700160 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
161 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700162 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700163 mCountX = LauncherModel.getCellCountX();
164 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700165 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
167 a.recycle();
168
169 setAlwaysDrawnWithCacheEnabled(false);
170
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700171 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700172
Winson Chung967289b2011-06-30 18:09:30 -0700173 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700174 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800175
Adam Cohenb5ba0972011-09-07 18:02:31 -0700176 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
177 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
178 mForegroundPadding =
179 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800180
Winson Chungb26f3d62011-06-02 10:49:29 -0700181 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700182 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700183
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700184 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700185
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700186 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700187 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700188
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700189 // Set up the animation for fading the crosshairs in and out
190 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700191 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700192 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700193 public void onAnimationUpdate(ValueAnimator animation) {
194 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700195 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700196 }
197 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700198 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700199
Winson Chungb8c69f32011-10-19 21:36:08 -0700200 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700201 for (int i = 0; i < mDragOutlines.length; i++) {
202 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700203 }
204
205 // When dragging things around the home screens, we show a green outline of
206 // where the item will land. The outlines gradually fade out, leaving a trail
207 // behind the drag path.
208 // Set up all the animations that are used to implement this fading.
209 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700210 final float fromAlphaValue = 0;
211 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700212
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700213 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700214
215 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700216 final InterruptibleInOutAnimator anim =
217 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700218 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700219 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700220 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700221 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700222 final Bitmap outline = (Bitmap)anim.getTag();
223
224 // If an animation is started and then stopped very quickly, we can still
225 // get spurious updates we've cleared the tag. Guard against this.
226 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700227 if (false) {
228 Object val = animation.getAnimatedValue();
229 Log.d(TAG, "anim " + thisIndex + " update: " + val +
230 ", isStopped " + anim.isStopped());
231 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700232 // Try to prevent it from continuing to run
233 animation.cancel();
234 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700235 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700236 final int left = mDragOutlines[thisIndex].x;
237 final int top = mDragOutlines[thisIndex].y;
238 CellLayout.this.invalidate(left, top,
239 left + outline.getWidth(), top + outline.getHeight());
240 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700241 }
242 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700243 // The animation holds a reference to the drag outline bitmap as long is it's
244 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700245 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700246 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700247 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700248 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700249 anim.setTag(null);
250 }
251 }
252 });
253 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700254 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700255
Michael Jurka18014792010-10-14 09:01:34 -0700256 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700257 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800258
Michael Jurka8c920dd2011-01-20 14:16:56 -0800259 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700260 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800261 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700262 }
263
Michael Jurkaf6440da2011-04-05 14:50:34 -0700264 static int widthInPortrait(Resources r, int numCells) {
265 // We use this method from Workspace to figure out how many rows/columns Launcher should
266 // have. We ignore the left/right padding on CellLayout because it turns out in our design
267 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700268 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700269 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
270 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700271
Winson Chung4b825dcd2011-06-19 12:41:22 -0700272 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700273 }
274
Michael Jurkaf6440da2011-04-05 14:50:34 -0700275 static int heightInLandscape(Resources r, int numCells) {
276 // We use this method from Workspace to figure out how many rows/columns Launcher should
277 // have. We ignore the left/right padding on CellLayout because it turns out in our design
278 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700279 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700280 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
281 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700282
Winson Chung4b825dcd2011-06-19 12:41:22 -0700283 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700284 }
285
Adam Cohen2801caf2011-05-13 20:57:39 -0700286 public void enableHardwareLayers() {
Adam Cohen7ef91832011-08-25 14:06:02 -0700287 mChildren.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700288 }
289
290 public void setGridSize(int x, int y) {
291 mCountX = x;
292 mCountY = y;
293 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700294 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700295 }
296
Patrick Dubroy96864c32011-03-10 17:17:23 -0800297 private void invalidateBubbleTextView(BubbleTextView icon) {
298 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700299 invalidate(icon.getLeft() + getPaddingLeft() - padding,
300 icon.getTop() + getPaddingTop() - padding,
301 icon.getRight() + getPaddingLeft() + padding,
302 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800303 }
304
Adam Cohenb5ba0972011-09-07 18:02:31 -0700305 void setOverScrollAmount(float r, boolean left) {
306 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
307 mOverScrollForegroundDrawable = mOverScrollLeft;
308 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
309 mOverScrollForegroundDrawable = mOverScrollRight;
310 }
311
312 mForegroundAlpha = (int) Math.round((r * 255));
313 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
314 invalidate();
315 }
316
Patrick Dubroy96864c32011-03-10 17:17:23 -0800317 void setPressedOrFocusedIcon(BubbleTextView icon) {
318 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
319 // requires an expanded clip rect (due to the glow's blur radius)
320 BubbleTextView oldIcon = mPressedOrFocusedIcon;
321 mPressedOrFocusedIcon = icon;
322 if (oldIcon != null) {
323 invalidateBubbleTextView(oldIcon);
324 }
325 if (mPressedOrFocusedIcon != null) {
326 invalidateBubbleTextView(mPressedOrFocusedIcon);
327 }
328 }
329
Winson Chung6e314082011-01-27 16:46:51 -0800330 public CellLayoutChildren getChildrenLayout() {
331 if (getChildCount() > 0) {
332 return (CellLayoutChildren) getChildAt(0);
333 }
334 return null;
335 }
336
Michael Jurka33945b22010-12-21 18:19:38 -0800337 void setIsDragOverlapping(boolean isDragOverlapping) {
338 if (mIsDragOverlapping != isDragOverlapping) {
339 mIsDragOverlapping = isDragOverlapping;
340 invalidate();
341 }
342 }
343
344 boolean getIsDragOverlapping() {
345 return mIsDragOverlapping;
346 }
347
Adam Cohenebea84d2011-11-09 17:20:41 -0800348 protected void setOverscrollTransformsDirty(boolean dirty) {
349 mScrollingTransformsDirty = dirty;
350 }
351
352 protected void resetOverscrollTransforms() {
353 if (mScrollingTransformsDirty) {
354 setOverscrollTransformsDirty(false);
355 setTranslationX(0);
356 setRotationY(0);
357 // It doesn't matter if we pass true or false here, the important thing is that we
358 // pass 0, which results in the overscroll drawable not being drawn any more.
359 setOverScrollAmount(0, false);
360 setPivotX(getMeasuredWidth() / 2);
361 setPivotY(getMeasuredHeight() / 2);
362 }
363 }
364
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700365 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700366 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700367 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
368 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
369 // When we're small, we are either drawn normally or in the "accepts drops" state (during
370 // a drag). However, we also drag the mini hover background *over* one of those two
371 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700372 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700373 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800374
375 if (mIsDragOverlapping) {
376 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700377 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700378 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700379 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700380 }
Michael Jurka33945b22010-12-21 18:19:38 -0800381
382 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
383 bg.setBounds(mBackgroundRect);
384 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700385 }
Romain Guya6abce82009-11-10 02:54:41 -0800386
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700387 if (mCrosshairsVisibility > 0.0f) {
388 final int countX = mCountX;
389 final int countY = mCountY;
390
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700391 final float MAX_ALPHA = 0.4f;
392 final int MAX_VISIBLE_DISTANCE = 600;
393 final float DISTANCE_MULTIPLIER = 0.002f;
394
395 final Drawable d = mCrosshairsDrawable;
396 final int width = d.getIntrinsicWidth();
397 final int height = d.getIntrinsicHeight();
398
Winson Chung4b825dcd2011-06-19 12:41:22 -0700399 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700400 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700401 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700402 for (int row = 0; row <= countY; row++) {
403 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
404 float dist = mTmpPointF.length();
405 // Crosshairs further from the drag point are more faint
406 float alpha = Math.min(MAX_ALPHA,
407 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
408 if (alpha > 0.0f) {
409 d.setBounds(x, y, x + width, y + height);
410 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
411 d.draw(canvas);
412 }
413 y += mCellHeight + mHeightGap;
414 }
415 x += mCellWidth + mWidthGap;
416 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700417 }
Winson Chung150fbab2010-09-29 17:14:26 -0700418
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700419 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700420 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700421 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700422 if (alpha > 0) {
423 final Point p = mDragOutlines[i];
424 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700425 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700426 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700427 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700428 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800429
430 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
431 // requires an expanded clip rect (due to the glow's blur radius)
432 if (mPressedOrFocusedIcon != null) {
433 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
434 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
435 if (b != null) {
436 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700437 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
438 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800439 null);
440 }
441 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700442
443 // The folder outer / inner ring image(s)
444 for (int i = 0; i < mFolderOuterRings.size(); i++) {
445 FolderRingAnimator fra = mFolderOuterRings.get(i);
446
447 // Draw outer ring
448 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
449 int width = (int) fra.getOuterRingSize();
450 int height = width;
451 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
452
453 int centerX = mTempLocation[0] + mCellWidth / 2;
454 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
455
456 canvas.save();
457 canvas.translate(centerX - width / 2, centerY - height / 2);
458 d.setBounds(0, 0, width, height);
459 d.draw(canvas);
460 canvas.restore();
461
462 // Draw inner ring
463 d = FolderRingAnimator.sSharedInnerRingDrawable;
464 width = (int) fra.getInnerRingSize();
465 height = width;
466 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
467
468 centerX = mTempLocation[0] + mCellWidth / 2;
469 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
470 canvas.save();
471 canvas.translate(centerX - width / 2, centerY - width / 2);
472 d.setBounds(0, 0, width, height);
473 d.draw(canvas);
474 canvas.restore();
475 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700476
477 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
478 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
479 int width = d.getIntrinsicWidth();
480 int height = d.getIntrinsicHeight();
481
482 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
483 int centerX = mTempLocation[0] + mCellWidth / 2;
484 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
485
486 canvas.save();
487 canvas.translate(centerX - width / 2, centerY - width / 2);
488 d.setBounds(0, 0, width, height);
489 d.draw(canvas);
490 canvas.restore();
491 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700492 }
493
Adam Cohenb5ba0972011-09-07 18:02:31 -0700494 @Override
495 protected void dispatchDraw(Canvas canvas) {
496 super.dispatchDraw(canvas);
497 if (mForegroundAlpha > 0) {
498 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
499 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
500 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
501 mOverScrollForegroundDrawable.draw(canvas);
502 p.setXfermode(null);
503 }
504 }
505
Adam Cohen69ce2e52011-07-03 19:25:21 -0700506 public void showFolderAccept(FolderRingAnimator fra) {
507 mFolderOuterRings.add(fra);
508 }
509
510 public void hideFolderAccept(FolderRingAnimator fra) {
511 if (mFolderOuterRings.contains(fra)) {
512 mFolderOuterRings.remove(fra);
513 }
514 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700515 }
516
Adam Cohenc51934b2011-07-26 21:07:43 -0700517 public void setFolderLeaveBehindCell(int x, int y) {
518 mFolderLeaveBehindCell[0] = x;
519 mFolderLeaveBehindCell[1] = y;
520 invalidate();
521 }
522
523 public void clearFolderLeaveBehind() {
524 mFolderLeaveBehindCell[0] = -1;
525 mFolderLeaveBehindCell[1] = -1;
526 invalidate();
527 }
528
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700529 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700530 public boolean shouldDelayChildPressedState() {
531 return false;
532 }
533
534 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700535 public void cancelLongPress() {
536 super.cancelLongPress();
537
538 // Cancel long press for all children
539 final int count = getChildCount();
540 for (int i = 0; i < count; i++) {
541 final View child = getChildAt(i);
542 child.cancelLongPress();
543 }
544 }
545
Michael Jurkadee05892010-07-27 10:01:56 -0700546 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
547 mInterceptTouchListener = listener;
548 }
549
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700551 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 }
553
554 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700555 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 }
557
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700558 public boolean addViewToCellLayout(
559 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700560 final LayoutParams lp = params;
561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 // Generate an id for each view, this assumes we have at most 256x256 cells
563 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700564 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700565 // If the horizontal or vertical span is set to -1, it is taken to
566 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700567 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
568 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569
Winson Chungaafa03c2010-06-11 17:34:16 -0700570 child.setId(childId);
571
Michael Jurka8c920dd2011-01-20 14:16:56 -0800572 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700573
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700574 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700575
Winson Chungaafa03c2010-06-11 17:34:16 -0700576 return true;
577 }
578 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700582 public void removeAllViews() {
583 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800584 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700585 }
586
587 @Override
588 public void removeAllViewsInLayout() {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700589 if (mChildren.getChildCount() > 0) {
590 clearOccupiedCells();
591 mChildren.removeAllViewsInLayout();
592 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700593 }
594
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700595 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800596 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700597 }
598
Michael Jurka0280c3b2010-09-17 15:00:07 -0700599 @Override
600 public void removeView(View view) {
601 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800602 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700603 }
604
605 @Override
606 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800607 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
608 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700609 }
610
611 @Override
612 public void removeViewInLayout(View view) {
613 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800614 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700615 }
616
617 @Override
618 public void removeViews(int start, int count) {
619 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800620 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700621 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800622 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700623 }
624
625 @Override
626 public void removeViewsInLayout(int start, int count) {
627 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800628 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700629 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800630 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700631 }
632
Michael Jurka8c920dd2011-01-20 14:16:56 -0800633 public void drawChildren(Canvas canvas) {
634 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 }
636
Michael Jurkaabded662011-03-04 12:06:57 -0800637 void buildChildrenLayer() {
638 mChildren.buildLayer();
639 }
640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 @Override
642 protected void onAttachedToWindow() {
643 super.onAttachedToWindow();
644 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
645 }
646
Michael Jurkaaf442092010-06-10 17:01:57 -0700647 public void setTagToCellInfoForPoint(int touchX, int touchY) {
648 final CellInfo cellInfo = mCellInfo;
649 final Rect frame = mRect;
650 final int x = touchX + mScrollX;
651 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800652 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700653
654 boolean found = false;
655 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800656 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800657 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700658
Adam Cohen1b607ed2011-03-03 17:26:50 -0800659 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
660 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700661 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700662
663 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
664 // offset that by this CellLayout's padding to test an (x,y) point that is relative
665 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700666 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700667
Michael Jurkaaf442092010-06-10 17:01:57 -0700668 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700669 cellInfo.cell = child;
670 cellInfo.cellX = lp.cellX;
671 cellInfo.cellY = lp.cellY;
672 cellInfo.spanX = lp.cellHSpan;
673 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700674 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700675 break;
676 }
677 }
678 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700679
Michael Jurkad771c962011-08-09 15:00:48 -0700680 mLastDownOnOccupiedCell = found;
681
Michael Jurkaaf442092010-06-10 17:01:57 -0700682 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700683 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700684 pointToCellExact(x, y, cellXY);
685
Michael Jurkaaf442092010-06-10 17:01:57 -0700686 cellInfo.cell = null;
687 cellInfo.cellX = cellXY[0];
688 cellInfo.cellY = cellXY[1];
689 cellInfo.spanX = 1;
690 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700691 }
692 setTag(cellInfo);
693 }
694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 @Override
696 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700697 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
698 // even in the case where we return early. Not clearing here was causing bugs whereby on
699 // long-press we'd end up picking up an item from a previous drag operation.
700 final int action = ev.getAction();
701
702 if (action == MotionEvent.ACTION_DOWN) {
703 clearTagCellInfo();
704 }
705
Michael Jurkadee05892010-07-27 10:01:56 -0700706 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
707 return true;
708 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709
710 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700711 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 return false;
714 }
715
Adam Cohenc1997fd2011-08-15 18:26:39 -0700716 private void clearTagCellInfo() {
717 final CellInfo cellInfo = mCellInfo;
718 cellInfo.cell = null;
719 cellInfo.cellX = -1;
720 cellInfo.cellY = -1;
721 cellInfo.spanX = 0;
722 cellInfo.spanY = 0;
723 setTag(cellInfo);
724 }
725
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700727 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 }
729
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700730 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700731 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 * @param x X coordinate of the point
733 * @param y Y coordinate of the point
734 * @param result Array of 2 ints to hold the x and y coordinate of the cell
735 */
736 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700737 final int hStartPadding = getPaddingLeft();
738 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739
740 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
741 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
742
Adam Cohend22015c2010-07-26 22:02:18 -0700743 final int xAxis = mCountX;
744 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745
746 if (result[0] < 0) result[0] = 0;
747 if (result[0] >= xAxis) result[0] = xAxis - 1;
748 if (result[1] < 0) result[1] = 0;
749 if (result[1] >= yAxis) result[1] = yAxis - 1;
750 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700751
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 /**
753 * Given a point, return the cell that most closely encloses that point
754 * @param x X coordinate of the point
755 * @param y Y coordinate of the point
756 * @param result Array of 2 ints to hold the x and y coordinate of the cell
757 */
758 void pointToCellRounded(int x, int y, int[] result) {
759 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
760 }
761
762 /**
763 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700764 *
765 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700767 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 * @param result Array of 2 ints to hold the x and y coordinate of the point
769 */
770 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700771 final int hStartPadding = getPaddingLeft();
772 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773
774 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
775 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
776 }
777
Adam Cohene3e27a82011-04-15 12:07:39 -0700778 /**
779 * Given a cell coordinate, return the point that represents the upper left corner of that cell
780 *
781 * @param cellX X coordinate of the cell
782 * @param cellY Y coordinate of the cell
783 *
784 * @param result Array of 2 ints to hold the x and y coordinate of the point
785 */
786 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700787 final int hStartPadding = getPaddingLeft();
788 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700789
790 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
791 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
792 }
793
Romain Guy84f296c2009-11-04 15:00:44 -0800794 int getCellWidth() {
795 return mCellWidth;
796 }
797
798 int getCellHeight() {
799 return mCellHeight;
800 }
801
Adam Cohend4844c32011-02-18 19:25:06 -0800802 int getWidthGap() {
803 return mWidthGap;
804 }
805
806 int getHeightGap() {
807 return mHeightGap;
808 }
809
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700810 Rect getContentRect(Rect r) {
811 if (r == null) {
812 r = new Rect();
813 }
814 int left = getPaddingLeft();
815 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700816 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
817 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700818 r.set(left, top, right, bottom);
819 return r;
820 }
821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 @Override
823 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
824 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700825
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700827 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
828
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
830 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700831
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
833 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
834 }
835
Adam Cohend22015c2010-07-26 22:02:18 -0700836 int numWidthGaps = mCountX - 1;
837 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838
Adam Cohen234c4cd2011-07-17 21:03:04 -0700839 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700840 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
841 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
842 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
843 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
844 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
845 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700846 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700847 } else {
848 mWidthGap = mOriginalWidthGap;
849 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700850 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700851
Michael Jurka8c920dd2011-01-20 14:16:56 -0800852 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
853 int newWidth = widthSpecSize;
854 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700855 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700856 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700857 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700858 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700859 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700860 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700861 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800862
863 int count = getChildCount();
864 for (int i = 0; i < count; i++) {
865 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700866 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
867 mPaddingRight, MeasureSpec.EXACTLY);
868 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
869 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800870 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
871 }
872 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 }
874
875 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700876 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800879 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700880 child.layout(mPaddingLeft, mPaddingTop,
881 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 }
883 }
884
885 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700886 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
887 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700888 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700889 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
890 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -0700891 }
892
893 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800895 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 }
897
898 @Override
899 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800900 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 }
902
Michael Jurka5f1c5092010-09-03 14:15:02 -0700903 public float getBackgroundAlpha() {
904 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700905 }
906
Michael Jurka742574b2011-02-02 23:51:01 -0800907 public void setFastBackgroundAlpha(float alpha) {
908 mBackgroundAlpha = alpha;
909 }
910
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700911 public void setBackgroundAlphaMultiplier(float multiplier) {
912 mBackgroundAlphaMultiplier = multiplier;
913 }
914
Adam Cohenddb82192010-11-10 16:32:54 -0800915 public float getBackgroundAlphaMultiplier() {
916 return mBackgroundAlphaMultiplier;
917 }
918
Michael Jurka5f1c5092010-09-03 14:15:02 -0700919 public void setBackgroundAlpha(float alpha) {
920 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700921 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700922 }
923
Michael Jurka5f1c5092010-09-03 14:15:02 -0700924 // Need to return true to let the view system know we know how to handle alpha-- this is
925 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
926 // versions
927 @Override
928 protected boolean onSetAlpha(int alpha) {
929 return true;
930 }
931
932 public void setAlpha(float alpha) {
933 setChildrenAlpha(alpha);
934 super.setAlpha(alpha);
935 }
936
Michael Jurka742574b2011-02-02 23:51:01 -0800937 public void setFastAlpha(float alpha) {
938 setFastChildrenAlpha(alpha);
939 super.setFastAlpha(alpha);
940 }
941
Michael Jurkadee05892010-07-27 10:01:56 -0700942 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700943 final int childCount = getChildCount();
944 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700945 getChildAt(i).setAlpha(alpha);
946 }
947 }
948
Michael Jurka742574b2011-02-02 23:51:01 -0800949 private void setFastChildrenAlpha(float alpha) {
950 final int childCount = getChildCount();
951 for (int i = 0; i < childCount; i++) {
952 getChildAt(i).setFastAlpha(alpha);
953 }
954 }
955
Patrick Dubroy440c3602010-07-13 17:50:32 -0700956 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800957 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700958 }
959
Adam Cohen76fc0852011-06-17 13:26:23 -0700960 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
961 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700962 CellLayoutChildren clc = getChildrenLayout();
963 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
964 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
965 final ItemInfo info = (ItemInfo) child.getTag();
966
967 // We cancel any existing animations
968 if (mReorderAnimators.containsKey(lp)) {
969 mReorderAnimators.get(lp).cancel();
970 mReorderAnimators.remove(lp);
971 }
972
973 int oldX = lp.x;
974 int oldY = lp.y;
975 mOccupied[lp.cellX][lp.cellY] = false;
976 mOccupied[cellX][cellY] = true;
977
978 lp.isLockedToGrid = true;
979 lp.cellX = info.cellX = cellX;
980 lp.cellY = info.cellY = cellY;
981 clc.setupLp(lp);
982 lp.isLockedToGrid = false;
983 int newX = lp.x;
984 int newY = lp.y;
985
Adam Cohen76fc0852011-06-17 13:26:23 -0700986 lp.x = oldX;
987 lp.y = oldY;
988 child.requestLayout();
989
Adam Cohenbfbfd262011-06-13 16:55:12 -0700990 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
991 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
992 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
993 oa.setDuration(duration);
994 mReorderAnimators.put(lp, oa);
995 oa.addUpdateListener(new AnimatorUpdateListener() {
996 public void onAnimationUpdate(ValueAnimator animation) {
997 child.requestLayout();
998 }
999 });
1000 oa.addListener(new AnimatorListenerAdapter() {
1001 boolean cancelled = false;
1002 public void onAnimationEnd(Animator animation) {
1003 // If the animation was cancelled, it means that another animation
1004 // has interrupted this one, and we don't want to lock the item into
1005 // place just yet.
1006 if (!cancelled) {
1007 lp.isLockedToGrid = true;
1008 }
1009 if (mReorderAnimators.containsKey(lp)) {
1010 mReorderAnimators.remove(lp);
1011 }
1012 }
1013 public void onAnimationCancel(Animator animation) {
1014 cancelled = true;
1015 }
1016 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001017 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001018 oa.start();
1019 return true;
1020 }
1021 return false;
1022 }
1023
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001024 /**
1025 * Estimate where the top left cell of the dragged item will land if it is dropped.
1026 *
1027 * @param originX The X value of the top left corner of the item
1028 * @param originY The Y value of the top left corner of the item
1029 * @param spanX The number of horizontal cells that the item spans
1030 * @param spanY The number of vertical cells that the item spans
1031 * @param result The estimated drop cell X and Y.
1032 */
1033 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001034 final int countX = mCountX;
1035 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001036
Michael Jurkaa63c4522010-08-19 13:52:27 -07001037 // pointToCellRounded takes the top left of a cell but will pad that with
1038 // cellWidth/2 and cellHeight/2 when finding the matching cell
1039 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001040
1041 // If the item isn't fully on this screen, snap to the edges
1042 int rightOverhang = result[0] + spanX - countX;
1043 if (rightOverhang > 0) {
1044 result[0] -= rightOverhang; // Snap to right
1045 }
1046 result[0] = Math.max(0, result[0]); // Snap to left
1047 int bottomOverhang = result[1] + spanY - countY;
1048 if (bottomOverhang > 0) {
1049 result[1] -= bottomOverhang; // Snap to bottom
1050 }
1051 result[1] = Math.max(0, result[1]); // Snap to top
1052 }
1053
Winson Chungb8c69f32011-10-19 21:36:08 -07001054 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY,
1055 int spanX, int spanY, Point dragOffset, Rect dragRegion) {
Joe Onorato4be866d2010-10-10 11:26:02 -07001056
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001057 final int oldDragCellX = mDragCell[0];
1058 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001059 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chungb8c69f32011-10-19 21:36:08 -07001060 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001061 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1062 } else {
1063 mDragCenter.set(originX, originY);
1064 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001065
Adam Cohen2801caf2011-05-13 20:57:39 -07001066 if (dragOutline == null && v == null) {
1067 if (mCrosshairsDrawable != null) {
1068 invalidate();
1069 }
1070 return;
1071 }
1072
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001073 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001074 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001075 final int[] topLeft = mTmpPoint;
1076 cellToPoint(nearest[0], nearest[1], topLeft);
1077
Joe Onorato4be866d2010-10-10 11:26:02 -07001078 int left = topLeft[0];
1079 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001080
Winson Chungb8c69f32011-10-19 21:36:08 -07001081 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001082 // When drawing the drag outline, it did not account for margin offsets
1083 // added by the view's parent.
1084 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1085 left += lp.leftMargin;
1086 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001087
Adam Cohen99e8b402011-03-25 19:23:43 -07001088 // Offsets due to the size difference between the View and the dragOutline.
1089 // There is a size difference to account for the outer blur, which may lie
1090 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001091 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001092 // We center about the x axis
1093 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1094 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001095 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001096 if (dragOffset != null && dragRegion != null) {
1097 // Center the drag region *horizontally* in the cell and apply a drag
1098 // outline offset
1099 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1100 - dragRegion.width()) / 2;
1101 top += dragOffset.y;
1102 } else {
1103 // Center the drag outline in the cell
1104 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1105 - dragOutline.getWidth()) / 2;
1106 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1107 - dragOutline.getHeight()) / 2;
1108 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001109 }
Winson Chung150fbab2010-09-29 17:14:26 -07001110
Joe Onorato4be866d2010-10-10 11:26:02 -07001111 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001112 mDragOutlineAnims[oldIndex].animateOut();
1113 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001114
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001115 mDragOutlines[mDragOutlineCurrent].set(left, top);
1116 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1117 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001118 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001119
1120 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1121 if (mCrosshairsDrawable != null) {
1122 invalidate();
1123 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001124 }
1125
Adam Cohene0310962011-04-18 16:15:31 -07001126 public void clearDragOutlines() {
1127 final int oldIndex = mDragOutlineCurrent;
1128 mDragOutlineAnims[oldIndex].animateOut();
1129 mDragCell[0] = -1;
1130 mDragCell[1] = -1;
1131 }
1132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001134 * Find a vacant area that will fit the given bounds nearest the requested
1135 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001136 *
Romain Guy51afc022009-05-04 18:03:43 -07001137 * @param pixelX The X location at which you want to search for a vacant area.
1138 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001139 * @param spanX Horizontal span of the object.
1140 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001141 * @param result Array in which to place the result, or null (in which case a new array will
1142 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001143 * @return The X, Y cell of a vacant area that can contain this object,
1144 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001146 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001147 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1148 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001149 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001150
Michael Jurka6a1435d2010-09-27 17:35:12 -07001151 /**
1152 * Find a vacant area that will fit the given bounds nearest the requested
1153 * cell location. Uses Euclidean distance to score multiple vacant areas.
1154 *
1155 * @param pixelX The X location at which you want to search for a vacant area.
1156 * @param pixelY The Y location at which you want to search for a vacant area.
1157 * @param spanX Horizontal span of the object.
1158 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001159 * @param ignoreOccupied If true, the result can be an occupied cell
1160 * @param result Array in which to place the result, or null (in which case a new array will
1161 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001162 * @return The X, Y cell of a vacant area that can contain this object,
1163 * nearest the requested location.
1164 */
Adam Cohendf035382011-04-11 17:22:04 -07001165 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1166 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001167 // mark space take by ignoreView as available (method checks if ignoreView is null)
1168 markCellsAsUnoccupiedForView(ignoreView);
1169
Adam Cohene3e27a82011-04-15 12:07:39 -07001170 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1171 // to the center of the item, but we are searching based on the top-left cell, so
1172 // we translate the point over to correspond to the top-left.
1173 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1174 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1175
Jeff Sharkey70864282009-04-07 21:08:40 -07001176 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001177 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001178 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001179
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001180 final int countX = mCountX;
1181 final int countY = mCountY;
1182 final boolean[][] occupied = mOccupied;
1183
Winson Chungbbc60d82010-11-11 16:34:41 -08001184 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001185 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001186 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001187 if (ignoreOccupied) {
1188 for (int i = 0; i < spanX; i++) {
1189 for (int j = 0; j < spanY; j++) {
1190 if (occupied[x + i][y + j]) {
1191 // small optimization: we can skip to after the column we
1192 // just found an occupied cell
1193 x += i;
1194 continue inner;
1195 }
Michael Jurkac28de512010-08-13 11:27:44 -07001196 }
1197 }
1198 }
Winson Chung0be025d2011-05-23 17:45:09 -07001199 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001200 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201
Michael Jurkac28de512010-08-13 11:27:44 -07001202 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1203 + Math.pow(cellXY[1] - pixelY, 2));
1204 if (distance <= bestDistance) {
1205 bestDistance = distance;
1206 bestXY[0] = x;
1207 bestXY[1] = y;
1208 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
1210 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001211 // re-mark space taken by ignoreView as occupied
1212 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213
Adam Cohenc0dcf592011-06-01 15:30:43 -07001214 // Return -1, -1 if no suitable location found
1215 if (bestDistance == Double.MAX_VALUE) {
1216 bestXY[0] = -1;
1217 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001218 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001219 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001221
Adam Cohendf035382011-04-11 17:22:04 -07001222 /**
1223 * Find a vacant area that will fit the given bounds nearest the requested
1224 * cell location. Uses Euclidean distance to score multiple vacant areas.
1225 *
1226 * @param pixelX The X location at which you want to search for a vacant area.
1227 * @param pixelY The Y location at which you want to search for a vacant area.
1228 * @param spanX Horizontal span of the object.
1229 * @param spanY Vertical span of the object.
1230 * @param ignoreView Considers space occupied by this view as unoccupied
1231 * @param result Previously returned value to possibly recycle.
1232 * @return The X, Y cell of a vacant area that can contain this object,
1233 * nearest the requested location.
1234 */
1235 int[] findNearestVacantArea(
1236 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1237 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1238 }
1239
1240 /**
1241 * Find a starting cell position that will fit the given bounds nearest the requested
1242 * cell location. Uses Euclidean distance to score multiple vacant areas.
1243 *
1244 * @param pixelX The X location at which you want to search for a vacant area.
1245 * @param pixelY The Y location at which you want to search for a vacant area.
1246 * @param spanX Horizontal span of the object.
1247 * @param spanY Vertical span of the object.
1248 * @param ignoreView Considers space occupied by this view as unoccupied
1249 * @param result Previously returned value to possibly recycle.
1250 * @return The X, Y cell of a vacant area that can contain this object,
1251 * nearest the requested location.
1252 */
1253 int[] findNearestArea(
1254 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1255 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1256 }
1257
Michael Jurka0280c3b2010-09-17 15:00:07 -07001258 boolean existsEmptyCell() {
1259 return findCellForSpan(null, 1, 1);
1260 }
1261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001263 * Finds the upper-left coordinate of the first rectangle in the grid that can
1264 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1265 * then this method will only return coordinates for rectangles that contain the cell
1266 * (intersectX, intersectY)
1267 *
1268 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1269 * can be found.
1270 * @param spanX The horizontal span of the cell we want to find.
1271 * @param spanY The vertical span of the cell we want to find.
1272 *
1273 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001274 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001275 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1276 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1277 }
1278
1279 /**
1280 * Like above, but ignores any cells occupied by the item "ignoreView"
1281 *
1282 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1283 * can be found.
1284 * @param spanX The horizontal span of the cell we want to find.
1285 * @param spanY The vertical span of the cell we want to find.
1286 * @param ignoreView The home screen item we should treat as not occupying any space
1287 * @return
1288 */
1289 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1290 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1291 }
1292
1293 /**
1294 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1295 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1296 *
1297 * @param spanX The horizontal span of the cell we want to find.
1298 * @param spanY The vertical span of the cell we want to find.
1299 * @param ignoreView The home screen item we should treat as not occupying any space
1300 * @param intersectX The X coordinate of the cell that we should try to overlap
1301 * @param intersectX The Y coordinate of the cell that we should try to overlap
1302 *
1303 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1304 */
1305 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1306 int intersectX, int intersectY) {
1307 return findCellForSpanThatIntersectsIgnoring(
1308 cellXY, spanX, spanY, intersectX, intersectY, null);
1309 }
1310
1311 /**
1312 * The superset of the above two methods
1313 */
1314 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1315 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001316 // mark space take by ignoreView as available (method checks if ignoreView is null)
1317 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001318
Michael Jurka28750fb2010-09-24 17:43:49 -07001319 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001320 while (true) {
1321 int startX = 0;
1322 if (intersectX >= 0) {
1323 startX = Math.max(startX, intersectX - (spanX - 1));
1324 }
1325 int endX = mCountX - (spanX - 1);
1326 if (intersectX >= 0) {
1327 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1328 }
1329 int startY = 0;
1330 if (intersectY >= 0) {
1331 startY = Math.max(startY, intersectY - (spanY - 1));
1332 }
1333 int endY = mCountY - (spanY - 1);
1334 if (intersectY >= 0) {
1335 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1336 }
1337
Winson Chungbbc60d82010-11-11 16:34:41 -08001338 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001339 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001340 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001341 for (int i = 0; i < spanX; i++) {
1342 for (int j = 0; j < spanY; j++) {
1343 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001344 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001345 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001346 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001347 continue inner;
1348 }
1349 }
1350 }
1351 if (cellXY != null) {
1352 cellXY[0] = x;
1353 cellXY[1] = y;
1354 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001355 foundCell = true;
1356 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001357 }
1358 }
1359 if (intersectX == -1 && intersectY == -1) {
1360 break;
1361 } else {
1362 // if we failed to find anything, try again but without any requirements of
1363 // intersecting
1364 intersectX = -1;
1365 intersectY = -1;
1366 continue;
1367 }
1368 }
1369
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001370 // re-mark space taken by ignoreView as occupied
1371 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001372 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001373 }
1374
1375 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001376 * A drag event has begun over this layout.
1377 * It may have begun over this layout (in which case onDragChild is called first),
1378 * or it may have begun on another layout.
1379 */
1380 void onDragEnter() {
1381 if (!mDragging) {
1382 // Fade in the drag indicators
1383 if (mCrosshairsAnimator != null) {
1384 mCrosshairsAnimator.animateIn();
1385 }
1386 }
1387 mDragging = true;
1388 }
1389
1390 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001391 * Called when drag has left this CellLayout or has been completed (successfully or not)
1392 */
1393 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001394 // This can actually be called when we aren't in a drag, e.g. when adding a new
1395 // item to this layout via the customize drawer.
1396 // Guard against that case.
1397 if (mDragging) {
1398 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001399
Joe Onorato4be866d2010-10-10 11:26:02 -07001400 // Fade out the drag indicators
1401 if (mCrosshairsAnimator != null) {
1402 mCrosshairsAnimator.animateOut();
1403 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001404 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001405
1406 // Invalidate the drag data
1407 mDragCell[0] = -1;
1408 mDragCell[1] = -1;
1409 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1410 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1411
Michael Jurka33945b22010-12-21 18:19:38 -08001412 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001413 }
1414
1415 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001416 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001417 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001418 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 *
1420 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001422 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001423 if (child != null) {
1424 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001425 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001426 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
1429
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001432 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 * @param cellX X coordinate of upper left corner expressed as a cell position
1434 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001435 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001437 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001439 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 final int cellWidth = mCellWidth;
1441 final int cellHeight = mCellHeight;
1442 final int widthGap = mWidthGap;
1443 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001444
Winson Chung4b825dcd2011-06-19 12:41:22 -07001445 final int hStartPadding = getPaddingLeft();
1446 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1449 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1450
1451 int x = hStartPadding + cellX * (cellWidth + widthGap);
1452 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001453
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001454 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001458 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001460 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 * @param width Width in pixels
1462 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001463 * @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 -08001464 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001465 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001466 return rectToCell(getResources(), width, height, result);
1467 }
1468
1469 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 // Always assume we're working with the smallest span to make sure we
1471 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001472 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1473 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001475
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07001477 int spanX = (int) Math.ceil(width / (float) smallerSize);
1478 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04001479
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001480 if (result == null) {
1481 return new int[] { spanX, spanY };
1482 }
1483 result[0] = spanX;
1484 result[1] = spanY;
1485 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 }
1487
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001488 public int[] cellSpansToSize(int hSpans, int vSpans) {
1489 int[] size = new int[2];
1490 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1491 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1492 return size;
1493 }
1494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001496 * Calculate the grid spans needed to fit given item
1497 */
1498 public void calculateSpans(ItemInfo info) {
1499 final int minWidth;
1500 final int minHeight;
1501
1502 if (info instanceof LauncherAppWidgetInfo) {
1503 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1504 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1505 } else if (info instanceof PendingAddWidgetInfo) {
1506 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1507 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1508 } else {
1509 // It's not a widget, so it must be 1x1
1510 info.spanX = info.spanY = 1;
1511 return;
1512 }
1513 int[] spans = rectToCell(minWidth, minHeight, null);
1514 info.spanX = spans[0];
1515 info.spanY = spans[1];
1516 }
1517
1518 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 * Find the first vacant cell, if there is one.
1520 *
1521 * @param vacant Holds the x and y coordinate of the vacant cell
1522 * @param spanX Horizontal cell span.
1523 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001524 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 * @return True if a vacant cell was found
1526 */
1527 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528
Michael Jurka0280c3b2010-09-17 15:00:07 -07001529 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
1531
1532 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1533 int xCount, int yCount, boolean[][] occupied) {
1534
Adam Cohen2801caf2011-05-13 20:57:39 -07001535 for (int y = 0; y < yCount; y++) {
1536 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 boolean available = !occupied[x][y];
1538out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1539 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1540 available = available && !occupied[i][j];
1541 if (!available) break out;
1542 }
1543 }
1544
1545 if (available) {
1546 vacant[0] = x;
1547 vacant[1] = y;
1548 return true;
1549 }
1550 }
1551 }
1552
1553 return false;
1554 }
1555
Michael Jurka0280c3b2010-09-17 15:00:07 -07001556 private void clearOccupiedCells() {
1557 for (int x = 0; x < mCountX; x++) {
1558 for (int y = 0; y < mCountY; y++) {
1559 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 }
1561 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563
Adam Cohen1b607ed2011-03-03 17:26:50 -08001564 /**
1565 * Given a view, determines how much that view can be expanded in all directions, in terms of
1566 * whether or not there are other items occupying adjacent cells. Used by the
1567 * AppWidgetResizeFrame to determine how the widget can be resized.
1568 */
Adam Cohend4844c32011-02-18 19:25:06 -08001569 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001570 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001571 boolean flag;
1572
Adam Cohen1b607ed2011-03-03 17:26:50 -08001573 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001574 for (int x = lp.cellX - 1; x >= 0; x--) {
1575 flag = false;
1576 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1577 if (mOccupied[x][y]) flag = true;
1578 }
1579 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001580 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001581 }
1582
Adam Cohen1b607ed2011-03-03 17:26:50 -08001583 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001584 for (int y = lp.cellY - 1; y >= 0; y--) {
1585 flag = false;
1586 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1587 if (mOccupied[x][y]) flag = true;
1588 }
1589 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001590 expandability[AppWidgetResizeFrame.TOP]++;
1591 }
Adam Cohend4844c32011-02-18 19:25:06 -08001592
Adam Cohen1b607ed2011-03-03 17:26:50 -08001593 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001594 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1595 flag = false;
1596 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1597 if (mOccupied[x][y]) flag = true;
1598 }
1599 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001600 expandability[AppWidgetResizeFrame.RIGHT]++;
1601 }
Adam Cohend4844c32011-02-18 19:25:06 -08001602
Adam Cohen1b607ed2011-03-03 17:26:50 -08001603 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001604 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1605 flag = false;
1606 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1607 if (mOccupied[x][y]) flag = true;
1608 }
1609 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001610 expandability[AppWidgetResizeFrame.BOTTOM]++;
1611 }
Adam Cohend4844c32011-02-18 19:25:06 -08001612 }
1613
Michael Jurka0280c3b2010-09-17 15:00:07 -07001614 public void onMove(View view, int newCellX, int newCellY) {
1615 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1616 markCellsAsUnoccupiedForView(view);
1617 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1618 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619
Adam Cohend4844c32011-02-18 19:25:06 -08001620 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001621 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001622 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1623 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1624 }
1625
Adam Cohend4844c32011-02-18 19:25:06 -08001626 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001627 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001628 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1629 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1630 }
1631
1632 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1633 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1634 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1635 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 }
1637 }
1638 }
1639
Adam Cohen2801caf2011-05-13 20:57:39 -07001640 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001641 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001642 (Math.max((mCountX - 1), 0) * mWidthGap);
1643 }
1644
1645 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001646 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001647 (Math.max((mCountY - 1), 0) * mHeightGap);
1648 }
1649
Michael Jurka66d72172011-04-12 16:29:25 -07001650 public boolean isOccupied(int x, int y) {
1651 if (x < mCountX && y < mCountY) {
1652 return mOccupied[x][y];
1653 } else {
1654 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1655 }
1656 }
1657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 @Override
1659 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1660 return new CellLayout.LayoutParams(getContext(), attrs);
1661 }
1662
1663 @Override
1664 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1665 return p instanceof CellLayout.LayoutParams;
1666 }
1667
1668 @Override
1669 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1670 return new CellLayout.LayoutParams(p);
1671 }
1672
Winson Chungaafa03c2010-06-11 17:34:16 -07001673 public static class CellLayoutAnimationController extends LayoutAnimationController {
1674 public CellLayoutAnimationController(Animation animation, float delay) {
1675 super(animation, delay);
1676 }
1677
1678 @Override
1679 protected long getDelayForView(View view) {
1680 return (int) (Math.random() * 150);
1681 }
1682 }
1683
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1685 /**
1686 * Horizontal location of the item in the grid.
1687 */
1688 @ViewDebug.ExportedProperty
1689 public int cellX;
1690
1691 /**
1692 * Vertical location of the item in the grid.
1693 */
1694 @ViewDebug.ExportedProperty
1695 public int cellY;
1696
1697 /**
1698 * Number of cells spanned horizontally by the item.
1699 */
1700 @ViewDebug.ExportedProperty
1701 public int cellHSpan;
1702
1703 /**
1704 * Number of cells spanned vertically by the item.
1705 */
1706 @ViewDebug.ExportedProperty
1707 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001708
Adam Cohen1b607ed2011-03-03 17:26:50 -08001709 /**
1710 * Indicates whether the item will set its x, y, width and height parameters freely,
1711 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1712 */
Adam Cohend4844c32011-02-18 19:25:06 -08001713 public boolean isLockedToGrid = true;
1714
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 // X coordinate of the view in the layout.
1716 @ViewDebug.ExportedProperty
1717 int x;
1718 // Y coordinate of the view in the layout.
1719 @ViewDebug.ExportedProperty
1720 int y;
1721
Romain Guy84f296c2009-11-04 15:00:44 -08001722 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001723
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 public LayoutParams(Context c, AttributeSet attrs) {
1725 super(c, attrs);
1726 cellHSpan = 1;
1727 cellVSpan = 1;
1728 }
1729
1730 public LayoutParams(ViewGroup.LayoutParams source) {
1731 super(source);
1732 cellHSpan = 1;
1733 cellVSpan = 1;
1734 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001735
1736 public LayoutParams(LayoutParams source) {
1737 super(source);
1738 this.cellX = source.cellX;
1739 this.cellY = source.cellY;
1740 this.cellHSpan = source.cellHSpan;
1741 this.cellVSpan = source.cellVSpan;
1742 }
1743
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001744 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001745 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 this.cellX = cellX;
1747 this.cellY = cellY;
1748 this.cellHSpan = cellHSpan;
1749 this.cellVSpan = cellVSpan;
1750 }
1751
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001752 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001753 if (isLockedToGrid) {
1754 final int myCellHSpan = cellHSpan;
1755 final int myCellVSpan = cellVSpan;
1756 final int myCellX = cellX;
1757 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001758
Adam Cohend4844c32011-02-18 19:25:06 -08001759 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1760 leftMargin - rightMargin;
1761 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1762 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001763 x = myCellX * (cellWidth + widthGap) + leftMargin;
1764 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001765 }
1766 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001767
Winson Chungaafa03c2010-06-11 17:34:16 -07001768 public String toString() {
1769 return "(" + this.cellX + ", " + this.cellY + ")";
1770 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001771
1772 public void setWidth(int width) {
1773 this.width = width;
1774 }
1775
1776 public int getWidth() {
1777 return width;
1778 }
1779
1780 public void setHeight(int height) {
1781 this.height = height;
1782 }
1783
1784 public int getHeight() {
1785 return height;
1786 }
1787
1788 public void setX(int x) {
1789 this.x = x;
1790 }
1791
1792 public int getX() {
1793 return x;
1794 }
1795
1796 public void setY(int y) {
1797 this.y = y;
1798 }
1799
1800 public int getY() {
1801 return y;
1802 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 }
1804
Michael Jurka0280c3b2010-09-17 15:00:07 -07001805 // This class stores info for two purposes:
1806 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1807 // its spanX, spanY, and the screen it is on
1808 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1809 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1810 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001811 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001813 int cellX = -1;
1814 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 int spanX;
1816 int spanY;
1817 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001818 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 @Override
1821 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001822 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1823 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 }
1825 }
Michael Jurkad771c962011-08-09 15:00:48 -07001826
1827 public boolean lastDownOnOccupiedCell() {
1828 return mLastDownOnOccupiedCell;
1829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830}