blob: 1223d1c356d78da2392297e134818a17d791a7c6 [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;
Chet Haase00397b12010-10-07 11:13:10 -070021import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040025import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070026import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070027import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080029import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070031import android.graphics.Point;
32import android.graphics.PointF;
Adam Cohenb5ba0972011-09-07 18:02:31 -070033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080036import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070037import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070038import android.graphics.drawable.NinePatchDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.MotionEvent;
42import android.view.View;
43import android.view.ViewDebug;
44import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070046import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Adam Cohen66396872011-04-15 17:50:36 -070049import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070050import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070051
Adam Cohen69ce2e52011-07-03 19:25:21 -070052import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070054import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080055import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070056
Michael Jurkabdb5c532011-02-01 15:05:06 -080057public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070058 static final String TAG = "CellLayout";
59
Adam Cohen2acce882012-03-28 19:03:19 -070060 private Launcher mLauncher;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 private int mCellWidth;
62 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070063
Adam Cohend22015c2010-07-26 22:02:18 -070064 private int mCountX;
65 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
Adam Cohen234c4cd2011-07-17 21:03:04 -070067 private int mOriginalWidthGap;
68 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 private int mWidthGap;
70 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070071 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080072 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073
74 private final Rect mRect = new Rect();
75 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070076
Patrick Dubroyde7658b2010-09-27 11:15:43 -070077 // These are temporary variables to prevent having to allocate a new object just to
78 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070079 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070080 private final int[] mTmpPoint = new int[2];
81 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070082 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070083
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080085 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070086 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087
Michael Jurkadee05892010-07-27 10:01:56 -070088 private OnTouchListener mInterceptTouchListener;
89
Adam Cohen69ce2e52011-07-03 19:25:21 -070090 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070091 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070092
Adam Cohenb5ba0972011-09-07 18:02:31 -070093 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070094 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070095 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070096
Michael Jurka33945b22010-12-21 18:19:38 -080097 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080098 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -070099 private Drawable mOverScrollForegroundDrawable;
100 private Drawable mOverScrollLeft;
101 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700102 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700103 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700104 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700105
Michael Jurka33945b22010-12-21 18:19:38 -0800106 // If we're actively dragging something over this screen, mIsDragOverlapping is true
107 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700108 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700109
Winson Chung150fbab2010-09-29 17:14:26 -0700110 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700111 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800112 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700113 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700114 private InterruptibleInOutAnimator[] mDragOutlineAnims =
115 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700116
117 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700118 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700119 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700120
Patrick Dubroy96864c32011-03-10 17:17:23 -0800121 private BubbleTextView mPressedOrFocusedIcon;
122
Adam Cohen482ed822012-03-02 14:15:13 -0800123 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
124 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700125 private HashMap<View, ReorderHintAnimation>
126 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
127
128 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700129
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700130 // When a drag operation is in progress, holds the nearest cell to the touch point
131 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Joe Onorato4be866d2010-10-10 11:26:02 -0700133 private boolean mDragging = false;
134
Patrick Dubroyce34a972010-10-19 10:34:32 -0700135 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700136 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700137
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800138 private boolean mIsHotseat = false;
Winson Chungeecf02d2012-03-02 17:14:58 -0800139 private float mChildScale = 1f;
140 private float mHotseatChildScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800141
Adam Cohen482ed822012-03-02 14:15:13 -0800142 public static final int MODE_DRAG_OVER = 0;
143 public static final int MODE_ON_DROP = 1;
144 public static final int MODE_ON_DROP_EXTERNAL = 2;
145 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700146 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800147 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
148
Adam Cohena897f392012-04-27 18:12:05 -0700149 static final int LANDSCAPE = 0;
150 static final int PORTRAIT = 1;
151
Adam Cohen19f37922012-03-21 11:59:11 -0700152 private static final float REORDER_HINT_MAGNITUDE = 0.27f;
153 private static final int REORDER_ANIMATION_DURATION = 150;
154 private float mReorderHintAnimationMagnitude;
155
Adam Cohen482ed822012-03-02 14:15:13 -0800156 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
157 private Rect mOccupiedRect = new Rect();
158 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700159 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700160 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700161 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 public CellLayout(Context context) {
164 this(context, null);
165 }
166
167 public CellLayout(Context context, AttributeSet attrs) {
168 this(context, attrs, 0);
169 }
170
171 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
172 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700173 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700174
175 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
176 // the user where a dragged item will land when dropped.
177 setWillNotDraw(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700178 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
181
Adam Cohenf4bd5792012-04-27 11:35:29 -0700182 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
183 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700184 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
185 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700186 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700187 mCountX = LauncherModel.getCellCountX();
188 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700189 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800190 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700191 mPreviousReorderDirection[0] = INVALID_DIRECTION;
192 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
194 a.recycle();
195
196 setAlwaysDrawnWithCacheEnabled(false);
197
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700198 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700199
Winson Chung967289b2011-06-30 18:09:30 -0700200 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700201 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800202
Adam Cohenb5ba0972011-09-07 18:02:31 -0700203 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
204 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
205 mForegroundPadding =
206 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800207
Adam Cohen19f37922012-03-21 11:59:11 -0700208 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
209 res.getDimensionPixelSize(R.dimen.app_icon_size));
210
Winson Chungb26f3d62011-06-02 10:49:29 -0700211 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700212 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700213
Winson Chungeecf02d2012-03-02 17:14:58 -0800214 int iconScale = res.getInteger(R.integer.app_icon_scale_percent);
215 if (iconScale >= 0) {
216 mChildScale = iconScale / 100f;
217 }
218 int hotseatIconScale = res.getInteger(R.integer.app_icon_hotseat_scale_percent);
219 if (hotseatIconScale >= 0) {
220 mHotseatChildScale = hotseatIconScale / 100f;
221 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800222
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700223 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700224
Patrick Dubroyce34a972010-10-19 10:34:32 -0700225 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700226
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700227
Winson Chungb8c69f32011-10-19 21:36:08 -0700228 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700229 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800230 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700231 }
232
233 // When dragging things around the home screens, we show a green outline of
234 // where the item will land. The outlines gradually fade out, leaving a trail
235 // behind the drag path.
236 // Set up all the animations that are used to implement this fading.
237 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700238 final float fromAlphaValue = 0;
239 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700240
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700241 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700242
243 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700244 final InterruptibleInOutAnimator anim =
245 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700246 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700247 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700248 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700249 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700250 final Bitmap outline = (Bitmap)anim.getTag();
251
252 // If an animation is started and then stopped very quickly, we can still
253 // get spurious updates we've cleared the tag. Guard against this.
254 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700255 @SuppressWarnings("all") // suppress dead code warning
256 final boolean debug = false;
257 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700258 Object val = animation.getAnimatedValue();
259 Log.d(TAG, "anim " + thisIndex + " update: " + val +
260 ", isStopped " + anim.isStopped());
261 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700262 // Try to prevent it from continuing to run
263 animation.cancel();
264 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700265 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800266 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700267 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700268 }
269 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700270 // The animation holds a reference to the drag outline bitmap as long is it's
271 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700272 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700273 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700274 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700275 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700276 anim.setTag(null);
277 }
278 }
279 });
280 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700281 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700282
Michael Jurka18014792010-10-14 09:01:34 -0700283 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700284 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800285
Michael Jurkaa52570f2012-03-20 03:18:20 -0700286 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
287 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
288 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700289 }
290
Michael Jurkaf6440da2011-04-05 14:50:34 -0700291 static int widthInPortrait(Resources r, int numCells) {
292 // We use this method from Workspace to figure out how many rows/columns Launcher should
293 // have. We ignore the left/right padding on CellLayout because it turns out in our design
294 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700295 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700296 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
297 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700298
Winson Chung4b825dcd2011-06-19 12:41:22 -0700299 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700300 }
301
Michael Jurkaf6440da2011-04-05 14:50:34 -0700302 static int heightInLandscape(Resources r, int numCells) {
303 // We use this method from Workspace to figure out how many rows/columns Launcher should
304 // have. We ignore the left/right padding on CellLayout because it turns out in our design
305 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700306 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700307 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
308 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700309
Winson Chung4b825dcd2011-06-19 12:41:22 -0700310 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700311 }
312
Adam Cohen2801caf2011-05-13 20:57:39 -0700313 public void enableHardwareLayers() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700314 mShortcutsAndWidgets.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700315 }
316
317 public void setGridSize(int x, int y) {
318 mCountX = x;
319 mCountY = y;
320 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800321 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700322 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700323 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700324 }
325
Patrick Dubroy96864c32011-03-10 17:17:23 -0800326 private void invalidateBubbleTextView(BubbleTextView icon) {
327 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700328 invalidate(icon.getLeft() + getPaddingLeft() - padding,
329 icon.getTop() + getPaddingTop() - padding,
330 icon.getRight() + getPaddingLeft() + padding,
331 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800332 }
333
Adam Cohenb5ba0972011-09-07 18:02:31 -0700334 void setOverScrollAmount(float r, boolean left) {
335 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
336 mOverScrollForegroundDrawable = mOverScrollLeft;
337 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
338 mOverScrollForegroundDrawable = mOverScrollRight;
339 }
340
341 mForegroundAlpha = (int) Math.round((r * 255));
342 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
343 invalidate();
344 }
345
Patrick Dubroy96864c32011-03-10 17:17:23 -0800346 void setPressedOrFocusedIcon(BubbleTextView icon) {
347 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
348 // requires an expanded clip rect (due to the glow's blur radius)
349 BubbleTextView oldIcon = mPressedOrFocusedIcon;
350 mPressedOrFocusedIcon = icon;
351 if (oldIcon != null) {
352 invalidateBubbleTextView(oldIcon);
353 }
354 if (mPressedOrFocusedIcon != null) {
355 invalidateBubbleTextView(mPressedOrFocusedIcon);
356 }
357 }
358
Michael Jurka33945b22010-12-21 18:19:38 -0800359 void setIsDragOverlapping(boolean isDragOverlapping) {
360 if (mIsDragOverlapping != isDragOverlapping) {
361 mIsDragOverlapping = isDragOverlapping;
362 invalidate();
363 }
364 }
365
366 boolean getIsDragOverlapping() {
367 return mIsDragOverlapping;
368 }
369
Adam Cohenebea84d2011-11-09 17:20:41 -0800370 protected void setOverscrollTransformsDirty(boolean dirty) {
371 mScrollingTransformsDirty = dirty;
372 }
373
374 protected void resetOverscrollTransforms() {
375 if (mScrollingTransformsDirty) {
376 setOverscrollTransformsDirty(false);
377 setTranslationX(0);
378 setRotationY(0);
379 // It doesn't matter if we pass true or false here, the important thing is that we
380 // pass 0, which results in the overscroll drawable not being drawn any more.
381 setOverScrollAmount(0, false);
382 setPivotX(getMeasuredWidth() / 2);
383 setPivotY(getMeasuredHeight() / 2);
384 }
385 }
386
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700387 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700388 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700389 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
390 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
391 // When we're small, we are either drawn normally or in the "accepts drops" state (during
392 // a drag). However, we also drag the mini hover background *over* one of those two
393 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700394 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700395 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800396
397 if (mIsDragOverlapping) {
398 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700399 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700400 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700401 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700402 }
Michael Jurka33945b22010-12-21 18:19:38 -0800403
404 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
405 bg.setBounds(mBackgroundRect);
406 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700407 }
Romain Guya6abce82009-11-10 02:54:41 -0800408
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700409 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700410 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700411 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700412 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800413 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700414 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700415 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800416 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700417 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700418 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800419
420 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
421 // requires an expanded clip rect (due to the glow's blur radius)
422 if (mPressedOrFocusedIcon != null) {
423 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
424 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
425 if (b != null) {
426 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700427 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
428 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800429 null);
430 }
431 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700432
Adam Cohen482ed822012-03-02 14:15:13 -0800433 if (DEBUG_VISUALIZE_OCCUPIED) {
434 int[] pt = new int[2];
435 ColorDrawable cd = new ColorDrawable(Color.RED);
436 cd.setBounds(0, 0, 80, 80);
437 for (int i = 0; i < mCountX; i++) {
438 for (int j = 0; j < mCountY; j++) {
439 if (mOccupied[i][j]) {
440 cellToPoint(i, j, pt);
441 canvas.save();
442 canvas.translate(pt[0], pt[1]);
443 cd.draw(canvas);
444 canvas.restore();
445 }
446 }
447 }
448 }
449
Adam Cohen69ce2e52011-07-03 19:25:21 -0700450 // The folder outer / inner ring image(s)
451 for (int i = 0; i < mFolderOuterRings.size(); i++) {
452 FolderRingAnimator fra = mFolderOuterRings.get(i);
453
454 // Draw outer ring
455 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
456 int width = (int) fra.getOuterRingSize();
457 int height = width;
458 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
459
460 int centerX = mTempLocation[0] + mCellWidth / 2;
461 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
462
463 canvas.save();
464 canvas.translate(centerX - width / 2, centerY - height / 2);
465 d.setBounds(0, 0, width, height);
466 d.draw(canvas);
467 canvas.restore();
468
469 // Draw inner ring
470 d = FolderRingAnimator.sSharedInnerRingDrawable;
471 width = (int) fra.getInnerRingSize();
472 height = width;
473 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
474
475 centerX = mTempLocation[0] + mCellWidth / 2;
476 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
477 canvas.save();
478 canvas.translate(centerX - width / 2, centerY - width / 2);
479 d.setBounds(0, 0, width, height);
480 d.draw(canvas);
481 canvas.restore();
482 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700483
484 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
485 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
486 int width = d.getIntrinsicWidth();
487 int height = d.getIntrinsicHeight();
488
489 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
490 int centerX = mTempLocation[0] + mCellWidth / 2;
491 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
492
493 canvas.save();
494 canvas.translate(centerX - width / 2, centerY - width / 2);
495 d.setBounds(0, 0, width, height);
496 d.draw(canvas);
497 canvas.restore();
498 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700499 }
500
Adam Cohenb5ba0972011-09-07 18:02:31 -0700501 @Override
502 protected void dispatchDraw(Canvas canvas) {
503 super.dispatchDraw(canvas);
504 if (mForegroundAlpha > 0) {
505 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
506 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
507 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
508 mOverScrollForegroundDrawable.draw(canvas);
509 p.setXfermode(null);
510 }
511 }
512
Adam Cohen69ce2e52011-07-03 19:25:21 -0700513 public void showFolderAccept(FolderRingAnimator fra) {
514 mFolderOuterRings.add(fra);
515 }
516
517 public void hideFolderAccept(FolderRingAnimator fra) {
518 if (mFolderOuterRings.contains(fra)) {
519 mFolderOuterRings.remove(fra);
520 }
521 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700522 }
523
Adam Cohenc51934b2011-07-26 21:07:43 -0700524 public void setFolderLeaveBehindCell(int x, int y) {
525 mFolderLeaveBehindCell[0] = x;
526 mFolderLeaveBehindCell[1] = y;
527 invalidate();
528 }
529
530 public void clearFolderLeaveBehind() {
531 mFolderLeaveBehindCell[0] = -1;
532 mFolderLeaveBehindCell[1] = -1;
533 invalidate();
534 }
535
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700536 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700537 public boolean shouldDelayChildPressedState() {
538 return false;
539 }
540
541 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700542 public void cancelLongPress() {
543 super.cancelLongPress();
544
545 // Cancel long press for all children
546 final int count = getChildCount();
547 for (int i = 0; i < count; i++) {
548 final View child = getChildAt(i);
549 child.cancelLongPress();
550 }
551 }
552
Michael Jurkadee05892010-07-27 10:01:56 -0700553 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
554 mInterceptTouchListener = listener;
555 }
556
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700558 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 }
560
561 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700562 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 }
564
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800565 public void setIsHotseat(boolean isHotseat) {
566 mIsHotseat = isHotseat;
567 }
568
Winson Chungeecf02d2012-03-02 17:14:58 -0800569 public float getChildrenScale() {
570 return mIsHotseat ? mHotseatChildScale : mChildScale;
571 }
572
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700573 public boolean addViewToCellLayout(
574 View child, int index, int childId, LayoutParams params, boolean markCells) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800575 return addViewToCellLayout(child, index, childId, params, markCells, false);
576 }
577
Winson Chungeecf02d2012-03-02 17:14:58 -0800578 private void scaleChild(BubbleTextView bubbleChild, float pivot, float scale) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800579 // If we haven't measured the child yet, do it now
580 // (this happens if we're being dropped from all-apps
581 if (bubbleChild.getLayoutParams() instanceof LayoutParams &&
582 (bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700583 getShortcutsAndWidgets().measureChild(bubbleChild);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800584 }
Andrew Flynnbc239a12012-03-06 11:39:49 -0800585
Andrew Flynnbc239a12012-03-06 11:39:49 -0800586 bubbleChild.setScaleX(scale);
587 bubbleChild.setScaleY(scale);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800588 }
589
590 private void resetChild(BubbleTextView bubbleChild) {
591 bubbleChild.setScaleX(1f);
592 bubbleChild.setScaleY(1f);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800593
594 bubbleChild.setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
595 }
596
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800597 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
598 boolean markCells, boolean allApps) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700599 final LayoutParams lp = params;
600
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800601 // Hotseat icons - scale down and remove text
602 // Don't scale the all apps button
603 // scale percent set to -1 means do not scale
604 // Only scale BubbleTextViews
605 if (child instanceof BubbleTextView) {
606 BubbleTextView bubbleChild = (BubbleTextView) child;
607
Andrew Flynnbc239a12012-03-06 11:39:49 -0800608 // Start the child with 100% scale and visible text
609 resetChild(bubbleChild);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800610
Winson Chungeecf02d2012-03-02 17:14:58 -0800611 if (mIsHotseat && !allApps && mHotseatChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800612 // Scale/make transparent for a hotseat
Winson Chungeecf02d2012-03-02 17:14:58 -0800613 scaleChild(bubbleChild, 0f, mHotseatChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800614
Andrew Flynnbc239a12012-03-06 11:39:49 -0800615 bubbleChild.setTextColor(getResources().getColor(android.R.color.transparent));
Winson Chungeecf02d2012-03-02 17:14:58 -0800616 } else if (mChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800617 // Else possibly still scale it if we need to for smaller icons
Winson Chungeecf02d2012-03-02 17:14:58 -0800618 scaleChild(bubbleChild, 0f, mChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800619 }
620 }
621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 // Generate an id for each view, this assumes we have at most 256x256 cells
623 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700624 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700625 // If the horizontal or vertical span is set to -1, it is taken to
626 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700627 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
628 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629
Winson Chungaafa03c2010-06-11 17:34:16 -0700630 child.setId(childId);
631
Michael Jurkaa52570f2012-03-20 03:18:20 -0700632 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700633
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700634 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700635
Winson Chungaafa03c2010-06-11 17:34:16 -0700636 return true;
637 }
638 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700642 public void removeAllViews() {
643 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700644 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700645 }
646
647 @Override
648 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700649 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700650 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700651 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700652 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700653 }
654
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700655 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700656 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700657 }
658
Michael Jurka0280c3b2010-09-17 15:00:07 -0700659 @Override
660 public void removeView(View view) {
661 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700662 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700663 }
664
665 @Override
666 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700667 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
668 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700669 }
670
671 @Override
672 public void removeViewInLayout(View view) {
673 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700674 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700675 }
676
677 @Override
678 public void removeViews(int start, int count) {
679 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700680 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700681 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700682 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700683 }
684
685 @Override
686 public void removeViewsInLayout(int start, int count) {
687 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700688 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700689 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700690 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800691 }
692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 @Override
694 protected void onAttachedToWindow() {
695 super.onAttachedToWindow();
696 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
697 }
698
Michael Jurkaaf442092010-06-10 17:01:57 -0700699 public void setTagToCellInfoForPoint(int touchX, int touchY) {
700 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800701 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700702 final int x = touchX + getScrollX();
703 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700704 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700705
706 boolean found = false;
707 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700708 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800709 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700710
Adam Cohen1b607ed2011-03-03 17:26:50 -0800711 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
712 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700713 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700714
Winson Chungeecf02d2012-03-02 17:14:58 -0800715 float scale = child.getScaleX();
716 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
717 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700718 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
719 // offset that by this CellLayout's padding to test an (x,y) point that is relative
720 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800722 frame.inset((int) (frame.width() * (1f - scale) / 2),
723 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700724
Michael Jurkaaf442092010-06-10 17:01:57 -0700725 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700726 cellInfo.cell = child;
727 cellInfo.cellX = lp.cellX;
728 cellInfo.cellY = lp.cellY;
729 cellInfo.spanX = lp.cellHSpan;
730 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700731 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700732 break;
733 }
734 }
735 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700736
Michael Jurkad771c962011-08-09 15:00:48 -0700737 mLastDownOnOccupiedCell = found;
738
Michael Jurkaaf442092010-06-10 17:01:57 -0700739 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700740 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700741 pointToCellExact(x, y, cellXY);
742
Michael Jurkaaf442092010-06-10 17:01:57 -0700743 cellInfo.cell = null;
744 cellInfo.cellX = cellXY[0];
745 cellInfo.cellY = cellXY[1];
746 cellInfo.spanX = 1;
747 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700748 }
749 setTag(cellInfo);
750 }
751
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 @Override
753 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700754 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
755 // even in the case where we return early. Not clearing here was causing bugs whereby on
756 // long-press we'd end up picking up an item from a previous drag operation.
757 final int action = ev.getAction();
758
759 if (action == MotionEvent.ACTION_DOWN) {
760 clearTagCellInfo();
761 }
762
Michael Jurkadee05892010-07-27 10:01:56 -0700763 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
764 return true;
765 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766
767 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700768 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800770
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 return false;
772 }
773
Adam Cohenc1997fd2011-08-15 18:26:39 -0700774 private void clearTagCellInfo() {
775 final CellInfo cellInfo = mCellInfo;
776 cellInfo.cell = null;
777 cellInfo.cellX = -1;
778 cellInfo.cellY = -1;
779 cellInfo.spanX = 0;
780 cellInfo.spanY = 0;
781 setTag(cellInfo);
782 }
783
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700785 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 }
787
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700788 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700789 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 * @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 pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700795 final int hStartPadding = getPaddingLeft();
796 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797
798 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
799 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
800
Adam Cohend22015c2010-07-26 22:02:18 -0700801 final int xAxis = mCountX;
802 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803
804 if (result[0] < 0) result[0] = 0;
805 if (result[0] >= xAxis) result[0] = xAxis - 1;
806 if (result[1] < 0) result[1] = 0;
807 if (result[1] >= yAxis) result[1] = yAxis - 1;
808 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 /**
811 * Given a point, return the cell that most closely encloses that point
812 * @param x X coordinate of the point
813 * @param y Y coordinate of the point
814 * @param result Array of 2 ints to hold the x and y coordinate of the cell
815 */
816 void pointToCellRounded(int x, int y, int[] result) {
817 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
818 }
819
820 /**
821 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700822 *
823 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700825 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 * @param result Array of 2 ints to hold the x and y coordinate of the point
827 */
828 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700829 final int hStartPadding = getPaddingLeft();
830 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831
832 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
833 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
834 }
835
Adam Cohene3e27a82011-04-15 12:07:39 -0700836 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800837 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700838 *
839 * @param cellX X coordinate of the cell
840 * @param cellY Y coordinate of the cell
841 *
842 * @param result Array of 2 ints to hold the x and y coordinate of the point
843 */
844 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700845 regionToCenterPoint(cellX, cellY, 1, 1, result);
846 }
847
848 /**
849 * Given a cell coordinate and span return the point that represents the center of the regio
850 *
851 * @param cellX X coordinate of the cell
852 * @param cellY Y coordinate of the cell
853 *
854 * @param result Array of 2 ints to hold the x and y coordinate of the point
855 */
856 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700857 final int hStartPadding = getPaddingLeft();
858 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700859 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
860 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
861 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
862 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700863 }
864
Adam Cohen19f37922012-03-21 11:59:11 -0700865 /**
866 * Given a cell coordinate and span fills out a corresponding pixel rect
867 *
868 * @param cellX X coordinate of the cell
869 * @param cellY Y coordinate of the cell
870 * @param result Rect in which to write the result
871 */
872 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
873 final int hStartPadding = getPaddingLeft();
874 final int vStartPadding = getPaddingTop();
875 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
876 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
877 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
878 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
879 }
880
Adam Cohen482ed822012-03-02 14:15:13 -0800881 public float getDistanceFromCell(float x, float y, int[] cell) {
882 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
883 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
884 Math.pow(y - mTmpPoint[1], 2));
885 return distance;
886 }
887
Romain Guy84f296c2009-11-04 15:00:44 -0800888 int getCellWidth() {
889 return mCellWidth;
890 }
891
892 int getCellHeight() {
893 return mCellHeight;
894 }
895
Adam Cohend4844c32011-02-18 19:25:06 -0800896 int getWidthGap() {
897 return mWidthGap;
898 }
899
900 int getHeightGap() {
901 return mHeightGap;
902 }
903
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700904 Rect getContentRect(Rect r) {
905 if (r == null) {
906 r = new Rect();
907 }
908 int left = getPaddingLeft();
909 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700910 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
911 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700912 r.set(left, top, right, bottom);
913 return r;
914 }
915
Adam Cohena897f392012-04-27 18:12:05 -0700916 static void getMetrics(Rect metrics, Resources res, int measureWidth, int measureHeight,
917 int countX, int countY, int orientation) {
918 int numWidthGaps = countX - 1;
919 int numHeightGaps = countY - 1;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700920
921 int widthGap;
922 int heightGap;
923 int cellWidth;
924 int cellHeight;
925 int paddingLeft;
926 int paddingRight;
927 int paddingTop;
928 int paddingBottom;
929
Adam Cohena897f392012-04-27 18:12:05 -0700930 int maxGap = res.getDimensionPixelSize(R.dimen.workspace_max_gap);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700931 if (orientation == LANDSCAPE) {
932 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_land);
933 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_land);
934 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_land);
935 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
936 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_land);
937 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_land);
938 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_land);
939 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_land);
940 } else {
941 // PORTRAIT
942 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_port);
943 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_port);
944 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
945 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_port);
946 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_port);
947 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_port);
948 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_port);
949 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_port);
950 }
951
952 if (widthGap < 0 || heightGap < 0) {
953 int hSpace = measureWidth - paddingLeft - paddingRight;
954 int vSpace = measureHeight - paddingTop - paddingBottom;
Adam Cohena897f392012-04-27 18:12:05 -0700955 int hFreeSpace = hSpace - (countX * cellWidth);
956 int vFreeSpace = vSpace - (countY * cellHeight);
957 widthGap = Math.min(maxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
958 heightGap = Math.min(maxGap, numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700959 }
960 metrics.set(cellWidth, cellHeight, widthGap, heightGap);
961 }
962
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 @Override
964 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700966 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
967
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
969 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700970
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
972 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
973 }
974
Adam Cohend22015c2010-07-26 22:02:18 -0700975 int numWidthGaps = mCountX - 1;
976 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977
Adam Cohen234c4cd2011-07-17 21:03:04 -0700978 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Michael Jurkadd13e3d2012-05-01 12:38:17 -0700979 int hSpace = widthSpecSize - getPaddingLeft() - getPaddingRight();
980 int vSpace = heightSpecSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700981 int hFreeSpace = hSpace - (mCountX * mCellWidth);
982 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700983 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
984 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700985 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700986 } else {
987 mWidthGap = mOriginalWidthGap;
988 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700989 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700990
Michael Jurka8c920dd2011-01-20 14:16:56 -0800991 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
992 int newWidth = widthSpecSize;
993 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700994 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700995 newWidth = getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700996 ((mCountX - 1) * mWidthGap);
Michael Jurka8b805b12012-04-18 14:23:14 -0700997 newHeight = getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700998 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700999 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001000 }
Michael Jurka8c920dd2011-01-20 14:16:56 -08001001
1002 int count = getChildCount();
1003 for (int i = 0; i < count; i++) {
1004 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001005 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
1006 getPaddingRight(), MeasureSpec.EXACTLY);
1007 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
1008 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -08001009 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
1010 }
1011 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 }
1013
1014 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001015 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001018 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001019 child.layout(getPaddingLeft(), getPaddingTop(),
1020 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 }
1022 }
1023
1024 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001025 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1026 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001027 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001028 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
1029 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001030 }
1031
1032 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001034 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 }
1036
1037 @Override
1038 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001039 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 }
1041
Michael Jurka5f1c5092010-09-03 14:15:02 -07001042 public float getBackgroundAlpha() {
1043 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001044 }
1045
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001046 public void setBackgroundAlphaMultiplier(float multiplier) {
1047 mBackgroundAlphaMultiplier = multiplier;
1048 }
1049
Adam Cohenddb82192010-11-10 16:32:54 -08001050 public float getBackgroundAlphaMultiplier() {
1051 return mBackgroundAlphaMultiplier;
1052 }
1053
Michael Jurka5f1c5092010-09-03 14:15:02 -07001054 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001055 if (mBackgroundAlpha != alpha) {
1056 mBackgroundAlpha = alpha;
1057 invalidate();
1058 }
Michael Jurkadee05892010-07-27 10:01:56 -07001059 }
1060
Michael Jurkaa52570f2012-03-20 03:18:20 -07001061 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001062 final int childCount = getChildCount();
1063 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001064 getChildAt(i).setAlpha(alpha);
1065 }
1066 }
1067
Michael Jurkaa52570f2012-03-20 03:18:20 -07001068 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1069 if (getChildCount() > 0) {
1070 return (ShortcutAndWidgetContainer) getChildAt(0);
1071 }
1072 return null;
1073 }
1074
Patrick Dubroy440c3602010-07-13 17:50:32 -07001075 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001076 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001077 }
1078
Adam Cohen76fc0852011-06-17 13:26:23 -07001079 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001080 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001081 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001082 boolean[][] occupied = mOccupied;
1083 if (!permanent) {
1084 occupied = mTmpOccupied;
1085 }
1086
Adam Cohen19f37922012-03-21 11:59:11 -07001087 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001088 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1089 final ItemInfo info = (ItemInfo) child.getTag();
1090
1091 // We cancel any existing animations
1092 if (mReorderAnimators.containsKey(lp)) {
1093 mReorderAnimators.get(lp).cancel();
1094 mReorderAnimators.remove(lp);
1095 }
1096
Adam Cohen482ed822012-03-02 14:15:13 -08001097 final int oldX = lp.x;
1098 final int oldY = lp.y;
1099 if (adjustOccupied) {
1100 occupied[lp.cellX][lp.cellY] = false;
1101 occupied[cellX][cellY] = true;
1102 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001103 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001104 if (permanent) {
1105 lp.cellX = info.cellX = cellX;
1106 lp.cellY = info.cellY = cellY;
1107 } else {
1108 lp.tmpCellX = cellX;
1109 lp.tmpCellY = cellY;
1110 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001111 clc.setupLp(lp);
1112 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001113 final int newX = lp.x;
1114 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001115
Adam Cohen76fc0852011-06-17 13:26:23 -07001116 lp.x = oldX;
1117 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001118
Adam Cohen482ed822012-03-02 14:15:13 -08001119 // Exit early if we're not actually moving the view
1120 if (oldX == newX && oldY == newY) {
1121 lp.isLockedToGrid = true;
1122 return true;
1123 }
1124
1125 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
1126 va.setDuration(duration);
1127 mReorderAnimators.put(lp, va);
1128
1129 va.addUpdateListener(new AnimatorUpdateListener() {
1130 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001131 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001132 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001133 lp.x = (int) ((1 - r) * oldX + r * newX);
1134 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001135 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001136 }
1137 });
Adam Cohen482ed822012-03-02 14:15:13 -08001138 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001139 boolean cancelled = false;
1140 public void onAnimationEnd(Animator animation) {
1141 // If the animation was cancelled, it means that another animation
1142 // has interrupted this one, and we don't want to lock the item into
1143 // place just yet.
1144 if (!cancelled) {
1145 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001146 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001147 }
1148 if (mReorderAnimators.containsKey(lp)) {
1149 mReorderAnimators.remove(lp);
1150 }
1151 }
1152 public void onAnimationCancel(Animator animation) {
1153 cancelled = true;
1154 }
1155 });
Adam Cohen482ed822012-03-02 14:15:13 -08001156 va.setStartDelay(delay);
1157 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001158 return true;
1159 }
1160 return false;
1161 }
1162
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001163 /**
1164 * Estimate where the top left cell of the dragged item will land if it is dropped.
1165 *
1166 * @param originX The X value of the top left corner of the item
1167 * @param originY The Y value of the top left corner of the item
1168 * @param spanX The number of horizontal cells that the item spans
1169 * @param spanY The number of vertical cells that the item spans
1170 * @param result The estimated drop cell X and Y.
1171 */
1172 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001173 final int countX = mCountX;
1174 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001175
Michael Jurkaa63c4522010-08-19 13:52:27 -07001176 // pointToCellRounded takes the top left of a cell but will pad that with
1177 // cellWidth/2 and cellHeight/2 when finding the matching cell
1178 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001179
1180 // If the item isn't fully on this screen, snap to the edges
1181 int rightOverhang = result[0] + spanX - countX;
1182 if (rightOverhang > 0) {
1183 result[0] -= rightOverhang; // Snap to right
1184 }
1185 result[0] = Math.max(0, result[0]); // Snap to left
1186 int bottomOverhang = result[1] + spanY - countY;
1187 if (bottomOverhang > 0) {
1188 result[1] -= bottomOverhang; // Snap to bottom
1189 }
1190 result[1] = Math.max(0, result[1]); // Snap to top
1191 }
1192
Adam Cohen482ed822012-03-02 14:15:13 -08001193 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1194 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001195 final int oldDragCellX = mDragCell[0];
1196 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001197
Winson Chungb8c69f32011-10-19 21:36:08 -07001198 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001199 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1200 } else {
1201 mDragCenter.set(originX, originY);
1202 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001203
Adam Cohen2801caf2011-05-13 20:57:39 -07001204 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001205 return;
1206 }
1207
Adam Cohen482ed822012-03-02 14:15:13 -08001208 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1209 mDragCell[0] = cellX;
1210 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001211 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001212 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001213 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001214
Joe Onorato4be866d2010-10-10 11:26:02 -07001215 int left = topLeft[0];
1216 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001217
Winson Chungb8c69f32011-10-19 21:36:08 -07001218 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001219 // When drawing the drag outline, it did not account for margin offsets
1220 // added by the view's parent.
1221 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1222 left += lp.leftMargin;
1223 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001224
Adam Cohen99e8b402011-03-25 19:23:43 -07001225 // Offsets due to the size difference between the View and the dragOutline.
1226 // There is a size difference to account for the outer blur, which may lie
1227 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001228 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001229 // We center about the x axis
1230 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1231 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001232 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001233 if (dragOffset != null && dragRegion != null) {
1234 // Center the drag region *horizontally* in the cell and apply a drag
1235 // outline offset
1236 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1237 - dragRegion.width()) / 2;
1238 top += dragOffset.y;
1239 } else {
1240 // Center the drag outline in the cell
1241 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1242 - dragOutline.getWidth()) / 2;
1243 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1244 - dragOutline.getHeight()) / 2;
1245 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001246 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001247 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001248 mDragOutlineAnims[oldIndex].animateOut();
1249 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001250 Rect r = mDragOutlines[mDragOutlineCurrent];
1251 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1252 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001253 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001254 }
Winson Chung150fbab2010-09-29 17:14:26 -07001255
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001256 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1257 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001258 }
1259 }
1260
Adam Cohene0310962011-04-18 16:15:31 -07001261 public void clearDragOutlines() {
1262 final int oldIndex = mDragOutlineCurrent;
1263 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001264 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001265 }
1266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001268 * Find a vacant area that will fit the given bounds nearest the requested
1269 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001270 *
Romain Guy51afc022009-05-04 18:03:43 -07001271 * @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.
Jeff Sharkey70864282009-04-07 21:08:40 -07001273 * @param spanX Horizontal span of the object.
1274 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001275 * @param result Array in which to place the result, or null (in which case a new array will
1276 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001277 * @return The X, Y cell of a vacant area that can contain this object,
1278 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001280 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1281 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001282 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001283 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001284
Michael Jurka6a1435d2010-09-27 17:35:12 -07001285 /**
1286 * Find a vacant area that will fit the given bounds nearest the requested
1287 * cell location. Uses Euclidean distance to score multiple vacant areas.
1288 *
1289 * @param pixelX The X location at which you want to search for a vacant area.
1290 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001291 * @param minSpanX The minimum horizontal span required
1292 * @param minSpanY The minimum vertical span required
1293 * @param spanX Horizontal span of the object.
1294 * @param spanY Vertical span of the object.
1295 * @param result Array in which to place the result, or null (in which case a new array will
1296 * be allocated)
1297 * @return The X, Y cell of a vacant area that can contain this object,
1298 * nearest the requested location.
1299 */
1300 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1301 int spanY, int[] result, int[] resultSpan) {
1302 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1303 result, resultSpan);
1304 }
1305
1306 /**
1307 * Find a vacant area that will fit the given bounds nearest the requested
1308 * cell location. Uses Euclidean distance to score multiple vacant areas.
1309 *
1310 * @param pixelX The X location at which you want to search for a vacant area.
1311 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001312 * @param spanX Horizontal span of the object.
1313 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001314 * @param ignoreOccupied If true, the result can be an occupied cell
1315 * @param result Array in which to place the result, or null (in which case a new array will
1316 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001317 * @return The X, Y cell of a vacant area that can contain this object,
1318 * nearest the requested location.
1319 */
Adam Cohendf035382011-04-11 17:22:04 -07001320 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1321 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001322 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001323 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001324 }
1325
1326 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1327 private void lazyInitTempRectStack() {
1328 if (mTempRectStack.isEmpty()) {
1329 for (int i = 0; i < mCountX * mCountY; i++) {
1330 mTempRectStack.push(new Rect());
1331 }
1332 }
1333 }
Adam Cohen482ed822012-03-02 14:15:13 -08001334
Adam Cohend41fbf52012-02-16 23:53:59 -08001335 private void recycleTempRects(Stack<Rect> used) {
1336 while (!used.isEmpty()) {
1337 mTempRectStack.push(used.pop());
1338 }
1339 }
1340
1341 /**
1342 * Find a vacant area that will fit the given bounds nearest the requested
1343 * cell location. Uses Euclidean distance to score multiple vacant areas.
1344 *
1345 * @param pixelX The X location at which you want to search for a vacant area.
1346 * @param pixelY The Y location at which you want to search for a vacant area.
1347 * @param minSpanX The minimum horizontal span required
1348 * @param minSpanY The minimum vertical span required
1349 * @param spanX Horizontal span of the object.
1350 * @param spanY Vertical span of the object.
1351 * @param ignoreOccupied If true, the result can be an occupied cell
1352 * @param result Array in which to place the result, or null (in which case a new array will
1353 * be allocated)
1354 * @return The X, Y cell of a vacant area that can contain this object,
1355 * nearest the requested location.
1356 */
1357 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001358 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1359 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001360 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001361 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001362 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001363
Adam Cohene3e27a82011-04-15 12:07:39 -07001364 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1365 // to the center of the item, but we are searching based on the top-left cell, so
1366 // we translate the point over to correspond to the top-left.
1367 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1368 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1369
Jeff Sharkey70864282009-04-07 21:08:40 -07001370 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001371 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001372 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001373 final Rect bestRect = new Rect(-1, -1, -1, -1);
1374 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001375
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001376 final int countX = mCountX;
1377 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001378
Adam Cohend41fbf52012-02-16 23:53:59 -08001379 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1380 spanX < minSpanX || spanY < minSpanY) {
1381 return bestXY;
1382 }
1383
1384 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001385 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001386 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1387 int ySize = -1;
1388 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001389 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001390 // First, let's see if this thing fits anywhere
1391 for (int i = 0; i < minSpanX; i++) {
1392 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001393 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001394 continue inner;
1395 }
Michael Jurkac28de512010-08-13 11:27:44 -07001396 }
1397 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001398 xSize = minSpanX;
1399 ySize = minSpanY;
1400
1401 // We know that the item will fit at _some_ acceptable size, now let's see
1402 // how big we can make it. We'll alternate between incrementing x and y spans
1403 // until we hit a limit.
1404 boolean incX = true;
1405 boolean hitMaxX = xSize >= spanX;
1406 boolean hitMaxY = ySize >= spanY;
1407 while (!(hitMaxX && hitMaxY)) {
1408 if (incX && !hitMaxX) {
1409 for (int j = 0; j < ySize; j++) {
1410 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1411 // We can't move out horizontally
1412 hitMaxX = true;
1413 }
1414 }
1415 if (!hitMaxX) {
1416 xSize++;
1417 }
1418 } else if (!hitMaxY) {
1419 for (int i = 0; i < xSize; i++) {
1420 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1421 // We can't move out vertically
1422 hitMaxY = true;
1423 }
1424 }
1425 if (!hitMaxY) {
1426 ySize++;
1427 }
1428 }
1429 hitMaxX |= xSize >= spanX;
1430 hitMaxY |= ySize >= spanY;
1431 incX = !incX;
1432 }
1433 incX = true;
1434 hitMaxX = xSize >= spanX;
1435 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001436 }
Winson Chung0be025d2011-05-23 17:45:09 -07001437 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001438 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439
Adam Cohend41fbf52012-02-16 23:53:59 -08001440 // We verify that the current rect is not a sub-rect of any of our previous
1441 // candidates. In this case, the current rect is disqualified in favour of the
1442 // containing rect.
1443 Rect currentRect = mTempRectStack.pop();
1444 currentRect.set(x, y, x + xSize, y + ySize);
1445 boolean contained = false;
1446 for (Rect r : validRegions) {
1447 if (r.contains(currentRect)) {
1448 contained = true;
1449 break;
1450 }
1451 }
1452 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001453 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1454 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001455
Adam Cohend41fbf52012-02-16 23:53:59 -08001456 if ((distance <= bestDistance && !contained) ||
1457 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001458 bestDistance = distance;
1459 bestXY[0] = x;
1460 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001461 if (resultSpan != null) {
1462 resultSpan[0] = xSize;
1463 resultSpan[1] = ySize;
1464 }
1465 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001469 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001470 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471
Adam Cohenc0dcf592011-06-01 15:30:43 -07001472 // Return -1, -1 if no suitable location found
1473 if (bestDistance == Double.MAX_VALUE) {
1474 bestXY[0] = -1;
1475 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001476 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001477 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001478 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001480
Adam Cohen482ed822012-03-02 14:15:13 -08001481 /**
1482 * Find a vacant area that will fit the given bounds nearest the requested
1483 * cell location, and will also weigh in a suggested direction vector of the
1484 * desired location. This method computers distance based on unit grid distances,
1485 * not pixel distances.
1486 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001487 * @param cellX The X cell nearest to which you want to search for a vacant area.
1488 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001489 * @param spanX Horizontal span of the object.
1490 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001491 * @param direction The favored direction in which the views should move from x, y
1492 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1493 * matches exactly. Otherwise we find the best matching direction.
1494 * @param occoupied The array which represents which cells in the CellLayout are occupied
1495 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1496 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001497 * @param result Array in which to place the result, or null (in which case a new array will
1498 * be allocated)
1499 * @return The X, Y cell of a vacant area that can contain this object,
1500 * nearest the requested location.
1501 */
1502 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001503 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001504 // Keep track of best-scoring drop area
1505 final int[] bestXY = result != null ? result : new int[2];
1506 float bestDistance = Float.MAX_VALUE;
1507 int bestDirectionScore = Integer.MIN_VALUE;
1508
1509 final int countX = mCountX;
1510 final int countY = mCountY;
1511
1512 for (int y = 0; y < countY - (spanY - 1); y++) {
1513 inner:
1514 for (int x = 0; x < countX - (spanX - 1); x++) {
1515 // First, let's see if this thing fits anywhere
1516 for (int i = 0; i < spanX; i++) {
1517 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001518 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001519 continue inner;
1520 }
1521 }
1522 }
1523
1524 float distance = (float)
1525 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1526 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001527 computeDirectionVector(x - cellX, y - cellY, curDirection);
1528 // The direction score is just the dot product of the two candidate direction
1529 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001530 int curDirectionScore = direction[0] * curDirection[0] +
1531 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001532 boolean exactDirectionOnly = false;
1533 boolean directionMatches = direction[0] == curDirection[0] &&
1534 direction[0] == curDirection[0];
1535 if ((directionMatches || !exactDirectionOnly) &&
1536 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001537 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1538 bestDistance = distance;
1539 bestDirectionScore = curDirectionScore;
1540 bestXY[0] = x;
1541 bestXY[1] = y;
1542 }
1543 }
1544 }
1545
1546 // Return -1, -1 if no suitable location found
1547 if (bestDistance == Float.MAX_VALUE) {
1548 bestXY[0] = -1;
1549 bestXY[1] = -1;
1550 }
1551 return bestXY;
1552 }
1553
Adam Cohen47a876d2012-03-19 13:21:41 -07001554 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1555 int[] direction,boolean[][] occupied,
1556 boolean blockOccupied[][], int[] result) {
1557 // Keep track of best-scoring drop area
1558 final int[] bestXY = result != null ? result : new int[2];
1559 bestXY[0] = -1;
1560 bestXY[1] = -1;
1561 float bestDistance = Float.MAX_VALUE;
1562
1563 // We use this to march in a single direction
Adam Cohen5b53f292012-03-29 14:30:35 -07001564 if ((direction[0] != 0 && direction[1] != 0) ||
1565 (direction[0] == 0 && direction[1] == 0)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001566 return bestXY;
1567 }
1568
1569 // This will only incrememnet one of x or y based on the assertion above
1570 int x = cellX + direction[0];
1571 int y = cellY + direction[1];
1572 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
1573
1574 boolean fail = false;
1575 for (int i = 0; i < spanX; i++) {
1576 for (int j = 0; j < spanY; j++) {
1577 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1578 fail = true;
1579 }
1580 }
1581 }
1582 if (!fail) {
1583 float distance = (float)
1584 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1585 if (Float.compare(distance, bestDistance) < 0) {
1586 bestDistance = distance;
1587 bestXY[0] = x;
1588 bestXY[1] = y;
1589 }
1590 }
1591 x += direction[0];
1592 y += direction[1];
1593 }
1594 return bestXY;
1595 }
1596
Adam Cohen482ed822012-03-02 14:15:13 -08001597 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001598 int[] direction, ItemConfiguration currentState) {
1599 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001600 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001601 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001602 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1603
Adam Cohen8baab352012-03-20 17:39:21 -07001604 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001605
1606 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001607 c.x = mTempLocation[0];
1608 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001609 success = true;
1610
1611 }
Adam Cohen8baab352012-03-20 17:39:21 -07001612 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001613 return success;
1614 }
1615
Adam Cohen47a876d2012-03-19 13:21:41 -07001616 // This method looks in the specified direction to see if there is an additional view
1617 // immediately adjecent in that direction
1618 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
Adam Cohen19f37922012-03-21 11:59:11 -07001619 boolean[][] occupied, View dragView, ItemConfiguration currentState) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001620 boolean found = false;
1621
Michael Jurkaa52570f2012-03-20 03:18:20 -07001622 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001623 Rect r0 = new Rect(boundingRect);
1624 Rect r1 = new Rect();
1625
1626 int deltaX = 0;
1627 int deltaY = 0;
1628 if (direction[1] < 0) {
1629 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom);
1630 deltaY = -1;
1631 } else if (direction[1] > 0) {
1632 r0.set(r0.left, r0.top, r0.right, r0.bottom + 1);
1633 deltaY = 1;
1634 } else if (direction[0] < 0) {
1635 r0.set(r0.left - 1, r0.top, r0.right, r0.bottom);
1636 deltaX = -1;
1637 } else if (direction[0] > 0) {
1638 r0.set(r0.left, r0.top, r0.right + 1, r0.bottom);
1639 deltaX = 1;
1640 }
1641
1642 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001643 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen19f37922012-03-21 11:59:11 -07001644 if (views.contains(child) || child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001645 CellAndSpan c = currentState.map.get(child);
Adam Cohen47a876d2012-03-19 13:21:41 -07001646
Adam Cohen8baab352012-03-20 17:39:21 -07001647 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1648 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001649 if (Rect.intersects(r0, r1)) {
1650 if (!lp.canReorder) {
1651 return false;
1652 }
1653 boolean pushed = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001654 for (int x = c.x; x < c.x + c.spanX; x++) {
1655 for (int y = c.y; y < c.y + c.spanY; y++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001656 boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX
1657 && y - deltaY >= 0 && y - deltaY < mCountY;
1658 if (inBounds && occupied[x - deltaX][y - deltaY]) {
1659 pushed = true;
1660 }
1661 }
1662 }
1663 if (pushed) {
1664 views.add(child);
Adam Cohen8baab352012-03-20 17:39:21 -07001665 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001666 found = true;
1667 }
1668 }
1669 }
1670 return found;
1671 }
1672
Adam Cohen482ed822012-03-02 14:15:13 -08001673 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohen19f37922012-03-21 11:59:11 -07001674 int[] direction, boolean push, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001675 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001676
Adam Cohen8baab352012-03-20 17:39:21 -07001677 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001678 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001679 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001680 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001681 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001682 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001683 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001684 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001685 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001686 }
1687 }
Adam Cohen8baab352012-03-20 17:39:21 -07001688
1689 @SuppressWarnings("unchecked")
1690 ArrayList<View> dup = (ArrayList<View>) views.clone();
1691 // We try and expand the group of views in the direction vector passed, based on
1692 // whether they are physically adjacent, ie. based on "push mechanics".
Adam Cohen19f37922012-03-21 11:59:11 -07001693 while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied, dragView,
Adam Cohen8baab352012-03-20 17:39:21 -07001694 currentState)) {
1695 }
1696
1697 // Mark the occupied state as false for the group of views we want to move.
1698 for (View v: dup) {
1699 CellAndSpan c = currentState.map.get(v);
1700 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1701 }
1702
Adam Cohen47a876d2012-03-19 13:21:41 -07001703 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1704 int top = boundingRect.top;
1705 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001706 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
1707 // for tetris-style interlocking.
1708 for (View v: dup) {
1709 CellAndSpan c = currentState.map.get(v);
1710 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001711 }
1712
Adam Cohen482ed822012-03-02 14:15:13 -08001713 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1714
Adam Cohen8baab352012-03-20 17:39:21 -07001715 if (push) {
1716 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1717 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1718 } else {
1719 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1720 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1721 }
Adam Cohen482ed822012-03-02 14:15:13 -08001722
Adam Cohen8baab352012-03-20 17:39:21 -07001723 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001724 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001725 int deltaX = mTempLocation[0] - boundingRect.left;
1726 int deltaY = mTempLocation[1] - boundingRect.top;
1727 for (View v: dup) {
1728 CellAndSpan c = currentState.map.get(v);
1729 c.x += deltaX;
1730 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001731 }
1732 success = true;
1733 }
Adam Cohen8baab352012-03-20 17:39:21 -07001734
1735 // In either case, we set the occupied array as marked for the location of the views
1736 for (View v: dup) {
1737 CellAndSpan c = currentState.map.get(v);
1738 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001739 }
1740 return success;
1741 }
1742
1743 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1744 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1745 }
1746
1747 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001748 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001749 // Return early if get invalid cell positions
1750 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001751
Adam Cohen8baab352012-03-20 17:39:21 -07001752 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001753 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001754
Adam Cohen8baab352012-03-20 17:39:21 -07001755 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001756 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001757 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001758 if (c != null) {
1759 c.x = cellX;
1760 c.y = cellY;
1761 }
Adam Cohen482ed822012-03-02 14:15:13 -08001762 }
Adam Cohen482ed822012-03-02 14:15:13 -08001763 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1764 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001765 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001766 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001767 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001768 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001769 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001770 if (Rect.intersects(r0, r1)) {
1771 if (!lp.canReorder) {
1772 return false;
1773 }
1774 mIntersectingViews.add(child);
1775 }
1776 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001777
Adam Cohen8baab352012-03-20 17:39:21 -07001778 // We try to move the intersecting views as a block using the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001779 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1780 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001781 return true;
1782 }
1783 // Try the opposite direction
1784 direction[0] *= -1;
1785 direction[1] *= -1;
Adam Cohen19f37922012-03-21 11:59:11 -07001786 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1787 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001788 return true;
1789 }
1790 // Switch the direction back
1791 direction[0] *= -1;
1792 direction[1] *= -1;
1793
Adam Cohen8baab352012-03-20 17:39:21 -07001794 // Next we try moving the views as a block , but without requiring the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001795 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, ignoreView,
1796 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001797 return true;
1798 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001799
Adam Cohen482ed822012-03-02 14:15:13 -08001800 // Ok, they couldn't move as a block, let's move them individually
1801 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001802 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001803 return false;
1804 }
1805 }
1806 return true;
1807 }
1808
1809 /*
1810 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1811 * the provided point and the provided cell
1812 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001813 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001814 double angle = Math.atan(((float) deltaY) / deltaX);
1815
1816 result[0] = 0;
1817 result[1] = 0;
1818 if (Math.abs(Math.cos(angle)) > 0.5f) {
1819 result[0] = (int) Math.signum(deltaX);
1820 }
1821 if (Math.abs(Math.sin(angle)) > 0.5f) {
1822 result[1] = (int) Math.signum(deltaY);
1823 }
1824 }
1825
Adam Cohen8baab352012-03-20 17:39:21 -07001826 private void copyOccupiedArray(boolean[][] occupied) {
1827 for (int i = 0; i < mCountX; i++) {
1828 for (int j = 0; j < mCountY; j++) {
1829 occupied[i][j] = mOccupied[i][j];
1830 }
1831 }
1832 }
1833
Adam Cohen482ed822012-03-02 14:15:13 -08001834 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1835 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001836 // Copy the current state into the solution. This solution will be manipulated as necessary.
1837 copyCurrentStateToSolution(solution, false);
1838 // Copy the current occupied array into the temporary occupied array. This array will be
1839 // manipulated as necessary to find a solution.
1840 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001841
1842 // We find the nearest cell into which we would place the dragged item, assuming there's
1843 // nothing in its way.
1844 int result[] = new int[2];
1845 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1846
1847 boolean success = false;
1848 // First we try the exact nearest position of the item being dragged,
1849 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001850 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1851 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001852
1853 if (!success) {
1854 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1855 // x, then 1 in y etc.
1856 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1857 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1858 dragView, false, solution);
1859 } else if (spanY > minSpanY) {
1860 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1861 dragView, true, solution);
1862 }
1863 solution.isSolution = false;
1864 } else {
1865 solution.isSolution = true;
1866 solution.dragViewX = result[0];
1867 solution.dragViewY = result[1];
1868 solution.dragViewSpanX = spanX;
1869 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001870 }
1871 return solution;
1872 }
1873
1874 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001875 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001876 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001877 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001878 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001879 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001880 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001881 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001882 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001883 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001884 }
Adam Cohen8baab352012-03-20 17:39:21 -07001885 solution.map.put(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001886 }
1887 }
1888
1889 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1890 for (int i = 0; i < mCountX; i++) {
1891 for (int j = 0; j < mCountY; j++) {
1892 mTmpOccupied[i][j] = false;
1893 }
1894 }
1895
Michael Jurkaa52570f2012-03-20 03:18:20 -07001896 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001897 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001898 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001899 if (child == dragView) continue;
1900 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001901 CellAndSpan c = solution.map.get(child);
1902 if (c != null) {
1903 lp.tmpCellX = c.x;
1904 lp.tmpCellY = c.y;
1905 lp.cellHSpan = c.spanX;
1906 lp.cellVSpan = c.spanY;
1907 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001908 }
1909 }
1910 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1911 solution.dragViewSpanY, mTmpOccupied, true);
1912 }
1913
1914 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1915 commitDragView) {
1916
1917 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1918 for (int i = 0; i < mCountX; i++) {
1919 for (int j = 0; j < mCountY; j++) {
1920 occupied[i][j] = false;
1921 }
1922 }
1923
Michael Jurkaa52570f2012-03-20 03:18:20 -07001924 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001925 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001926 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001927 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001928 CellAndSpan c = solution.map.get(child);
1929 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07001930 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
1931 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001932 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001933 }
1934 }
1935 if (commitDragView) {
1936 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1937 solution.dragViewSpanY, occupied, true);
1938 }
1939 }
1940
Adam Cohen19f37922012-03-21 11:59:11 -07001941 // This method starts or changes the reorder hint animations
1942 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
1943 int childCount = mShortcutsAndWidgets.getChildCount();
1944 int timeForPriorAnimationToComplete = getMaxCompletionTime();
1945 for (int i = 0; i < childCount; i++) {
1946 View child = mShortcutsAndWidgets.getChildAt(i);
1947 if (child == dragView) continue;
1948 CellAndSpan c = solution.map.get(child);
1949 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1950 if (c != null) {
1951 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
1952 c.x, c.y, c.spanX, c.spanY);
1953 rha.animate(timeForPriorAnimationToComplete);
1954 }
1955 }
1956 }
1957
1958 // Class which represents the reorder hint animations. These animations show that an item is
1959 // in a temporary state, and hint at where the item will return to.
1960 class ReorderHintAnimation {
1961 View child;
1962 float deltaX;
1963 float deltaY;
1964 private static final int DURATION = 140;
1965 private int repeatCount;
1966 private boolean cancelOnCycleComplete = false;
1967 ValueAnimator va;
1968
1969 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
1970 int spanX, int spanY) {
1971 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1972 final int x0 = mTmpPoint[0];
1973 final int y0 = mTmpPoint[1];
1974 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1975 final int x1 = mTmpPoint[0];
1976 final int y1 = mTmpPoint[1];
1977 final int dX = x1 - x0;
1978 final int dY = y1 - y0;
1979 deltaX = 0;
1980 deltaY = 0;
1981 if (dX == dY && dX == 0) {
1982 } else {
1983 if (dY == 0) {
1984 deltaX = mReorderHintAnimationMagnitude;
1985 } else if (dX == 0) {
1986 deltaY = mReorderHintAnimationMagnitude;
1987 } else {
1988 double angle = Math.atan( (float) (dY) / dX);
1989 deltaX = (int) (Math.cos(angle) * mReorderHintAnimationMagnitude);
1990 deltaY = (int) (Math.sin(angle) * mReorderHintAnimationMagnitude);
1991 }
1992 }
1993 this.child = child;
1994 }
1995
1996 void animate(int delay) {
1997 if (mShakeAnimators.containsKey(child)) {
1998 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
1999 oldAnimation.completeAnimation();
2000 mShakeAnimators.remove(child);
2001 }
2002 if (deltaX == 0 && deltaY == 0) {
2003 return;
2004 }
2005 va = ValueAnimator.ofFloat(0f, 1f);
2006 va.setRepeatMode(ValueAnimator.REVERSE);
2007 va.setRepeatCount(ValueAnimator.INFINITE);
2008 va.setDuration(DURATION);
2009 va.addUpdateListener(new AnimatorUpdateListener() {
2010 @Override
2011 public void onAnimationUpdate(ValueAnimator animation) {
2012 float r = ((Float) animation.getAnimatedValue()).floatValue();
2013 float x = r * deltaX;
2014 float y = r * deltaY;
2015 child.setTranslationX(x);
2016 child.setTranslationY(y);
2017 }
2018 });
2019 va.addListener(new AnimatorListenerAdapter() {
2020 public void onAnimationRepeat(Animator animation) {
2021 repeatCount++;
2022 // We make sure to end only after a full period
2023 if (cancelOnCycleComplete && repeatCount % 2 == 0) {
2024 va.cancel();
2025 }
2026 }
2027 });
2028 va.setStartDelay(Math.max(REORDER_ANIMATION_DURATION, delay));
2029 mShakeAnimators.put(child, this);
2030 va.start();
2031 }
2032
2033
2034 private void completeAnimation() {
2035 cancelOnCycleComplete = true;
2036 }
2037
2038 // Returns the time required to complete the current oscillating animation
2039 private int completionTime() {
2040 if (repeatCount % 2 == 0) {
2041 return (int) (va.getDuration() - va.getCurrentPlayTime() + DURATION);
2042 } else {
2043 return (int) (va.getDuration() - va.getCurrentPlayTime());
2044 }
2045 }
2046 }
2047
2048 private void completeAndClearReorderHintAnimations() {
2049 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2050 a.completeAnimation();
2051 }
2052 mShakeAnimators.clear();
2053 }
2054
2055 private int getMaxCompletionTime() {
2056 int maxTime = 0;
2057 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2058 maxTime = Math.max(maxTime, a.completionTime());
2059 }
2060 return maxTime;
2061 }
2062
Adam Cohen482ed822012-03-02 14:15:13 -08002063 private void commitTempPlacement() {
2064 for (int i = 0; i < mCountX; i++) {
2065 for (int j = 0; j < mCountY; j++) {
2066 mOccupied[i][j] = mTmpOccupied[i][j];
2067 }
2068 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002069 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002070 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002071 View child = mShortcutsAndWidgets.getChildAt(i);
2072 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2073 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002074 // We do a null check here because the item info can be null in the case of the
2075 // AllApps button in the hotseat.
2076 if (info != null) {
2077 info.cellX = lp.cellX = lp.tmpCellX;
2078 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002079 info.spanX = lp.cellHSpan;
2080 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002081 }
Adam Cohen482ed822012-03-02 14:15:13 -08002082 }
Adam Cohen2acce882012-03-28 19:03:19 -07002083 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002084 }
2085
2086 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002087 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002088 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002089 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002090 lp.useTmpCoords = useTempCoords;
2091 }
2092 }
2093
Adam Cohen482ed822012-03-02 14:15:13 -08002094 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2095 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2096 int[] result = new int[2];
2097 int[] resultSpan = new int[2];
2098 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2099 resultSpan);
2100 if (result[0] >= 0 && result[1] >= 0) {
2101 copyCurrentStateToSolution(solution, false);
2102 solution.dragViewX = result[0];
2103 solution.dragViewY = result[1];
2104 solution.dragViewSpanX = resultSpan[0];
2105 solution.dragViewSpanY = resultSpan[1];
2106 solution.isSolution = true;
2107 } else {
2108 solution.isSolution = false;
2109 }
2110 return solution;
2111 }
2112
2113 public void prepareChildForDrag(View child) {
2114 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002115 }
2116
Adam Cohen19f37922012-03-21 11:59:11 -07002117 /* This seems like it should be obvious and straight-forward, but when the direction vector
2118 needs to match with the notion of the dragView pushing other views, we have to employ
2119 a slightly more subtle notion of the direction vector. The question is what two points is
2120 the vector between? The center of the dragView and its desired destination? Not quite, as
2121 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2122 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2123 or right, which helps make pushing feel right.
2124 */
2125 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2126 int spanY, View dragView, int[] resultDirection) {
2127 int[] targetDestination = new int[2];
2128
2129 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2130 Rect dragRect = new Rect();
2131 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2132 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2133
2134 Rect dropRegionRect = new Rect();
2135 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2136 dragView, dropRegionRect, mIntersectingViews);
2137
2138 int dropRegionSpanX = dropRegionRect.width();
2139 int dropRegionSpanY = dropRegionRect.height();
2140
2141 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2142 dropRegionRect.height(), dropRegionRect);
2143
2144 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2145 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2146
2147 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2148 deltaX = 0;
2149 }
2150 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2151 deltaY = 0;
2152 }
2153
2154 if (deltaX == 0 && deltaY == 0) {
2155 // No idea what to do, give a random direction.
2156 resultDirection[0] = 1;
2157 resultDirection[1] = 0;
2158 } else {
2159 computeDirectionVector(deltaX, deltaY, resultDirection);
2160 }
2161 }
2162
2163 // For a given cell and span, fetch the set of views intersecting the region.
2164 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2165 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2166 if (boundingRect != null) {
2167 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2168 }
2169 intersectingViews.clear();
2170 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2171 Rect r1 = new Rect();
2172 final int count = mShortcutsAndWidgets.getChildCount();
2173 for (int i = 0; i < count; i++) {
2174 View child = mShortcutsAndWidgets.getChildAt(i);
2175 if (child == dragView) continue;
2176 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2177 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2178 if (Rect.intersects(r0, r1)) {
2179 mIntersectingViews.add(child);
2180 if (boundingRect != null) {
2181 boundingRect.union(r1);
2182 }
2183 }
2184 }
2185 }
2186
2187 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2188 View dragView, int[] result) {
2189 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2190 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2191 mIntersectingViews);
2192 return !mIntersectingViews.isEmpty();
2193 }
2194
2195 void revertTempState() {
2196 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2197 final int count = mShortcutsAndWidgets.getChildCount();
2198 for (int i = 0; i < count; i++) {
2199 View child = mShortcutsAndWidgets.getChildAt(i);
2200 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2201 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2202 lp.tmpCellX = lp.cellX;
2203 lp.tmpCellY = lp.cellY;
2204 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2205 0, false, false);
2206 }
2207 }
2208 completeAndClearReorderHintAnimations();
2209 setItemPlacementDirty(false);
2210 }
2211
Adam Cohenbebf0422012-04-11 18:06:28 -07002212 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2213 View dragView, int[] direction, boolean commit) {
2214 int[] pixelXY = new int[2];
2215 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2216
2217 // First we determine if things have moved enough to cause a different layout
2218 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2219 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2220
2221 setUseTempCoords(true);
2222 if (swapSolution != null && swapSolution.isSolution) {
2223 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2224 // committing anything or animating anything as we just want to determine if a solution
2225 // exists
2226 copySolutionToTempState(swapSolution, dragView);
2227 setItemPlacementDirty(true);
2228 animateItemsToSolution(swapSolution, dragView, commit);
2229
2230 if (commit) {
2231 commitTempPlacement();
2232 completeAndClearReorderHintAnimations();
2233 setItemPlacementDirty(false);
2234 } else {
2235 beginOrAdjustHintAnimations(swapSolution, dragView,
2236 REORDER_ANIMATION_DURATION);
2237 }
2238 mShortcutsAndWidgets.requestLayout();
2239 }
2240 return swapSolution.isSolution;
2241 }
2242
Adam Cohen482ed822012-03-02 14:15:13 -08002243 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2244 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002245 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002246 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002247
2248 if (resultSpan == null) {
2249 resultSpan = new int[2];
2250 }
2251
Adam Cohen19f37922012-03-21 11:59:11 -07002252 // When we are checking drop validity or actually dropping, we don't recompute the
2253 // direction vector, since we want the solution to match the preview, and it's possible
2254 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002255 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2256 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002257 mDirectionVector[0] = mPreviousReorderDirection[0];
2258 mDirectionVector[1] = mPreviousReorderDirection[1];
2259 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002260 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2261 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2262 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002263 }
2264 } else {
2265 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2266 mPreviousReorderDirection[0] = mDirectionVector[0];
2267 mPreviousReorderDirection[1] = mDirectionVector[1];
2268 }
2269
Adam Cohen482ed822012-03-02 14:15:13 -08002270 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2271 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2272
2273 // We attempt the approach which doesn't shuffle views at all
2274 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2275 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2276
2277 ItemConfiguration finalSolution = null;
2278 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2279 finalSolution = swapSolution;
2280 } else if (noShuffleSolution.isSolution) {
2281 finalSolution = noShuffleSolution;
2282 }
2283
2284 boolean foundSolution = true;
2285 if (!DESTRUCTIVE_REORDER) {
2286 setUseTempCoords(true);
2287 }
2288
2289 if (finalSolution != null) {
2290 result[0] = finalSolution.dragViewX;
2291 result[1] = finalSolution.dragViewY;
2292 resultSpan[0] = finalSolution.dragViewSpanX;
2293 resultSpan[1] = finalSolution.dragViewSpanY;
2294
2295 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2296 // committing anything or animating anything as we just want to determine if a solution
2297 // exists
2298 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2299 if (!DESTRUCTIVE_REORDER) {
2300 copySolutionToTempState(finalSolution, dragView);
2301 }
2302 setItemPlacementDirty(true);
2303 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2304
Adam Cohen19f37922012-03-21 11:59:11 -07002305 if (!DESTRUCTIVE_REORDER &&
2306 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002307 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002308 completeAndClearReorderHintAnimations();
2309 setItemPlacementDirty(false);
2310 } else {
2311 beginOrAdjustHintAnimations(finalSolution, dragView,
2312 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002313 }
2314 }
2315 } else {
2316 foundSolution = false;
2317 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2318 }
2319
2320 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2321 setUseTempCoords(false);
2322 }
Adam Cohen482ed822012-03-02 14:15:13 -08002323
Michael Jurkaa52570f2012-03-20 03:18:20 -07002324 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002325 return result;
2326 }
2327
Adam Cohen19f37922012-03-21 11:59:11 -07002328 void setItemPlacementDirty(boolean dirty) {
2329 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002330 }
Adam Cohen19f37922012-03-21 11:59:11 -07002331 boolean isItemPlacementDirty() {
2332 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002333 }
2334
2335 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002336 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002337 boolean isSolution = false;
2338 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2339
2340 int area() {
2341 return dragViewSpanX * dragViewSpanY;
2342 }
Adam Cohen8baab352012-03-20 17:39:21 -07002343 }
2344
2345 private class CellAndSpan {
2346 int x, y;
2347 int spanX, spanY;
2348
2349 public CellAndSpan(int x, int y, int spanX, int spanY) {
2350 this.x = x;
2351 this.y = y;
2352 this.spanX = spanX;
2353 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002354 }
2355 }
2356
Adam Cohendf035382011-04-11 17:22:04 -07002357 /**
2358 * Find a vacant area that will fit the given bounds nearest the requested
2359 * cell location. Uses Euclidean distance to score multiple vacant areas.
2360 *
2361 * @param pixelX The X location at which you want to search for a vacant area.
2362 * @param pixelY The Y location at which you want to search for a vacant area.
2363 * @param spanX Horizontal span of the object.
2364 * @param spanY Vertical span of the object.
2365 * @param ignoreView Considers space occupied by this view as unoccupied
2366 * @param result Previously returned value to possibly recycle.
2367 * @return The X, Y cell of a vacant area that can contain this object,
2368 * nearest the requested location.
2369 */
2370 int[] findNearestVacantArea(
2371 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2372 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2373 }
2374
2375 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002376 * Find a vacant area that will fit the given bounds nearest the requested
2377 * cell location. Uses Euclidean distance to score multiple vacant areas.
2378 *
2379 * @param pixelX The X location at which you want to search for a vacant area.
2380 * @param pixelY The Y location at which you want to search for a vacant area.
2381 * @param minSpanX The minimum horizontal span required
2382 * @param minSpanY The minimum vertical span required
2383 * @param spanX Horizontal span of the object.
2384 * @param spanY Vertical span of the object.
2385 * @param ignoreView Considers space occupied by this view as unoccupied
2386 * @param result Previously returned value to possibly recycle.
2387 * @return The X, Y cell of a vacant area that can contain this object,
2388 * nearest the requested location.
2389 */
2390 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2391 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002392 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2393 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002394 }
2395
2396 /**
Adam Cohendf035382011-04-11 17:22:04 -07002397 * Find a starting cell position that will fit the given bounds nearest the requested
2398 * cell location. Uses Euclidean distance to score multiple vacant areas.
2399 *
2400 * @param pixelX The X location at which you want to search for a vacant area.
2401 * @param pixelY The Y location at which you want to search for a vacant area.
2402 * @param spanX Horizontal span of the object.
2403 * @param spanY Vertical span of the object.
2404 * @param ignoreView Considers space occupied by this view as unoccupied
2405 * @param result Previously returned value to possibly recycle.
2406 * @return The X, Y cell of a vacant area that can contain this object,
2407 * nearest the requested location.
2408 */
2409 int[] findNearestArea(
2410 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2411 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2412 }
2413
Michael Jurka0280c3b2010-09-17 15:00:07 -07002414 boolean existsEmptyCell() {
2415 return findCellForSpan(null, 1, 1);
2416 }
2417
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002419 * Finds the upper-left coordinate of the first rectangle in the grid that can
2420 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2421 * then this method will only return coordinates for rectangles that contain the cell
2422 * (intersectX, intersectY)
2423 *
2424 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2425 * can be found.
2426 * @param spanX The horizontal span of the cell we want to find.
2427 * @param spanY The vertical span of the cell we want to find.
2428 *
2429 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002430 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002431 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002432 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002433 }
2434
2435 /**
2436 * Like above, but ignores any cells occupied by the item "ignoreView"
2437 *
2438 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2439 * can be found.
2440 * @param spanX The horizontal span of the cell we want to find.
2441 * @param spanY The vertical span of the cell we want to find.
2442 * @param ignoreView The home screen item we should treat as not occupying any space
2443 * @return
2444 */
2445 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002446 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2447 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002448 }
2449
2450 /**
2451 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2452 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2453 *
2454 * @param spanX The horizontal span of the cell we want to find.
2455 * @param spanY The vertical span of the cell we want to find.
2456 * @param ignoreView The home screen item we should treat as not occupying any space
2457 * @param intersectX The X coordinate of the cell that we should try to overlap
2458 * @param intersectX The Y coordinate of the cell that we should try to overlap
2459 *
2460 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2461 */
2462 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2463 int intersectX, int intersectY) {
2464 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002465 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002466 }
2467
2468 /**
2469 * The superset of the above two methods
2470 */
2471 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002472 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002473 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002474 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002475
Michael Jurka28750fb2010-09-24 17:43:49 -07002476 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002477 while (true) {
2478 int startX = 0;
2479 if (intersectX >= 0) {
2480 startX = Math.max(startX, intersectX - (spanX - 1));
2481 }
2482 int endX = mCountX - (spanX - 1);
2483 if (intersectX >= 0) {
2484 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2485 }
2486 int startY = 0;
2487 if (intersectY >= 0) {
2488 startY = Math.max(startY, intersectY - (spanY - 1));
2489 }
2490 int endY = mCountY - (spanY - 1);
2491 if (intersectY >= 0) {
2492 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2493 }
2494
Winson Chungbbc60d82010-11-11 16:34:41 -08002495 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002496 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002497 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002498 for (int i = 0; i < spanX; i++) {
2499 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002500 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002501 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002502 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002503 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002504 continue inner;
2505 }
2506 }
2507 }
2508 if (cellXY != null) {
2509 cellXY[0] = x;
2510 cellXY[1] = y;
2511 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002512 foundCell = true;
2513 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002514 }
2515 }
2516 if (intersectX == -1 && intersectY == -1) {
2517 break;
2518 } else {
2519 // if we failed to find anything, try again but without any requirements of
2520 // intersecting
2521 intersectX = -1;
2522 intersectY = -1;
2523 continue;
2524 }
2525 }
2526
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002527 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002528 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002529 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002530 }
2531
2532 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002533 * A drag event has begun over this layout.
2534 * It may have begun over this layout (in which case onDragChild is called first),
2535 * or it may have begun on another layout.
2536 */
2537 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002538 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002539 mDragging = true;
2540 }
2541
2542 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002543 * Called when drag has left this CellLayout or has been completed (successfully or not)
2544 */
2545 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002546 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002547 // This can actually be called when we aren't in a drag, e.g. when adding a new
2548 // item to this layout via the customize drawer.
2549 // Guard against that case.
2550 if (mDragging) {
2551 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002552 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002553
2554 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002555 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002556 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2557 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002558 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002559 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002560 }
2561
2562 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002563 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002564 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002565 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566 *
2567 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002569 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002570 if (child != null) {
2571 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002572 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002573 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002574 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002575 }
2576
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002578 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002579 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 * @param cellX X coordinate of upper left corner expressed as a cell position
2581 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002582 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002583 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002584 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002585 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002586 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 final int cellWidth = mCellWidth;
2588 final int cellHeight = mCellHeight;
2589 final int widthGap = mWidthGap;
2590 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002591
Winson Chung4b825dcd2011-06-19 12:41:22 -07002592 final int hStartPadding = getPaddingLeft();
2593 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002594
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2596 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2597
2598 int x = hStartPadding + cellX * (cellWidth + widthGap);
2599 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002600
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002601 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002603
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002604 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002605 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002606 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002607 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002608 * @param width Width in pixels
2609 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002610 * @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 -08002611 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002612 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002613 return rectToCell(getResources(), width, height, result);
2614 }
2615
2616 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002617 // Always assume we're working with the smallest span to make sure we
2618 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002619 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2620 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002621 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002622
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002623 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002624 int spanX = (int) Math.ceil(width / (float) smallerSize);
2625 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002626
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002627 if (result == null) {
2628 return new int[] { spanX, spanY };
2629 }
2630 result[0] = spanX;
2631 result[1] = spanY;
2632 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002633 }
2634
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002635 public int[] cellSpansToSize(int hSpans, int vSpans) {
2636 int[] size = new int[2];
2637 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2638 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2639 return size;
2640 }
2641
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002642 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002643 * Calculate the grid spans needed to fit given item
2644 */
2645 public void calculateSpans(ItemInfo info) {
2646 final int minWidth;
2647 final int minHeight;
2648
2649 if (info instanceof LauncherAppWidgetInfo) {
2650 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2651 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2652 } else if (info instanceof PendingAddWidgetInfo) {
2653 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2654 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2655 } else {
2656 // It's not a widget, so it must be 1x1
2657 info.spanX = info.spanY = 1;
2658 return;
2659 }
2660 int[] spans = rectToCell(minWidth, minHeight, null);
2661 info.spanX = spans[0];
2662 info.spanY = spans[1];
2663 }
2664
2665 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002666 * Find the first vacant cell, if there is one.
2667 *
2668 * @param vacant Holds the x and y coordinate of the vacant cell
2669 * @param spanX Horizontal cell span.
2670 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002671 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002672 * @return True if a vacant cell was found
2673 */
2674 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002675
Michael Jurka0280c3b2010-09-17 15:00:07 -07002676 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002677 }
2678
2679 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2680 int xCount, int yCount, boolean[][] occupied) {
2681
Adam Cohen2801caf2011-05-13 20:57:39 -07002682 for (int y = 0; y < yCount; y++) {
2683 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002684 boolean available = !occupied[x][y];
2685out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2686 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2687 available = available && !occupied[i][j];
2688 if (!available) break out;
2689 }
2690 }
2691
2692 if (available) {
2693 vacant[0] = x;
2694 vacant[1] = y;
2695 return true;
2696 }
2697 }
2698 }
2699
2700 return false;
2701 }
2702
Michael Jurka0280c3b2010-09-17 15:00:07 -07002703 private void clearOccupiedCells() {
2704 for (int x = 0; x < mCountX; x++) {
2705 for (int y = 0; y < mCountY; y++) {
2706 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002707 }
2708 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002709 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002710
Adam Cohend41fbf52012-02-16 23:53:59 -08002711 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002712 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002713 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002714 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002715
Adam Cohend4844c32011-02-18 19:25:06 -08002716 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002717 markCellsAsOccupiedForView(view, mOccupied);
2718 }
2719 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002720 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002721 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002722 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002723 }
2724
Adam Cohend4844c32011-02-18 19:25:06 -08002725 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002726 markCellsAsUnoccupiedForView(view, mOccupied);
2727 }
2728 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002729 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002730 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002731 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002732 }
2733
Adam Cohen482ed822012-03-02 14:15:13 -08002734 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2735 boolean value) {
2736 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002737 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2738 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002739 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002740 }
2741 }
2742 }
2743
Adam Cohen2801caf2011-05-13 20:57:39 -07002744 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002745 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002746 (Math.max((mCountX - 1), 0) * mWidthGap);
2747 }
2748
2749 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002750 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002751 (Math.max((mCountY - 1), 0) * mHeightGap);
2752 }
2753
Michael Jurka66d72172011-04-12 16:29:25 -07002754 public boolean isOccupied(int x, int y) {
2755 if (x < mCountX && y < mCountY) {
2756 return mOccupied[x][y];
2757 } else {
2758 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2759 }
2760 }
2761
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002762 @Override
2763 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2764 return new CellLayout.LayoutParams(getContext(), attrs);
2765 }
2766
2767 @Override
2768 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2769 return p instanceof CellLayout.LayoutParams;
2770 }
2771
2772 @Override
2773 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2774 return new CellLayout.LayoutParams(p);
2775 }
2776
Winson Chungaafa03c2010-06-11 17:34:16 -07002777 public static class CellLayoutAnimationController extends LayoutAnimationController {
2778 public CellLayoutAnimationController(Animation animation, float delay) {
2779 super(animation, delay);
2780 }
2781
2782 @Override
2783 protected long getDelayForView(View view) {
2784 return (int) (Math.random() * 150);
2785 }
2786 }
2787
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002788 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2789 /**
2790 * Horizontal location of the item in the grid.
2791 */
2792 @ViewDebug.ExportedProperty
2793 public int cellX;
2794
2795 /**
2796 * Vertical location of the item in the grid.
2797 */
2798 @ViewDebug.ExportedProperty
2799 public int cellY;
2800
2801 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002802 * Temporary horizontal location of the item in the grid during reorder
2803 */
2804 public int tmpCellX;
2805
2806 /**
2807 * Temporary vertical location of the item in the grid during reorder
2808 */
2809 public int tmpCellY;
2810
2811 /**
2812 * Indicates that the temporary coordinates should be used to layout the items
2813 */
2814 public boolean useTmpCoords;
2815
2816 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002817 * Number of cells spanned horizontally by the item.
2818 */
2819 @ViewDebug.ExportedProperty
2820 public int cellHSpan;
2821
2822 /**
2823 * Number of cells spanned vertically by the item.
2824 */
2825 @ViewDebug.ExportedProperty
2826 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002827
Adam Cohen1b607ed2011-03-03 17:26:50 -08002828 /**
2829 * Indicates whether the item will set its x, y, width and height parameters freely,
2830 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2831 */
Adam Cohend4844c32011-02-18 19:25:06 -08002832 public boolean isLockedToGrid = true;
2833
Adam Cohen482ed822012-03-02 14:15:13 -08002834 /**
2835 * Indicates whether this item can be reordered. Always true except in the case of the
2836 * the AllApps button.
2837 */
2838 public boolean canReorder = true;
2839
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840 // X coordinate of the view in the layout.
2841 @ViewDebug.ExportedProperty
2842 int x;
2843 // Y coordinate of the view in the layout.
2844 @ViewDebug.ExportedProperty
2845 int y;
2846
Romain Guy84f296c2009-11-04 15:00:44 -08002847 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002848
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002849 public LayoutParams(Context c, AttributeSet attrs) {
2850 super(c, attrs);
2851 cellHSpan = 1;
2852 cellVSpan = 1;
2853 }
2854
2855 public LayoutParams(ViewGroup.LayoutParams source) {
2856 super(source);
2857 cellHSpan = 1;
2858 cellVSpan = 1;
2859 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002860
2861 public LayoutParams(LayoutParams source) {
2862 super(source);
2863 this.cellX = source.cellX;
2864 this.cellY = source.cellY;
2865 this.cellHSpan = source.cellHSpan;
2866 this.cellVSpan = source.cellVSpan;
2867 }
2868
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002869 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002870 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002871 this.cellX = cellX;
2872 this.cellY = cellY;
2873 this.cellHSpan = cellHSpan;
2874 this.cellVSpan = cellVSpan;
2875 }
2876
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002877 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002878 if (isLockedToGrid) {
2879 final int myCellHSpan = cellHSpan;
2880 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002881 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2882 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002883
Adam Cohend4844c32011-02-18 19:25:06 -08002884 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2885 leftMargin - rightMargin;
2886 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2887 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002888 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2889 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002890 }
2891 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002892
Winson Chungaafa03c2010-06-11 17:34:16 -07002893 public String toString() {
2894 return "(" + this.cellX + ", " + this.cellY + ")";
2895 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002896
2897 public void setWidth(int width) {
2898 this.width = width;
2899 }
2900
2901 public int getWidth() {
2902 return width;
2903 }
2904
2905 public void setHeight(int height) {
2906 this.height = height;
2907 }
2908
2909 public int getHeight() {
2910 return height;
2911 }
2912
2913 public void setX(int x) {
2914 this.x = x;
2915 }
2916
2917 public int getX() {
2918 return x;
2919 }
2920
2921 public void setY(int y) {
2922 this.y = y;
2923 }
2924
2925 public int getY() {
2926 return y;
2927 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002928 }
2929
Michael Jurka0280c3b2010-09-17 15:00:07 -07002930 // This class stores info for two purposes:
2931 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2932 // its spanX, spanY, and the screen it is on
2933 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2934 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2935 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002936 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002937 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002938 int cellX = -1;
2939 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002940 int spanX;
2941 int spanY;
2942 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002943 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002945 @Override
2946 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002947 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2948 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 }
2950 }
Michael Jurkad771c962011-08-09 15:00:48 -07002951
2952 public boolean lastDownOnOccupiedCell() {
2953 return mLastDownOnOccupiedCell;
2954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002955}