blob: e27345bfe4525f373dfd03b5003c41d37ce05568 [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;
37import android.graphics.RectF;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070038import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070039import android.graphics.drawable.NinePatchDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.MotionEvent;
43import android.view.View;
44import android.view.ViewDebug;
45import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070047import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
Adam Cohen66396872011-04-15 17:50:36 -070050import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070051import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070052
Adam Cohen69ce2e52011-07-03 19:25:21 -070053import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070054import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070055import java.util.HashMap;
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.
Winson Chung63257c12011-05-05 17:06:13 -0700112 private Point[] mDragOutlines = new Point[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++) {
201 mDragOutlines[i] = new Point(-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();
Joe Onorato4be866d2010-10-10 11:26:02 -0700235 final int left = mDragOutlines[thisIndex].x;
236 final int top = mDragOutlines[thisIndex].y;
237 CellLayout.this.invalidate(left, top,
238 left + outline.getWidth(), top + outline.getHeight());
239 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700240 }
241 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700242 // The animation holds a reference to the drag outline bitmap as long is it's
243 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700244 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700245 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700246 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700247 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700248 anim.setTag(null);
249 }
250 }
251 });
252 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700253 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700254
Michael Jurka18014792010-10-14 09:01:34 -0700255 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700256 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800257
Michael Jurka8c920dd2011-01-20 14:16:56 -0800258 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700259 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800260 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700261 }
262
Michael Jurkaf6440da2011-04-05 14:50:34 -0700263 static int widthInPortrait(Resources r, int numCells) {
264 // We use this method from Workspace to figure out how many rows/columns Launcher should
265 // have. We ignore the left/right padding on CellLayout because it turns out in our design
266 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700267 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700268 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
269 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700270
Winson Chung4b825dcd2011-06-19 12:41:22 -0700271 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700272 }
273
Michael Jurkaf6440da2011-04-05 14:50:34 -0700274 static int heightInLandscape(Resources r, int numCells) {
275 // We use this method from Workspace to figure out how many rows/columns Launcher should
276 // have. We ignore the left/right padding on CellLayout because it turns out in our design
277 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700278 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700279 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
280 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700281
Winson Chung4b825dcd2011-06-19 12:41:22 -0700282 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700283 }
284
Adam Cohen2801caf2011-05-13 20:57:39 -0700285 public void enableHardwareLayers() {
Adam Cohen7ef91832011-08-25 14:06:02 -0700286 mChildren.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700287 }
288
289 public void setGridSize(int x, int y) {
290 mCountX = x;
291 mCountY = y;
292 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700293 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700294 }
295
Patrick Dubroy96864c32011-03-10 17:17:23 -0800296 private void invalidateBubbleTextView(BubbleTextView icon) {
297 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700298 invalidate(icon.getLeft() + getPaddingLeft() - padding,
299 icon.getTop() + getPaddingTop() - padding,
300 icon.getRight() + getPaddingLeft() + padding,
301 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800302 }
303
Adam Cohenb5ba0972011-09-07 18:02:31 -0700304 void setOverScrollAmount(float r, boolean left) {
305 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
306 mOverScrollForegroundDrawable = mOverScrollLeft;
307 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
308 mOverScrollForegroundDrawable = mOverScrollRight;
309 }
310
311 mForegroundAlpha = (int) Math.round((r * 255));
312 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
313 invalidate();
314 }
315
Patrick Dubroy96864c32011-03-10 17:17:23 -0800316 void setPressedOrFocusedIcon(BubbleTextView icon) {
317 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
318 // requires an expanded clip rect (due to the glow's blur radius)
319 BubbleTextView oldIcon = mPressedOrFocusedIcon;
320 mPressedOrFocusedIcon = icon;
321 if (oldIcon != null) {
322 invalidateBubbleTextView(oldIcon);
323 }
324 if (mPressedOrFocusedIcon != null) {
325 invalidateBubbleTextView(mPressedOrFocusedIcon);
326 }
327 }
328
Winson Chung6e314082011-01-27 16:46:51 -0800329 public CellLayoutChildren getChildrenLayout() {
330 if (getChildCount() > 0) {
331 return (CellLayoutChildren) getChildAt(0);
332 }
333 return null;
334 }
335
Michael Jurka33945b22010-12-21 18:19:38 -0800336 void setIsDragOverlapping(boolean isDragOverlapping) {
337 if (mIsDragOverlapping != isDragOverlapping) {
338 mIsDragOverlapping = isDragOverlapping;
339 invalidate();
340 }
341 }
342
343 boolean getIsDragOverlapping() {
344 return mIsDragOverlapping;
345 }
346
Adam Cohenebea84d2011-11-09 17:20:41 -0800347 protected void setOverscrollTransformsDirty(boolean dirty) {
348 mScrollingTransformsDirty = dirty;
349 }
350
351 protected void resetOverscrollTransforms() {
352 if (mScrollingTransformsDirty) {
353 setOverscrollTransformsDirty(false);
354 setTranslationX(0);
355 setRotationY(0);
356 // It doesn't matter if we pass true or false here, the important thing is that we
357 // pass 0, which results in the overscroll drawable not being drawn any more.
358 setOverScrollAmount(0, false);
359 setPivotX(getMeasuredWidth() / 2);
360 setPivotY(getMeasuredHeight() / 2);
361 }
362 }
363
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700364 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700365 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700366 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
367 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
368 // When we're small, we are either drawn normally or in the "accepts drops" state (during
369 // a drag). However, we also drag the mini hover background *over* one of those two
370 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700371 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700372 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800373
374 if (mIsDragOverlapping) {
375 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700376 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700377 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700378 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700379 }
Michael Jurka33945b22010-12-21 18:19:38 -0800380
381 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
382 bg.setBounds(mBackgroundRect);
383 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700384 }
Romain Guya6abce82009-11-10 02:54:41 -0800385
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700386 if (mCrosshairsVisibility > 0.0f) {
387 final int countX = mCountX;
388 final int countY = mCountY;
389
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700390 final float MAX_ALPHA = 0.4f;
391 final int MAX_VISIBLE_DISTANCE = 600;
392 final float DISTANCE_MULTIPLIER = 0.002f;
393
394 final Drawable d = mCrosshairsDrawable;
395 final int width = d.getIntrinsicWidth();
396 final int height = d.getIntrinsicHeight();
397
Winson Chung4b825dcd2011-06-19 12:41:22 -0700398 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700399 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700400 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700401 for (int row = 0; row <= countY; row++) {
402 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
403 float dist = mTmpPointF.length();
404 // Crosshairs further from the drag point are more faint
405 float alpha = Math.min(MAX_ALPHA,
406 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
407 if (alpha > 0.0f) {
408 d.setBounds(x, y, x + width, y + height);
409 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
410 d.draw(canvas);
411 }
412 y += mCellHeight + mHeightGap;
413 }
414 x += mCellWidth + mWidthGap;
415 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700416 }
Winson Chung150fbab2010-09-29 17:14:26 -0700417
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700418 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700419 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700420 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700421 if (alpha > 0) {
422 final Point p = mDragOutlines[i];
423 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700424 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700425 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700426 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700427 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800428
429 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
430 // requires an expanded clip rect (due to the glow's blur radius)
431 if (mPressedOrFocusedIcon != null) {
432 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
433 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
434 if (b != null) {
435 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700436 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
437 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800438 null);
439 }
440 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700441
442 // The folder outer / inner ring image(s)
443 for (int i = 0; i < mFolderOuterRings.size(); i++) {
444 FolderRingAnimator fra = mFolderOuterRings.get(i);
445
446 // Draw outer ring
447 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
448 int width = (int) fra.getOuterRingSize();
449 int height = width;
450 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
451
452 int centerX = mTempLocation[0] + mCellWidth / 2;
453 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
454
455 canvas.save();
456 canvas.translate(centerX - width / 2, centerY - height / 2);
457 d.setBounds(0, 0, width, height);
458 d.draw(canvas);
459 canvas.restore();
460
461 // Draw inner ring
462 d = FolderRingAnimator.sSharedInnerRingDrawable;
463 width = (int) fra.getInnerRingSize();
464 height = width;
465 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
466
467 centerX = mTempLocation[0] + mCellWidth / 2;
468 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
469 canvas.save();
470 canvas.translate(centerX - width / 2, centerY - width / 2);
471 d.setBounds(0, 0, width, height);
472 d.draw(canvas);
473 canvas.restore();
474 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700475
476 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
477 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
478 int width = d.getIntrinsicWidth();
479 int height = d.getIntrinsicHeight();
480
481 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
482 int centerX = mTempLocation[0] + mCellWidth / 2;
483 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
484
485 canvas.save();
486 canvas.translate(centerX - width / 2, centerY - width / 2);
487 d.setBounds(0, 0, width, height);
488 d.draw(canvas);
489 canvas.restore();
490 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700491 }
492
Adam Cohenb5ba0972011-09-07 18:02:31 -0700493 @Override
494 protected void dispatchDraw(Canvas canvas) {
495 super.dispatchDraw(canvas);
496 if (mForegroundAlpha > 0) {
497 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
498 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
499 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
500 mOverScrollForegroundDrawable.draw(canvas);
501 p.setXfermode(null);
502 }
503 }
504
Adam Cohen69ce2e52011-07-03 19:25:21 -0700505 public void showFolderAccept(FolderRingAnimator fra) {
506 mFolderOuterRings.add(fra);
507 }
508
509 public void hideFolderAccept(FolderRingAnimator fra) {
510 if (mFolderOuterRings.contains(fra)) {
511 mFolderOuterRings.remove(fra);
512 }
513 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700514 }
515
Adam Cohenc51934b2011-07-26 21:07:43 -0700516 public void setFolderLeaveBehindCell(int x, int y) {
517 mFolderLeaveBehindCell[0] = x;
518 mFolderLeaveBehindCell[1] = y;
519 invalidate();
520 }
521
522 public void clearFolderLeaveBehind() {
523 mFolderLeaveBehindCell[0] = -1;
524 mFolderLeaveBehindCell[1] = -1;
525 invalidate();
526 }
527
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700528 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700529 public boolean shouldDelayChildPressedState() {
530 return false;
531 }
532
533 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700534 public void cancelLongPress() {
535 super.cancelLongPress();
536
537 // Cancel long press for all children
538 final int count = getChildCount();
539 for (int i = 0; i < count; i++) {
540 final View child = getChildAt(i);
541 child.cancelLongPress();
542 }
543 }
544
Michael Jurkadee05892010-07-27 10:01:56 -0700545 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
546 mInterceptTouchListener = listener;
547 }
548
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700550 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 }
552
553 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700554 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 }
556
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700557 public boolean addViewToCellLayout(
558 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700559 final LayoutParams lp = params;
560
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 // Generate an id for each view, this assumes we have at most 256x256 cells
562 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700563 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700564 // If the horizontal or vertical span is set to -1, it is taken to
565 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700566 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
567 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568
Winson Chungaafa03c2010-06-11 17:34:16 -0700569 child.setId(childId);
570
Michael Jurka8c920dd2011-01-20 14:16:56 -0800571 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700572
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700573 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700574
Winson Chungaafa03c2010-06-11 17:34:16 -0700575 return true;
576 }
577 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700579
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700581 public void removeAllViews() {
582 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800583 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700584 }
585
586 @Override
587 public void removeAllViewsInLayout() {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700588 if (mChildren.getChildCount() > 0) {
589 clearOccupiedCells();
590 mChildren.removeAllViewsInLayout();
591 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700592 }
593
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700594 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800595 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700596 }
597
Michael Jurka0280c3b2010-09-17 15:00:07 -0700598 @Override
599 public void removeView(View view) {
600 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800601 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700602 }
603
604 @Override
605 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800606 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
607 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700608 }
609
610 @Override
611 public void removeViewInLayout(View view) {
612 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800613 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700614 }
615
616 @Override
617 public void removeViews(int start, int count) {
618 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800619 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700620 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800621 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700622 }
623
624 @Override
625 public void removeViewsInLayout(int start, int count) {
626 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800627 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700628 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800629 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700630 }
631
Michael Jurka8c920dd2011-01-20 14:16:56 -0800632 public void drawChildren(Canvas canvas) {
633 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 }
635
Michael Jurkaabded662011-03-04 12:06:57 -0800636 void buildChildrenLayer() {
637 mChildren.buildLayer();
638 }
639
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 @Override
641 protected void onAttachedToWindow() {
642 super.onAttachedToWindow();
643 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
644 }
645
Michael Jurkaaf442092010-06-10 17:01:57 -0700646 public void setTagToCellInfoForPoint(int touchX, int touchY) {
647 final CellInfo cellInfo = mCellInfo;
648 final Rect frame = mRect;
649 final int x = touchX + mScrollX;
650 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800651 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700652
653 boolean found = false;
654 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800655 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800656 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700657
Adam Cohen1b607ed2011-03-03 17:26:50 -0800658 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
659 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700660 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700661
662 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
663 // offset that by this CellLayout's padding to test an (x,y) point that is relative
664 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700665 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700666
Michael Jurkaaf442092010-06-10 17:01:57 -0700667 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700668 cellInfo.cell = child;
669 cellInfo.cellX = lp.cellX;
670 cellInfo.cellY = lp.cellY;
671 cellInfo.spanX = lp.cellHSpan;
672 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700673 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700674 break;
675 }
676 }
677 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700678
Michael Jurkad771c962011-08-09 15:00:48 -0700679 mLastDownOnOccupiedCell = found;
680
Michael Jurkaaf442092010-06-10 17:01:57 -0700681 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700682 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700683 pointToCellExact(x, y, cellXY);
684
Michael Jurkaaf442092010-06-10 17:01:57 -0700685 cellInfo.cell = null;
686 cellInfo.cellX = cellXY[0];
687 cellInfo.cellY = cellXY[1];
688 cellInfo.spanX = 1;
689 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700690 }
691 setTag(cellInfo);
692 }
693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 @Override
695 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700696 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
697 // even in the case where we return early. Not clearing here was causing bugs whereby on
698 // long-press we'd end up picking up an item from a previous drag operation.
699 final int action = ev.getAction();
700
701 if (action == MotionEvent.ACTION_DOWN) {
702 clearTagCellInfo();
703 }
704
Michael Jurkadee05892010-07-27 10:01:56 -0700705 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
706 return true;
707 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708
709 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700710 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 return false;
713 }
714
Adam Cohenc1997fd2011-08-15 18:26:39 -0700715 private void clearTagCellInfo() {
716 final CellInfo cellInfo = mCellInfo;
717 cellInfo.cell = null;
718 cellInfo.cellX = -1;
719 cellInfo.cellY = -1;
720 cellInfo.spanX = 0;
721 cellInfo.spanY = 0;
722 setTag(cellInfo);
723 }
724
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700726 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 }
728
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700729 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700730 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 * @param x X coordinate of the point
732 * @param y Y coordinate of the point
733 * @param result Array of 2 ints to hold the x and y coordinate of the cell
734 */
735 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700736 final int hStartPadding = getPaddingLeft();
737 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738
739 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
740 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
741
Adam Cohend22015c2010-07-26 22:02:18 -0700742 final int xAxis = mCountX;
743 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744
745 if (result[0] < 0) result[0] = 0;
746 if (result[0] >= xAxis) result[0] = xAxis - 1;
747 if (result[1] < 0) result[1] = 0;
748 if (result[1] >= yAxis) result[1] = yAxis - 1;
749 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700750
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 /**
752 * Given a point, return the cell that most closely encloses that point
753 * @param x X coordinate of the point
754 * @param y Y coordinate of the point
755 * @param result Array of 2 ints to hold the x and y coordinate of the cell
756 */
757 void pointToCellRounded(int x, int y, int[] result) {
758 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
759 }
760
761 /**
762 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700763 *
764 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700766 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 * @param result Array of 2 ints to hold the x and y coordinate of the point
768 */
769 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700770 final int hStartPadding = getPaddingLeft();
771 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772
773 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
774 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
775 }
776
Adam Cohene3e27a82011-04-15 12:07:39 -0700777 /**
778 * Given a cell coordinate, return the point that represents the upper left corner of that cell
779 *
780 * @param cellX X coordinate of the cell
781 * @param cellY Y coordinate of the cell
782 *
783 * @param result Array of 2 ints to hold the x and y coordinate of the point
784 */
785 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700786 final int hStartPadding = getPaddingLeft();
787 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700788
789 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
790 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
791 }
792
Romain Guy84f296c2009-11-04 15:00:44 -0800793 int getCellWidth() {
794 return mCellWidth;
795 }
796
797 int getCellHeight() {
798 return mCellHeight;
799 }
800
Adam Cohend4844c32011-02-18 19:25:06 -0800801 int getWidthGap() {
802 return mWidthGap;
803 }
804
805 int getHeightGap() {
806 return mHeightGap;
807 }
808
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700809 Rect getContentRect(Rect r) {
810 if (r == null) {
811 r = new Rect();
812 }
813 int left = getPaddingLeft();
814 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700815 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
816 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700817 r.set(left, top, right, bottom);
818 return r;
819 }
820
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 @Override
822 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
823 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700824
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700826 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
827
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
829 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700830
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
832 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
833 }
834
Adam Cohend22015c2010-07-26 22:02:18 -0700835 int numWidthGaps = mCountX - 1;
836 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837
Adam Cohen234c4cd2011-07-17 21:03:04 -0700838 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700839 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
840 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
841 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
842 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
843 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
844 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700845 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700846 } else {
847 mWidthGap = mOriginalWidthGap;
848 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700849 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700850
Michael Jurka8c920dd2011-01-20 14:16:56 -0800851 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
852 int newWidth = widthSpecSize;
853 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700854 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700855 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700856 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700857 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700858 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700859 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700860 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800861
862 int count = getChildCount();
863 for (int i = 0; i < count; i++) {
864 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700865 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
866 mPaddingRight, MeasureSpec.EXACTLY);
867 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
868 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800869 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
870 }
871 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 }
873
874 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700875 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800878 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700879 child.layout(mPaddingLeft, mPaddingTop,
880 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 }
882 }
883
884 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700885 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
886 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700887 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700888 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
889 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -0700890 }
891
892 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800894 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
896
897 @Override
898 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800899 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 }
901
Michael Jurka5f1c5092010-09-03 14:15:02 -0700902 public float getBackgroundAlpha() {
903 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700904 }
905
Michael Jurka742574b2011-02-02 23:51:01 -0800906 public void setFastBackgroundAlpha(float alpha) {
907 mBackgroundAlpha = alpha;
908 }
909
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700910 public void setBackgroundAlphaMultiplier(float multiplier) {
911 mBackgroundAlphaMultiplier = multiplier;
912 }
913
Adam Cohenddb82192010-11-10 16:32:54 -0800914 public float getBackgroundAlphaMultiplier() {
915 return mBackgroundAlphaMultiplier;
916 }
917
Michael Jurka5f1c5092010-09-03 14:15:02 -0700918 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800919 if (mBackgroundAlpha != alpha) {
920 mBackgroundAlpha = alpha;
921 invalidate();
922 }
Michael Jurkadee05892010-07-27 10:01:56 -0700923 }
924
Michael Jurka5f1c5092010-09-03 14:15:02 -0700925 // Need to return true to let the view system know we know how to handle alpha-- this is
926 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
927 // versions
928 @Override
929 protected boolean onSetAlpha(int alpha) {
930 return true;
931 }
932
Michael Jurkaafaa0502011-12-13 18:22:50 -0800933 @Override
Michael Jurka5f1c5092010-09-03 14:15:02 -0700934 public void setAlpha(float alpha) {
935 setChildrenAlpha(alpha);
936 super.setAlpha(alpha);
937 }
938
Michael Jurka742574b2011-02-02 23:51:01 -0800939 public void setFastAlpha(float alpha) {
940 setFastChildrenAlpha(alpha);
941 super.setFastAlpha(alpha);
942 }
943
Michael Jurkadee05892010-07-27 10:01:56 -0700944 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700945 final int childCount = getChildCount();
946 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700947 getChildAt(i).setAlpha(alpha);
948 }
949 }
950
Michael Jurka742574b2011-02-02 23:51:01 -0800951 private void setFastChildrenAlpha(float alpha) {
952 final int childCount = getChildCount();
953 for (int i = 0; i < childCount; i++) {
954 getChildAt(i).setFastAlpha(alpha);
955 }
956 }
957
Patrick Dubroy440c3602010-07-13 17:50:32 -0700958 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800959 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700960 }
961
Adam Cohen76fc0852011-06-17 13:26:23 -0700962 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
963 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700964 CellLayoutChildren clc = getChildrenLayout();
965 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
966 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
967 final ItemInfo info = (ItemInfo) child.getTag();
968
969 // We cancel any existing animations
970 if (mReorderAnimators.containsKey(lp)) {
971 mReorderAnimators.get(lp).cancel();
972 mReorderAnimators.remove(lp);
973 }
974
975 int oldX = lp.x;
976 int oldY = lp.y;
977 mOccupied[lp.cellX][lp.cellY] = false;
978 mOccupied[cellX][cellY] = true;
979
980 lp.isLockedToGrid = true;
981 lp.cellX = info.cellX = cellX;
982 lp.cellY = info.cellY = cellY;
983 clc.setupLp(lp);
984 lp.isLockedToGrid = false;
985 int newX = lp.x;
986 int newY = lp.y;
987
Adam Cohen76fc0852011-06-17 13:26:23 -0700988 lp.x = oldX;
989 lp.y = oldY;
990 child.requestLayout();
991
Adam Cohenbfbfd262011-06-13 16:55:12 -0700992 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
993 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
994 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
995 oa.setDuration(duration);
996 mReorderAnimators.put(lp, oa);
997 oa.addUpdateListener(new AnimatorUpdateListener() {
998 public void onAnimationUpdate(ValueAnimator animation) {
999 child.requestLayout();
1000 }
1001 });
1002 oa.addListener(new AnimatorListenerAdapter() {
1003 boolean cancelled = false;
1004 public void onAnimationEnd(Animator animation) {
1005 // If the animation was cancelled, it means that another animation
1006 // has interrupted this one, and we don't want to lock the item into
1007 // place just yet.
1008 if (!cancelled) {
1009 lp.isLockedToGrid = true;
1010 }
1011 if (mReorderAnimators.containsKey(lp)) {
1012 mReorderAnimators.remove(lp);
1013 }
1014 }
1015 public void onAnimationCancel(Animator animation) {
1016 cancelled = true;
1017 }
1018 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001019 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001020 oa.start();
1021 return true;
1022 }
1023 return false;
1024 }
1025
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001026 /**
1027 * Estimate where the top left cell of the dragged item will land if it is dropped.
1028 *
1029 * @param originX The X value of the top left corner of the item
1030 * @param originY The Y value of the top left corner of the item
1031 * @param spanX The number of horizontal cells that the item spans
1032 * @param spanY The number of vertical cells that the item spans
1033 * @param result The estimated drop cell X and Y.
1034 */
1035 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001036 final int countX = mCountX;
1037 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001038
Michael Jurkaa63c4522010-08-19 13:52:27 -07001039 // pointToCellRounded takes the top left of a cell but will pad that with
1040 // cellWidth/2 and cellHeight/2 when finding the matching cell
1041 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001042
1043 // If the item isn't fully on this screen, snap to the edges
1044 int rightOverhang = result[0] + spanX - countX;
1045 if (rightOverhang > 0) {
1046 result[0] -= rightOverhang; // Snap to right
1047 }
1048 result[0] = Math.max(0, result[0]); // Snap to left
1049 int bottomOverhang = result[1] + spanY - countY;
1050 if (bottomOverhang > 0) {
1051 result[1] -= bottomOverhang; // Snap to bottom
1052 }
1053 result[1] = Math.max(0, result[1]); // Snap to top
1054 }
1055
Winson Chungb8c69f32011-10-19 21:36:08 -07001056 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY,
1057 int spanX, int spanY, Point dragOffset, Rect dragRegion) {
Joe Onorato4be866d2010-10-10 11:26:02 -07001058
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001059 final int oldDragCellX = mDragCell[0];
1060 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001061 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chungb8c69f32011-10-19 21:36:08 -07001062 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001063 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1064 } else {
1065 mDragCenter.set(originX, originY);
1066 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001067
Adam Cohen2801caf2011-05-13 20:57:39 -07001068 if (dragOutline == null && v == null) {
1069 if (mCrosshairsDrawable != null) {
1070 invalidate();
1071 }
1072 return;
1073 }
1074
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001075 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001076 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001077 final int[] topLeft = mTmpPoint;
1078 cellToPoint(nearest[0], nearest[1], topLeft);
1079
Joe Onorato4be866d2010-10-10 11:26:02 -07001080 int left = topLeft[0];
1081 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001082
Winson Chungb8c69f32011-10-19 21:36:08 -07001083 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001084 // When drawing the drag outline, it did not account for margin offsets
1085 // added by the view's parent.
1086 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1087 left += lp.leftMargin;
1088 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001089
Adam Cohen99e8b402011-03-25 19:23:43 -07001090 // Offsets due to the size difference between the View and the dragOutline.
1091 // There is a size difference to account for the outer blur, which may lie
1092 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001093 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001094 // We center about the x axis
1095 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1096 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001097 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001098 if (dragOffset != null && dragRegion != null) {
1099 // Center the drag region *horizontally* in the cell and apply a drag
1100 // outline offset
1101 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1102 - dragRegion.width()) / 2;
1103 top += dragOffset.y;
1104 } else {
1105 // Center the drag outline in the cell
1106 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1107 - dragOutline.getWidth()) / 2;
1108 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1109 - dragOutline.getHeight()) / 2;
1110 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001111 }
Winson Chung150fbab2010-09-29 17:14:26 -07001112
Joe Onorato4be866d2010-10-10 11:26:02 -07001113 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001114 mDragOutlineAnims[oldIndex].animateOut();
1115 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001116
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001117 mDragOutlines[mDragOutlineCurrent].set(left, top);
1118 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1119 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001120 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001121
1122 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1123 if (mCrosshairsDrawable != null) {
1124 invalidate();
1125 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001126 }
1127
Adam Cohene0310962011-04-18 16:15:31 -07001128 public void clearDragOutlines() {
1129 final int oldIndex = mDragOutlineCurrent;
1130 mDragOutlineAnims[oldIndex].animateOut();
1131 mDragCell[0] = -1;
1132 mDragCell[1] = -1;
1133 }
1134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001136 * Find a vacant area that will fit the given bounds nearest the requested
1137 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001138 *
Romain Guy51afc022009-05-04 18:03:43 -07001139 * @param pixelX The X location at which you want to search for a vacant area.
1140 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001141 * @param spanX Horizontal span of the object.
1142 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001143 * @param result Array in which to place the result, or null (in which case a new array will
1144 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001145 * @return The X, Y cell of a vacant area that can contain this object,
1146 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001148 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001149 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1150 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001151 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001152
Michael Jurka6a1435d2010-09-27 17:35:12 -07001153 /**
1154 * Find a vacant area that will fit the given bounds nearest the requested
1155 * cell location. Uses Euclidean distance to score multiple vacant areas.
1156 *
1157 * @param pixelX The X location at which you want to search for a vacant area.
1158 * @param pixelY The Y location at which you want to search for a vacant area.
1159 * @param spanX Horizontal span of the object.
1160 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001161 * @param ignoreOccupied If true, the result can be an occupied cell
1162 * @param result Array in which to place the result, or null (in which case a new array will
1163 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001164 * @return The X, Y cell of a vacant area that can contain this object,
1165 * nearest the requested location.
1166 */
Adam Cohendf035382011-04-11 17:22:04 -07001167 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1168 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001169 // mark space take by ignoreView as available (method checks if ignoreView is null)
1170 markCellsAsUnoccupiedForView(ignoreView);
1171
Adam Cohene3e27a82011-04-15 12:07:39 -07001172 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1173 // to the center of the item, but we are searching based on the top-left cell, so
1174 // we translate the point over to correspond to the top-left.
1175 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1176 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1177
Jeff Sharkey70864282009-04-07 21:08:40 -07001178 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001179 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001180 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001181
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001182 final int countX = mCountX;
1183 final int countY = mCountY;
1184 final boolean[][] occupied = mOccupied;
1185
Winson Chungbbc60d82010-11-11 16:34:41 -08001186 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001187 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001188 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001189 if (ignoreOccupied) {
1190 for (int i = 0; i < spanX; i++) {
1191 for (int j = 0; j < spanY; j++) {
1192 if (occupied[x + i][y + j]) {
1193 // small optimization: we can skip to after the column we
1194 // just found an occupied cell
1195 x += i;
1196 continue inner;
1197 }
Michael Jurkac28de512010-08-13 11:27:44 -07001198 }
1199 }
1200 }
Winson Chung0be025d2011-05-23 17:45:09 -07001201 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001202 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203
Michael Jurkac28de512010-08-13 11:27:44 -07001204 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1205 + Math.pow(cellXY[1] - pixelY, 2));
1206 if (distance <= bestDistance) {
1207 bestDistance = distance;
1208 bestXY[0] = x;
1209 bestXY[1] = y;
1210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 }
1212 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001213 // re-mark space taken by ignoreView as occupied
1214 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215
Adam Cohenc0dcf592011-06-01 15:30:43 -07001216 // Return -1, -1 if no suitable location found
1217 if (bestDistance == Double.MAX_VALUE) {
1218 bestXY[0] = -1;
1219 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001220 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001221 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001223
Adam Cohendf035382011-04-11 17:22:04 -07001224 /**
1225 * Find a vacant area that will fit the given bounds nearest the requested
1226 * cell location. Uses Euclidean distance to score multiple vacant areas.
1227 *
1228 * @param pixelX The X location at which you want to search for a vacant area.
1229 * @param pixelY The Y location at which you want to search for a vacant area.
1230 * @param spanX Horizontal span of the object.
1231 * @param spanY Vertical span of the object.
1232 * @param ignoreView Considers space occupied by this view as unoccupied
1233 * @param result Previously returned value to possibly recycle.
1234 * @return The X, Y cell of a vacant area that can contain this object,
1235 * nearest the requested location.
1236 */
1237 int[] findNearestVacantArea(
1238 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1239 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1240 }
1241
1242 /**
1243 * Find a starting cell position that will fit the given bounds nearest the requested
1244 * cell location. Uses Euclidean distance to score multiple vacant areas.
1245 *
1246 * @param pixelX The X location at which you want to search for a vacant area.
1247 * @param pixelY The Y location at which you want to search for a vacant area.
1248 * @param spanX Horizontal span of the object.
1249 * @param spanY Vertical span of the object.
1250 * @param ignoreView Considers space occupied by this view as unoccupied
1251 * @param result Previously returned value to possibly recycle.
1252 * @return The X, Y cell of a vacant area that can contain this object,
1253 * nearest the requested location.
1254 */
1255 int[] findNearestArea(
1256 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1257 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1258 }
1259
Michael Jurka0280c3b2010-09-17 15:00:07 -07001260 boolean existsEmptyCell() {
1261 return findCellForSpan(null, 1, 1);
1262 }
1263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001265 * Finds the upper-left coordinate of the first rectangle in the grid that can
1266 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1267 * then this method will only return coordinates for rectangles that contain the cell
1268 * (intersectX, intersectY)
1269 *
1270 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1271 * can be found.
1272 * @param spanX The horizontal span of the cell we want to find.
1273 * @param spanY The vertical span of the cell we want to find.
1274 *
1275 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001276 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001277 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1278 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1279 }
1280
1281 /**
1282 * Like above, but ignores any cells occupied by the item "ignoreView"
1283 *
1284 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1285 * can be found.
1286 * @param spanX The horizontal span of the cell we want to find.
1287 * @param spanY The vertical span of the cell we want to find.
1288 * @param ignoreView The home screen item we should treat as not occupying any space
1289 * @return
1290 */
1291 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1292 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1293 }
1294
1295 /**
1296 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1297 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1298 *
1299 * @param spanX The horizontal span of the cell we want to find.
1300 * @param spanY The vertical span of the cell we want to find.
1301 * @param ignoreView The home screen item we should treat as not occupying any space
1302 * @param intersectX The X coordinate of the cell that we should try to overlap
1303 * @param intersectX The Y coordinate of the cell that we should try to overlap
1304 *
1305 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1306 */
1307 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1308 int intersectX, int intersectY) {
1309 return findCellForSpanThatIntersectsIgnoring(
1310 cellXY, spanX, spanY, intersectX, intersectY, null);
1311 }
1312
1313 /**
1314 * The superset of the above two methods
1315 */
1316 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1317 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001318 // mark space take by ignoreView as available (method checks if ignoreView is null)
1319 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001320
Michael Jurka28750fb2010-09-24 17:43:49 -07001321 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001322 while (true) {
1323 int startX = 0;
1324 if (intersectX >= 0) {
1325 startX = Math.max(startX, intersectX - (spanX - 1));
1326 }
1327 int endX = mCountX - (spanX - 1);
1328 if (intersectX >= 0) {
1329 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1330 }
1331 int startY = 0;
1332 if (intersectY >= 0) {
1333 startY = Math.max(startY, intersectY - (spanY - 1));
1334 }
1335 int endY = mCountY - (spanY - 1);
1336 if (intersectY >= 0) {
1337 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1338 }
1339
Winson Chungbbc60d82010-11-11 16:34:41 -08001340 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001341 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001342 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001343 for (int i = 0; i < spanX; i++) {
1344 for (int j = 0; j < spanY; j++) {
1345 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001346 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001347 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001348 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001349 continue inner;
1350 }
1351 }
1352 }
1353 if (cellXY != null) {
1354 cellXY[0] = x;
1355 cellXY[1] = y;
1356 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001357 foundCell = true;
1358 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001359 }
1360 }
1361 if (intersectX == -1 && intersectY == -1) {
1362 break;
1363 } else {
1364 // if we failed to find anything, try again but without any requirements of
1365 // intersecting
1366 intersectX = -1;
1367 intersectY = -1;
1368 continue;
1369 }
1370 }
1371
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001372 // re-mark space taken by ignoreView as occupied
1373 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001374 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001375 }
1376
1377 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001378 * A drag event has begun over this layout.
1379 * It may have begun over this layout (in which case onDragChild is called first),
1380 * or it may have begun on another layout.
1381 */
1382 void onDragEnter() {
1383 if (!mDragging) {
1384 // Fade in the drag indicators
1385 if (mCrosshairsAnimator != null) {
1386 mCrosshairsAnimator.animateIn();
1387 }
1388 }
1389 mDragging = true;
1390 }
1391
1392 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001393 * Called when drag has left this CellLayout or has been completed (successfully or not)
1394 */
1395 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001396 // This can actually be called when we aren't in a drag, e.g. when adding a new
1397 // item to this layout via the customize drawer.
1398 // Guard against that case.
1399 if (mDragging) {
1400 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001401
Joe Onorato4be866d2010-10-10 11:26:02 -07001402 // Fade out the drag indicators
1403 if (mCrosshairsAnimator != null) {
1404 mCrosshairsAnimator.animateOut();
1405 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001406 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001407
1408 // Invalidate the drag data
1409 mDragCell[0] = -1;
1410 mDragCell[1] = -1;
1411 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1412 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1413
Michael Jurka33945b22010-12-21 18:19:38 -08001414 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001415 }
1416
1417 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001418 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001419 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001420 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 *
1422 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001424 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001425 if (child != null) {
1426 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001427 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001428 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 }
1431
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001434 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 * @param cellX X coordinate of upper left corner expressed as a cell position
1436 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001437 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001439 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001441 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 final int cellWidth = mCellWidth;
1443 final int cellHeight = mCellHeight;
1444 final int widthGap = mWidthGap;
1445 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001446
Winson Chung4b825dcd2011-06-19 12:41:22 -07001447 final int hStartPadding = getPaddingLeft();
1448 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001449
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1451 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1452
1453 int x = hStartPadding + cellX * (cellWidth + widthGap);
1454 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001455
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001456 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001458
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001460 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001462 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 * @param width Width in pixels
1464 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001465 * @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 -08001466 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001467 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001468 return rectToCell(getResources(), width, height, result);
1469 }
1470
1471 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 // Always assume we're working with the smallest span to make sure we
1473 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001474 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1475 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07001479 int spanX = (int) Math.ceil(width / (float) smallerSize);
1480 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04001481
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001482 if (result == null) {
1483 return new int[] { spanX, spanY };
1484 }
1485 result[0] = spanX;
1486 result[1] = spanY;
1487 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001490 public int[] cellSpansToSize(int hSpans, int vSpans) {
1491 int[] size = new int[2];
1492 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1493 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1494 return size;
1495 }
1496
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001498 * Calculate the grid spans needed to fit given item
1499 */
1500 public void calculateSpans(ItemInfo info) {
1501 final int minWidth;
1502 final int minHeight;
1503
1504 if (info instanceof LauncherAppWidgetInfo) {
1505 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1506 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1507 } else if (info instanceof PendingAddWidgetInfo) {
1508 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1509 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1510 } else {
1511 // It's not a widget, so it must be 1x1
1512 info.spanX = info.spanY = 1;
1513 return;
1514 }
1515 int[] spans = rectToCell(minWidth, minHeight, null);
1516 info.spanX = spans[0];
1517 info.spanY = spans[1];
1518 }
1519
1520 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 * Find the first vacant cell, if there is one.
1522 *
1523 * @param vacant Holds the x and y coordinate of the vacant cell
1524 * @param spanX Horizontal cell span.
1525 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001526 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 * @return True if a vacant cell was found
1528 */
1529 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530
Michael Jurka0280c3b2010-09-17 15:00:07 -07001531 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 }
1533
1534 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1535 int xCount, int yCount, boolean[][] occupied) {
1536
Adam Cohen2801caf2011-05-13 20:57:39 -07001537 for (int y = 0; y < yCount; y++) {
1538 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 boolean available = !occupied[x][y];
1540out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1541 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1542 available = available && !occupied[i][j];
1543 if (!available) break out;
1544 }
1545 }
1546
1547 if (available) {
1548 vacant[0] = x;
1549 vacant[1] = y;
1550 return true;
1551 }
1552 }
1553 }
1554
1555 return false;
1556 }
1557
Michael Jurka0280c3b2010-09-17 15:00:07 -07001558 private void clearOccupiedCells() {
1559 for (int x = 0; x < mCountX; x++) {
1560 for (int y = 0; y < mCountY; y++) {
1561 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
1563 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001564 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565
Adam Cohen1b607ed2011-03-03 17:26:50 -08001566 /**
1567 * Given a view, determines how much that view can be expanded in all directions, in terms of
1568 * whether or not there are other items occupying adjacent cells. Used by the
1569 * AppWidgetResizeFrame to determine how the widget can be resized.
1570 */
Adam Cohend4844c32011-02-18 19:25:06 -08001571 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001572 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001573 boolean flag;
1574
Adam Cohen1b607ed2011-03-03 17:26:50 -08001575 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001576 for (int x = lp.cellX - 1; x >= 0; x--) {
1577 flag = false;
1578 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1579 if (mOccupied[x][y]) flag = true;
1580 }
1581 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001582 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001583 }
1584
Adam Cohen1b607ed2011-03-03 17:26:50 -08001585 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001586 for (int y = lp.cellY - 1; y >= 0; y--) {
1587 flag = false;
1588 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1589 if (mOccupied[x][y]) flag = true;
1590 }
1591 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001592 expandability[AppWidgetResizeFrame.TOP]++;
1593 }
Adam Cohend4844c32011-02-18 19:25:06 -08001594
Adam Cohen1b607ed2011-03-03 17:26:50 -08001595 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001596 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1597 flag = false;
1598 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1599 if (mOccupied[x][y]) flag = true;
1600 }
1601 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001602 expandability[AppWidgetResizeFrame.RIGHT]++;
1603 }
Adam Cohend4844c32011-02-18 19:25:06 -08001604
Adam Cohen1b607ed2011-03-03 17:26:50 -08001605 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001606 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1607 flag = false;
1608 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1609 if (mOccupied[x][y]) flag = true;
1610 }
1611 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001612 expandability[AppWidgetResizeFrame.BOTTOM]++;
1613 }
Adam Cohend4844c32011-02-18 19:25:06 -08001614 }
1615
Michael Jurka0280c3b2010-09-17 15:00:07 -07001616 public void onMove(View view, int newCellX, int newCellY) {
1617 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1618 markCellsAsUnoccupiedForView(view);
1619 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1620 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621
Adam Cohend4844c32011-02-18 19:25:06 -08001622 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001623 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001624 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1625 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1626 }
1627
Adam Cohend4844c32011-02-18 19:25:06 -08001628 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001629 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001630 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1631 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1632 }
1633
1634 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1635 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1636 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1637 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 }
1639 }
1640 }
1641
Adam Cohen2801caf2011-05-13 20:57:39 -07001642 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001643 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001644 (Math.max((mCountX - 1), 0) * mWidthGap);
1645 }
1646
1647 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001648 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001649 (Math.max((mCountY - 1), 0) * mHeightGap);
1650 }
1651
Michael Jurka66d72172011-04-12 16:29:25 -07001652 public boolean isOccupied(int x, int y) {
1653 if (x < mCountX && y < mCountY) {
1654 return mOccupied[x][y];
1655 } else {
1656 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1657 }
1658 }
1659
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 @Override
1661 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1662 return new CellLayout.LayoutParams(getContext(), attrs);
1663 }
1664
1665 @Override
1666 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1667 return p instanceof CellLayout.LayoutParams;
1668 }
1669
1670 @Override
1671 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1672 return new CellLayout.LayoutParams(p);
1673 }
1674
Winson Chungaafa03c2010-06-11 17:34:16 -07001675 public static class CellLayoutAnimationController extends LayoutAnimationController {
1676 public CellLayoutAnimationController(Animation animation, float delay) {
1677 super(animation, delay);
1678 }
1679
1680 @Override
1681 protected long getDelayForView(View view) {
1682 return (int) (Math.random() * 150);
1683 }
1684 }
1685
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1687 /**
1688 * Horizontal location of the item in the grid.
1689 */
1690 @ViewDebug.ExportedProperty
1691 public int cellX;
1692
1693 /**
1694 * Vertical location of the item in the grid.
1695 */
1696 @ViewDebug.ExportedProperty
1697 public int cellY;
1698
1699 /**
1700 * Number of cells spanned horizontally by the item.
1701 */
1702 @ViewDebug.ExportedProperty
1703 public int cellHSpan;
1704
1705 /**
1706 * Number of cells spanned vertically by the item.
1707 */
1708 @ViewDebug.ExportedProperty
1709 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001710
Adam Cohen1b607ed2011-03-03 17:26:50 -08001711 /**
1712 * Indicates whether the item will set its x, y, width and height parameters freely,
1713 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1714 */
Adam Cohend4844c32011-02-18 19:25:06 -08001715 public boolean isLockedToGrid = true;
1716
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 // X coordinate of the view in the layout.
1718 @ViewDebug.ExportedProperty
1719 int x;
1720 // Y coordinate of the view in the layout.
1721 @ViewDebug.ExportedProperty
1722 int y;
1723
Romain Guy84f296c2009-11-04 15:00:44 -08001724 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001725
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 public LayoutParams(Context c, AttributeSet attrs) {
1727 super(c, attrs);
1728 cellHSpan = 1;
1729 cellVSpan = 1;
1730 }
1731
1732 public LayoutParams(ViewGroup.LayoutParams source) {
1733 super(source);
1734 cellHSpan = 1;
1735 cellVSpan = 1;
1736 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001737
1738 public LayoutParams(LayoutParams source) {
1739 super(source);
1740 this.cellX = source.cellX;
1741 this.cellY = source.cellY;
1742 this.cellHSpan = source.cellHSpan;
1743 this.cellVSpan = source.cellVSpan;
1744 }
1745
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001747 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 this.cellX = cellX;
1749 this.cellY = cellY;
1750 this.cellHSpan = cellHSpan;
1751 this.cellVSpan = cellVSpan;
1752 }
1753
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001754 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001755 if (isLockedToGrid) {
1756 final int myCellHSpan = cellHSpan;
1757 final int myCellVSpan = cellVSpan;
1758 final int myCellX = cellX;
1759 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001760
Adam Cohend4844c32011-02-18 19:25:06 -08001761 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1762 leftMargin - rightMargin;
1763 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1764 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001765 x = myCellX * (cellWidth + widthGap) + leftMargin;
1766 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001767 }
1768 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001769
Winson Chungaafa03c2010-06-11 17:34:16 -07001770 public String toString() {
1771 return "(" + this.cellX + ", " + this.cellY + ")";
1772 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001773
1774 public void setWidth(int width) {
1775 this.width = width;
1776 }
1777
1778 public int getWidth() {
1779 return width;
1780 }
1781
1782 public void setHeight(int height) {
1783 this.height = height;
1784 }
1785
1786 public int getHeight() {
1787 return height;
1788 }
1789
1790 public void setX(int x) {
1791 this.x = x;
1792 }
1793
1794 public int getX() {
1795 return x;
1796 }
1797
1798 public void setY(int y) {
1799 this.y = y;
1800 }
1801
1802 public int getY() {
1803 return y;
1804 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 }
1806
Michael Jurka0280c3b2010-09-17 15:00:07 -07001807 // This class stores info for two purposes:
1808 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1809 // its spanX, spanY, and the screen it is on
1810 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1811 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1812 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001813 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001815 int cellX = -1;
1816 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 int spanX;
1818 int spanY;
1819 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001820 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 @Override
1823 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001824 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1825 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 }
1827 }
Michael Jurkad771c962011-08-09 15:00:48 -07001828
1829 public boolean lastDownOnOccupiedCell() {
1830 return mLastDownOnOccupiedCell;
1831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832}