blob: 37328ef2e8edcba1da5284254cd8a3c8ce4da3f6 [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.ObjectAnimator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070022import android.animation.PropertyValuesHolder;
Chet Haase00397b12010-10-07 11:13:10 -070023import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070024import android.animation.ValueAnimator;
25import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040027import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070029import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070032import android.graphics.Point;
33import android.graphics.PointF;
Adam Cohenb5ba0972011-09-07 18:02:31 -070034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.graphics.Rect;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070037import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070038import android.graphics.drawable.NinePatchDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.MotionEvent;
42import android.view.View;
43import android.view.ViewDebug;
44import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070046import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Adam Cohen66396872011-04-15 17:50:36 -070049import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070050import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070051
Adam Cohen69ce2e52011-07-03 19:25:21 -070052import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070054import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080055import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070056
Michael Jurkabdb5c532011-02-01 15:05:06 -080057public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070058 static final String TAG = "CellLayout";
59
Winson Chung4b825dcd2011-06-19 12:41:22 -070060 private int mOriginalCellWidth;
61 private int mOriginalCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062 private int mCellWidth;
63 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070064
Adam Cohend22015c2010-07-26 22:02:18 -070065 private int mCountX;
66 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
Adam Cohen234c4cd2011-07-17 21:03:04 -070068 private int mOriginalWidthGap;
69 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 private int mWidthGap;
71 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070072 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080073 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
75 private final Rect mRect = new Rect();
76 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070077
Patrick Dubroyde7658b2010-09-27 11:15:43 -070078 // These are temporary variables to prevent having to allocate a new object just to
79 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070080 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070081 private final int[] mTmpPoint = new int[2];
82 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070083 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070084
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 boolean[][] mOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070086 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087
Michael Jurkadee05892010-07-27 10:01:56 -070088 private OnTouchListener mInterceptTouchListener;
89
Adam Cohen69ce2e52011-07-03 19:25:21 -070090 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070091 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070092
Adam Cohenb5ba0972011-09-07 18:02:31 -070093 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070094 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070095 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070096
Michael Jurka33945b22010-12-21 18:19:38 -080097 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080098 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -070099 private Drawable mOverScrollForegroundDrawable;
100 private Drawable mOverScrollLeft;
101 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700102 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700103 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700104 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700105
Michael Jurka33945b22010-12-21 18:19:38 -0800106 // If we're actively dragging something over this screen, mIsDragOverlapping is true
107 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700108 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700109
Winson Chung150fbab2010-09-29 17:14:26 -0700110 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700111 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800112 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700113 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700114 private InterruptibleInOutAnimator[] mDragOutlineAnims =
115 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700116
117 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700118 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700119 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700120
Patrick Dubroy96864c32011-03-10 17:17:23 -0800121 private BubbleTextView mPressedOrFocusedIcon;
122
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700123 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700124 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700125 private float mCrosshairsVisibility = 0.0f;
126
Adam Cohenbfbfd262011-06-13 16:55:12 -0700127 private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new
128 HashMap<CellLayout.LayoutParams, ObjectAnimator>();
129
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700130 // When a drag operation is in progress, holds the nearest cell to the touch point
131 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Joe Onorato4be866d2010-10-10 11:26:02 -0700133 private boolean mDragging = false;
134
Patrick Dubroyce34a972010-10-19 10:34:32 -0700135 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800136 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700137
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 public CellLayout(Context context) {
139 this(context, null);
140 }
141
142 public CellLayout(Context context, AttributeSet attrs) {
143 this(context, attrs, 0);
144 }
145
146 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
147 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700148
149 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
150 // the user where a dragged item will land when dropped.
151 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
154
Winson Chung4b825dcd2011-06-19 12:41:22 -0700155 mOriginalCellWidth =
156 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
157 mOriginalCellHeight =
158 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700159 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
160 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700161 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700162 mCountX = LauncherModel.getCellCountX();
163 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700164 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
166 a.recycle();
167
168 setAlwaysDrawnWithCacheEnabled(false);
169
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700170 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700171
Winson Chung967289b2011-06-30 18:09:30 -0700172 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700173 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800174
Adam Cohenb5ba0972011-09-07 18:02:31 -0700175 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
176 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
177 mForegroundPadding =
178 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800179
Winson Chungb26f3d62011-06-02 10:49:29 -0700180 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700181 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700182
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700183 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700184
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700185 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700186 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700187
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700188 // Set up the animation for fading the crosshairs in and out
189 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700190 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700191 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700192 public void onAnimationUpdate(ValueAnimator animation) {
193 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700194 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700195 }
196 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700197 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700198
Winson Chungb8c69f32011-10-19 21:36:08 -0700199 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700200 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800201 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700202 }
203
204 // When dragging things around the home screens, we show a green outline of
205 // where the item will land. The outlines gradually fade out, leaving a trail
206 // behind the drag path.
207 // Set up all the animations that are used to implement this fading.
208 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700209 final float fromAlphaValue = 0;
210 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700211
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700212 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700213
214 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700215 final InterruptibleInOutAnimator anim =
216 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700217 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700218 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700219 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700220 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700221 final Bitmap outline = (Bitmap)anim.getTag();
222
223 // If an animation is started and then stopped very quickly, we can still
224 // get spurious updates we've cleared the tag. Guard against this.
225 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700226 if (false) {
227 Object val = animation.getAnimatedValue();
228 Log.d(TAG, "anim " + thisIndex + " update: " + val +
229 ", isStopped " + anim.isStopped());
230 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700231 // Try to prevent it from continuing to run
232 animation.cancel();
233 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700234 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800235 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700236 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700237 }
238 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700239 // The animation holds a reference to the drag outline bitmap as long is it's
240 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700241 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700242 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700243 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700244 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700245 anim.setTag(null);
246 }
247 }
248 });
249 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700250 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700251
Michael Jurka18014792010-10-14 09:01:34 -0700252 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700253 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800254
Michael Jurka8c920dd2011-01-20 14:16:56 -0800255 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700256 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800257 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700258 }
259
Michael Jurkaf6440da2011-04-05 14:50:34 -0700260 static int widthInPortrait(Resources r, int numCells) {
261 // We use this method from Workspace to figure out how many rows/columns Launcher should
262 // have. We ignore the left/right padding on CellLayout because it turns out in our design
263 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700264 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700265 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
266 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700267
Winson Chung4b825dcd2011-06-19 12:41:22 -0700268 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700269 }
270
Michael Jurkaf6440da2011-04-05 14:50:34 -0700271 static int heightInLandscape(Resources r, int numCells) {
272 // We use this method from Workspace to figure out how many rows/columns Launcher should
273 // have. We ignore the left/right padding on CellLayout because it turns out in our design
274 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700275 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700276 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
277 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700278
Winson Chung4b825dcd2011-06-19 12:41:22 -0700279 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700280 }
281
Adam Cohen2801caf2011-05-13 20:57:39 -0700282 public void enableHardwareLayers() {
Adam Cohen7ef91832011-08-25 14:06:02 -0700283 mChildren.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700284 }
285
286 public void setGridSize(int x, int y) {
287 mCountX = x;
288 mCountY = y;
289 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700290 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700291 }
292
Patrick Dubroy96864c32011-03-10 17:17:23 -0800293 private void invalidateBubbleTextView(BubbleTextView icon) {
294 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700295 invalidate(icon.getLeft() + getPaddingLeft() - padding,
296 icon.getTop() + getPaddingTop() - padding,
297 icon.getRight() + getPaddingLeft() + padding,
298 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800299 }
300
Adam Cohenb5ba0972011-09-07 18:02:31 -0700301 void setOverScrollAmount(float r, boolean left) {
302 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
303 mOverScrollForegroundDrawable = mOverScrollLeft;
304 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
305 mOverScrollForegroundDrawable = mOverScrollRight;
306 }
307
308 mForegroundAlpha = (int) Math.round((r * 255));
309 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
310 invalidate();
311 }
312
Patrick Dubroy96864c32011-03-10 17:17:23 -0800313 void setPressedOrFocusedIcon(BubbleTextView icon) {
314 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
315 // requires an expanded clip rect (due to the glow's blur radius)
316 BubbleTextView oldIcon = mPressedOrFocusedIcon;
317 mPressedOrFocusedIcon = icon;
318 if (oldIcon != null) {
319 invalidateBubbleTextView(oldIcon);
320 }
321 if (mPressedOrFocusedIcon != null) {
322 invalidateBubbleTextView(mPressedOrFocusedIcon);
323 }
324 }
325
Winson Chung6e314082011-01-27 16:46:51 -0800326 public CellLayoutChildren getChildrenLayout() {
327 if (getChildCount() > 0) {
328 return (CellLayoutChildren) getChildAt(0);
329 }
330 return null;
331 }
332
Michael Jurka33945b22010-12-21 18:19:38 -0800333 void setIsDragOverlapping(boolean isDragOverlapping) {
334 if (mIsDragOverlapping != isDragOverlapping) {
335 mIsDragOverlapping = isDragOverlapping;
336 invalidate();
337 }
338 }
339
340 boolean getIsDragOverlapping() {
341 return mIsDragOverlapping;
342 }
343
Adam Cohenebea84d2011-11-09 17:20:41 -0800344 protected void setOverscrollTransformsDirty(boolean dirty) {
345 mScrollingTransformsDirty = dirty;
346 }
347
348 protected void resetOverscrollTransforms() {
349 if (mScrollingTransformsDirty) {
350 setOverscrollTransformsDirty(false);
351 setTranslationX(0);
352 setRotationY(0);
353 // It doesn't matter if we pass true or false here, the important thing is that we
354 // pass 0, which results in the overscroll drawable not being drawn any more.
355 setOverScrollAmount(0, false);
356 setPivotX(getMeasuredWidth() / 2);
357 setPivotY(getMeasuredHeight() / 2);
358 }
359 }
360
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700361 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700362 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700363 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
364 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
365 // When we're small, we are either drawn normally or in the "accepts drops" state (during
366 // a drag). However, we also drag the mini hover background *over* one of those two
367 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700368 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700369 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800370
371 if (mIsDragOverlapping) {
372 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700373 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700374 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700375 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700376 }
Michael Jurka33945b22010-12-21 18:19:38 -0800377
378 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
379 bg.setBounds(mBackgroundRect);
380 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700381 }
Romain Guya6abce82009-11-10 02:54:41 -0800382
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700383 if (mCrosshairsVisibility > 0.0f) {
384 final int countX = mCountX;
385 final int countY = mCountY;
386
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700387 final float MAX_ALPHA = 0.4f;
388 final int MAX_VISIBLE_DISTANCE = 600;
389 final float DISTANCE_MULTIPLIER = 0.002f;
390
391 final Drawable d = mCrosshairsDrawable;
392 final int width = d.getIntrinsicWidth();
393 final int height = d.getIntrinsicHeight();
394
Winson Chung4b825dcd2011-06-19 12:41:22 -0700395 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700396 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700397 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700398 for (int row = 0; row <= countY; row++) {
399 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
400 float dist = mTmpPointF.length();
401 // Crosshairs further from the drag point are more faint
402 float alpha = Math.min(MAX_ALPHA,
403 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
404 if (alpha > 0.0f) {
405 d.setBounds(x, y, x + width, y + height);
406 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
407 d.draw(canvas);
408 }
409 y += mCellHeight + mHeightGap;
410 }
411 x += mCellWidth + mWidthGap;
412 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700413 }
Winson Chung150fbab2010-09-29 17:14:26 -0700414
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700415 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700416 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700417 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700418 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800419 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700420 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700421 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800422 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700423 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700424 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800425
426 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
427 // requires an expanded clip rect (due to the glow's blur radius)
428 if (mPressedOrFocusedIcon != null) {
429 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
430 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
431 if (b != null) {
432 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700433 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
434 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800435 null);
436 }
437 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700438
439 // The folder outer / inner ring image(s)
440 for (int i = 0; i < mFolderOuterRings.size(); i++) {
441 FolderRingAnimator fra = mFolderOuterRings.get(i);
442
443 // Draw outer ring
444 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
445 int width = (int) fra.getOuterRingSize();
446 int height = width;
447 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
448
449 int centerX = mTempLocation[0] + mCellWidth / 2;
450 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
451
452 canvas.save();
453 canvas.translate(centerX - width / 2, centerY - height / 2);
454 d.setBounds(0, 0, width, height);
455 d.draw(canvas);
456 canvas.restore();
457
458 // Draw inner ring
459 d = FolderRingAnimator.sSharedInnerRingDrawable;
460 width = (int) fra.getInnerRingSize();
461 height = width;
462 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
463
464 centerX = mTempLocation[0] + mCellWidth / 2;
465 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
466 canvas.save();
467 canvas.translate(centerX - width / 2, centerY - width / 2);
468 d.setBounds(0, 0, width, height);
469 d.draw(canvas);
470 canvas.restore();
471 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700472
473 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
474 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
475 int width = d.getIntrinsicWidth();
476 int height = d.getIntrinsicHeight();
477
478 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
479 int centerX = mTempLocation[0] + mCellWidth / 2;
480 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
481
482 canvas.save();
483 canvas.translate(centerX - width / 2, centerY - width / 2);
484 d.setBounds(0, 0, width, height);
485 d.draw(canvas);
486 canvas.restore();
487 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700488 }
489
Adam Cohenb5ba0972011-09-07 18:02:31 -0700490 @Override
491 protected void dispatchDraw(Canvas canvas) {
492 super.dispatchDraw(canvas);
493 if (mForegroundAlpha > 0) {
494 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
495 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
496 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
497 mOverScrollForegroundDrawable.draw(canvas);
498 p.setXfermode(null);
499 }
500 }
501
Adam Cohen69ce2e52011-07-03 19:25:21 -0700502 public void showFolderAccept(FolderRingAnimator fra) {
503 mFolderOuterRings.add(fra);
504 }
505
506 public void hideFolderAccept(FolderRingAnimator fra) {
507 if (mFolderOuterRings.contains(fra)) {
508 mFolderOuterRings.remove(fra);
509 }
510 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700511 }
512
Adam Cohenc51934b2011-07-26 21:07:43 -0700513 public void setFolderLeaveBehindCell(int x, int y) {
514 mFolderLeaveBehindCell[0] = x;
515 mFolderLeaveBehindCell[1] = y;
516 invalidate();
517 }
518
519 public void clearFolderLeaveBehind() {
520 mFolderLeaveBehindCell[0] = -1;
521 mFolderLeaveBehindCell[1] = -1;
522 invalidate();
523 }
524
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700525 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700526 public boolean shouldDelayChildPressedState() {
527 return false;
528 }
529
530 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700531 public void cancelLongPress() {
532 super.cancelLongPress();
533
534 // Cancel long press for all children
535 final int count = getChildCount();
536 for (int i = 0; i < count; i++) {
537 final View child = getChildAt(i);
538 child.cancelLongPress();
539 }
540 }
541
Michael Jurkadee05892010-07-27 10:01:56 -0700542 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
543 mInterceptTouchListener = listener;
544 }
545
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700547 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 }
549
550 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700551 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 }
553
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700554 public boolean addViewToCellLayout(
555 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700556 final LayoutParams lp = params;
557
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 // Generate an id for each view, this assumes we have at most 256x256 cells
559 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700560 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700561 // If the horizontal or vertical span is set to -1, it is taken to
562 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700563 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
564 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565
Winson Chungaafa03c2010-06-11 17:34:16 -0700566 child.setId(childId);
567
Michael Jurka8c920dd2011-01-20 14:16:56 -0800568 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700569
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700570 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700571
Winson Chungaafa03c2010-06-11 17:34:16 -0700572 return true;
573 }
574 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700576
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700578 public void removeAllViews() {
579 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800580 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700581 }
582
583 @Override
584 public void removeAllViewsInLayout() {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700585 if (mChildren.getChildCount() > 0) {
586 clearOccupiedCells();
587 mChildren.removeAllViewsInLayout();
588 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700589 }
590
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700591 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800592 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700593 }
594
Michael Jurka0280c3b2010-09-17 15:00:07 -0700595 @Override
596 public void removeView(View view) {
597 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800598 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700599 }
600
601 @Override
602 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800603 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
604 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700605 }
606
607 @Override
608 public void removeViewInLayout(View view) {
609 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800610 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700611 }
612
613 @Override
614 public void removeViews(int start, int count) {
615 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800616 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700617 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800618 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700619 }
620
621 @Override
622 public void removeViewsInLayout(int start, int count) {
623 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800624 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700625 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800626 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700627 }
628
Michael Jurka8c920dd2011-01-20 14:16:56 -0800629 public void drawChildren(Canvas canvas) {
630 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 }
632
Michael Jurkaabded662011-03-04 12:06:57 -0800633 void buildChildrenLayer() {
634 mChildren.buildLayer();
635 }
636
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 @Override
638 protected void onAttachedToWindow() {
639 super.onAttachedToWindow();
640 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
641 }
642
Michael Jurkaaf442092010-06-10 17:01:57 -0700643 public void setTagToCellInfoForPoint(int touchX, int touchY) {
644 final CellInfo cellInfo = mCellInfo;
645 final Rect frame = mRect;
646 final int x = touchX + mScrollX;
647 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800648 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700649
650 boolean found = false;
651 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800652 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800653 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700654
Adam Cohen1b607ed2011-03-03 17:26:50 -0800655 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
656 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700657 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700658
659 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
660 // offset that by this CellLayout's padding to test an (x,y) point that is relative
661 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700662 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700663
Michael Jurkaaf442092010-06-10 17:01:57 -0700664 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700665 cellInfo.cell = child;
666 cellInfo.cellX = lp.cellX;
667 cellInfo.cellY = lp.cellY;
668 cellInfo.spanX = lp.cellHSpan;
669 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700670 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700671 break;
672 }
673 }
674 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700675
Michael Jurkad771c962011-08-09 15:00:48 -0700676 mLastDownOnOccupiedCell = found;
677
Michael Jurkaaf442092010-06-10 17:01:57 -0700678 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700679 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700680 pointToCellExact(x, y, cellXY);
681
Michael Jurkaaf442092010-06-10 17:01:57 -0700682 cellInfo.cell = null;
683 cellInfo.cellX = cellXY[0];
684 cellInfo.cellY = cellXY[1];
685 cellInfo.spanX = 1;
686 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700687 }
688 setTag(cellInfo);
689 }
690
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 @Override
692 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700693 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
694 // even in the case where we return early. Not clearing here was causing bugs whereby on
695 // long-press we'd end up picking up an item from a previous drag operation.
696 final int action = ev.getAction();
697
698 if (action == MotionEvent.ACTION_DOWN) {
699 clearTagCellInfo();
700 }
701
Michael Jurkadee05892010-07-27 10:01:56 -0700702 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
703 return true;
704 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705
706 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700707 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 return false;
710 }
711
Adam Cohenc1997fd2011-08-15 18:26:39 -0700712 private void clearTagCellInfo() {
713 final CellInfo cellInfo = mCellInfo;
714 cellInfo.cell = null;
715 cellInfo.cellX = -1;
716 cellInfo.cellY = -1;
717 cellInfo.spanX = 0;
718 cellInfo.spanY = 0;
719 setTag(cellInfo);
720 }
721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700723 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 }
725
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700726 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700727 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 * @param x X coordinate of the point
729 * @param y Y coordinate of the point
730 * @param result Array of 2 ints to hold the x and y coordinate of the cell
731 */
732 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700733 final int hStartPadding = getPaddingLeft();
734 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735
736 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
737 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
738
Adam Cohend22015c2010-07-26 22:02:18 -0700739 final int xAxis = mCountX;
740 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741
742 if (result[0] < 0) result[0] = 0;
743 if (result[0] >= xAxis) result[0] = xAxis - 1;
744 if (result[1] < 0) result[1] = 0;
745 if (result[1] >= yAxis) result[1] = yAxis - 1;
746 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700747
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 /**
749 * Given a point, return the cell that most closely encloses that point
750 * @param x X coordinate of the point
751 * @param y Y coordinate of the point
752 * @param result Array of 2 ints to hold the x and y coordinate of the cell
753 */
754 void pointToCellRounded(int x, int y, int[] result) {
755 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
756 }
757
758 /**
759 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700760 *
761 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700763 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 * @param result Array of 2 ints to hold the x and y coordinate of the point
765 */
766 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700767 final int hStartPadding = getPaddingLeft();
768 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769
770 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
771 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
772 }
773
Adam Cohene3e27a82011-04-15 12:07:39 -0700774 /**
775 * Given a cell coordinate, return the point that represents the upper left corner of that cell
776 *
777 * @param cellX X coordinate of the cell
778 * @param cellY Y coordinate of the cell
779 *
780 * @param result Array of 2 ints to hold the x and y coordinate of the point
781 */
782 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700783 final int hStartPadding = getPaddingLeft();
784 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700785
786 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
787 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
788 }
789
Romain Guy84f296c2009-11-04 15:00:44 -0800790 int getCellWidth() {
791 return mCellWidth;
792 }
793
794 int getCellHeight() {
795 return mCellHeight;
796 }
797
Adam Cohend4844c32011-02-18 19:25:06 -0800798 int getWidthGap() {
799 return mWidthGap;
800 }
801
802 int getHeightGap() {
803 return mHeightGap;
804 }
805
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700806 Rect getContentRect(Rect r) {
807 if (r == null) {
808 r = new Rect();
809 }
810 int left = getPaddingLeft();
811 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700812 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
813 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700814 r.set(left, top, right, bottom);
815 return r;
816 }
817
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 @Override
819 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
820 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700823 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
824
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
826 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700827
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
829 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
830 }
831
Adam Cohend22015c2010-07-26 22:02:18 -0700832 int numWidthGaps = mCountX - 1;
833 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834
Adam Cohen234c4cd2011-07-17 21:03:04 -0700835 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700836 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
837 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
838 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
839 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
840 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
841 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700842 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700843 } else {
844 mWidthGap = mOriginalWidthGap;
845 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700846 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700847
Michael Jurka8c920dd2011-01-20 14:16:56 -0800848 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
849 int newWidth = widthSpecSize;
850 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700851 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700852 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700853 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700854 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700855 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700856 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700857 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800858
859 int count = getChildCount();
860 for (int i = 0; i < count; i++) {
861 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700862 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
863 mPaddingRight, MeasureSpec.EXACTLY);
864 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
865 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800866 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
867 }
868 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 }
870
871 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700872 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800875 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700876 child.layout(mPaddingLeft, mPaddingTop,
877 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 }
879 }
880
881 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700882 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
883 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700884 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700885 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
886 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -0700887 }
888
889 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800891 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 }
893
894 @Override
895 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800896 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 }
898
Michael Jurka5f1c5092010-09-03 14:15:02 -0700899 public float getBackgroundAlpha() {
900 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700901 }
902
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700903 public void setBackgroundAlphaMultiplier(float multiplier) {
904 mBackgroundAlphaMultiplier = multiplier;
905 }
906
Adam Cohenddb82192010-11-10 16:32:54 -0800907 public float getBackgroundAlphaMultiplier() {
908 return mBackgroundAlphaMultiplier;
909 }
910
Michael Jurka5f1c5092010-09-03 14:15:02 -0700911 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800912 if (mBackgroundAlpha != alpha) {
913 mBackgroundAlpha = alpha;
914 invalidate();
915 }
Michael Jurkadee05892010-07-27 10:01:56 -0700916 }
917
Michael Jurka5f1c5092010-09-03 14:15:02 -0700918 // Need to return true to let the view system know we know how to handle alpha-- this is
919 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
920 // versions
921 @Override
922 protected boolean onSetAlpha(int alpha) {
923 return true;
924 }
925
Michael Jurkaafaa0502011-12-13 18:22:50 -0800926 @Override
Michael Jurka5f1c5092010-09-03 14:15:02 -0700927 public void setAlpha(float alpha) {
928 setChildrenAlpha(alpha);
929 super.setAlpha(alpha);
930 }
931
Michael Jurkadee05892010-07-27 10:01:56 -0700932 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700933 final int childCount = getChildCount();
934 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700935 getChildAt(i).setAlpha(alpha);
936 }
937 }
938
Patrick Dubroy440c3602010-07-13 17:50:32 -0700939 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800940 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700941 }
942
Adam Cohen76fc0852011-06-17 13:26:23 -0700943 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
944 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700945 CellLayoutChildren clc = getChildrenLayout();
946 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
947 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
948 final ItemInfo info = (ItemInfo) child.getTag();
949
950 // We cancel any existing animations
951 if (mReorderAnimators.containsKey(lp)) {
952 mReorderAnimators.get(lp).cancel();
953 mReorderAnimators.remove(lp);
954 }
955
956 int oldX = lp.x;
957 int oldY = lp.y;
958 mOccupied[lp.cellX][lp.cellY] = false;
959 mOccupied[cellX][cellY] = true;
960
961 lp.isLockedToGrid = true;
962 lp.cellX = info.cellX = cellX;
963 lp.cellY = info.cellY = cellY;
964 clc.setupLp(lp);
965 lp.isLockedToGrid = false;
966 int newX = lp.x;
967 int newY = lp.y;
968
Adam Cohen76fc0852011-06-17 13:26:23 -0700969 lp.x = oldX;
970 lp.y = oldY;
971 child.requestLayout();
972
Adam Cohenbfbfd262011-06-13 16:55:12 -0700973 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
974 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
975 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
976 oa.setDuration(duration);
977 mReorderAnimators.put(lp, oa);
978 oa.addUpdateListener(new AnimatorUpdateListener() {
979 public void onAnimationUpdate(ValueAnimator animation) {
980 child.requestLayout();
981 }
982 });
983 oa.addListener(new AnimatorListenerAdapter() {
984 boolean cancelled = false;
985 public void onAnimationEnd(Animator animation) {
986 // If the animation was cancelled, it means that another animation
987 // has interrupted this one, and we don't want to lock the item into
988 // place just yet.
989 if (!cancelled) {
990 lp.isLockedToGrid = true;
991 }
992 if (mReorderAnimators.containsKey(lp)) {
993 mReorderAnimators.remove(lp);
994 }
995 }
996 public void onAnimationCancel(Animator animation) {
997 cancelled = true;
998 }
999 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001000 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001001 oa.start();
1002 return true;
1003 }
1004 return false;
1005 }
1006
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001007 /**
1008 * Estimate where the top left cell of the dragged item will land if it is dropped.
1009 *
1010 * @param originX The X value of the top left corner of the item
1011 * @param originY The Y value of the top left corner of the item
1012 * @param spanX The number of horizontal cells that the item spans
1013 * @param spanY The number of vertical cells that the item spans
1014 * @param result The estimated drop cell X and Y.
1015 */
1016 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001017 final int countX = mCountX;
1018 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001019
Michael Jurkaa63c4522010-08-19 13:52:27 -07001020 // pointToCellRounded takes the top left of a cell but will pad that with
1021 // cellWidth/2 and cellHeight/2 when finding the matching cell
1022 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001023
1024 // If the item isn't fully on this screen, snap to the edges
1025 int rightOverhang = result[0] + spanX - countX;
1026 if (rightOverhang > 0) {
1027 result[0] -= rightOverhang; // Snap to right
1028 }
1029 result[0] = Math.max(0, result[0]); // Snap to left
1030 int bottomOverhang = result[1] + spanY - countY;
1031 if (bottomOverhang > 0) {
1032 result[1] -= bottomOverhang; // Snap to bottom
1033 }
1034 result[1] = Math.max(0, result[1]); // Snap to top
1035 }
1036
Winson Chungb8c69f32011-10-19 21:36:08 -07001037 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY,
Adam Cohend41fbf52012-02-16 23:53:59 -08001038 int minSpanX, int minSpanY, int spanX, int spanY, Point dragOffset, Rect dragRegion) {
Joe Onorato4be866d2010-10-10 11:26:02 -07001039
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001040 final int oldDragCellX = mDragCell[0];
1041 final int oldDragCellY = mDragCell[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001042 int[] resultSpan = new int[2];
1043 final int[] nearest = findNearestVacantArea(originX, originY, minSpanX, minSpanY,
1044 spanX, spanY, v, mDragCell, resultSpan);
1045 boolean resize = spanX > resultSpan[0] || spanY > resultSpan[1];
1046 spanX = resultSpan[0];
1047 spanY = resultSpan[1];
Winson Chungb8c69f32011-10-19 21:36:08 -07001048 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001049 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1050 } else {
1051 mDragCenter.set(originX, originY);
1052 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001053
Adam Cohen2801caf2011-05-13 20:57:39 -07001054 if (dragOutline == null && v == null) {
1055 if (mCrosshairsDrawable != null) {
1056 invalidate();
1057 }
1058 return;
1059 }
1060
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001061 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001062 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001063 final int[] topLeft = mTmpPoint;
1064 cellToPoint(nearest[0], nearest[1], topLeft);
1065
Joe Onorato4be866d2010-10-10 11:26:02 -07001066 int left = topLeft[0];
1067 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001068
Winson Chungb8c69f32011-10-19 21:36:08 -07001069 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001070 // When drawing the drag outline, it did not account for margin offsets
1071 // added by the view's parent.
1072 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1073 left += lp.leftMargin;
1074 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001075
Adam Cohen99e8b402011-03-25 19:23:43 -07001076 // Offsets due to the size difference between the View and the dragOutline.
1077 // There is a size difference to account for the outer blur, which may lie
1078 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001079 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001080 // We center about the x axis
1081 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1082 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001083 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001084 if (dragOffset != null && dragRegion != null) {
1085 // Center the drag region *horizontally* in the cell and apply a drag
1086 // outline offset
1087 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1088 - dragRegion.width()) / 2;
1089 top += dragOffset.y;
1090 } else {
1091 // Center the drag outline in the cell
1092 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1093 - dragOutline.getWidth()) / 2;
1094 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1095 - dragOutline.getHeight()) / 2;
1096 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001097 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001098 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001099 mDragOutlineAnims[oldIndex].animateOut();
1100 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001101 Rect r = mDragOutlines[mDragOutlineCurrent];
1102 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1103 if (resize) {
1104 cellToRect(nearest[0], nearest[1], spanX, spanY, r);
1105 }
Winson Chung150fbab2010-09-29 17:14:26 -07001106
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001107 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1108 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001109 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001110
1111 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1112 if (mCrosshairsDrawable != null) {
1113 invalidate();
1114 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001115 }
1116
Adam Cohene0310962011-04-18 16:15:31 -07001117 public void clearDragOutlines() {
1118 final int oldIndex = mDragOutlineCurrent;
1119 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001120 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001121 }
1122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001124 * Find a vacant area that will fit the given bounds nearest the requested
1125 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001126 *
Romain Guy51afc022009-05-04 18:03:43 -07001127 * @param pixelX The X location at which you want to search for a vacant area.
1128 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001129 * @param spanX Horizontal span of the object.
1130 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001131 * @param result Array in which to place the result, or null (in which case a new array will
1132 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001133 * @return The X, Y cell of a vacant area that can contain this object,
1134 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001136 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1137 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001138 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001139 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001140
Michael Jurka6a1435d2010-09-27 17:35:12 -07001141 /**
1142 * Find a vacant area that will fit the given bounds nearest the requested
1143 * cell location. Uses Euclidean distance to score multiple vacant areas.
1144 *
1145 * @param pixelX The X location at which you want to search for a vacant area.
1146 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001147 * @param minSpanX The minimum horizontal span required
1148 * @param minSpanY The minimum vertical span required
1149 * @param spanX Horizontal span of the object.
1150 * @param spanY Vertical span of the object.
1151 * @param result Array in which to place the result, or null (in which case a new array will
1152 * be allocated)
1153 * @return The X, Y cell of a vacant area that can contain this object,
1154 * nearest the requested location.
1155 */
1156 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1157 int spanY, int[] result, int[] resultSpan) {
1158 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1159 result, resultSpan);
1160 }
1161
1162 /**
1163 * Find a vacant area that will fit the given bounds nearest the requested
1164 * cell location. Uses Euclidean distance to score multiple vacant areas.
1165 *
1166 * @param pixelX The X location at which you want to search for a vacant area.
1167 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001168 * @param spanX Horizontal span of the object.
1169 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001170 * @param ignoreOccupied If true, the result can be an occupied cell
1171 * @param result Array in which to place the result, or null (in which case a new array will
1172 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001173 * @return The X, Y cell of a vacant area that can contain this object,
1174 * nearest the requested location.
1175 */
Adam Cohendf035382011-04-11 17:22:04 -07001176 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1177 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001178 return findNearestArea(pixelX, pixelY, spanX, spanY,
1179 spanX, spanY, ignoreView, ignoreOccupied, result, null);
1180 }
1181
1182 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1183 private void lazyInitTempRectStack() {
1184 if (mTempRectStack.isEmpty()) {
1185 for (int i = 0; i < mCountX * mCountY; i++) {
1186 mTempRectStack.push(new Rect());
1187 }
1188 }
1189 }
1190 private void recycleTempRects(Stack<Rect> used) {
1191 while (!used.isEmpty()) {
1192 mTempRectStack.push(used.pop());
1193 }
1194 }
1195
1196 /**
1197 * Find a vacant area that will fit the given bounds nearest the requested
1198 * cell location. Uses Euclidean distance to score multiple vacant areas.
1199 *
1200 * @param pixelX The X location at which you want to search for a vacant area.
1201 * @param pixelY The Y location at which you want to search for a vacant area.
1202 * @param minSpanX The minimum horizontal span required
1203 * @param minSpanY The minimum vertical span required
1204 * @param spanX Horizontal span of the object.
1205 * @param spanY Vertical span of the object.
1206 * @param ignoreOccupied If true, the result can be an occupied cell
1207 * @param result Array in which to place the result, or null (in which case a new array will
1208 * be allocated)
1209 * @return The X, Y cell of a vacant area that can contain this object,
1210 * nearest the requested location.
1211 */
1212 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
1213 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan) {
1214 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001215 // mark space take by ignoreView as available (method checks if ignoreView is null)
1216 markCellsAsUnoccupiedForView(ignoreView);
1217
Adam Cohene3e27a82011-04-15 12:07:39 -07001218 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1219 // to the center of the item, but we are searching based on the top-left cell, so
1220 // we translate the point over to correspond to the top-left.
1221 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1222 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1223
Jeff Sharkey70864282009-04-07 21:08:40 -07001224 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001225 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001226 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001227 final Rect bestRect = new Rect(-1, -1, -1, -1);
1228 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001229
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001230 final int countX = mCountX;
1231 final int countY = mCountY;
1232 final boolean[][] occupied = mOccupied;
1233
Adam Cohend41fbf52012-02-16 23:53:59 -08001234 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1235 spanX < minSpanX || spanY < minSpanY) {
1236 return bestXY;
1237 }
1238
1239 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001240 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001241 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1242 int ySize = -1;
1243 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001244 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001245 // First, let's see if this thing fits anywhere
1246 for (int i = 0; i < minSpanX; i++) {
1247 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001248 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001249 continue inner;
1250 }
Michael Jurkac28de512010-08-13 11:27:44 -07001251 }
1252 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001253 xSize = minSpanX;
1254 ySize = minSpanY;
1255
1256 // We know that the item will fit at _some_ acceptable size, now let's see
1257 // how big we can make it. We'll alternate between incrementing x and y spans
1258 // until we hit a limit.
1259 boolean incX = true;
1260 boolean hitMaxX = xSize >= spanX;
1261 boolean hitMaxY = ySize >= spanY;
1262 while (!(hitMaxX && hitMaxY)) {
1263 if (incX && !hitMaxX) {
1264 for (int j = 0; j < ySize; j++) {
1265 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1266 // We can't move out horizontally
1267 hitMaxX = true;
1268 }
1269 }
1270 if (!hitMaxX) {
1271 xSize++;
1272 }
1273 } else if (!hitMaxY) {
1274 for (int i = 0; i < xSize; i++) {
1275 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1276 // We can't move out vertically
1277 hitMaxY = true;
1278 }
1279 }
1280 if (!hitMaxY) {
1281 ySize++;
1282 }
1283 }
1284 hitMaxX |= xSize >= spanX;
1285 hitMaxY |= ySize >= spanY;
1286 incX = !incX;
1287 }
1288 incX = true;
1289 hitMaxX = xSize >= spanX;
1290 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001291 }
Winson Chung0be025d2011-05-23 17:45:09 -07001292 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001293 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294
Adam Cohend41fbf52012-02-16 23:53:59 -08001295 // We verify that the current rect is not a sub-rect of any of our previous
1296 // candidates. In this case, the current rect is disqualified in favour of the
1297 // containing rect.
1298 Rect currentRect = mTempRectStack.pop();
1299 currentRect.set(x, y, x + xSize, y + ySize);
1300 boolean contained = false;
1301 for (Rect r : validRegions) {
1302 if (r.contains(currentRect)) {
1303 contained = true;
1304 break;
1305 }
1306 }
1307 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001308 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1309 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohend41fbf52012-02-16 23:53:59 -08001310 if ((distance <= bestDistance && !contained) ||
1311 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001312 bestDistance = distance;
1313 bestXY[0] = x;
1314 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001315 if (resultSpan != null) {
1316 resultSpan[0] = xSize;
1317 resultSpan[1] = ySize;
1318 }
1319 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001323 // re-mark space taken by ignoreView as occupied
1324 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
Adam Cohenc0dcf592011-06-01 15:30:43 -07001326 // Return -1, -1 if no suitable location found
1327 if (bestDistance == Double.MAX_VALUE) {
1328 bestXY[0] = -1;
1329 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001330 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001331 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001332 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001334
Adam Cohendf035382011-04-11 17:22:04 -07001335 /**
1336 * Find a vacant area that will fit the given bounds nearest the requested
1337 * cell location. Uses Euclidean distance to score multiple vacant areas.
1338 *
1339 * @param pixelX The X location at which you want to search for a vacant area.
1340 * @param pixelY The Y location at which you want to search for a vacant area.
1341 * @param spanX Horizontal span of the object.
1342 * @param spanY Vertical span of the object.
1343 * @param ignoreView Considers space occupied by this view as unoccupied
1344 * @param result Previously returned value to possibly recycle.
1345 * @return The X, Y cell of a vacant area that can contain this object,
1346 * nearest the requested location.
1347 */
1348 int[] findNearestVacantArea(
1349 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1350 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1351 }
1352
1353 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08001354 * Find a vacant area that will fit the given bounds nearest the requested
1355 * cell location. Uses Euclidean distance to score multiple vacant areas.
1356 *
1357 * @param pixelX The X location at which you want to search for a vacant area.
1358 * @param pixelY The Y location at which you want to search for a vacant area.
1359 * @param minSpanX The minimum horizontal span required
1360 * @param minSpanY The minimum vertical span required
1361 * @param spanX Horizontal span of the object.
1362 * @param spanY Vertical span of the object.
1363 * @param ignoreView Considers space occupied by this view as unoccupied
1364 * @param result Previously returned value to possibly recycle.
1365 * @return The X, Y cell of a vacant area that can contain this object,
1366 * nearest the requested location.
1367 */
1368 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
1369 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
1370 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY,
1371 spanX, spanY, ignoreView, true, result, resultSpan);
1372 }
1373
1374 /**
Adam Cohendf035382011-04-11 17:22:04 -07001375 * Find a starting cell position that will fit the given bounds nearest the requested
1376 * cell location. Uses Euclidean distance to score multiple vacant areas.
1377 *
1378 * @param pixelX The X location at which you want to search for a vacant area.
1379 * @param pixelY The Y location at which you want to search for a vacant area.
1380 * @param spanX Horizontal span of the object.
1381 * @param spanY Vertical span of the object.
1382 * @param ignoreView Considers space occupied by this view as unoccupied
1383 * @param result Previously returned value to possibly recycle.
1384 * @return The X, Y cell of a vacant area that can contain this object,
1385 * nearest the requested location.
1386 */
1387 int[] findNearestArea(
1388 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1389 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1390 }
1391
Michael Jurka0280c3b2010-09-17 15:00:07 -07001392 boolean existsEmptyCell() {
1393 return findCellForSpan(null, 1, 1);
1394 }
1395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001397 * Finds the upper-left coordinate of the first rectangle in the grid that can
1398 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1399 * then this method will only return coordinates for rectangles that contain the cell
1400 * (intersectX, intersectY)
1401 *
1402 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1403 * can be found.
1404 * @param spanX The horizontal span of the cell we want to find.
1405 * @param spanY The vertical span of the cell we want to find.
1406 *
1407 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001408 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001409 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1410 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1411 }
1412
1413 /**
1414 * Like above, but ignores any cells occupied by the item "ignoreView"
1415 *
1416 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1417 * can be found.
1418 * @param spanX The horizontal span of the cell we want to find.
1419 * @param spanY The vertical span of the cell we want to find.
1420 * @param ignoreView The home screen item we should treat as not occupying any space
1421 * @return
1422 */
1423 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1424 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1425 }
1426
1427 /**
1428 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1429 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1430 *
1431 * @param spanX The horizontal span of the cell we want to find.
1432 * @param spanY The vertical span of the cell we want to find.
1433 * @param ignoreView The home screen item we should treat as not occupying any space
1434 * @param intersectX The X coordinate of the cell that we should try to overlap
1435 * @param intersectX The Y coordinate of the cell that we should try to overlap
1436 *
1437 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1438 */
1439 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1440 int intersectX, int intersectY) {
1441 return findCellForSpanThatIntersectsIgnoring(
1442 cellXY, spanX, spanY, intersectX, intersectY, null);
1443 }
1444
1445 /**
1446 * The superset of the above two methods
1447 */
1448 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1449 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001450 // mark space take by ignoreView as available (method checks if ignoreView is null)
1451 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001452
Michael Jurka28750fb2010-09-24 17:43:49 -07001453 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001454 while (true) {
1455 int startX = 0;
1456 if (intersectX >= 0) {
1457 startX = Math.max(startX, intersectX - (spanX - 1));
1458 }
1459 int endX = mCountX - (spanX - 1);
1460 if (intersectX >= 0) {
1461 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1462 }
1463 int startY = 0;
1464 if (intersectY >= 0) {
1465 startY = Math.max(startY, intersectY - (spanY - 1));
1466 }
1467 int endY = mCountY - (spanY - 1);
1468 if (intersectY >= 0) {
1469 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1470 }
1471
Winson Chungbbc60d82010-11-11 16:34:41 -08001472 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001473 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001474 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001475 for (int i = 0; i < spanX; i++) {
1476 for (int j = 0; j < spanY; j++) {
1477 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001478 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001479 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001480 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001481 continue inner;
1482 }
1483 }
1484 }
1485 if (cellXY != null) {
1486 cellXY[0] = x;
1487 cellXY[1] = y;
1488 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001489 foundCell = true;
1490 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001491 }
1492 }
1493 if (intersectX == -1 && intersectY == -1) {
1494 break;
1495 } else {
1496 // if we failed to find anything, try again but without any requirements of
1497 // intersecting
1498 intersectX = -1;
1499 intersectY = -1;
1500 continue;
1501 }
1502 }
1503
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001504 // re-mark space taken by ignoreView as occupied
1505 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001506 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001507 }
1508
1509 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001510 * A drag event has begun over this layout.
1511 * It may have begun over this layout (in which case onDragChild is called first),
1512 * or it may have begun on another layout.
1513 */
1514 void onDragEnter() {
1515 if (!mDragging) {
1516 // Fade in the drag indicators
1517 if (mCrosshairsAnimator != null) {
1518 mCrosshairsAnimator.animateIn();
1519 }
1520 }
1521 mDragging = true;
1522 }
1523
1524 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001525 * Called when drag has left this CellLayout or has been completed (successfully or not)
1526 */
1527 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001528 // This can actually be called when we aren't in a drag, e.g. when adding a new
1529 // item to this layout via the customize drawer.
1530 // Guard against that case.
1531 if (mDragging) {
1532 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001533
Joe Onorato4be866d2010-10-10 11:26:02 -07001534 // Fade out the drag indicators
1535 if (mCrosshairsAnimator != null) {
1536 mCrosshairsAnimator.animateOut();
1537 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001538 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001539
1540 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08001541 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001542 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1543 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1544
Michael Jurka33945b22010-12-21 18:19:38 -08001545 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001546 }
1547
1548 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001549 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001550 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001551 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 *
1553 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001555 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001556 if (child != null) {
1557 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001558 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001559 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001560 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
1562
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001565 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 * @param cellX X coordinate of upper left corner expressed as a cell position
1567 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001568 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001570 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001572 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 final int cellWidth = mCellWidth;
1574 final int cellHeight = mCellHeight;
1575 final int widthGap = mWidthGap;
1576 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001577
Winson Chung4b825dcd2011-06-19 12:41:22 -07001578 final int hStartPadding = getPaddingLeft();
1579 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001580
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1582 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1583
1584 int x = hStartPadding + cellX * (cellWidth + widthGap);
1585 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001586
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001587 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001589
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001591 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001593 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 * @param width Width in pixels
1595 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001596 * @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 -08001597 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001598 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001599 return rectToCell(getResources(), width, height, result);
1600 }
1601
1602 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 // Always assume we're working with the smallest span to make sure we
1604 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001605 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1606 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001608
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07001610 int spanX = (int) Math.ceil(width / (float) smallerSize);
1611 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04001612
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001613 if (result == null) {
1614 return new int[] { spanX, spanY };
1615 }
1616 result[0] = spanX;
1617 result[1] = spanY;
1618 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 }
1620
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001621 public int[] cellSpansToSize(int hSpans, int vSpans) {
1622 int[] size = new int[2];
1623 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1624 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1625 return size;
1626 }
1627
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001629 * Calculate the grid spans needed to fit given item
1630 */
1631 public void calculateSpans(ItemInfo info) {
1632 final int minWidth;
1633 final int minHeight;
1634
1635 if (info instanceof LauncherAppWidgetInfo) {
1636 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1637 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1638 } else if (info instanceof PendingAddWidgetInfo) {
1639 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1640 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1641 } else {
1642 // It's not a widget, so it must be 1x1
1643 info.spanX = info.spanY = 1;
1644 return;
1645 }
1646 int[] spans = rectToCell(minWidth, minHeight, null);
1647 info.spanX = spans[0];
1648 info.spanY = spans[1];
1649 }
1650
1651 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 * Find the first vacant cell, if there is one.
1653 *
1654 * @param vacant Holds the x and y coordinate of the vacant cell
1655 * @param spanX Horizontal cell span.
1656 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001657 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 * @return True if a vacant cell was found
1659 */
1660 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661
Michael Jurka0280c3b2010-09-17 15:00:07 -07001662 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 }
1664
1665 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1666 int xCount, int yCount, boolean[][] occupied) {
1667
Adam Cohen2801caf2011-05-13 20:57:39 -07001668 for (int y = 0; y < yCount; y++) {
1669 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 boolean available = !occupied[x][y];
1671out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1672 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1673 available = available && !occupied[i][j];
1674 if (!available) break out;
1675 }
1676 }
1677
1678 if (available) {
1679 vacant[0] = x;
1680 vacant[1] = y;
1681 return true;
1682 }
1683 }
1684 }
1685
1686 return false;
1687 }
1688
Michael Jurka0280c3b2010-09-17 15:00:07 -07001689 private void clearOccupiedCells() {
1690 for (int x = 0; x < mCountX; x++) {
1691 for (int y = 0; y < mCountY; y++) {
1692 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 }
1694 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001695 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696
Adam Cohen1b607ed2011-03-03 17:26:50 -08001697 /**
1698 * Given a view, determines how much that view can be expanded in all directions, in terms of
1699 * whether or not there are other items occupying adjacent cells. Used by the
1700 * AppWidgetResizeFrame to determine how the widget can be resized.
1701 */
Adam Cohend4844c32011-02-18 19:25:06 -08001702 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001703 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001704 boolean flag;
1705
Adam Cohen1b607ed2011-03-03 17:26:50 -08001706 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001707 for (int x = lp.cellX - 1; x >= 0; x--) {
1708 flag = false;
1709 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1710 if (mOccupied[x][y]) flag = true;
1711 }
1712 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001713 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001714 }
1715
Adam Cohen1b607ed2011-03-03 17:26:50 -08001716 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001717 for (int y = lp.cellY - 1; y >= 0; y--) {
1718 flag = false;
1719 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1720 if (mOccupied[x][y]) flag = true;
1721 }
1722 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001723 expandability[AppWidgetResizeFrame.TOP]++;
1724 }
Adam Cohend4844c32011-02-18 19:25:06 -08001725
Adam Cohen1b607ed2011-03-03 17:26:50 -08001726 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001727 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1728 flag = false;
1729 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1730 if (mOccupied[x][y]) flag = true;
1731 }
1732 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001733 expandability[AppWidgetResizeFrame.RIGHT]++;
1734 }
Adam Cohend4844c32011-02-18 19:25:06 -08001735
Adam Cohen1b607ed2011-03-03 17:26:50 -08001736 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001737 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1738 flag = false;
1739 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1740 if (mOccupied[x][y]) flag = true;
1741 }
1742 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001743 expandability[AppWidgetResizeFrame.BOTTOM]++;
1744 }
Adam Cohend4844c32011-02-18 19:25:06 -08001745 }
1746
Adam Cohend41fbf52012-02-16 23:53:59 -08001747 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001748 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1749 markCellsAsUnoccupiedForView(view);
Adam Cohend41fbf52012-02-16 23:53:59 -08001750 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752
Adam Cohend4844c32011-02-18 19:25:06 -08001753 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001754 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001755 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1756 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1757 }
1758
Adam Cohend4844c32011-02-18 19:25:06 -08001759 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001760 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001761 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1762 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1763 }
1764
1765 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1766 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1767 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1768 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 }
1770 }
1771 }
1772
Adam Cohen2801caf2011-05-13 20:57:39 -07001773 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001774 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001775 (Math.max((mCountX - 1), 0) * mWidthGap);
1776 }
1777
1778 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001779 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001780 (Math.max((mCountY - 1), 0) * mHeightGap);
1781 }
1782
Michael Jurka66d72172011-04-12 16:29:25 -07001783 public boolean isOccupied(int x, int y) {
1784 if (x < mCountX && y < mCountY) {
1785 return mOccupied[x][y];
1786 } else {
1787 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1788 }
1789 }
1790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 @Override
1792 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1793 return new CellLayout.LayoutParams(getContext(), attrs);
1794 }
1795
1796 @Override
1797 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1798 return p instanceof CellLayout.LayoutParams;
1799 }
1800
1801 @Override
1802 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1803 return new CellLayout.LayoutParams(p);
1804 }
1805
Winson Chungaafa03c2010-06-11 17:34:16 -07001806 public static class CellLayoutAnimationController extends LayoutAnimationController {
1807 public CellLayoutAnimationController(Animation animation, float delay) {
1808 super(animation, delay);
1809 }
1810
1811 @Override
1812 protected long getDelayForView(View view) {
1813 return (int) (Math.random() * 150);
1814 }
1815 }
1816
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1818 /**
1819 * Horizontal location of the item in the grid.
1820 */
1821 @ViewDebug.ExportedProperty
1822 public int cellX;
1823
1824 /**
1825 * Vertical location of the item in the grid.
1826 */
1827 @ViewDebug.ExportedProperty
1828 public int cellY;
1829
1830 /**
1831 * Number of cells spanned horizontally by the item.
1832 */
1833 @ViewDebug.ExportedProperty
1834 public int cellHSpan;
1835
1836 /**
1837 * Number of cells spanned vertically by the item.
1838 */
1839 @ViewDebug.ExportedProperty
1840 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001841
Adam Cohen1b607ed2011-03-03 17:26:50 -08001842 /**
1843 * Indicates whether the item will set its x, y, width and height parameters freely,
1844 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1845 */
Adam Cohend4844c32011-02-18 19:25:06 -08001846 public boolean isLockedToGrid = true;
1847
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 // X coordinate of the view in the layout.
1849 @ViewDebug.ExportedProperty
1850 int x;
1851 // Y coordinate of the view in the layout.
1852 @ViewDebug.ExportedProperty
1853 int y;
1854
Romain Guy84f296c2009-11-04 15:00:44 -08001855 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001856
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 public LayoutParams(Context c, AttributeSet attrs) {
1858 super(c, attrs);
1859 cellHSpan = 1;
1860 cellVSpan = 1;
1861 }
1862
1863 public LayoutParams(ViewGroup.LayoutParams source) {
1864 super(source);
1865 cellHSpan = 1;
1866 cellVSpan = 1;
1867 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001868
1869 public LayoutParams(LayoutParams source) {
1870 super(source);
1871 this.cellX = source.cellX;
1872 this.cellY = source.cellY;
1873 this.cellHSpan = source.cellHSpan;
1874 this.cellVSpan = source.cellVSpan;
1875 }
1876
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001878 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 this.cellX = cellX;
1880 this.cellY = cellY;
1881 this.cellHSpan = cellHSpan;
1882 this.cellVSpan = cellVSpan;
1883 }
1884
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001885 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001886 if (isLockedToGrid) {
1887 final int myCellHSpan = cellHSpan;
1888 final int myCellVSpan = cellVSpan;
1889 final int myCellX = cellX;
1890 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001891
Adam Cohend4844c32011-02-18 19:25:06 -08001892 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1893 leftMargin - rightMargin;
1894 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1895 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001896 x = myCellX * (cellWidth + widthGap) + leftMargin;
1897 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001898 }
1899 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001900
Winson Chungaafa03c2010-06-11 17:34:16 -07001901 public String toString() {
1902 return "(" + this.cellX + ", " + this.cellY + ")";
1903 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001904
1905 public void setWidth(int width) {
1906 this.width = width;
1907 }
1908
1909 public int getWidth() {
1910 return width;
1911 }
1912
1913 public void setHeight(int height) {
1914 this.height = height;
1915 }
1916
1917 public int getHeight() {
1918 return height;
1919 }
1920
1921 public void setX(int x) {
1922 this.x = x;
1923 }
1924
1925 public int getX() {
1926 return x;
1927 }
1928
1929 public void setY(int y) {
1930 this.y = y;
1931 }
1932
1933 public int getY() {
1934 return y;
1935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
1937
Michael Jurka0280c3b2010-09-17 15:00:07 -07001938 // This class stores info for two purposes:
1939 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1940 // its spanX, spanY, and the screen it is on
1941 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1942 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1943 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001944 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001946 int cellX = -1;
1947 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 int spanX;
1949 int spanY;
1950 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001951 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 @Override
1954 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001955 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1956 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
1958 }
Michael Jurkad771c962011-08-09 15:00:48 -07001959
1960 public boolean lastDownOnOccupiedCell() {
1961 return mLastDownOnOccupiedCell;
1962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963}