blob: 22516cc333f03942770feed1d31e6e3b7d6602f5 [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
67 private int mWidthGap;
68 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070069 private int mMaxGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070
71 private final Rect mRect = new Rect();
72 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070073
Patrick Dubroyde7658b2010-09-27 11:15:43 -070074 // These are temporary variables to prevent having to allocate a new object just to
75 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070076 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070077 private final int[] mTmpPoint = new int[2];
78 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070079 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070080
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 boolean[][] mOccupied;
82
Michael Jurkadee05892010-07-27 10:01:56 -070083 private OnTouchListener mInterceptTouchListener;
84
Adam Cohen69ce2e52011-07-03 19:25:21 -070085 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
86
Michael Jurka5f1c5092010-09-03 14:15:02 -070087 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070088 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070089
Michael Jurka33945b22010-12-21 18:19:38 -080090 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080091 private Drawable mActiveBackground;
92 private Drawable mActiveGlowBackground;
93 private Drawable mNormalBackgroundMini;
94 private Drawable mNormalGlowBackgroundMini;
95 private Drawable mActiveBackgroundMini;
96 private Drawable mActiveGlowBackgroundMini;
Michael Jurka18014792010-10-14 09:01:34 -070097 private Rect mBackgroundRect;
Michael Jurka33945b22010-12-21 18:19:38 -080098 private Rect mGlowBackgroundRect;
99 private float mGlowBackgroundScale;
100 private float mGlowBackgroundAlpha;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700101
Adam Cohendf035382011-04-11 17:22:04 -0700102 private boolean mAcceptsDrops = true;
Michael Jurka33945b22010-12-21 18:19:38 -0800103 // If we're actively dragging something over this screen, mIsDragOverlapping is true
104 private boolean mIsDragOverlapping = false;
105 private boolean mIsDragOccuring = false;
106 private boolean mIsDefaultDropTarget = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700107 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700108
Winson Chung150fbab2010-09-29 17:14:26 -0700109 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700110 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Winson Chung63257c12011-05-05 17:06:13 -0700111 private Point[] mDragOutlines = new Point[4];
Chet Haase472b2812010-10-14 07:02:04 -0700112 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700113 private InterruptibleInOutAnimator[] mDragOutlineAnims =
114 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700115
116 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700117 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700118 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700119
Patrick Dubroy96864c32011-03-10 17:17:23 -0800120 private BubbleTextView mPressedOrFocusedIcon;
121
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700122 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700123 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700124 private float mCrosshairsVisibility = 0.0f;
125
Adam Cohenbfbfd262011-06-13 16:55:12 -0700126 private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new
127 HashMap<CellLayout.LayoutParams, ObjectAnimator>();
128
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700129 // When a drag operation is in progress, holds the nearest cell to the touch point
130 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Joe Onorato4be866d2010-10-10 11:26:02 -0700132 private boolean mDragging = false;
133
Patrick Dubroyce34a972010-10-19 10:34:32 -0700134 private TimeInterpolator mEaseOutInterpolator;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800135 private CellLayoutChildren mChildren;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 public CellLayout(Context context) {
138 this(context, null);
139 }
140
141 public CellLayout(Context context, AttributeSet attrs) {
142 this(context, attrs, 0);
143 }
144
145 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
146 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700147
148 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
149 // the user where a dragged item will land when dropped.
150 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
153
Winson Chung4b825dcd2011-06-19 12:41:22 -0700154 mOriginalCellWidth =
155 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
156 mOriginalCellHeight =
157 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
158 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
159 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
160 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700161 mCountX = LauncherModel.getCellCountX();
162 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700163 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
165 a.recycle();
166
167 setAlwaysDrawnWithCacheEnabled(false);
168
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700169 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700170
Winson Chung967289b2011-06-30 18:09:30 -0700171 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
172 mActiveBackground = res.getDrawable(R.drawable.homescreen_green_normal_holo);
173 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_green_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800174
Winson Chungb26f3d62011-06-02 10:49:29 -0700175 mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
176 mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
177 mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
178 mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
Michael Jurka33945b22010-12-21 18:19:38 -0800179
Winson Chungb26f3d62011-06-02 10:49:29 -0700180 mNormalBackground.setFilterBitmap(true);
181 mActiveBackground.setFilterBitmap(true);
182 mActiveGlowBackground.setFilterBitmap(true);
183 mNormalBackgroundMini.setFilterBitmap(true);
184 mNormalGlowBackgroundMini.setFilterBitmap(true);
185 mActiveBackgroundMini.setFilterBitmap(true);
186 mActiveGlowBackgroundMini.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700187
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700188 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700189
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700190 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700191 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700192
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700193 // Set up the animation for fading the crosshairs in and out
194 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700195 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700196 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700197 public void onAnimationUpdate(ValueAnimator animation) {
198 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700199 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700200 }
201 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700202 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700203
Joe Onorato4be866d2010-10-10 11:26:02 -0700204 for (int i = 0; i < mDragOutlines.length; i++) {
205 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700206 }
207
208 // When dragging things around the home screens, we show a green outline of
209 // where the item will land. The outlines gradually fade out, leaving a trail
210 // behind the drag path.
211 // Set up all the animations that are used to implement this fading.
212 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700213 final float fromAlphaValue = 0;
214 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700215
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700216 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700217
218 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700219 final InterruptibleInOutAnimator anim =
220 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700221 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700222 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700223 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700224 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700225 final Bitmap outline = (Bitmap)anim.getTag();
226
227 // If an animation is started and then stopped very quickly, we can still
228 // get spurious updates we've cleared the tag. Guard against this.
229 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700230 if (false) {
231 Object val = animation.getAnimatedValue();
232 Log.d(TAG, "anim " + thisIndex + " update: " + val +
233 ", isStopped " + anim.isStopped());
234 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700235 // Try to prevent it from continuing to run
236 animation.cancel();
237 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700238 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700239 final int left = mDragOutlines[thisIndex].x;
240 final int top = mDragOutlines[thisIndex].y;
241 CellLayout.this.invalidate(left, top,
242 left + outline.getWidth(), top + outline.getHeight());
243 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700244 }
245 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700246 // The animation holds a reference to the drag outline bitmap as long is it's
247 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700248 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700249 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700250 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700251 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700252 anim.setTag(null);
253 }
254 }
255 });
256 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700257 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700258
Michael Jurka18014792010-10-14 09:01:34 -0700259 mBackgroundRect = new Rect();
Michael Jurka33945b22010-12-21 18:19:38 -0800260 mGlowBackgroundRect = new Rect();
Michael Jurka18014792010-10-14 09:01:34 -0700261 setHoverScale(1.0f);
262 setHoverAlpha(1.0f);
Michael Jurkabea15192010-11-17 12:33:46 -0800263
Michael Jurka8c920dd2011-01-20 14:16:56 -0800264 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700265 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800266 addView(mChildren);
Michael Jurka18014792010-10-14 09:01:34 -0700267 }
268
Michael Jurkaf6440da2011-04-05 14:50:34 -0700269 static int widthInPortrait(Resources r, int numCells) {
270 // We use this method from Workspace to figure out how many rows/columns Launcher should
271 // have. We ignore the left/right padding on CellLayout because it turns out in our design
272 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700273 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700274 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
275 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700276
Winson Chung4b825dcd2011-06-19 12:41:22 -0700277 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700278 }
279
Michael Jurkaf6440da2011-04-05 14:50:34 -0700280 static int heightInLandscape(Resources r, int numCells) {
281 // We use this method from Workspace to figure out how many rows/columns Launcher should
282 // have. We ignore the left/right padding on CellLayout because it turns out in our design
283 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700284 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700285 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
286 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700287
Winson Chung4b825dcd2011-06-19 12:41:22 -0700288 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700289 }
290
Adam Cohen2801caf2011-05-13 20:57:39 -0700291 public void enableHardwareLayers() {
292 mChildren.enableHardwareLayers();
293 }
294
295 public void setGridSize(int x, int y) {
296 mCountX = x;
297 mCountY = y;
298 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700299 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700300 }
301
Patrick Dubroy96864c32011-03-10 17:17:23 -0800302 private void invalidateBubbleTextView(BubbleTextView icon) {
303 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700304 invalidate(icon.getLeft() + getPaddingLeft() - padding,
305 icon.getTop() + getPaddingTop() - padding,
306 icon.getRight() + getPaddingLeft() + padding,
307 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800308 }
309
310 void setPressedOrFocusedIcon(BubbleTextView icon) {
311 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
312 // requires an expanded clip rect (due to the glow's blur radius)
313 BubbleTextView oldIcon = mPressedOrFocusedIcon;
314 mPressedOrFocusedIcon = icon;
315 if (oldIcon != null) {
316 invalidateBubbleTextView(oldIcon);
317 }
318 if (mPressedOrFocusedIcon != null) {
319 invalidateBubbleTextView(mPressedOrFocusedIcon);
320 }
321 }
322
Winson Chung6e314082011-01-27 16:46:51 -0800323 public CellLayoutChildren getChildrenLayout() {
324 if (getChildCount() > 0) {
325 return (CellLayoutChildren) getChildAt(0);
326 }
327 return null;
328 }
329
Michael Jurka33945b22010-12-21 18:19:38 -0800330 public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
331 if (mIsDefaultDropTarget != isDefaultDropTarget) {
332 mIsDefaultDropTarget = isDefaultDropTarget;
333 invalidate();
334 }
335 }
336
Michael Jurka33945b22010-12-21 18:19:38 -0800337 void setIsDragOccuring(boolean isDragOccuring) {
338 if (mIsDragOccuring != isDragOccuring) {
339 mIsDragOccuring = isDragOccuring;
340 invalidate();
341 }
342 }
343
344 void setIsDragOverlapping(boolean isDragOverlapping) {
345 if (mIsDragOverlapping != isDragOverlapping) {
346 mIsDragOverlapping = isDragOverlapping;
347 invalidate();
348 }
349 }
350
351 boolean getIsDragOverlapping() {
352 return mIsDragOverlapping;
353 }
354
355 private void updateGlowRect() {
356 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700357 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
358 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
Michael Jurka33945b22010-12-21 18:19:38 -0800359 mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
Michael Jurka18014792010-10-14 09:01:34 -0700360 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
361 invalidate();
362 }
363
364 public void setHoverScale(float scaleFactor) {
Michael Jurka33945b22010-12-21 18:19:38 -0800365 if (scaleFactor != mGlowBackgroundScale) {
366 mGlowBackgroundScale = scaleFactor;
367 updateGlowRect();
Michael Jurka8deb1e62011-01-25 16:27:43 -0800368 if (getParent() != null) {
369 ((View) getParent()).invalidate();
370 }
Michael Jurka18014792010-10-14 09:01:34 -0700371 }
372 }
373
374 public float getHoverScale() {
Michael Jurka33945b22010-12-21 18:19:38 -0800375 return mGlowBackgroundScale;
Michael Jurka18014792010-10-14 09:01:34 -0700376 }
377
378 public float getHoverAlpha() {
Michael Jurka33945b22010-12-21 18:19:38 -0800379 return mGlowBackgroundAlpha;
Michael Jurka18014792010-10-14 09:01:34 -0700380 }
381
382 public void setHoverAlpha(float alpha) {
Michael Jurka33945b22010-12-21 18:19:38 -0800383 mGlowBackgroundAlpha = alpha;
Michael Jurka18014792010-10-14 09:01:34 -0700384 invalidate();
385 }
386
387 void animateDrop() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700388 Resources res = getResources();
389 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
390 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
391 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
392 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
393 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
394 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
Michael Jurka18014792010-10-14 09:01:34 -0700395
Winson Chungb26f3d62011-06-02 10:49:29 -0700396 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
397 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDuration));
Michael Jurka18014792010-10-14 09:01:34 -0700398
Winson Chungb26f3d62011-06-02 10:49:29 -0700399 AnimatorSet bouncer = new AnimatorSet();
400 bouncer.play(scaleUp).before(scaleDown);
401 bouncer.play(scaleUp).with(alphaFadeOut);
402 bouncer.addListener(new AnimatorListenerAdapter() {
403 @Override
404 public void onAnimationStart(Animator animation) {
405 setIsDragOverlapping(true);
406 }
407 @Override
408 public void onAnimationEnd(Animator animation) {
409 setIsDragOverlapping(false);
410 setHoverScale(1.0f);
411 setHoverAlpha(1.0f);
412 }
413 });
414 bouncer.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 }
416
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700417 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700418 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700419 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
420 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
421 // When we're small, we are either drawn normally or in the "accepts drops" state (during
422 // a drag). However, we also drag the mini hover background *over* one of those two
423 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700424 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700425 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800426 boolean mini = getScaleX() < 0.5f;
427
428 if (mIsDragOverlapping) {
429 // In the mini case, we draw the active_glow bg *over* the active background
430 bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
431 } else if (mIsDragOccuring && mAcceptsDrops) {
432 bg = mini ? mActiveBackgroundMini : mActiveBackground;
Adam Cohen3af863b2011-01-25 12:16:51 -0800433 } else if (mIsDefaultDropTarget && mini) {
434 bg = mNormalGlowBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700435 } else {
Michael Jurka33945b22010-12-21 18:19:38 -0800436 bg = mini ? mNormalBackgroundMini : mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700437 }
Michael Jurka33945b22010-12-21 18:19:38 -0800438
439 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
440 bg.setBounds(mBackgroundRect);
441 bg.draw(canvas);
442
443 if (mini && mIsDragOverlapping) {
Michael Jurka18014792010-10-14 09:01:34 -0700444 boolean modifiedClipRect = false;
Michael Jurka33945b22010-12-21 18:19:38 -0800445 if (mGlowBackgroundScale > 1.0f) {
Michael Jurka18014792010-10-14 09:01:34 -0700446 // If the hover background's scale is greater than 1, we'll be drawing outside
447 // the bounds of this CellLayout. Get around that by temporarily increasing the
448 // size of the clip rect
Michael Jurka33945b22010-12-21 18:19:38 -0800449 float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Michael Jurka18014792010-10-14 09:01:34 -0700450 Rect clipRect = canvas.getClipBounds();
451 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
452 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
453 canvas.save(Canvas.CLIP_SAVE_FLAG);
454 canvas.clipRect(-marginX, -marginY,
455 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
456 modifiedClipRect = true;
457 }
458
Michael Jurka33945b22010-12-21 18:19:38 -0800459 mActiveGlowBackgroundMini.setAlpha(
460 (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
461 mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
462 mActiveGlowBackgroundMini.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700463 if (modifiedClipRect) {
464 canvas.restore();
465 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700466 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700467 }
Romain Guya6abce82009-11-10 02:54:41 -0800468
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700469 if (mCrosshairsVisibility > 0.0f) {
470 final int countX = mCountX;
471 final int countY = mCountY;
472
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700473 final float MAX_ALPHA = 0.4f;
474 final int MAX_VISIBLE_DISTANCE = 600;
475 final float DISTANCE_MULTIPLIER = 0.002f;
476
477 final Drawable d = mCrosshairsDrawable;
478 final int width = d.getIntrinsicWidth();
479 final int height = d.getIntrinsicHeight();
480
Winson Chung4b825dcd2011-06-19 12:41:22 -0700481 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700482 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700483 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700484 for (int row = 0; row <= countY; row++) {
485 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
486 float dist = mTmpPointF.length();
487 // Crosshairs further from the drag point are more faint
488 float alpha = Math.min(MAX_ALPHA,
489 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
490 if (alpha > 0.0f) {
491 d.setBounds(x, y, x + width, y + height);
492 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
493 d.draw(canvas);
494 }
495 y += mCellHeight + mHeightGap;
496 }
497 x += mCellWidth + mWidthGap;
498 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700499 }
Winson Chung150fbab2010-09-29 17:14:26 -0700500
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700501 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700502 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700503 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700504 if (alpha > 0) {
505 final Point p = mDragOutlines[i];
506 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700507 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700508 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700509 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700510 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800511
512 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
513 // requires an expanded clip rect (due to the glow's blur radius)
514 if (mPressedOrFocusedIcon != null) {
515 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
516 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
517 if (b != null) {
518 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700519 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
520 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800521 null);
522 }
523 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700524
525 // The folder outer / inner ring image(s)
526 for (int i = 0; i < mFolderOuterRings.size(); i++) {
527 FolderRingAnimator fra = mFolderOuterRings.get(i);
528
529 // Draw outer ring
530 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
531 int width = (int) fra.getOuterRingSize();
532 int height = width;
533 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
534
535 int centerX = mTempLocation[0] + mCellWidth / 2;
536 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
537
538 canvas.save();
539 canvas.translate(centerX - width / 2, centerY - height / 2);
540 d.setBounds(0, 0, width, height);
541 d.draw(canvas);
542 canvas.restore();
543
544 // Draw inner ring
545 d = FolderRingAnimator.sSharedInnerRingDrawable;
546 width = (int) fra.getInnerRingSize();
547 height = width;
548 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
549
550 centerX = mTempLocation[0] + mCellWidth / 2;
551 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
552 canvas.save();
553 canvas.translate(centerX - width / 2, centerY - width / 2);
554 d.setBounds(0, 0, width, height);
555 d.draw(canvas);
556 canvas.restore();
557 }
558 }
559
560 public void showFolderAccept(FolderRingAnimator fra) {
561 mFolderOuterRings.add(fra);
562 }
563
564 public void hideFolderAccept(FolderRingAnimator fra) {
565 if (mFolderOuterRings.contains(fra)) {
566 mFolderOuterRings.remove(fra);
567 }
568 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700569 }
570
571 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700572 public void cancelLongPress() {
573 super.cancelLongPress();
574
575 // Cancel long press for all children
576 final int count = getChildCount();
577 for (int i = 0; i < count; i++) {
578 final View child = getChildAt(i);
579 child.cancelLongPress();
580 }
581 }
582
Michael Jurkadee05892010-07-27 10:01:56 -0700583 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
584 mInterceptTouchListener = listener;
585 }
586
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700588 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 }
590
591 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700592 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 }
594
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700595 public boolean addViewToCellLayout(
596 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700597 final LayoutParams lp = params;
598
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 // Generate an id for each view, this assumes we have at most 256x256 cells
600 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700601 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700602 // If the horizontal or vertical span is set to -1, it is taken to
603 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700604 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
605 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606
Winson Chungaafa03c2010-06-11 17:34:16 -0700607 child.setId(childId);
608
Michael Jurka8c920dd2011-01-20 14:16:56 -0800609 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700610
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700611 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700612
Winson Chungaafa03c2010-06-11 17:34:16 -0700613 return true;
614 }
615 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700617
Michael Jurkabea15192010-11-17 12:33:46 -0800618 public void setAcceptsDrops(boolean acceptsDrops) {
619 if (mAcceptsDrops != acceptsDrops) {
620 mAcceptsDrops = acceptsDrops;
621 invalidate();
622 }
623 }
624
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700626 public void removeAllViews() {
627 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800628 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700629 }
630
631 @Override
632 public void removeAllViewsInLayout() {
633 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800634 mChildren.removeAllViewsInLayout();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700635 }
636
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700637 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800638 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700639 }
640
Michael Jurka0280c3b2010-09-17 15:00:07 -0700641 @Override
642 public void removeView(View view) {
643 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800644 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700645 }
646
647 @Override
648 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800649 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
650 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700651 }
652
653 @Override
654 public void removeViewInLayout(View view) {
655 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800656 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700657 }
658
659 @Override
660 public void removeViews(int start, int count) {
661 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800662 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700663 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800664 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700665 }
666
667 @Override
668 public void removeViewsInLayout(int start, int count) {
669 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800670 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700671 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800672 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700673 }
674
Michael Jurka8c920dd2011-01-20 14:16:56 -0800675 public void drawChildren(Canvas canvas) {
676 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 }
678
Michael Jurkaabded662011-03-04 12:06:57 -0800679 void buildChildrenLayer() {
680 mChildren.buildLayer();
681 }
682
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 @Override
684 protected void onAttachedToWindow() {
685 super.onAttachedToWindow();
686 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
687 }
688
Michael Jurkaaf442092010-06-10 17:01:57 -0700689 public void setTagToCellInfoForPoint(int touchX, int touchY) {
690 final CellInfo cellInfo = mCellInfo;
691 final Rect frame = mRect;
692 final int x = touchX + mScrollX;
693 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800694 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700695
696 boolean found = false;
697 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800698 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800699 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700700
Adam Cohen1b607ed2011-03-03 17:26:50 -0800701 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
702 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700703 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700704
705 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
706 // offset that by this CellLayout's padding to test an (x,y) point that is relative
707 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700708 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chung0be025d2011-05-23 17:45:09 -0700709
Michael Jurkaaf442092010-06-10 17:01:57 -0700710 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700711 cellInfo.cell = child;
712 cellInfo.cellX = lp.cellX;
713 cellInfo.cellY = lp.cellY;
714 cellInfo.spanX = lp.cellHSpan;
715 cellInfo.spanY = lp.cellVSpan;
716 cellInfo.valid = true;
717 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700718 break;
719 }
720 }
721 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700722
Michael Jurkaaf442092010-06-10 17:01:57 -0700723 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700724 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700725 pointToCellExact(x, y, cellXY);
726
Michael Jurkaaf442092010-06-10 17:01:57 -0700727 cellInfo.cell = null;
728 cellInfo.cellX = cellXY[0];
729 cellInfo.cellY = cellXY[1];
730 cellInfo.spanX = 1;
731 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700732 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
733 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700734 }
735 setTag(cellInfo);
736 }
737
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 @Override
739 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700740 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
741 return true;
742 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 final int action = ev.getAction();
744 final CellInfo cellInfo = mCellInfo;
745
746 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700747 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 } else if (action == MotionEvent.ACTION_UP) {
749 cellInfo.cell = null;
750 cellInfo.cellX = -1;
751 cellInfo.cellY = -1;
752 cellInfo.spanX = 0;
753 cellInfo.spanY = 0;
754 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 setTag(cellInfo);
756 }
757
758 return false;
759 }
760
761 @Override
762 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700763 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 }
765
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700766 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700767 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 * @param x X coordinate of the point
769 * @param y Y coordinate of the point
770 * @param result Array of 2 ints to hold the x and y coordinate of the cell
771 */
772 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700773 final int hStartPadding = getPaddingLeft();
774 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775
776 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
777 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
778
Adam Cohend22015c2010-07-26 22:02:18 -0700779 final int xAxis = mCountX;
780 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781
782 if (result[0] < 0) result[0] = 0;
783 if (result[0] >= xAxis) result[0] = xAxis - 1;
784 if (result[1] < 0) result[1] = 0;
785 if (result[1] >= yAxis) result[1] = yAxis - 1;
786 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700787
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 /**
789 * Given a point, return the cell that most closely encloses that point
790 * @param x X coordinate of the point
791 * @param y Y coordinate of the point
792 * @param result Array of 2 ints to hold the x and y coordinate of the cell
793 */
794 void pointToCellRounded(int x, int y, int[] result) {
795 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
796 }
797
798 /**
799 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700800 *
801 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700803 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 * @param result Array of 2 ints to hold the x and y coordinate of the point
805 */
806 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700807 final int hStartPadding = getPaddingLeft();
808 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809
810 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
811 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
812 }
813
Adam Cohene3e27a82011-04-15 12:07:39 -0700814 /**
815 * Given a cell coordinate, return the point that represents the upper left corner of that cell
816 *
817 * @param cellX X coordinate of the cell
818 * @param cellY Y coordinate of the cell
819 *
820 * @param result Array of 2 ints to hold the x and y coordinate of the point
821 */
822 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700823 final int hStartPadding = getPaddingLeft();
824 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700825
826 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
827 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
828 }
829
Romain Guy84f296c2009-11-04 15:00:44 -0800830 int getCellWidth() {
831 return mCellWidth;
832 }
833
834 int getCellHeight() {
835 return mCellHeight;
836 }
837
Adam Cohend4844c32011-02-18 19:25:06 -0800838 int getWidthGap() {
839 return mWidthGap;
840 }
841
842 int getHeightGap() {
843 return mHeightGap;
844 }
845
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700846 Rect getContentRect(Rect r) {
847 if (r == null) {
848 r = new Rect();
849 }
850 int left = getPaddingLeft();
851 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700852 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
853 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700854 r.set(left, top, right, bottom);
855 return r;
856 }
857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 @Override
859 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
860 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700861
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700863 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
864
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
866 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700867
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
869 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
870 }
871
Adam Cohend22015c2010-07-26 22:02:18 -0700872 int numWidthGaps = mCountX - 1;
873 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874
Winson Chungece7f5b2010-10-22 14:54:12 -0700875 if (mWidthGap < 0 || mHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700876 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
877 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
878 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
879 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
880 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
881 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
882 int remainingHSpace = hFreeSpace - (numWidthGaps * mWidthGap);
883 int remainingVSpace = vFreeSpace - (numHeightGaps * mHeightGap);
884 mCellWidth = mOriginalCellWidth + remainingHSpace / mCountX;
885 mCellHeight = mOriginalCellHeight + remainingVSpace / mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886
Winson Chung4b825dcd2011-06-19 12:41:22 -0700887 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Winson Chungece7f5b2010-10-22 14:54:12 -0700888 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700889
Michael Jurka8c920dd2011-01-20 14:16:56 -0800890 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
891 int newWidth = widthSpecSize;
892 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700893 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700894 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700895 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700896 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700897 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700898 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700899 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800900
901 int count = getChildCount();
902 for (int i = 0; i < count; i++) {
903 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700904 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
905 mPaddingRight, MeasureSpec.EXACTLY);
906 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
907 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800908 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
909 }
910 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 }
912
913 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700914 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800917 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700918 child.layout(mPaddingLeft, mPaddingTop,
919 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 }
921 }
922
923 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700924 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
925 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700926 mBackgroundRect.set(0, 0, w, h);
Michael Jurka33945b22010-12-21 18:19:38 -0800927 updateGlowRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700928 }
929
930 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800932 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 }
934
935 @Override
936 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800937 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
Michael Jurka5f1c5092010-09-03 14:15:02 -0700940 public float getBackgroundAlpha() {
941 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700942 }
943
Michael Jurka742574b2011-02-02 23:51:01 -0800944 public void setFastBackgroundAlpha(float alpha) {
945 mBackgroundAlpha = alpha;
946 }
947
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700948 public void setBackgroundAlphaMultiplier(float multiplier) {
949 mBackgroundAlphaMultiplier = multiplier;
950 }
951
Adam Cohenddb82192010-11-10 16:32:54 -0800952 public float getBackgroundAlphaMultiplier() {
953 return mBackgroundAlphaMultiplier;
954 }
955
Michael Jurka5f1c5092010-09-03 14:15:02 -0700956 public void setBackgroundAlpha(float alpha) {
957 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700958 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700959 }
960
Michael Jurka5f1c5092010-09-03 14:15:02 -0700961 // Need to return true to let the view system know we know how to handle alpha-- this is
962 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
963 // versions
964 @Override
965 protected boolean onSetAlpha(int alpha) {
966 return true;
967 }
968
969 public void setAlpha(float alpha) {
970 setChildrenAlpha(alpha);
971 super.setAlpha(alpha);
972 }
973
Michael Jurka742574b2011-02-02 23:51:01 -0800974 public void setFastAlpha(float alpha) {
975 setFastChildrenAlpha(alpha);
976 super.setFastAlpha(alpha);
977 }
978
Michael Jurkadee05892010-07-27 10:01:56 -0700979 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700980 final int childCount = getChildCount();
981 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700982 getChildAt(i).setAlpha(alpha);
983 }
984 }
985
Michael Jurka742574b2011-02-02 23:51:01 -0800986 private void setFastChildrenAlpha(float alpha) {
987 final int childCount = getChildCount();
988 for (int i = 0; i < childCount; i++) {
989 getChildAt(i).setFastAlpha(alpha);
990 }
991 }
992
Patrick Dubroy440c3602010-07-13 17:50:32 -0700993 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800994 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700995 }
996
Adam Cohen76fc0852011-06-17 13:26:23 -0700997 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
998 int delay) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700999 CellLayoutChildren clc = getChildrenLayout();
1000 if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) {
1001 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1002 final ItemInfo info = (ItemInfo) child.getTag();
1003
1004 // We cancel any existing animations
1005 if (mReorderAnimators.containsKey(lp)) {
1006 mReorderAnimators.get(lp).cancel();
1007 mReorderAnimators.remove(lp);
1008 }
1009
1010 int oldX = lp.x;
1011 int oldY = lp.y;
1012 mOccupied[lp.cellX][lp.cellY] = false;
1013 mOccupied[cellX][cellY] = true;
1014
1015 lp.isLockedToGrid = true;
1016 lp.cellX = info.cellX = cellX;
1017 lp.cellY = info.cellY = cellY;
1018 clc.setupLp(lp);
1019 lp.isLockedToGrid = false;
1020 int newX = lp.x;
1021 int newY = lp.y;
1022
Adam Cohen76fc0852011-06-17 13:26:23 -07001023 lp.x = oldX;
1024 lp.y = oldY;
1025 child.requestLayout();
1026
Adam Cohenbfbfd262011-06-13 16:55:12 -07001027 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX);
1028 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY);
1029 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y);
1030 oa.setDuration(duration);
1031 mReorderAnimators.put(lp, oa);
1032 oa.addUpdateListener(new AnimatorUpdateListener() {
1033 public void onAnimationUpdate(ValueAnimator animation) {
1034 child.requestLayout();
1035 }
1036 });
1037 oa.addListener(new AnimatorListenerAdapter() {
1038 boolean cancelled = false;
1039 public void onAnimationEnd(Animator animation) {
1040 // If the animation was cancelled, it means that another animation
1041 // has interrupted this one, and we don't want to lock the item into
1042 // place just yet.
1043 if (!cancelled) {
1044 lp.isLockedToGrid = true;
1045 }
1046 if (mReorderAnimators.containsKey(lp)) {
1047 mReorderAnimators.remove(lp);
1048 }
1049 }
1050 public void onAnimationCancel(Animator animation) {
1051 cancelled = true;
1052 }
1053 });
Adam Cohen76fc0852011-06-17 13:26:23 -07001054 oa.setStartDelay(delay);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001055 oa.start();
1056 return true;
1057 }
1058 return false;
1059 }
1060
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001061 /**
1062 * Estimate where the top left cell of the dragged item will land if it is dropped.
1063 *
1064 * @param originX The X value of the top left corner of the item
1065 * @param originY The Y value of the top left corner of the item
1066 * @param spanX The number of horizontal cells that the item spans
1067 * @param spanY The number of vertical cells that the item spans
1068 * @param result The estimated drop cell X and Y.
1069 */
1070 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001071 final int countX = mCountX;
1072 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001073
Michael Jurkaa63c4522010-08-19 13:52:27 -07001074 // pointToCellRounded takes the top left of a cell but will pad that with
1075 // cellWidth/2 and cellHeight/2 when finding the matching cell
1076 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001077
1078 // If the item isn't fully on this screen, snap to the edges
1079 int rightOverhang = result[0] + spanX - countX;
1080 if (rightOverhang > 0) {
1081 result[0] -= rightOverhang; // Snap to right
1082 }
1083 result[0] = Math.max(0, result[0]); // Snap to left
1084 int bottomOverhang = result[1] + spanY - countY;
1085 if (bottomOverhang > 0) {
1086 result[1] -= bottomOverhang; // Snap to bottom
1087 }
1088 result[1] = Math.max(0, result[1]); // Snap to top
1089 }
1090
Joe Onorato4be866d2010-10-10 11:26:02 -07001091 void visualizeDropLocation(
1092 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
1093
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001094 final int oldDragCellX = mDragCell[0];
1095 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -07001096 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001097 if (v != null) {
1098 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1099 } else {
1100 mDragCenter.set(originX, originY);
1101 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001102
Adam Cohen2801caf2011-05-13 20:57:39 -07001103 if (dragOutline == null && v == null) {
1104 if (mCrosshairsDrawable != null) {
1105 invalidate();
1106 }
1107 return;
1108 }
1109
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001110 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001111 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001112 final int[] topLeft = mTmpPoint;
1113 cellToPoint(nearest[0], nearest[1], topLeft);
1114
Joe Onorato4be866d2010-10-10 11:26:02 -07001115 int left = topLeft[0];
1116 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001117
Winson Chunga9abd0e2010-10-27 17:18:37 -07001118 if (v != null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001119 // When drawing the drag outline, it did not account for margin offsets
1120 // added by the view's parent.
1121 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1122 left += lp.leftMargin;
1123 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001124
Adam Cohen99e8b402011-03-25 19:23:43 -07001125 // Offsets due to the size difference between the View and the dragOutline.
1126 // There is a size difference to account for the outer blur, which may lie
1127 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001128 left += (v.getWidth() - dragOutline.getWidth()) / 2;
1129 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001130 } else {
1131 // Center the drag outline in the cell
Winson Chung4b576be2011-04-27 17:40:20 -07001132 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1133 - dragOutline.getWidth()) / 2;
1134 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1135 - dragOutline.getHeight()) / 2;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001136 }
Winson Chung150fbab2010-09-29 17:14:26 -07001137
Joe Onorato4be866d2010-10-10 11:26:02 -07001138 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001139 mDragOutlineAnims[oldIndex].animateOut();
1140 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -07001141
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001142 mDragOutlines[mDragOutlineCurrent].set(left, top);
1143 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1144 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001145 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001146
1147 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1148 if (mCrosshairsDrawable != null) {
1149 invalidate();
1150 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001151 }
1152
Adam Cohene0310962011-04-18 16:15:31 -07001153 public void clearDragOutlines() {
1154 final int oldIndex = mDragOutlineCurrent;
1155 mDragOutlineAnims[oldIndex].animateOut();
1156 mDragCell[0] = -1;
1157 mDragCell[1] = -1;
1158 }
1159
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001161 * Find a vacant area that will fit the given bounds nearest the requested
1162 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001163 *
Romain Guy51afc022009-05-04 18:03:43 -07001164 * @param pixelX The X location at which you want to search for a vacant area.
1165 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001166 * @param spanX Horizontal span of the object.
1167 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001168 * @param result Array in which to place the result, or null (in which case a new array will
1169 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001170 * @return The X, Y cell of a vacant area that can contain this object,
1171 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001173 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001174 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1175 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001176 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001177
Michael Jurka6a1435d2010-09-27 17:35:12 -07001178 /**
1179 * Find a vacant area that will fit the given bounds nearest the requested
1180 * cell location. Uses Euclidean distance to score multiple vacant areas.
1181 *
1182 * @param pixelX The X location at which you want to search for a vacant area.
1183 * @param pixelY The Y location at which you want to search for a vacant area.
1184 * @param spanX Horizontal span of the object.
1185 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001186 * @param ignoreOccupied If true, the result can be an occupied cell
1187 * @param result Array in which to place the result, or null (in which case a new array will
1188 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001189 * @return The X, Y cell of a vacant area that can contain this object,
1190 * nearest the requested location.
1191 */
Adam Cohendf035382011-04-11 17:22:04 -07001192 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1193 boolean ignoreOccupied, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001194 // mark space take by ignoreView as available (method checks if ignoreView is null)
1195 markCellsAsUnoccupiedForView(ignoreView);
1196
Adam Cohene3e27a82011-04-15 12:07:39 -07001197 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1198 // to the center of the item, but we are searching based on the top-left cell, so
1199 // we translate the point over to correspond to the top-left.
1200 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1201 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1202
Jeff Sharkey70864282009-04-07 21:08:40 -07001203 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001204 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001205 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001206
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001207 final int countX = mCountX;
1208 final int countY = mCountY;
1209 final boolean[][] occupied = mOccupied;
1210
Winson Chungbbc60d82010-11-11 16:34:41 -08001211 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001212 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001213 for (int x = 0; x < countX - (spanX - 1); x++) {
Adam Cohendf035382011-04-11 17:22:04 -07001214 if (ignoreOccupied) {
1215 for (int i = 0; i < spanX; i++) {
1216 for (int j = 0; j < spanY; j++) {
1217 if (occupied[x + i][y + j]) {
1218 // small optimization: we can skip to after the column we
1219 // just found an occupied cell
1220 x += i;
1221 continue inner;
1222 }
Michael Jurkac28de512010-08-13 11:27:44 -07001223 }
1224 }
1225 }
Winson Chung0be025d2011-05-23 17:45:09 -07001226 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001227 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228
Michael Jurkac28de512010-08-13 11:27:44 -07001229 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1230 + Math.pow(cellXY[1] - pixelY, 2));
1231 if (distance <= bestDistance) {
1232 bestDistance = distance;
1233 bestXY[0] = x;
1234 bestXY[1] = y;
1235 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 }
1237 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001238 // re-mark space taken by ignoreView as occupied
1239 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240
Adam Cohenc0dcf592011-06-01 15:30:43 -07001241 // Return -1, -1 if no suitable location found
1242 if (bestDistance == Double.MAX_VALUE) {
1243 bestXY[0] = -1;
1244 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001245 }
Adam Cohenc0dcf592011-06-01 15:30:43 -07001246 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001248
Adam Cohendf035382011-04-11 17:22:04 -07001249 /**
1250 * Find a vacant area that will fit the given bounds nearest the requested
1251 * cell location. Uses Euclidean distance to score multiple vacant areas.
1252 *
1253 * @param pixelX The X location at which you want to search for a vacant area.
1254 * @param pixelY The Y location at which you want to search for a vacant area.
1255 * @param spanX Horizontal span of the object.
1256 * @param spanY Vertical span of the object.
1257 * @param ignoreView Considers space occupied by this view as unoccupied
1258 * @param result Previously returned value to possibly recycle.
1259 * @return The X, Y cell of a vacant area that can contain this object,
1260 * nearest the requested location.
1261 */
1262 int[] findNearestVacantArea(
1263 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1264 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1265 }
1266
1267 /**
1268 * Find a starting cell position that will fit the given bounds nearest the requested
1269 * cell location. Uses Euclidean distance to score multiple vacant areas.
1270 *
1271 * @param pixelX The X location at which you want to search for a vacant area.
1272 * @param pixelY The Y location at which you want to search for a vacant area.
1273 * @param spanX Horizontal span of the object.
1274 * @param spanY Vertical span of the object.
1275 * @param ignoreView Considers space occupied by this view as unoccupied
1276 * @param result Previously returned value to possibly recycle.
1277 * @return The X, Y cell of a vacant area that can contain this object,
1278 * nearest the requested location.
1279 */
1280 int[] findNearestArea(
1281 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1282 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1283 }
1284
Michael Jurka0280c3b2010-09-17 15:00:07 -07001285 boolean existsEmptyCell() {
1286 return findCellForSpan(null, 1, 1);
1287 }
1288
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001290 * Finds the upper-left coordinate of the first rectangle in the grid that can
1291 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1292 * then this method will only return coordinates for rectangles that contain the cell
1293 * (intersectX, intersectY)
1294 *
1295 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1296 * can be found.
1297 * @param spanX The horizontal span of the cell we want to find.
1298 * @param spanY The vertical span of the cell we want to find.
1299 *
1300 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001301 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001302 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1303 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1304 }
1305
1306 /**
1307 * Like above, but ignores any cells occupied by the item "ignoreView"
1308 *
1309 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1310 * can be found.
1311 * @param spanX The horizontal span of the cell we want to find.
1312 * @param spanY The vertical span of the cell we want to find.
1313 * @param ignoreView The home screen item we should treat as not occupying any space
1314 * @return
1315 */
1316 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1317 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1318 }
1319
1320 /**
1321 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1322 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1323 *
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 * @param ignoreView The home screen item we should treat as not occupying any space
1327 * @param intersectX The X coordinate of the cell that we should try to overlap
1328 * @param intersectX The Y coordinate of the cell that we should try to overlap
1329 *
1330 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1331 */
1332 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1333 int intersectX, int intersectY) {
1334 return findCellForSpanThatIntersectsIgnoring(
1335 cellXY, spanX, spanY, intersectX, intersectY, null);
1336 }
1337
1338 /**
1339 * The superset of the above two methods
1340 */
1341 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1342 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001343 // mark space take by ignoreView as available (method checks if ignoreView is null)
1344 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001345
Michael Jurka28750fb2010-09-24 17:43:49 -07001346 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001347 while (true) {
1348 int startX = 0;
1349 if (intersectX >= 0) {
1350 startX = Math.max(startX, intersectX - (spanX - 1));
1351 }
1352 int endX = mCountX - (spanX - 1);
1353 if (intersectX >= 0) {
1354 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1355 }
1356 int startY = 0;
1357 if (intersectY >= 0) {
1358 startY = Math.max(startY, intersectY - (spanY - 1));
1359 }
1360 int endY = mCountY - (spanY - 1);
1361 if (intersectY >= 0) {
1362 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1363 }
1364
Winson Chungbbc60d82010-11-11 16:34:41 -08001365 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001366 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08001367 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001368 for (int i = 0; i < spanX; i++) {
1369 for (int j = 0; j < spanY; j++) {
1370 if (mOccupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08001371 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07001372 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08001373 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001374 continue inner;
1375 }
1376 }
1377 }
1378 if (cellXY != null) {
1379 cellXY[0] = x;
1380 cellXY[1] = y;
1381 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001382 foundCell = true;
1383 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001384 }
1385 }
1386 if (intersectX == -1 && intersectY == -1) {
1387 break;
1388 } else {
1389 // if we failed to find anything, try again but without any requirements of
1390 // intersecting
1391 intersectX = -1;
1392 intersectY = -1;
1393 continue;
1394 }
1395 }
1396
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001397 // re-mark space taken by ignoreView as occupied
1398 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001399 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001400 }
1401
1402 /**
Winson Chungc07918d2011-07-01 15:35:26 -07001403 * A drag event has begun over this layout.
1404 * It may have begun over this layout (in which case onDragChild is called first),
1405 * or it may have begun on another layout.
1406 */
1407 void onDragEnter() {
1408 if (!mDragging) {
1409 // Fade in the drag indicators
1410 if (mCrosshairsAnimator != null) {
1411 mCrosshairsAnimator.animateIn();
1412 }
1413 }
1414 mDragging = true;
1415 }
1416
1417 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001418 * Called when drag has left this CellLayout or has been completed (successfully or not)
1419 */
1420 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001421 // This can actually be called when we aren't in a drag, e.g. when adding a new
1422 // item to this layout via the customize drawer.
1423 // Guard against that case.
1424 if (mDragging) {
1425 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001426
Joe Onorato4be866d2010-10-10 11:26:02 -07001427 // Fade out the drag indicators
1428 if (mCrosshairsAnimator != null) {
1429 mCrosshairsAnimator.animateOut();
1430 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001431 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001432
1433 // Invalidate the drag data
1434 mDragCell[0] = -1;
1435 mDragCell[1] = -1;
1436 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1437 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1438
Michael Jurka33945b22010-12-21 18:19:38 -08001439 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001440 }
1441
1442 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001443 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001444 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001445 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 *
1447 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 */
Adam Cohen716b51e2011-06-30 12:09:54 -07001449 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001450 if (child != null) {
1451 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001452 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001453 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07001454 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001455 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 }
1457
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 /**
1459 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001460 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 * @param child The child that is being dragged
1462 */
1463 void onDragChild(View child) {
1464 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1465 lp.isDragging = true;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001466 }
1467
1468 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001470 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 * @param cellX X coordinate of upper left corner expressed as a cell position
1472 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001473 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001475 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001477 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 final int cellWidth = mCellWidth;
1479 final int cellHeight = mCellHeight;
1480 final int widthGap = mWidthGap;
1481 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001482
Winson Chung4b825dcd2011-06-19 12:41:22 -07001483 final int hStartPadding = getPaddingLeft();
1484 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07001485
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1487 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1488
1489 int x = hStartPadding + cellX * (cellWidth + widthGap);
1490 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001491
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001492 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001496 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001498 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 * @param width Width in pixels
1500 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001501 * @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 -08001502 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001503 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001504 return rectToCell(getResources(), width, height, result);
1505 }
1506
1507 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 // Always assume we're working with the smallest span to make sure we
1509 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001510 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1511 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001513
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 // Always round up to next largest cell
1515 int spanX = (width + smallerSize) / smallerSize;
1516 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001517
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001518 if (result == null) {
1519 return new int[] { spanX, spanY };
1520 }
1521 result[0] = spanX;
1522 result[1] = spanY;
1523 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 }
1525
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001526 public int[] cellSpansToSize(int hSpans, int vSpans) {
1527 int[] size = new int[2];
1528 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
1529 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
1530 return size;
1531 }
1532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08001534 * Calculate the grid spans needed to fit given item
1535 */
1536 public void calculateSpans(ItemInfo info) {
1537 final int minWidth;
1538 final int minHeight;
1539
1540 if (info instanceof LauncherAppWidgetInfo) {
1541 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
1542 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
1543 } else if (info instanceof PendingAddWidgetInfo) {
1544 minWidth = ((PendingAddWidgetInfo) info).minWidth;
1545 minHeight = ((PendingAddWidgetInfo) info).minHeight;
1546 } else {
1547 // It's not a widget, so it must be 1x1
1548 info.spanX = info.spanY = 1;
1549 return;
1550 }
1551 int[] spans = rectToCell(minWidth, minHeight, null);
1552 info.spanX = spans[0];
1553 info.spanY = spans[1];
1554 }
1555
1556 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 * Find the first vacant cell, if there is one.
1558 *
1559 * @param vacant Holds the x and y coordinate of the vacant cell
1560 * @param spanX Horizontal cell span.
1561 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001562 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 * @return True if a vacant cell was found
1564 */
1565 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566
Michael Jurka0280c3b2010-09-17 15:00:07 -07001567 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
1569
1570 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1571 int xCount, int yCount, boolean[][] occupied) {
1572
Adam Cohen2801caf2011-05-13 20:57:39 -07001573 for (int y = 0; y < yCount; y++) {
1574 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 boolean available = !occupied[x][y];
1576out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1577 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1578 available = available && !occupied[i][j];
1579 if (!available) break out;
1580 }
1581 }
1582
1583 if (available) {
1584 vacant[0] = x;
1585 vacant[1] = y;
1586 return true;
1587 }
1588 }
1589 }
1590
1591 return false;
1592 }
1593
Michael Jurka0280c3b2010-09-17 15:00:07 -07001594 private void clearOccupiedCells() {
1595 for (int x = 0; x < mCountX; x++) {
1596 for (int y = 0; y < mCountY; y++) {
1597 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 }
1599 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001600 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601
Adam Cohen1b607ed2011-03-03 17:26:50 -08001602 /**
1603 * Given a view, determines how much that view can be expanded in all directions, in terms of
1604 * whether or not there are other items occupying adjacent cells. Used by the
1605 * AppWidgetResizeFrame to determine how the widget can be resized.
1606 */
Adam Cohend4844c32011-02-18 19:25:06 -08001607 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08001608 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08001609 boolean flag;
1610
Adam Cohen1b607ed2011-03-03 17:26:50 -08001611 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001612 for (int x = lp.cellX - 1; x >= 0; x--) {
1613 flag = false;
1614 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1615 if (mOccupied[x][y]) flag = true;
1616 }
1617 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001618 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08001619 }
1620
Adam Cohen1b607ed2011-03-03 17:26:50 -08001621 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001622 for (int y = lp.cellY - 1; y >= 0; y--) {
1623 flag = false;
1624 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1625 if (mOccupied[x][y]) flag = true;
1626 }
1627 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001628 expandability[AppWidgetResizeFrame.TOP]++;
1629 }
Adam Cohend4844c32011-02-18 19:25:06 -08001630
Adam Cohen1b607ed2011-03-03 17:26:50 -08001631 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001632 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
1633 flag = false;
1634 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
1635 if (mOccupied[x][y]) flag = true;
1636 }
1637 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001638 expandability[AppWidgetResizeFrame.RIGHT]++;
1639 }
Adam Cohend4844c32011-02-18 19:25:06 -08001640
Adam Cohen1b607ed2011-03-03 17:26:50 -08001641 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08001642 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
1643 flag = false;
1644 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
1645 if (mOccupied[x][y]) flag = true;
1646 }
1647 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001648 expandability[AppWidgetResizeFrame.BOTTOM]++;
1649 }
Adam Cohend4844c32011-02-18 19:25:06 -08001650 }
1651
Michael Jurka0280c3b2010-09-17 15:00:07 -07001652 public void onMove(View view, int newCellX, int newCellY) {
1653 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1654 markCellsAsUnoccupiedForView(view);
1655 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657
Adam Cohend4844c32011-02-18 19:25:06 -08001658 public void markCellsAsOccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001659 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001660 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1661 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1662 }
1663
Adam Cohend4844c32011-02-18 19:25:06 -08001664 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001665 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001666 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1667 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1668 }
1669
1670 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1671 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1672 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1673 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 }
1675 }
1676 }
1677
Adam Cohen2801caf2011-05-13 20:57:39 -07001678 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001679 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001680 (Math.max((mCountX - 1), 0) * mWidthGap);
1681 }
1682
1683 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07001684 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07001685 (Math.max((mCountY - 1), 0) * mHeightGap);
1686 }
1687
Michael Jurka66d72172011-04-12 16:29:25 -07001688 public boolean isOccupied(int x, int y) {
1689 if (x < mCountX && y < mCountY) {
1690 return mOccupied[x][y];
1691 } else {
1692 throw new RuntimeException("Position exceeds the bound of this CellLayout");
1693 }
1694 }
1695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 @Override
1697 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1698 return new CellLayout.LayoutParams(getContext(), attrs);
1699 }
1700
1701 @Override
1702 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1703 return p instanceof CellLayout.LayoutParams;
1704 }
1705
1706 @Override
1707 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1708 return new CellLayout.LayoutParams(p);
1709 }
1710
Winson Chungaafa03c2010-06-11 17:34:16 -07001711 public static class CellLayoutAnimationController extends LayoutAnimationController {
1712 public CellLayoutAnimationController(Animation animation, float delay) {
1713 super(animation, delay);
1714 }
1715
1716 @Override
1717 protected long getDelayForView(View view) {
1718 return (int) (Math.random() * 150);
1719 }
1720 }
1721
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1723 /**
1724 * Horizontal location of the item in the grid.
1725 */
1726 @ViewDebug.ExportedProperty
1727 public int cellX;
1728
1729 /**
1730 * Vertical location of the item in the grid.
1731 */
1732 @ViewDebug.ExportedProperty
1733 public int cellY;
1734
1735 /**
1736 * Number of cells spanned horizontally by the item.
1737 */
1738 @ViewDebug.ExportedProperty
1739 public int cellHSpan;
1740
1741 /**
1742 * Number of cells spanned vertically by the item.
1743 */
1744 @ViewDebug.ExportedProperty
1745 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001746
Adam Cohen1b607ed2011-03-03 17:26:50 -08001747 /**
1748 * Indicates whether the item will set its x, y, width and height parameters freely,
1749 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
1750 */
Adam Cohend4844c32011-02-18 19:25:06 -08001751 public boolean isLockedToGrid = true;
1752
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 /**
1754 * Is this item currently being dragged
1755 */
1756 public boolean isDragging;
1757
1758 // X coordinate of the view in the layout.
1759 @ViewDebug.ExportedProperty
1760 int x;
1761 // Y coordinate of the view in the layout.
1762 @ViewDebug.ExportedProperty
1763 int y;
1764
Romain Guy84f296c2009-11-04 15:00:44 -08001765 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001766
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 public LayoutParams(Context c, AttributeSet attrs) {
1768 super(c, attrs);
1769 cellHSpan = 1;
1770 cellVSpan = 1;
1771 }
1772
1773 public LayoutParams(ViewGroup.LayoutParams source) {
1774 super(source);
1775 cellHSpan = 1;
1776 cellVSpan = 1;
1777 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001778
1779 public LayoutParams(LayoutParams source) {
1780 super(source);
1781 this.cellX = source.cellX;
1782 this.cellY = source.cellY;
1783 this.cellHSpan = source.cellHSpan;
1784 this.cellVSpan = source.cellVSpan;
1785 }
1786
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001788 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 this.cellX = cellX;
1790 this.cellY = cellY;
1791 this.cellHSpan = cellHSpan;
1792 this.cellVSpan = cellVSpan;
1793 }
1794
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001795 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08001796 if (isLockedToGrid) {
1797 final int myCellHSpan = cellHSpan;
1798 final int myCellVSpan = cellVSpan;
1799 final int myCellX = cellX;
1800 final int myCellY = cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08001801
Adam Cohend4844c32011-02-18 19:25:06 -08001802 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1803 leftMargin - rightMargin;
1804 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1805 topMargin - bottomMargin;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001806 x = myCellX * (cellWidth + widthGap) + leftMargin;
1807 y = myCellY * (cellHeight + heightGap) + topMargin;
Adam Cohend4844c32011-02-18 19:25:06 -08001808 }
1809 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001810
Winson Chungaafa03c2010-06-11 17:34:16 -07001811 public String toString() {
1812 return "(" + this.cellX + ", " + this.cellY + ")";
1813 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001814
1815 public void setWidth(int width) {
1816 this.width = width;
1817 }
1818
1819 public int getWidth() {
1820 return width;
1821 }
1822
1823 public void setHeight(int height) {
1824 this.height = height;
1825 }
1826
1827 public int getHeight() {
1828 return height;
1829 }
1830
1831 public void setX(int x) {
1832 this.x = x;
1833 }
1834
1835 public int getX() {
1836 return x;
1837 }
1838
1839 public void setY(int y) {
1840 this.y = y;
1841 }
1842
1843 public int getY() {
1844 return y;
1845 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 }
1847
Michael Jurka0280c3b2010-09-17 15:00:07 -07001848 // This class stores info for two purposes:
1849 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1850 // its spanX, spanY, and the screen it is on
1851 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1852 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1853 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07001854 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001856 int cellX = -1;
1857 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 int spanX;
1859 int spanY;
1860 int screen;
1861 boolean valid;
1862
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 @Override
1864 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001865 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1866 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 }
1868 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869}