blob: d9d048788977c1fd402b47c3df1ba1e43a9edfa9 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Michael Jurka18014792010-10-14 09:01:34 -070021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070023import android.animation.PropertyValuesHolder;
Chet Haase00397b12010-10-07 11:13:10 -070024import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070025import android.animation.ValueAnimator;
26import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040028import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070031import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070033import android.graphics.Point;
34import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Rect;
36import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070037import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070038import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.MotionEvent;
42import android.view.View;
43import android.view.ViewDebug;
44import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070046import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Adam Cohen66396872011-04-15 17:50:36 -070049import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070050import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070051
Adam Cohen69ce2e52011-07-03 19:25:21 -070052import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070054import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055
Michael Jurkabdb5c532011-02-01 15:05:06 -080056public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070057 static final String TAG = "CellLayout";
58
Winson Chung4b825dcd2011-06-19 12:41:22 -070059 private int mOriginalCellWidth;
60 private int mOriginalCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 private int mCellWidth;
62 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070063
Adam Cohend22015c2010-07-26 22:02:18 -070064 private int mCountX;
65 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
Adam Cohen234c4cd2011-07-17 21:03:04 -070067 private int mOriginalWidthGap;
68 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 private int mWidthGap;
70 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070071 private int mMaxGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
73 private final Rect mRect = new Rect();
74 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070075
Patrick Dubroyde7658b2010-09-27 11:15:43 -070076 // These are temporary variables to prevent having to allocate a new object just to
77 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070078 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070079 private final int[] mTmpPoint = new int[2];
80 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070081 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 boolean[][] mOccupied;
84
Michael Jurkadee05892010-07-27 10:01:56 -070085 private OnTouchListener mInterceptTouchListener;
86
Adam Cohen69ce2e52011-07-03 19:25:21 -070087 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070088 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070089
Michael Jurka5f1c5092010-09-03 14:15:02 -070090 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070091 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070092
Michael Jurka33945b22010-12-21 18:19:38 -080093 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080094 private Drawable mActiveBackground;
95 private Drawable mActiveGlowBackground;
96 private Drawable mNormalBackgroundMini;
97 private Drawable mNormalGlowBackgroundMini;
98 private Drawable mActiveBackgroundMini;
99 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -0700100 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -0800101 private Rect mGlowBackgroundRect;
102 private float mGlowBackgroundScale;
103 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700104
Adam Cohendf035382011-04-11 17:22:04 -0700105 private boolean mAcceptsDrops = true;
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;
108 private boolean mIsDragOccuring = false;
109 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700110 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700111
Winson Chung150fbab2010-09-29 17:14:26 -0700112 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700113 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Winson Chung63257c12011-05-05 17:06:13 -0700114 private Point[] mDragOutlines = new Point[4];
Chet Haase472b2812010-10-14 07:02:04 -0700115 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700116 private InterruptibleInOutAnimator[] mDragOutlineAnims =
117 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700118
119 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700120 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700121 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700122
Patrick Dubroy96864c32011-03-10 17:17:23 -0800123 private BubbleTextView mPressedOrFocusedIcon;
124
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700125 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700126 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700127 private float mCrosshairsVisibility = 0.0f;
128
Adam Cohenbfbfd262011-06-13 16:55:12 -0700129 private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new
130 HashMap<CellLayout.LayoutParams, ObjectAnimator>();
131
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700132 // When a drag operation is in progress, holds the nearest cell to the touch point
133 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Joe Onorato4be866d2010-10-10 11:26:02 -0700135 private boolean mDragging = false;
136
Patrick Dubroyce34a972010-10-19 10:34:32 -0700137 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800138 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700139
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 public CellLayout(Context context) {
141 this(context, null);
142 }
143
144 public CellLayout(Context context, AttributeSet attrs) {
145 this(context, attrs, 0);
146 }
147
148 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
149 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700150
151 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
152 // the user where a dragged item will land when dropped.
153 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
156
Winson Chung4b825dcd2011-06-19 12:41:22 -0700157 mOriginalCellWidth =
158 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
159 mOriginalCellHeight =
160 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700161 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
162 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700163 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700164 mCountX = LauncherModel.getCellCountX();
165 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700166 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
168 a.recycle();
169
170 setAlwaysDrawnWithCacheEnabled(false);
171
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700172 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700173
Winson Chung967289b2011-06-30 18:09:30 -0700174 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
175 mActiveBackground = res.getDrawable(R.drawable.homescreen_green_normal_holo);
176 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_green_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800177
Winson Chungb26f3d62011-06-02 10:49:29 -0700178 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
179 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
180 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
181 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
Michael Jurka33945b22010-12-21 18:19:38 -0800182
Winson Chungb26f3d62011-06-02 10:49:29 -0700183 mNormalBackground.setFilterBitmap(true);
184 mActiveBackground.setFilterBitmap(true);
185 mActiveGlowBackground.setFilterBitmap(true);
186 mNormalBackgroundMini.setFilterBitmap(true);
187 mNormalGlowBackgroundMini.setFilterBitmap(true);
188 mActiveBackgroundMini.setFilterBitmap(true);
189 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700190
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700191 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700192
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700193 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700194 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700195
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700196 // Set up the animation for fading the crosshairs in and out
197 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700198 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700199 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700200 public void onAnimationUpdate(ValueAnimator animation) {
201 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700202 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700203 }
204 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700205 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700206
Joe Onorato4be866d2010-10-10 11:26:02 -0700207 for (int i = 0; i < mDragOutlines.length; i++) {
208 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700209 }
210
211 // When dragging things around the home screens, we show a green outline of
212 // where the item will land. The outlines gradually fade out, leaving a trail
213 // behind the drag path.
214 // Set up all the animations that are used to implement this fading.
215 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700216 final float fromAlphaValue = 0;
217 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700218
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700219 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700220
221 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700222 final InterruptibleInOutAnimator anim =
223 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700224 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700225 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700226 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700227 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700228 final Bitmap outline = (Bitmap)anim.getTag();
229
230 // If an animation is started and then stopped very quickly, we can still
231 // get spurious updates we've cleared the tag. Guard against this.
232 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700233 if (false) {
234 Object val = animation.getAnimatedValue();
235 Log.d(TAG, "anim " + thisIndex + " update: " + val +
236 ", isStopped " + anim.isStopped());
237 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700238 // Try to prevent it from continuing to run
239 animation.cancel();
240 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700241 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700242 final int left = mDragOutlines[thisIndex].x;
243 final int top = mDragOutlines[thisIndex].y;
244 CellLayout.this.invalidate(left, top,
245 left + outline.getWidth(), top + outline.getHeight());
246 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700247 }
248 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700249 // The animation holds a reference to the drag outline bitmap as long is it's
250 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700251 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700252 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700253 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700254 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700255 anim.setTag(null);
256 }
257 }
258 });
259 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700260 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700261
Michael Jurka18014792010-10-14 09:01:34 -0700262 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800263 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700264 setHoverScale(1.0f);
265 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800266
Michael Jurka8c920dd2011-01-20 14:16:56 -0800267 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700268 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800269 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700270 }
271
Michael Jurkaf6440da2011-04-05 14:50:34 -0700272 static int widthInPortrait(Resources r, int numCells) {
273 // We use this method from Workspace to figure out how many rows/columns Launcher should
274 // have. We ignore the left/right padding on CellLayout because it turns out in our design
275 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700276 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700277 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
278 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700279
Winson Chung4b825dcd2011-06-19 12:41:22 -0700280 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700281 }
282
Michael Jurkaf6440da2011-04-05 14:50:34 -0700283 static int heightInLandscape(Resources r, int numCells) {
284 // We use this method from Workspace to figure out how many rows/columns Launcher should
285 // have. We ignore the left/right padding on CellLayout because it turns out in our design
286 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700287 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700288 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
289 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700290
Winson Chung4b825dcd2011-06-19 12:41:22 -0700291 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700292 }
293
Adam Cohen2801caf2011-05-13 20:57:39 -0700294 public void enableHardwareLayers() {
295 mChildren.enableHardwareLayers();
296 }
297
298 public void setGridSize(int x, int y) {
299 mCountX = x;
300 mCountY = y;
301 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700302 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700303 }
304
Patrick Dubroy96864c32011-03-10 17:17:23 -0800305 private void invalidateBubbleTextView(BubbleTextView icon) {
306 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700307 invalidate(icon.getLeft() + getPaddingLeft() - padding,
308 icon.getTop() + getPaddingTop() - padding,
309 icon.getRight() + getPaddingLeft() + padding,
310 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800311 }
312
313 void setPressedOrFocusedIcon(BubbleTextView icon) {
314 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
315 // requires an expanded clip rect (due to the glow's blur radius)
316 BubbleTextView oldIcon = mPressedOrFocusedIcon;
317 mPressedOrFocusedIcon = icon;
318 if (oldIcon != null) {
319 invalidateBubbleTextView(oldIcon);
320 }
321 if (mPressedOrFocusedIcon != null) {
322 invalidateBubbleTextView(mPressedOrFocusedIcon);
323 }
324 }
325
Winson Chung6e314082011-01-27 16:46:51 -0800326 public CellLayoutChildren getChildrenLayout() {
327 if (getChildCount() > 0) {
328 return (CellLayoutChildren) getChildAt(0);
329 }
330 return null;
331 }
332
Michael Jurka33945b22010-12-21 18:19:38 -0800333 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
334 if (mIsDefaultDropTarget != isDefaultDropTarget) {
335 mIsDefaultDropTarget = isDefaultDropTarget;
336 invalidate();
337 }
338 }
339
Michael Jurka33945b22010-12-21 18:19:38 -0800340 void setIsDragOccuring(boolean isDragOccuring) {
341 if (mIsDragOccuring != isDragOccuring) {
342 mIsDragOccuring = isDragOccuring;
343 invalidate();
344 }
345 }
346
347 void setIsDragOverlapping(boolean isDragOverlapping) {
348 if (mIsDragOverlapping != isDragOverlapping) {
349 mIsDragOverlapping = isDragOverlapping;
350 invalidate();
351 }
352 }
353
354 boolean getIsDragOverlapping() {
355 return mIsDragOverlapping;
356 }
357
358 private void updateGlowRect() {
359 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700360 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
361 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800362 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700363 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
364 invalidate();
365 }
366
367 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800368 if (scaleFactor != mGlowBackgroundScale) {
369 mGlowBackgroundScale = scaleFactor;
370 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800371 if (getParent() != null) {
372 ((View) getParent()).invalidate();
373 }
Michael Jurka18014792010-10-14 09:01:34 -0700374 }
375 }
376
377 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800378 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700379 }
380
381 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800382 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700383 }
384
385 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800386 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700387 invalidate();
388 }
389
390 void animateDrop() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700391 Resources res = getResources();
392 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
393 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
394 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
395 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
396 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
397 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
Michael Jurka18014792010-10-14 09:01:34 -0700398
Winson Chungb26f3d62011-06-02 10:49:29 -0700399 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
400 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDuration));
Michael Jurka18014792010-10-14 09:01:34 -0700401
Winson Chungb26f3d62011-06-02 10:49:29 -0700402 AnimatorSet bouncer = new AnimatorSet();
403 bouncer.play(scaleUp).before(scaleDown);
404 bouncer.play(scaleUp).with(alphaFadeOut);
405 bouncer.addListener(new AnimatorListenerAdapter() {
406 @Override
407 public void onAnimationStart(Animator animation) {
408 setIsDragOverlapping(true);
409 }
410 @Override
411 public void onAnimationEnd(Animator animation) {
412 setIsDragOverlapping(false);
413 setHoverScale(1.0f);
414 setHoverAlpha(1.0f);
415 }
416 });
417 bouncer.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418 }
419
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700420 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700421 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700422 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
423 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
424 // When we're small, we are either drawn normally or in the "accepts drops" state (during
425 // a drag). However, we also drag the mini hover background *over* one of those two
426 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700427 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700428 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800429 boolean mini = getScaleX() < 0.5f;
430
431 if (mIsDragOverlapping) {
432 // In the mini case, we draw the active_glow bg *over* the active background
433 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
434 } else if (mIsDragOccuring && mAcceptsDrops) {
435 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800436 } else if (mIsDefaultDropTarget && mini) {
437 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700438 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800439 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700440 }
Michael Jurka33945b22010-12-21 18:19:38 -0800441
442 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
443 bg.setBounds(mBackgroundRect);
444 bg.draw(canvas);
445
446 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700447 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800448 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700449 // If the hover background's scale is greater than 1, we'll be drawing outside
450 // the bounds of this CellLayout. Get around that by temporarily increasing the
451 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800452 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700453 Rect clipRect = canvas.getClipBounds();
454 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
455 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
456 canvas.save(Canvas.CLIP_SAVE_FLAG);
457 canvas.clipRect(-marginX, -marginY,
458 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
459 modifiedClipRect = true;
460 }
461
Michael Jurka33945b22010-12-21 18:19:38 -0800462 mActiveGlowBackgroundMini.setAlpha(
463 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
464 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
465 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700466 if (modifiedClipRect) {
467 canvas.restore();
468 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700469 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700470 }
Romain Guya6abce82009-11-10 02:54:41 -0800471
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700472 if (mCrosshairsVisibility > 0.0f) {
473 final int countX = mCountX;
474 final int countY = mCountY;
475
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700476 final float MAX_ALPHA = 0.4f;
477 final int MAX_VISIBLE_DISTANCE = 600;
478 final float DISTANCE_MULTIPLIER = 0.002f;
479
480 final Drawable d = mCrosshairsDrawable;
481 final int width = d.getIntrinsicWidth();
482 final int height = d.getIntrinsicHeight();
483
Winson Chung4b825dcd2011-06-19 12:41:22 -0700484 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700485 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700486 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700487 for (int row = 0; row <= countY; row++) {
488 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
489 float dist = mTmpPointF.length();
490 // Crosshairs further from the drag point are more faint
491 float alpha = Math.min(MAX_ALPHA,
492 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
493 if (alpha > 0.0f) {
494 d.setBounds(x, y, x + width, y + height);
495 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
496 d.draw(canvas);
497 }
498 y += mCellHeight + mHeightGap;
499 }
500 x += mCellWidth + mWidthGap;
501 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700502 }
Winson Chung150fbab2010-09-29 17:14:26 -0700503
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700504 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700505 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700506 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700507 if (alpha > 0) {
508 final Point p = mDragOutlines[i];
509 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700510 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700511 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700512 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700513 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800514
515 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
516 // requires an expanded clip rect (due to the glow's blur radius)
517 if (mPressedOrFocusedIcon != null) {
518 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
519 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
520 if (b != null) {
521 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700522 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
523 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800524 null);
525 }
526 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700527
528 // The folder outer / inner ring image(s)
529 for (int i = 0; i < mFolderOuterRings.size(); i++) {
530 FolderRingAnimator fra = mFolderOuterRings.get(i);
531
532 // Draw outer ring
533 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
534 int width = (int) fra.getOuterRingSize();
535 int height = width;
536 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
537
538 int centerX = mTempLocation[0] + mCellWidth / 2;
539 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
540
541 canvas.save();
542 canvas.translate(centerX - width / 2, centerY - height / 2);
543 d.setBounds(0, 0, width, height);
544 d.draw(canvas);
545 canvas.restore();
546
547 // Draw inner ring
548 d = FolderRingAnimator.sSharedInnerRingDrawable;
549 width = (int) fra.getInnerRingSize();
550 height = width;
551 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
552
553 centerX = mTempLocation[0] + mCellWidth / 2;
554 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
555 canvas.save();
556 canvas.translate(centerX - width / 2, centerY - width / 2);
557 d.setBounds(0, 0, width, height);
558 d.draw(canvas);
559 canvas.restore();
560 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700561
562 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
563 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
564 int width = d.getIntrinsicWidth();
565 int height = d.getIntrinsicHeight();
566
567 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
568 int centerX = mTempLocation[0] + mCellWidth / 2;
569 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
570
571 canvas.save();
572 canvas.translate(centerX - width / 2, centerY - width / 2);
573 d.setBounds(0, 0, width, height);
574 d.draw(canvas);
575 canvas.restore();
576 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700577 }
578
579 public void showFolderAccept(FolderRingAnimator fra) {
580 mFolderOuterRings.add(fra);
581 }
582
583 public void hideFolderAccept(FolderRingAnimator fra) {
584 if (mFolderOuterRings.contains(fra)) {
585 mFolderOuterRings.remove(fra);
586 }
587 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700588 }
589
Adam Cohenc51934b2011-07-26 21:07:43 -0700590 public void setFolderLeaveBehindCell(int x, int y) {
591 mFolderLeaveBehindCell[0] = x;
592 mFolderLeaveBehindCell[1] = y;
593 invalidate();
594 }
595
596 public void clearFolderLeaveBehind() {
597 mFolderLeaveBehindCell[0] = -1;
598 mFolderLeaveBehindCell[1] = -1;
599 invalidate();
600 }
601
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700602 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700603 public void cancelLongPress() {
604 super.cancelLongPress();
605
606 // Cancel long press for all children
607 final int count = getChildCount();
608 for (int i = 0; i < count; i++) {
609 final View child = getChildAt(i);
610 child.cancelLongPress();
611 }
612 }
613
Michael Jurkadee05892010-07-27 10:01:56 -0700614 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
615 mInterceptTouchListener = listener;
616 }
617
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700619 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 }
621
622 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700623 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 }
625
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700626 public boolean addViewToCellLayout(
627 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700628 final LayoutParams lp = params;
629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 // Generate an id for each view, this assumes we have at most 256x256 cells
631 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700632 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700633 // If the horizontal or vertical span is set to -1, it is taken to
634 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700635 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
636 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637
Winson Chungaafa03c2010-06-11 17:34:16 -0700638 child.setId(childId);
639
Michael Jurka8c920dd2011-01-20 14:16:56 -0800640 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700641
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700642 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700643
Winson Chungaafa03c2010-06-11 17:34:16 -0700644 return true;
645 }
646 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700648
Michael Jurkabea15192010-11-17 12:33:46 -0800649 public void setAcceptsDrops(boolean acceptsDrops) {
650 if (mAcceptsDrops != acceptsDrops) {
651 mAcceptsDrops = acceptsDrops;
652 invalidate();
653 }
654 }
655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700657 public void removeAllViews() {
658 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800659 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700660 }
661
662 @Override
663 public void removeAllViewsInLayout() {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700664 if (mChildren.getChildCount() > 0) {
665 clearOccupiedCells();
666 mChildren.removeAllViewsInLayout();
667 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 }
669
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700670 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800671 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700672 }
673
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 @Override
675 public void removeView(View view) {
676 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800677 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 }
679
680 @Override
681 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800682 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
683 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
685
686 @Override
687 public void removeViewInLayout(View view) {
688 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800689 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 }
691
692 @Override
693 public void removeViews(int start, int count) {
694 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800695 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700696 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800697 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700698 }
699
700 @Override
701 public void removeViewsInLayout(int start, int count) {
702 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800703 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700704 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800705 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700706 }
707
Michael Jurka8c920dd2011-01-20 14:16:56 -0800708 public void drawChildren(Canvas canvas) {
709 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 }
711
Michael Jurkaabded662011-03-04 12:06:57 -0800712 void buildChildrenLayer() {
713 mChildren.buildLayer();
714 }
715
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 @Override
717 protected void onAttachedToWindow() {
718 super.onAttachedToWindow();
719 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
720 }
721
Michael Jurkaaf442092010-06-10 17:01:57 -0700722 public void setTagToCellInfoForPoint(int touchX, int touchY) {
723 final CellInfo cellInfo = mCellInfo;
724 final Rect frame = mRect;
725 final int x = touchX + mScrollX;
726 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800727 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700728
729 boolean found = false;
730 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800731 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800732 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700733
Adam Cohen1b607ed2011-03-03 17:26:50 -0800734 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
735 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700736 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700737
738 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
739 // offset that by this CellLayout's padding to test an (x,y) point that is relative
740 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700741 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700742
Michael Jurkaaf442092010-06-10 17:01:57 -0700743 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700744 cellInfo.cell = child;
745 cellInfo.cellX = lp.cellX;
746 cellInfo.cellY = lp.cellY;
747 cellInfo.spanX = lp.cellHSpan;
748 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700749 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700750 break;
751 }
752 }
753 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700754
Michael Jurkaaf442092010-06-10 17:01:57 -0700755 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700756 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700757 pointToCellExact(x, y, cellXY);
758
Michael Jurkaaf442092010-06-10 17:01:57 -0700759 cellInfo.cell = null;
760 cellInfo.cellX = cellXY[0];
761 cellInfo.cellY = cellXY[1];
762 cellInfo.spanX = 1;
763 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700764 }
765 setTag(cellInfo);
766 }
767
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 @Override
769 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700770 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
771 return true;
772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 final int action = ev.getAction();
774 final CellInfo cellInfo = mCellInfo;
775
776 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700777 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 } else if (action == MotionEvent.ACTION_UP) {
779 cellInfo.cell = null;
780 cellInfo.cellX = -1;
781 cellInfo.cellY = -1;
782 cellInfo.spanX = 0;
783 cellInfo.spanY = 0;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 setTag(cellInfo);
785 }
786
787 return false;
788 }
789
790 @Override
791 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700792 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 }
794
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700795 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700796 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 * @param x X coordinate of the point
798 * @param y Y coordinate of the point
799 * @param result Array of 2 ints to hold the x and y coordinate of the cell
800 */
801 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700802 final int hStartPadding = getPaddingLeft();
803 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804
805 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
806 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
807
Adam Cohend22015c2010-07-26 22:02:18 -0700808 final int xAxis = mCountX;
809 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810
811 if (result[0] < 0) result[0] = 0;
812 if (result[0] >= xAxis) result[0] = xAxis - 1;
813 if (result[1] < 0) result[1] = 0;
814 if (result[1] >= yAxis) result[1] = yAxis - 1;
815 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700816
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 /**
818 * Given a point, return the cell that most closely encloses that point
819 * @param x X coordinate of the point
820 * @param y Y coordinate of the point
821 * @param result Array of 2 ints to hold the x and y coordinate of the cell
822 */
823 void pointToCellRounded(int x, int y, int[] result) {
824 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
825 }
826
827 /**
828 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700829 *
830 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700832 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 * @param result Array of 2 ints to hold the x and y coordinate of the point
834 */
835 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700836 final int hStartPadding = getPaddingLeft();
837 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838
839 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
840 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
841 }
842
Adam Cohene3e27a82011-04-15 12:07:39 -0700843 /**
844 * Given a cell coordinate, return the point that represents the upper left corner of that cell
845 *
846 * @param cellX X coordinate of the cell
847 * @param cellY Y coordinate of the cell
848 *
849 * @param result Array of 2 ints to hold the x and y coordinate of the point
850 */
851 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700852 final int hStartPadding = getPaddingLeft();
853 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700854
855 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
856 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
857 }
858
Romain Guy84f296c2009-11-04 15:00:44 -0800859 int getCellWidth() {
860 return mCellWidth;
861 }
862
863 int getCellHeight() {
864 return mCellHeight;
865 }
866
Adam Cohend4844c32011-02-18 19:25:06 -0800867 int getWidthGap() {
868 return mWidthGap;
869 }
870
871 int getHeightGap() {
872 return mHeightGap;
873 }
874
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700875 Rect getContentRect(Rect r) {
876 if (r == null) {
877 r = new Rect();
878 }
879 int left = getPaddingLeft();
880 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700881 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
882 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700883 r.set(left, top, right, bottom);
884 return r;
885 }
886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 @Override
888 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
889 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700890
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700892 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
893
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
895 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700896
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
898 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
899 }
900
Adam Cohend22015c2010-07-26 22:02:18 -0700901 int numWidthGaps = mCountX - 1;
902 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903
Adam Cohen234c4cd2011-07-17 21:03:04 -0700904 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700905 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
906 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
907 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
908 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
909 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
910 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700911 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700912 } else {
913 mWidthGap = mOriginalWidthGap;
914 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700915 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700916
Michael Jurka8c920dd2011-01-20 14:16:56 -0800917 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
918 int newWidth = widthSpecSize;
919 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700920 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700921 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700922 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700923 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700924 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700925 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700926 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800927
928 int count = getChildCount();
929 for (int i = 0; i < count; i++) {
930 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700931 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
932 mPaddingRight, MeasureSpec.EXACTLY);
933 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
934 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800935 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
936 }
937 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
940 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700941 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800944 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700945 child.layout(mPaddingLeft, mPaddingTop,
946 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 }
948 }
949
950 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700951 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
952 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700953 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800954 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700955 }
956
957 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800959 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 }
961
962 @Override
963 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800964 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 }
966
Michael Jurka5f1c5092010-09-03 14:15:02 -0700967 public float getBackgroundAlpha() {
968 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700969 }
970
Michael Jurka742574b2011-02-02 23:51:01 -0800971 public void setFastBackgroundAlpha(float alpha) {
972 mBackgroundAlpha = alpha;
973 }
974
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700975 public void setBackgroundAlphaMultiplier(float multiplier) {
976 mBackgroundAlphaMultiplier = multiplier;
977 }
978
Adam Cohenddb82192010-11-10 16:32:54 -0800979 public float getBackgroundAlphaMultiplier() {
980 return mBackgroundAlphaMultiplier;
981 }
982
Michael Jurka5f1c5092010-09-03 14:15:02 -0700983 public void setBackgroundAlpha(float alpha) {
984 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700985 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700986 }
987
Michael Jurka5f1c5092010-09-03 14:15:02 -0700988 // Need to return true to let the view system know we know how to handle alpha-- this is
989 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
990 // versions
991 @Override
992 protected boolean onSetAlpha(int alpha) {
993 return true;
994 }
995
996 public void setAlpha(float alpha) {
997 setChildrenAlpha(alpha);
998 super.setAlpha(alpha);
999 }
1000
Michael Jurka742574b2011-02-02 23:51:01 -08001001 public void setFastAlpha(float alpha) {
1002 setFastChildrenAlpha(alpha);
1003 super.setFastAlpha(alpha);
1004 }
1005
Michael Jurkadee05892010-07-27 10:01:56 -07001006 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001007 final int childCount = getChildCount();
1008 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001009 getChildAt(i).setAlpha(alpha);
1010 }
1011 }
1012
Michael Jurka742574b2011-02-02 23:51:01 -08001013 private void setFastChildrenAlpha(float alpha) {
1014 final int childCount = getChildCount();
1015 for (int i = 0; i < childCount; i++) {
1016 getChildAt(i).setFastAlpha(alpha);
1017 }
1018 }
1019
Patrick Dubroy440c3602010-07-13 17:50:32 -07001020 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001021 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001022 }
1023
Adam Cohen76fc0852011-06-17 13:26:23 -07001024 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
1025 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001026 CellLayoutChildren clc = getChildrenLayout();
1027 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
1028 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1029 final ItemInfo info = (ItemInfo) child.getTag();
1030
1031 // We cancel any existing animations
1032 if (mReorderAnimators.containsKey(lp)) {
1033 mReorderAnimators.get(lp).cancel();
1034 mReorderAnimators.remove(lp);
1035 }
1036
1037 int oldX = lp.x;
1038 int oldY = lp.y;
1039 mOccupied[lp.cellX][lp.cellY] = false;
1040 mOccupied[cellX][cellY] = true;
1041
1042 lp.isLockedToGrid = true;
1043 lp.cellX = info.cellX = cellX;
1044 lp.cellY = info.cellY = cellY;
1045 clc.setupLp(lp);
1046 lp.isLockedToGrid = false;
1047 int newX = lp.x;
1048 int newY = lp.y;
1049
Adam Cohen76fc0852011-06-17 13:26:23 -07001050 lp.x = oldX;
1051 lp.y = oldY;
1052 child.requestLayout();
1053
Adam Cohenbfbfd262011-06-13 16:55:12 -07001054 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
1055 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
1056 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
1057 oa.setDuration(duration);
1058 mReorderAnimators.put(lp, oa);
1059 oa.addUpdateListener(new AnimatorUpdateListener() {
1060 public void onAnimationUpdate(ValueAnimator animation) {
1061 child.requestLayout();
1062 }
1063 });
1064 oa.addListener(new AnimatorListenerAdapter() {
1065 boolean cancelled = false;
1066 public void onAnimationEnd(Animator animation) {
1067 // If the animation was cancelled, it means that another animation
1068 // has interrupted this one, and we don't want to lock the item into
1069 // place just yet.
1070 if (!cancelled) {
1071 lp.isLockedToGrid = true;
1072 }
1073 if (mReorderAnimators.containsKey(lp)) {
1074 mReorderAnimators.remove(lp);
1075 }
1076 }
1077 public void onAnimationCancel(Animator animation) {
1078 cancelled = true;
1079 }
1080 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001081 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001082 oa.start();
1083 return true;
1084 }
1085 return false;
1086 }
1087
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001088 /**
1089 * Estimate where the top left cell of the dragged item will land if it is dropped.
1090 *
1091 * @param originX The X value of the top left corner of the item
1092 * @param originY The Y value of the top left corner of the item
1093 * @param spanX The number of horizontal cells that the item spans
1094 * @param spanY The number of vertical cells that the item spans
1095 * @param result The estimated drop cell X and Y.
1096 */
1097 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001098 final int countX = mCountX;
1099 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001100
Michael Jurkaa63c4522010-08-19 13:52:27 -07001101 // pointToCellRounded takes the top left of a cell but will pad that with
1102 // cellWidth/2 and cellHeight/2 when finding the matching cell
1103 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001104
1105 // If the item isn't fully on this screen, snap to the edges
1106 int rightOverhang = result[0] + spanX - countX;
1107 if (rightOverhang > 0) {
1108 result[0] -= rightOverhang; // Snap to right
1109 }
1110 result[0] = Math.max(0, result[0]); // Snap to left
1111 int bottomOverhang = result[1] + spanY - countY;
1112 if (bottomOverhang > 0) {
1113 result[1] -= bottomOverhang; // Snap to bottom
1114 }
1115 result[1] = Math.max(0, result[1]); // Snap to top
1116 }
1117
Joe Onorato4be866d2010-10-10 11:26:02 -07001118 void visualizeDropLocation(
1119 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1120
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001121 final int oldDragCellX = mDragCell[0];
1122 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001123 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001124 if (v != null) {
1125 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1126 } else {
1127 mDragCenter.set(originX, originY);
1128 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001129
Adam Cohen2801caf2011-05-13 20:57:39 -07001130 if (dragOutline == null && v == null) {
1131 if (mCrosshairsDrawable != null) {
1132 invalidate();
1133 }
1134 return;
1135 }
1136
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001137 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001138 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001139 final int[] topLeft = mTmpPoint;
1140 cellToPoint(nearest[0], nearest[1], topLeft);
1141
Joe Onorato4be866d2010-10-10 11:26:02 -07001142 int left = topLeft[0];
1143 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001144
Winson Chunga9abd0e2010-10-27 17:18:37 -07001145 if (v != null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001146 // When drawing the drag outline, it did not account for margin offsets
1147 // added by the view's parent.
1148 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1149 left += lp.leftMargin;
1150 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001151
Adam Cohen99e8b402011-03-25 19:23:43 -07001152 // Offsets due to the size difference between the View and the dragOutline.
1153 // There is a size difference to account for the outer blur, which may lie
1154 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001155 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1156 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001157 } else {
1158 // Center the drag outline in the cell
Winson Chung4b576be2011-04-27 17:40:20 -07001159 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1160 - dragOutline.getWidth()) / 2;
1161 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1162 - dragOutline.getHeight()) / 2;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001163 }
Winson Chung150fbab2010-09-29 17:14:26 -07001164
Joe Onorato4be866d2010-10-10 11:26:02 -07001165 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001166 mDragOutlineAnims[oldIndex].animateOut();
1167 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001168
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001169 mDragOutlines[mDragOutlineCurrent].set(left, top);
1170 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1171 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001172 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001173
1174 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1175 if (mCrosshairsDrawable != null) {
1176 invalidate();
1177 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001178 }
1179
Adam Cohene0310962011-04-18 16:15:31 -07001180 public void clearDragOutlines() {
1181 final int oldIndex = mDragOutlineCurrent;
1182 mDragOutlineAnims[oldIndex].animateOut();
1183 mDragCell[0] = -1;
1184 mDragCell[1] = -1;
1185 }
1186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001188 * Find a vacant area that will fit the given bounds nearest the requested
1189 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001190 *
Romain Guy51afc022009-05-04 18:03:43 -07001191 * @param pixelX The X location at which you want to search for a vacant area.
1192 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001193 * @param spanX Horizontal span of the object.
1194 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001195 * @param result Array in which to place the result, or null (in which case a new array will
1196 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001197 * @return The X, Y cell of a vacant area that can contain this object,
1198 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001200 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001201 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1202 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001203 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001204
Michael Jurka6a1435d2010-09-27 17:35:12 -07001205 /**
1206 * Find a vacant area that will fit the given bounds nearest the requested
1207 * cell location. Uses Euclidean distance to score multiple vacant areas.
1208 *
1209 * @param pixelX The X location at which you want to search for a vacant area.
1210 * @param pixelY The Y location at which you want to search for a vacant area.
1211 * @param spanX Horizontal span of the object.
1212 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001213 * @param ignoreOccupied If true, the result can be an occupied cell
1214 * @param result Array in which to place the result, or null (in which case a new array will
1215 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001216 * @return The X, Y cell of a vacant area that can contain this object,
1217 * nearest the requested location.
1218 */
Adam Cohendf035382011-04-11 17:22:04 -07001219 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1220 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001221 // mark space take by ignoreView as available (method checks if ignoreView is null)
1222 markCellsAsUnoccupiedForView(ignoreView);
1223
Adam Cohene3e27a82011-04-15 12:07:39 -07001224 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1225 // to the center of the item, but we are searching based on the top-left cell, so
1226 // we translate the point over to correspond to the top-left.
1227 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1228 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1229
Jeff Sharkey70864282009-04-07 21:08:40 -07001230 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001231 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001232 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001233
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001234 final int countX = mCountX;
1235 final int countY = mCountY;
1236 final boolean[][] occupied = mOccupied;
1237
Winson Chungbbc60d82010-11-11 16:34:41 -08001238 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001239 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001240 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001241 if (ignoreOccupied) {
1242 for (int i = 0; i < spanX; i++) {
1243 for (int j = 0; j < spanY; j++) {
1244 if (occupied[x + i][y + j]) {
1245 // small optimization: we can skip to after the column we
1246 // just found an occupied cell
1247 x += i;
1248 continue inner;
1249 }
Michael Jurkac28de512010-08-13 11:27:44 -07001250 }
1251 }
1252 }
Winson Chung0be025d2011-05-23 17:45:09 -07001253 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001254 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255
Michael Jurkac28de512010-08-13 11:27:44 -07001256 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1257 + Math.pow(cellXY[1] - pixelY, 2));
1258 if (distance <= bestDistance) {
1259 bestDistance = distance;
1260 bestXY[0] = x;
1261 bestXY[1] = y;
1262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 }
1264 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001265 // re-mark space taken by ignoreView as occupied
1266 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267
Adam Cohenc0dcf592011-06-01 15:30:43 -07001268 // Return -1, -1 if no suitable location found
1269 if (bestDistance == Double.MAX_VALUE) {
1270 bestXY[0] = -1;
1271 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001272 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001273 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001275
Adam Cohendf035382011-04-11 17:22:04 -07001276 /**
1277 * Find a vacant area that will fit the given bounds nearest the requested
1278 * cell location. Uses Euclidean distance to score multiple vacant areas.
1279 *
1280 * @param pixelX The X location at which you want to search for a vacant area.
1281 * @param pixelY The Y location at which you want to search for a vacant area.
1282 * @param spanX Horizontal span of the object.
1283 * @param spanY Vertical span of the object.
1284 * @param ignoreView Considers space occupied by this view as unoccupied
1285 * @param result Previously returned value to possibly recycle.
1286 * @return The X, Y cell of a vacant area that can contain this object,
1287 * nearest the requested location.
1288 */
1289 int[] findNearestVacantArea(
1290 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1291 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1292 }
1293
1294 /**
1295 * Find a starting cell position that will fit the given bounds nearest the requested
1296 * cell location. Uses Euclidean distance to score multiple vacant areas.
1297 *
1298 * @param pixelX The X location at which you want to search for a vacant area.
1299 * @param pixelY The Y location at which you want to search for a vacant area.
1300 * @param spanX Horizontal span of the object.
1301 * @param spanY Vertical span of the object.
1302 * @param ignoreView Considers space occupied by this view as unoccupied
1303 * @param result Previously returned value to possibly recycle.
1304 * @return The X, Y cell of a vacant area that can contain this object,
1305 * nearest the requested location.
1306 */
1307 int[] findNearestArea(
1308 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1309 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1310 }
1311
Michael Jurka0280c3b2010-09-17 15:00:07 -07001312 boolean existsEmptyCell() {
1313 return findCellForSpan(null, 1, 1);
1314 }
1315
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001317 * Finds the upper-left coordinate of the first rectangle in the grid that can
1318 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1319 * then this method will only return coordinates for rectangles that contain the cell
1320 * (intersectX, intersectY)
1321 *
1322 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1323 * can be found.
1324 * @param spanX The horizontal span of the cell we want to find.
1325 * @param spanY The vertical span of the cell we want to find.
1326 *
1327 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001328 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001329 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1330 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1331 }
1332
1333 /**
1334 * Like above, but ignores any cells occupied by the item "ignoreView"
1335 *
1336 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1337 * can be found.
1338 * @param spanX The horizontal span of the cell we want to find.
1339 * @param spanY The vertical span of the cell we want to find.
1340 * @param ignoreView The home screen item we should treat as not occupying any space
1341 * @return
1342 */
1343 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1344 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1345 }
1346
1347 /**
1348 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1349 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1350 *
1351 * @param spanX The horizontal span of the cell we want to find.
1352 * @param spanY The vertical span of the cell we want to find.
1353 * @param ignoreView The home screen item we should treat as not occupying any space
1354 * @param intersectX The X coordinate of the cell that we should try to overlap
1355 * @param intersectX The Y coordinate of the cell that we should try to overlap
1356 *
1357 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1358 */
1359 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1360 int intersectX, int intersectY) {
1361 return findCellForSpanThatIntersectsIgnoring(
1362 cellXY, spanX, spanY, intersectX, intersectY, null);
1363 }
1364
1365 /**
1366 * The superset of the above two methods
1367 */
1368 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1369 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001370 // mark space take by ignoreView as available (method checks if ignoreView is null)
1371 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001372
Michael Jurka28750fb2010-09-24 17:43:49 -07001373 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001374 while (true) {
1375 int startX = 0;
1376 if (intersectX >= 0) {
1377 startX = Math.max(startX, intersectX - (spanX - 1));
1378 }
1379 int endX = mCountX - (spanX - 1);
1380 if (intersectX >= 0) {
1381 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1382 }
1383 int startY = 0;
1384 if (intersectY >= 0) {
1385 startY = Math.max(startY, intersectY - (spanY - 1));
1386 }
1387 int endY = mCountY - (spanY - 1);
1388 if (intersectY >= 0) {
1389 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1390 }
1391
Winson Chungbbc60d82010-11-11 16:34:41 -08001392 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001393 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001394 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001395 for (int i = 0; i < spanX; i++) {
1396 for (int j = 0; j < spanY; j++) {
1397 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001398 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001399 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001400 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001401 continue inner;
1402 }
1403 }
1404 }
1405 if (cellXY != null) {
1406 cellXY[0] = x;
1407 cellXY[1] = y;
1408 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001409 foundCell = true;
1410 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001411 }
1412 }
1413 if (intersectX == -1 && intersectY == -1) {
1414 break;
1415 } else {
1416 // if we failed to find anything, try again but without any requirements of
1417 // intersecting
1418 intersectX = -1;
1419 intersectY = -1;
1420 continue;
1421 }
1422 }
1423
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001424 // re-mark space taken by ignoreView as occupied
1425 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001426 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001427 }
1428
1429 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001430 * A drag event has begun over this layout.
1431 * It may have begun over this layout (in which case onDragChild is called first),
1432 * or it may have begun on another layout.
1433 */
1434 void onDragEnter() {
1435 if (!mDragging) {
1436 // Fade in the drag indicators
1437 if (mCrosshairsAnimator != null) {
1438 mCrosshairsAnimator.animateIn();
1439 }
1440 }
1441 mDragging = true;
1442 }
1443
1444 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001445 * Called when drag has left this CellLayout or has been completed (successfully or not)
1446 */
1447 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001448 // This can actually be called when we aren't in a drag, e.g. when adding a new
1449 // item to this layout via the customize drawer.
1450 // Guard against that case.
1451 if (mDragging) {
1452 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001453
Joe Onorato4be866d2010-10-10 11:26:02 -07001454 // Fade out the drag indicators
1455 if (mCrosshairsAnimator != null) {
1456 mCrosshairsAnimator.animateOut();
1457 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001458 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001459
1460 // Invalidate the drag data
1461 mDragCell[0] = -1;
1462 mDragCell[1] = -1;
1463 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1464 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1465
Michael Jurka33945b22010-12-21 18:19:38 -08001466 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001467 }
1468
1469 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001470 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001471 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001472 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 *
1474 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001476 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001477 if (child != null) {
1478 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08001479 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001480 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001481 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 }
1483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001486 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 * @param cellX X coordinate of upper left corner expressed as a cell position
1488 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001489 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001491 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001493 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 final int cellWidth = mCellWidth;
1495 final int cellHeight = mCellHeight;
1496 final int widthGap = mWidthGap;
1497 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001498
Winson Chung4b825dcd2011-06-19 12:41:22 -07001499 final int hStartPadding = getPaddingLeft();
1500 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001501
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1503 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1504
1505 int x = hStartPadding + cellX * (cellWidth + widthGap);
1506 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001507
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001508 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001510
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001512 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001514 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 * @param width Width in pixels
1516 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001517 * @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 -08001518 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001519 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001520 return rectToCell(getResources(), width, height, result);
1521 }
1522
1523 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 // Always assume we're working with the smallest span to make sure we
1525 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001526 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1527 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001529
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07001531 int spanX = (int) Math.ceil(width / (float) smallerSize);
1532 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04001533
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001534 if (result == null) {
1535 return new int[] { spanX, spanY };
1536 }
1537 result[0] = spanX;
1538 result[1] = spanY;
1539 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
1541
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001542 public int[] cellSpansToSize(int hSpans, int vSpans) {
1543 int[] size = new int[2];
1544 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1545 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1546 return size;
1547 }
1548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001550 * Calculate the grid spans needed to fit given item
1551 */
1552 public void calculateSpans(ItemInfo info) {
1553 final int minWidth;
1554 final int minHeight;
1555
1556 if (info instanceof LauncherAppWidgetInfo) {
1557 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1558 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1559 } else if (info instanceof PendingAddWidgetInfo) {
1560 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1561 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1562 } else {
1563 // It's not a widget, so it must be 1x1
1564 info.spanX = info.spanY = 1;
1565 return;
1566 }
1567 int[] spans = rectToCell(minWidth, minHeight, null);
1568 info.spanX = spans[0];
1569 info.spanY = spans[1];
1570 }
1571
1572 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 * Find the first vacant cell, if there is one.
1574 *
1575 * @param vacant Holds the x and y coordinate of the vacant cell
1576 * @param spanX Horizontal cell span.
1577 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001578 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 * @return True if a vacant cell was found
1580 */
1581 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582
Michael Jurka0280c3b2010-09-17 15:00:07 -07001583 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
1585
1586 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1587 int xCount, int yCount, boolean[][] occupied) {
1588
Adam Cohen2801caf2011-05-13 20:57:39 -07001589 for (int y = 0; y < yCount; y++) {
1590 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 boolean available = !occupied[x][y];
1592out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1593 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1594 available = available && !occupied[i][j];
1595 if (!available) break out;
1596 }
1597 }
1598
1599 if (available) {
1600 vacant[0] = x;
1601 vacant[1] = y;
1602 return true;
1603 }
1604 }
1605 }
1606
1607 return false;
1608 }
1609
Michael Jurka0280c3b2010-09-17 15:00:07 -07001610 private void clearOccupiedCells() {
1611 for (int x = 0; x < mCountX; x++) {
1612 for (int y = 0; y < mCountY; y++) {
1613 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 }
1615 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001616 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617
Adam Cohen1b607ed2011-03-03 17:26:50 -08001618 /**
1619 * Given a view, determines how much that view can be expanded in all directions, in terms of
1620 * whether or not there are other items occupying adjacent cells. Used by the
1621 * AppWidgetResizeFrame to determine how the widget can be resized.
1622 */
Adam Cohend4844c32011-02-18 19:25:06 -08001623 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001624 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001625 boolean flag;
1626
Adam Cohen1b607ed2011-03-03 17:26:50 -08001627 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001628 for (int x = lp.cellX - 1; x >= 0; x--) {
1629 flag = false;
1630 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1631 if (mOccupied[x][y]) flag = true;
1632 }
1633 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001634 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001635 }
1636
Adam Cohen1b607ed2011-03-03 17:26:50 -08001637 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001638 for (int y = lp.cellY - 1; y >= 0; y--) {
1639 flag = false;
1640 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1641 if (mOccupied[x][y]) flag = true;
1642 }
1643 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001644 expandability[AppWidgetResizeFrame.TOP]++;
1645 }
Adam Cohend4844c32011-02-18 19:25:06 -08001646
Adam Cohen1b607ed2011-03-03 17:26:50 -08001647 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001648 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1649 flag = false;
1650 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1651 if (mOccupied[x][y]) flag = true;
1652 }
1653 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001654 expandability[AppWidgetResizeFrame.RIGHT]++;
1655 }
Adam Cohend4844c32011-02-18 19:25:06 -08001656
Adam Cohen1b607ed2011-03-03 17:26:50 -08001657 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001658 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1659 flag = false;
1660 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1661 if (mOccupied[x][y]) flag = true;
1662 }
1663 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001664 expandability[AppWidgetResizeFrame.BOTTOM]++;
1665 }
Adam Cohend4844c32011-02-18 19:25:06 -08001666 }
1667
Michael Jurka0280c3b2010-09-17 15:00:07 -07001668 public void onMove(View view, int newCellX, int newCellY) {
1669 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1670 markCellsAsUnoccupiedForView(view);
1671 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673
Adam Cohend4844c32011-02-18 19:25:06 -08001674 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001675 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001676 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1677 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1678 }
1679
Adam Cohend4844c32011-02-18 19:25:06 -08001680 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001681 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001682 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1683 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1684 }
1685
1686 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1687 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1688 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1689 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 }
1691 }
1692 }
1693
Adam Cohen2801caf2011-05-13 20:57:39 -07001694 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001695 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001696 (Math.max((mCountX - 1), 0) * mWidthGap);
1697 }
1698
1699 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001700 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001701 (Math.max((mCountY - 1), 0) * mHeightGap);
1702 }
1703
Michael Jurka66d72172011-04-12 16:29:25 -07001704 public boolean isOccupied(int x, int y) {
1705 if (x < mCountX && y < mCountY) {
1706 return mOccupied[x][y];
1707 } else {
1708 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1709 }
1710 }
1711
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 @Override
1713 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1714 return new CellLayout.LayoutParams(getContext(), attrs);
1715 }
1716
1717 @Override
1718 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1719 return p instanceof CellLayout.LayoutParams;
1720 }
1721
1722 @Override
1723 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1724 return new CellLayout.LayoutParams(p);
1725 }
1726
Winson Chungaafa03c2010-06-11 17:34:16 -07001727 public static class CellLayoutAnimationController extends LayoutAnimationController {
1728 public CellLayoutAnimationController(Animation animation, float delay) {
1729 super(animation, delay);
1730 }
1731
1732 @Override
1733 protected long getDelayForView(View view) {
1734 return (int) (Math.random() * 150);
1735 }
1736 }
1737
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1739 /**
1740 * Horizontal location of the item in the grid.
1741 */
1742 @ViewDebug.ExportedProperty
1743 public int cellX;
1744
1745 /**
1746 * Vertical location of the item in the grid.
1747 */
1748 @ViewDebug.ExportedProperty
1749 public int cellY;
1750
1751 /**
1752 * Number of cells spanned horizontally by the item.
1753 */
1754 @ViewDebug.ExportedProperty
1755 public int cellHSpan;
1756
1757 /**
1758 * Number of cells spanned vertically by the item.
1759 */
1760 @ViewDebug.ExportedProperty
1761 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001762
Adam Cohen1b607ed2011-03-03 17:26:50 -08001763 /**
1764 * Indicates whether the item will set its x, y, width and height parameters freely,
1765 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1766 */
Adam Cohend4844c32011-02-18 19:25:06 -08001767 public boolean isLockedToGrid = true;
1768
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 // X coordinate of the view in the layout.
1770 @ViewDebug.ExportedProperty
1771 int x;
1772 // Y coordinate of the view in the layout.
1773 @ViewDebug.ExportedProperty
1774 int y;
1775
Romain Guy84f296c2009-11-04 15:00:44 -08001776 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001777
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 public LayoutParams(Context c, AttributeSet attrs) {
1779 super(c, attrs);
1780 cellHSpan = 1;
1781 cellVSpan = 1;
1782 }
1783
1784 public LayoutParams(ViewGroup.LayoutParams source) {
1785 super(source);
1786 cellHSpan = 1;
1787 cellVSpan = 1;
1788 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001789
1790 public LayoutParams(LayoutParams source) {
1791 super(source);
1792 this.cellX = source.cellX;
1793 this.cellY = source.cellY;
1794 this.cellHSpan = source.cellHSpan;
1795 this.cellVSpan = source.cellVSpan;
1796 }
1797
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001799 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 this.cellX = cellX;
1801 this.cellY = cellY;
1802 this.cellHSpan = cellHSpan;
1803 this.cellVSpan = cellVSpan;
1804 }
1805
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001806 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001807 if (isLockedToGrid) {
1808 final int myCellHSpan = cellHSpan;
1809 final int myCellVSpan = cellVSpan;
1810 final int myCellX = cellX;
1811 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001812
Adam Cohend4844c32011-02-18 19:25:06 -08001813 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1814 leftMargin - rightMargin;
1815 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1816 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001817 x = myCellX * (cellWidth + widthGap) + leftMargin;
1818 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001819 }
1820 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001821
Winson Chungaafa03c2010-06-11 17:34:16 -07001822 public String toString() {
1823 return "(" + this.cellX + ", " + this.cellY + ")";
1824 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001825
1826 public void setWidth(int width) {
1827 this.width = width;
1828 }
1829
1830 public int getWidth() {
1831 return width;
1832 }
1833
1834 public void setHeight(int height) {
1835 this.height = height;
1836 }
1837
1838 public int getHeight() {
1839 return height;
1840 }
1841
1842 public void setX(int x) {
1843 this.x = x;
1844 }
1845
1846 public int getX() {
1847 return x;
1848 }
1849
1850 public void setY(int y) {
1851 this.y = y;
1852 }
1853
1854 public int getY() {
1855 return y;
1856 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 }
1858
Michael Jurka0280c3b2010-09-17 15:00:07 -07001859 // This class stores info for two purposes:
1860 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1861 // its spanX, spanY, and the screen it is on
1862 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1863 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1864 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001865 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001867 int cellX = -1;
1868 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 int spanX;
1870 int spanY;
1871 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001872 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 @Override
1875 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001876 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1877 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 }
1879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880}