blob: 89c623e5424746cd93dd6585d9edd410a976c7cf [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
Romain Guy8a0bff52012-05-06 13:14:33 -0700163 private final static PorterDuffXfermode sAddBlendMode =
164 new PorterDuffXfermode(PorterDuff.Mode.ADD);
165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 public CellLayout(Context context) {
167 this(context, null);
168 }
169
170 public CellLayout(Context context, AttributeSet attrs) {
171 this(context, attrs, 0);
172 }
173
174 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
175 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700176 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700177
178 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
179 // the user where a dragged item will land when dropped.
180 setWillNotDraw(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700181 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
184
Adam Cohenf4bd5792012-04-27 11:35:29 -0700185 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
186 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700187 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
188 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700189 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700190 mCountX = LauncherModel.getCellCountX();
191 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700192 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800193 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700194 mPreviousReorderDirection[0] = INVALID_DIRECTION;
195 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
197 a.recycle();
198
199 setAlwaysDrawnWithCacheEnabled(false);
200
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700201 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700202
Winson Chung967289b2011-06-30 18:09:30 -0700203 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700204 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800205
Adam Cohenb5ba0972011-09-07 18:02:31 -0700206 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
207 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
208 mForegroundPadding =
209 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800210
Adam Cohen19f37922012-03-21 11:59:11 -0700211 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
212 res.getDimensionPixelSize(R.dimen.app_icon_size));
213
Winson Chungb26f3d62011-06-02 10:49:29 -0700214 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700215 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700216
Winson Chungeecf02d2012-03-02 17:14:58 -0800217 int iconScale = res.getInteger(R.integer.app_icon_scale_percent);
218 if (iconScale >= 0) {
219 mChildScale = iconScale / 100f;
220 }
221 int hotseatIconScale = res.getInteger(R.integer.app_icon_hotseat_scale_percent);
222 if (hotseatIconScale >= 0) {
223 mHotseatChildScale = hotseatIconScale / 100f;
224 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800225
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700226 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700227
Patrick Dubroyce34a972010-10-19 10:34:32 -0700228 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700229
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700230
Winson Chungb8c69f32011-10-19 21:36:08 -0700231 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700232 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800233 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700234 }
235
236 // When dragging things around the home screens, we show a green outline of
237 // where the item will land. The outlines gradually fade out, leaving a trail
238 // behind the drag path.
239 // Set up all the animations that are used to implement this fading.
240 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700241 final float fromAlphaValue = 0;
242 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700243
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700244 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700245
246 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700247 final InterruptibleInOutAnimator anim =
248 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700249 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700250 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700251 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700252 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700253 final Bitmap outline = (Bitmap)anim.getTag();
254
255 // If an animation is started and then stopped very quickly, we can still
256 // get spurious updates we've cleared the tag. Guard against this.
257 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700258 @SuppressWarnings("all") // suppress dead code warning
259 final boolean debug = false;
260 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700261 Object val = animation.getAnimatedValue();
262 Log.d(TAG, "anim " + thisIndex + " update: " + val +
263 ", isStopped " + anim.isStopped());
264 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700265 // Try to prevent it from continuing to run
266 animation.cancel();
267 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700268 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800269 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700270 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700271 }
272 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 // The animation holds a reference to the drag outline bitmap as long is it's
274 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700275 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700276 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700277 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700278 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700279 anim.setTag(null);
280 }
281 }
282 });
283 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700284 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700285
Michael Jurka18014792010-10-14 09:01:34 -0700286 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700287 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800288
Michael Jurkaa52570f2012-03-20 03:18:20 -0700289 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
290 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
291 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700292 }
293
Michael Jurkaf6440da2011-04-05 14:50:34 -0700294 static int widthInPortrait(Resources r, int numCells) {
295 // We use this method from Workspace to figure out how many rows/columns Launcher should
296 // have. We ignore the left/right padding on CellLayout because it turns out in our design
297 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700298 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700299 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
300 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700301
Winson Chung4b825dcd2011-06-19 12:41:22 -0700302 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700303 }
304
Michael Jurkaf6440da2011-04-05 14:50:34 -0700305 static int heightInLandscape(Resources r, int numCells) {
306 // We use this method from Workspace to figure out how many rows/columns Launcher should
307 // have. We ignore the left/right padding on CellLayout because it turns out in our design
308 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700309 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700310 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
311 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700312
Winson Chung4b825dcd2011-06-19 12:41:22 -0700313 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700314 }
315
Adam Cohen2801caf2011-05-13 20:57:39 -0700316 public void enableHardwareLayers() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700317 mShortcutsAndWidgets.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700318 }
319
320 public void setGridSize(int x, int y) {
321 mCountX = x;
322 mCountY = y;
323 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800324 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700325 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700326 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700327 }
328
Patrick Dubroy96864c32011-03-10 17:17:23 -0800329 private void invalidateBubbleTextView(BubbleTextView icon) {
330 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700331 invalidate(icon.getLeft() + getPaddingLeft() - padding,
332 icon.getTop() + getPaddingTop() - padding,
333 icon.getRight() + getPaddingLeft() + padding,
334 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800335 }
336
Adam Cohenb5ba0972011-09-07 18:02:31 -0700337 void setOverScrollAmount(float r, boolean left) {
338 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
339 mOverScrollForegroundDrawable = mOverScrollLeft;
340 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
341 mOverScrollForegroundDrawable = mOverScrollRight;
342 }
343
344 mForegroundAlpha = (int) Math.round((r * 255));
345 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
346 invalidate();
347 }
348
Patrick Dubroy96864c32011-03-10 17:17:23 -0800349 void setPressedOrFocusedIcon(BubbleTextView icon) {
350 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
351 // requires an expanded clip rect (due to the glow's blur radius)
352 BubbleTextView oldIcon = mPressedOrFocusedIcon;
353 mPressedOrFocusedIcon = icon;
354 if (oldIcon != null) {
355 invalidateBubbleTextView(oldIcon);
356 }
357 if (mPressedOrFocusedIcon != null) {
358 invalidateBubbleTextView(mPressedOrFocusedIcon);
359 }
360 }
361
Michael Jurka33945b22010-12-21 18:19:38 -0800362 void setIsDragOverlapping(boolean isDragOverlapping) {
363 if (mIsDragOverlapping != isDragOverlapping) {
364 mIsDragOverlapping = isDragOverlapping;
365 invalidate();
366 }
367 }
368
369 boolean getIsDragOverlapping() {
370 return mIsDragOverlapping;
371 }
372
Adam Cohenebea84d2011-11-09 17:20:41 -0800373 protected void setOverscrollTransformsDirty(boolean dirty) {
374 mScrollingTransformsDirty = dirty;
375 }
376
377 protected void resetOverscrollTransforms() {
378 if (mScrollingTransformsDirty) {
379 setOverscrollTransformsDirty(false);
380 setTranslationX(0);
381 setRotationY(0);
382 // It doesn't matter if we pass true or false here, the important thing is that we
383 // pass 0, which results in the overscroll drawable not being drawn any more.
384 setOverScrollAmount(0, false);
385 setPivotX(getMeasuredWidth() / 2);
386 setPivotY(getMeasuredHeight() / 2);
387 }
388 }
389
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700390 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700391 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700392 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
393 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
394 // When we're small, we are either drawn normally or in the "accepts drops" state (during
395 // a drag). However, we also drag the mini hover background *over* one of those two
396 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700397 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700398 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800399
400 if (mIsDragOverlapping) {
401 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700402 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700403 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700404 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700405 }
Michael Jurka33945b22010-12-21 18:19:38 -0800406
407 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
408 bg.setBounds(mBackgroundRect);
409 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700410 }
Romain Guya6abce82009-11-10 02:54:41 -0800411
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700412 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700413 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700414 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700415 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800416 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700417 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700418 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800419 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700420 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700421 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800422
423 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
424 // requires an expanded clip rect (due to the glow's blur radius)
425 if (mPressedOrFocusedIcon != null) {
426 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
427 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
428 if (b != null) {
429 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700430 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
431 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800432 null);
433 }
434 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700435
Adam Cohen482ed822012-03-02 14:15:13 -0800436 if (DEBUG_VISUALIZE_OCCUPIED) {
437 int[] pt = new int[2];
438 ColorDrawable cd = new ColorDrawable(Color.RED);
439 cd.setBounds(0, 0, 80, 80);
440 for (int i = 0; i < mCountX; i++) {
441 for (int j = 0; j < mCountY; j++) {
442 if (mOccupied[i][j]) {
443 cellToPoint(i, j, pt);
444 canvas.save();
445 canvas.translate(pt[0], pt[1]);
446 cd.draw(canvas);
447 canvas.restore();
448 }
449 }
450 }
451 }
452
Andrew Flynn850d2e72012-04-26 16:51:20 -0700453 int previewOffset = FolderRingAnimator.sPreviewSize;
454
Adam Cohen69ce2e52011-07-03 19:25:21 -0700455 // The folder outer / inner ring image(s)
456 for (int i = 0; i < mFolderOuterRings.size(); i++) {
457 FolderRingAnimator fra = mFolderOuterRings.get(i);
458
459 // Draw outer ring
460 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
461 int width = (int) fra.getOuterRingSize();
462 int height = width;
463 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
464
465 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700466 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700467
468 canvas.save();
469 canvas.translate(centerX - width / 2, centerY - height / 2);
470 d.setBounds(0, 0, width, height);
471 d.draw(canvas);
472 canvas.restore();
473
474 // Draw inner ring
475 d = FolderRingAnimator.sSharedInnerRingDrawable;
476 width = (int) fra.getInnerRingSize();
477 height = width;
478 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
479
480 centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700481 centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700482 canvas.save();
483 canvas.translate(centerX - width / 2, centerY - width / 2);
484 d.setBounds(0, 0, width, height);
485 d.draw(canvas);
486 canvas.restore();
487 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700488
489 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
490 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
491 int width = d.getIntrinsicWidth();
492 int height = d.getIntrinsicHeight();
493
494 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
495 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700496 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohenc51934b2011-07-26 21:07:43 -0700497
498 canvas.save();
499 canvas.translate(centerX - width / 2, centerY - width / 2);
500 d.setBounds(0, 0, width, height);
501 d.draw(canvas);
502 canvas.restore();
503 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700504 }
505
Adam Cohenb5ba0972011-09-07 18:02:31 -0700506 @Override
507 protected void dispatchDraw(Canvas canvas) {
508 super.dispatchDraw(canvas);
509 if (mForegroundAlpha > 0) {
510 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
511 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700512 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700513 mOverScrollForegroundDrawable.draw(canvas);
514 p.setXfermode(null);
515 }
516 }
517
Adam Cohen69ce2e52011-07-03 19:25:21 -0700518 public void showFolderAccept(FolderRingAnimator fra) {
519 mFolderOuterRings.add(fra);
520 }
521
522 public void hideFolderAccept(FolderRingAnimator fra) {
523 if (mFolderOuterRings.contains(fra)) {
524 mFolderOuterRings.remove(fra);
525 }
526 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700527 }
528
Adam Cohenc51934b2011-07-26 21:07:43 -0700529 public void setFolderLeaveBehindCell(int x, int y) {
530 mFolderLeaveBehindCell[0] = x;
531 mFolderLeaveBehindCell[1] = y;
532 invalidate();
533 }
534
535 public void clearFolderLeaveBehind() {
536 mFolderLeaveBehindCell[0] = -1;
537 mFolderLeaveBehindCell[1] = -1;
538 invalidate();
539 }
540
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700541 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700542 public boolean shouldDelayChildPressedState() {
543 return false;
544 }
545
546 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700547 public void cancelLongPress() {
548 super.cancelLongPress();
549
550 // Cancel long press for all children
551 final int count = getChildCount();
552 for (int i = 0; i < count; i++) {
553 final View child = getChildAt(i);
554 child.cancelLongPress();
555 }
556 }
557
Michael Jurkadee05892010-07-27 10:01:56 -0700558 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
559 mInterceptTouchListener = listener;
560 }
561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700563 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 }
565
566 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700567 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 }
569
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800570 public void setIsHotseat(boolean isHotseat) {
571 mIsHotseat = isHotseat;
572 }
573
Winson Chungeecf02d2012-03-02 17:14:58 -0800574 public float getChildrenScale() {
575 return mIsHotseat ? mHotseatChildScale : mChildScale;
576 }
577
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800578
Andrew Flynn850d2e72012-04-26 16:51:20 -0700579 private void scaleChild(BubbleTextView bubbleChild, float scale) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800580 // If we haven't measured the child yet, do it now
581 // (this happens if we're being dropped from all-apps
582 if (bubbleChild.getLayoutParams() instanceof LayoutParams &&
583 (bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700584 getShortcutsAndWidgets().measureChild(bubbleChild);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800585 }
Andrew Flynnbc239a12012-03-06 11:39:49 -0800586
Andrew Flynnbc239a12012-03-06 11:39:49 -0800587 bubbleChild.setScaleX(scale);
588 bubbleChild.setScaleY(scale);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800589 }
590
591 private void resetChild(BubbleTextView bubbleChild) {
592 bubbleChild.setScaleX(1f);
593 bubbleChild.setScaleY(1f);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800594
595 bubbleChild.setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
596 }
597
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800598 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700599 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700600 final LayoutParams lp = params;
601
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800602 // Hotseat icons - scale down and remove text
603 // Don't scale the all apps button
604 // scale percent set to -1 means do not scale
605 // Only scale BubbleTextViews
606 if (child instanceof BubbleTextView) {
607 BubbleTextView bubbleChild = (BubbleTextView) child;
608
Andrew Flynnbc239a12012-03-06 11:39:49 -0800609 // Start the child with 100% scale and visible text
610 resetChild(bubbleChild);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800611
Andrew Flynn850d2e72012-04-26 16:51:20 -0700612 if (mIsHotseat && mHotseatChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800613 // Scale/make transparent for a hotseat
Andrew Flynn850d2e72012-04-26 16:51:20 -0700614 scaleChild(bubbleChild, mHotseatChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800615
Andrew Flynnbc239a12012-03-06 11:39:49 -0800616 bubbleChild.setTextColor(getResources().getColor(android.R.color.transparent));
Winson Chungeecf02d2012-03-02 17:14:58 -0800617 } else if (mChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800618 // Else possibly still scale it if we need to for smaller icons
Andrew Flynn850d2e72012-04-26 16:51:20 -0700619 scaleChild(bubbleChild, mChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800620 }
621 }
622
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 // Generate an id for each view, this assumes we have at most 256x256 cells
624 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700625 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700626 // If the horizontal or vertical span is set to -1, it is taken to
627 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700628 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
629 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630
Winson Chungaafa03c2010-06-11 17:34:16 -0700631 child.setId(childId);
632
Michael Jurkaa52570f2012-03-20 03:18:20 -0700633 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700634
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700635 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700636
Winson Chungaafa03c2010-06-11 17:34:16 -0700637 return true;
638 }
639 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700643 public void removeAllViews() {
644 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700645 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700646 }
647
648 @Override
649 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700650 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700651 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700652 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700653 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654 }
655
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700656 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700657 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700658 }
659
Michael Jurka0280c3b2010-09-17 15:00:07 -0700660 @Override
661 public void removeView(View view) {
662 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700663 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 }
665
666 @Override
667 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700668 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
669 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700670 }
671
672 @Override
673 public void removeViewInLayout(View view) {
674 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700675 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 }
677
678 @Override
679 public void removeViews(int start, int count) {
680 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700681 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700682 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
685
686 @Override
687 public void removeViewsInLayout(int start, int count) {
688 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700689 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700691 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800692 }
693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 @Override
695 protected void onAttachedToWindow() {
696 super.onAttachedToWindow();
697 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
698 }
699
Michael Jurkaaf442092010-06-10 17:01:57 -0700700 public void setTagToCellInfoForPoint(int touchX, int touchY) {
701 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800702 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700703 final int x = touchX + getScrollX();
704 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700705 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700706
707 boolean found = false;
708 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700709 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800710 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700711
Adam Cohen1b607ed2011-03-03 17:26:50 -0800712 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
713 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700714 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700715
Winson Chungeecf02d2012-03-02 17:14:58 -0800716 float scale = child.getScaleX();
717 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
718 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700719 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
720 // offset that by this CellLayout's padding to test an (x,y) point that is relative
721 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800723 frame.inset((int) (frame.width() * (1f - scale) / 2),
724 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700725
Michael Jurkaaf442092010-06-10 17:01:57 -0700726 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700727 cellInfo.cell = child;
728 cellInfo.cellX = lp.cellX;
729 cellInfo.cellY = lp.cellY;
730 cellInfo.spanX = lp.cellHSpan;
731 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700732 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700733 break;
734 }
735 }
736 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700737
Michael Jurkad771c962011-08-09 15:00:48 -0700738 mLastDownOnOccupiedCell = found;
739
Michael Jurkaaf442092010-06-10 17:01:57 -0700740 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700741 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700742 pointToCellExact(x, y, cellXY);
743
Michael Jurkaaf442092010-06-10 17:01:57 -0700744 cellInfo.cell = null;
745 cellInfo.cellX = cellXY[0];
746 cellInfo.cellY = cellXY[1];
747 cellInfo.spanX = 1;
748 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700749 }
750 setTag(cellInfo);
751 }
752
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 @Override
754 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700755 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
756 // even in the case where we return early. Not clearing here was causing bugs whereby on
757 // long-press we'd end up picking up an item from a previous drag operation.
758 final int action = ev.getAction();
759
760 if (action == MotionEvent.ACTION_DOWN) {
761 clearTagCellInfo();
762 }
763
Michael Jurkadee05892010-07-27 10:01:56 -0700764 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
765 return true;
766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767
768 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700769 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800771
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 return false;
773 }
774
Adam Cohenc1997fd2011-08-15 18:26:39 -0700775 private void clearTagCellInfo() {
776 final CellInfo cellInfo = mCellInfo;
777 cellInfo.cell = null;
778 cellInfo.cellX = -1;
779 cellInfo.cellY = -1;
780 cellInfo.spanX = 0;
781 cellInfo.spanY = 0;
782 setTag(cellInfo);
783 }
784
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700786 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 }
788
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700789 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700790 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 * @param x X coordinate of the point
792 * @param y Y coordinate of the point
793 * @param result Array of 2 ints to hold the x and y coordinate of the cell
794 */
795 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700796 final int hStartPadding = getPaddingLeft();
797 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798
799 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
800 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
801
Adam Cohend22015c2010-07-26 22:02:18 -0700802 final int xAxis = mCountX;
803 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804
805 if (result[0] < 0) result[0] = 0;
806 if (result[0] >= xAxis) result[0] = xAxis - 1;
807 if (result[1] < 0) result[1] = 0;
808 if (result[1] >= yAxis) result[1] = yAxis - 1;
809 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 /**
812 * Given a point, return the cell that most closely encloses that point
813 * @param x X coordinate of the point
814 * @param y Y coordinate of the point
815 * @param result Array of 2 ints to hold the x and y coordinate of the cell
816 */
817 void pointToCellRounded(int x, int y, int[] result) {
818 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
819 }
820
821 /**
822 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700823 *
824 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700826 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 * @param result Array of 2 ints to hold the x and y coordinate of the point
828 */
829 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700830 final int hStartPadding = getPaddingLeft();
831 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832
833 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
834 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
835 }
836
Adam Cohene3e27a82011-04-15 12:07:39 -0700837 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800838 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700839 *
840 * @param cellX X coordinate of the cell
841 * @param cellY Y coordinate of the cell
842 *
843 * @param result Array of 2 ints to hold the x and y coordinate of the point
844 */
845 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700846 regionToCenterPoint(cellX, cellY, 1, 1, result);
847 }
848
849 /**
850 * Given a cell coordinate and span return the point that represents the center of the regio
851 *
852 * @param cellX X coordinate of the cell
853 * @param cellY Y coordinate of the cell
854 *
855 * @param result Array of 2 ints to hold the x and y coordinate of the point
856 */
857 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700858 final int hStartPadding = getPaddingLeft();
859 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700860 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
861 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
862 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
863 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700864 }
865
Adam Cohen19f37922012-03-21 11:59:11 -0700866 /**
867 * Given a cell coordinate and span fills out a corresponding pixel rect
868 *
869 * @param cellX X coordinate of the cell
870 * @param cellY Y coordinate of the cell
871 * @param result Rect in which to write the result
872 */
873 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
874 final int hStartPadding = getPaddingLeft();
875 final int vStartPadding = getPaddingTop();
876 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
877 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
878 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
879 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
880 }
881
Adam Cohen482ed822012-03-02 14:15:13 -0800882 public float getDistanceFromCell(float x, float y, int[] cell) {
883 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
884 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
885 Math.pow(y - mTmpPoint[1], 2));
886 return distance;
887 }
888
Romain Guy84f296c2009-11-04 15:00:44 -0800889 int getCellWidth() {
890 return mCellWidth;
891 }
892
893 int getCellHeight() {
894 return mCellHeight;
895 }
896
Adam Cohend4844c32011-02-18 19:25:06 -0800897 int getWidthGap() {
898 return mWidthGap;
899 }
900
901 int getHeightGap() {
902 return mHeightGap;
903 }
904
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700905 Rect getContentRect(Rect r) {
906 if (r == null) {
907 r = new Rect();
908 }
909 int left = getPaddingLeft();
910 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700911 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
912 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700913 r.set(left, top, right, bottom);
914 return r;
915 }
916
Adam Cohena897f392012-04-27 18:12:05 -0700917 static void getMetrics(Rect metrics, Resources res, int measureWidth, int measureHeight,
918 int countX, int countY, int orientation) {
919 int numWidthGaps = countX - 1;
920 int numHeightGaps = countY - 1;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700921
922 int widthGap;
923 int heightGap;
924 int cellWidth;
925 int cellHeight;
926 int paddingLeft;
927 int paddingRight;
928 int paddingTop;
929 int paddingBottom;
930
Adam Cohena897f392012-04-27 18:12:05 -0700931 int maxGap = res.getDimensionPixelSize(R.dimen.workspace_max_gap);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700932 if (orientation == LANDSCAPE) {
933 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_land);
934 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_land);
935 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_land);
936 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
937 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_land);
938 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_land);
939 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_land);
940 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_land);
941 } else {
942 // PORTRAIT
943 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_port);
944 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_port);
945 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
946 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_port);
947 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_port);
948 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_port);
949 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_port);
950 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_port);
951 }
952
953 if (widthGap < 0 || heightGap < 0) {
954 int hSpace = measureWidth - paddingLeft - paddingRight;
955 int vSpace = measureHeight - paddingTop - paddingBottom;
Adam Cohena897f392012-04-27 18:12:05 -0700956 int hFreeSpace = hSpace - (countX * cellWidth);
957 int vFreeSpace = vSpace - (countY * cellHeight);
958 widthGap = Math.min(maxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
959 heightGap = Math.min(maxGap, numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700960 }
961 metrics.set(cellWidth, cellHeight, widthGap, heightGap);
962 }
963
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 @Override
965 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700967 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
968
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
970 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700971
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
973 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
974 }
975
Adam Cohend22015c2010-07-26 22:02:18 -0700976 int numWidthGaps = mCountX - 1;
977 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978
Adam Cohen234c4cd2011-07-17 21:03:04 -0700979 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Michael Jurkadd13e3d2012-05-01 12:38:17 -0700980 int hSpace = widthSpecSize - getPaddingLeft() - getPaddingRight();
981 int vSpace = heightSpecSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700982 int hFreeSpace = hSpace - (mCountX * mCellWidth);
983 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700984 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
985 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700986 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700987 } else {
988 mWidthGap = mOriginalWidthGap;
989 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700990 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700991
Michael Jurka8c920dd2011-01-20 14:16:56 -0800992 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
993 int newWidth = widthSpecSize;
994 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700995 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700996 newWidth = getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700997 ((mCountX - 1) * mWidthGap);
Michael Jurka8b805b12012-04-18 14:23:14 -0700998 newHeight = getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700999 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001000 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001001 }
Michael Jurka8c920dd2011-01-20 14:16:56 -08001002
1003 int count = getChildCount();
1004 for (int i = 0; i < count; i++) {
1005 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001006 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
1007 getPaddingRight(), MeasureSpec.EXACTLY);
1008 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
1009 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -08001010 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
1011 }
1012 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 }
1014
1015 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001016 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001019 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001020 child.layout(getPaddingLeft(), getPaddingTop(),
1021 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
1023 }
1024
1025 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001026 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1027 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001028 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001029 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
1030 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001031 }
1032
1033 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001035 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037
1038 @Override
1039 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001040 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 }
1042
Michael Jurka5f1c5092010-09-03 14:15:02 -07001043 public float getBackgroundAlpha() {
1044 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001045 }
1046
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001047 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001048 if (mBackgroundAlphaMultiplier != multiplier) {
1049 mBackgroundAlphaMultiplier = multiplier;
1050 invalidate();
1051 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001052 }
1053
Adam Cohenddb82192010-11-10 16:32:54 -08001054 public float getBackgroundAlphaMultiplier() {
1055 return mBackgroundAlphaMultiplier;
1056 }
1057
Michael Jurka5f1c5092010-09-03 14:15:02 -07001058 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001059 if (mBackgroundAlpha != alpha) {
1060 mBackgroundAlpha = alpha;
1061 invalidate();
1062 }
Michael Jurkadee05892010-07-27 10:01:56 -07001063 }
1064
Michael Jurkaa52570f2012-03-20 03:18:20 -07001065 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001066 final int childCount = getChildCount();
1067 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001068 getChildAt(i).setAlpha(alpha);
1069 }
1070 }
1071
Michael Jurkaa52570f2012-03-20 03:18:20 -07001072 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1073 if (getChildCount() > 0) {
1074 return (ShortcutAndWidgetContainer) getChildAt(0);
1075 }
1076 return null;
1077 }
1078
Patrick Dubroy440c3602010-07-13 17:50:32 -07001079 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001080 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001081 }
1082
Adam Cohen76fc0852011-06-17 13:26:23 -07001083 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001084 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001085 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001086 boolean[][] occupied = mOccupied;
1087 if (!permanent) {
1088 occupied = mTmpOccupied;
1089 }
1090
Adam Cohen19f37922012-03-21 11:59:11 -07001091 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001092 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1093 final ItemInfo info = (ItemInfo) child.getTag();
1094
1095 // We cancel any existing animations
1096 if (mReorderAnimators.containsKey(lp)) {
1097 mReorderAnimators.get(lp).cancel();
1098 mReorderAnimators.remove(lp);
1099 }
1100
Adam Cohen482ed822012-03-02 14:15:13 -08001101 final int oldX = lp.x;
1102 final int oldY = lp.y;
1103 if (adjustOccupied) {
1104 occupied[lp.cellX][lp.cellY] = false;
1105 occupied[cellX][cellY] = true;
1106 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001107 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001108 if (permanent) {
1109 lp.cellX = info.cellX = cellX;
1110 lp.cellY = info.cellY = cellY;
1111 } else {
1112 lp.tmpCellX = cellX;
1113 lp.tmpCellY = cellY;
1114 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001115 clc.setupLp(lp);
1116 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001117 final int newX = lp.x;
1118 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001119
Adam Cohen76fc0852011-06-17 13:26:23 -07001120 lp.x = oldX;
1121 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001122
Adam Cohen482ed822012-03-02 14:15:13 -08001123 // Exit early if we're not actually moving the view
1124 if (oldX == newX && oldY == newY) {
1125 lp.isLockedToGrid = true;
1126 return true;
1127 }
1128
1129 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
1130 va.setDuration(duration);
1131 mReorderAnimators.put(lp, va);
1132
1133 va.addUpdateListener(new AnimatorUpdateListener() {
1134 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001135 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001136 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001137 lp.x = (int) ((1 - r) * oldX + r * newX);
1138 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001139 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001140 }
1141 });
Adam Cohen482ed822012-03-02 14:15:13 -08001142 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001143 boolean cancelled = false;
1144 public void onAnimationEnd(Animator animation) {
1145 // If the animation was cancelled, it means that another animation
1146 // has interrupted this one, and we don't want to lock the item into
1147 // place just yet.
1148 if (!cancelled) {
1149 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001150 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001151 }
1152 if (mReorderAnimators.containsKey(lp)) {
1153 mReorderAnimators.remove(lp);
1154 }
1155 }
1156 public void onAnimationCancel(Animator animation) {
1157 cancelled = true;
1158 }
1159 });
Adam Cohen482ed822012-03-02 14:15:13 -08001160 va.setStartDelay(delay);
1161 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001162 return true;
1163 }
1164 return false;
1165 }
1166
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001167 /**
1168 * Estimate where the top left cell of the dragged item will land if it is dropped.
1169 *
1170 * @param originX The X value of the top left corner of the item
1171 * @param originY The Y value of the top left corner of the item
1172 * @param spanX The number of horizontal cells that the item spans
1173 * @param spanY The number of vertical cells that the item spans
1174 * @param result The estimated drop cell X and Y.
1175 */
1176 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001177 final int countX = mCountX;
1178 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001179
Michael Jurkaa63c4522010-08-19 13:52:27 -07001180 // pointToCellRounded takes the top left of a cell but will pad that with
1181 // cellWidth/2 and cellHeight/2 when finding the matching cell
1182 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001183
1184 // If the item isn't fully on this screen, snap to the edges
1185 int rightOverhang = result[0] + spanX - countX;
1186 if (rightOverhang > 0) {
1187 result[0] -= rightOverhang; // Snap to right
1188 }
1189 result[0] = Math.max(0, result[0]); // Snap to left
1190 int bottomOverhang = result[1] + spanY - countY;
1191 if (bottomOverhang > 0) {
1192 result[1] -= bottomOverhang; // Snap to bottom
1193 }
1194 result[1] = Math.max(0, result[1]); // Snap to top
1195 }
1196
Adam Cohen482ed822012-03-02 14:15:13 -08001197 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1198 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001199 final int oldDragCellX = mDragCell[0];
1200 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001201
Winson Chungb8c69f32011-10-19 21:36:08 -07001202 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001203 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1204 } else {
1205 mDragCenter.set(originX, originY);
1206 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001207
Adam Cohen2801caf2011-05-13 20:57:39 -07001208 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001209 return;
1210 }
1211
Adam Cohen482ed822012-03-02 14:15:13 -08001212 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1213 mDragCell[0] = cellX;
1214 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001215 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001216 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001217 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001218
Joe Onorato4be866d2010-10-10 11:26:02 -07001219 int left = topLeft[0];
1220 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001221
Winson Chungb8c69f32011-10-19 21:36:08 -07001222 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001223 // When drawing the drag outline, it did not account for margin offsets
1224 // added by the view's parent.
1225 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1226 left += lp.leftMargin;
1227 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001228
Adam Cohen99e8b402011-03-25 19:23:43 -07001229 // Offsets due to the size difference between the View and the dragOutline.
1230 // There is a size difference to account for the outer blur, which may lie
1231 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001232 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001233 // We center about the x axis
1234 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1235 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001236 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001237 if (dragOffset != null && dragRegion != null) {
1238 // Center the drag region *horizontally* in the cell and apply a drag
1239 // outline offset
1240 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1241 - dragRegion.width()) / 2;
1242 top += dragOffset.y;
1243 } else {
1244 // Center the drag outline in the cell
1245 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1246 - dragOutline.getWidth()) / 2;
1247 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1248 - dragOutline.getHeight()) / 2;
1249 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001250 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001251 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001252 mDragOutlineAnims[oldIndex].animateOut();
1253 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001254 Rect r = mDragOutlines[mDragOutlineCurrent];
1255 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1256 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001257 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001258 }
Winson Chung150fbab2010-09-29 17:14:26 -07001259
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001260 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1261 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001262 }
1263 }
1264
Adam Cohene0310962011-04-18 16:15:31 -07001265 public void clearDragOutlines() {
1266 final int oldIndex = mDragOutlineCurrent;
1267 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001268 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001269 }
1270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001272 * Find a vacant area that will fit the given bounds nearest the requested
1273 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001274 *
Romain Guy51afc022009-05-04 18:03:43 -07001275 * @param pixelX The X location at which you want to search for a vacant area.
1276 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001277 * @param spanX Horizontal span of the object.
1278 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001279 * @param result Array in which to place the result, or null (in which case a new array will
1280 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001281 * @return The X, Y cell of a vacant area that can contain this object,
1282 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001284 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1285 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001286 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001287 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001288
Michael Jurka6a1435d2010-09-27 17:35:12 -07001289 /**
1290 * Find a vacant area that will fit the given bounds nearest the requested
1291 * cell location. Uses Euclidean distance to score multiple vacant areas.
1292 *
1293 * @param pixelX The X location at which you want to search for a vacant area.
1294 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001295 * @param minSpanX The minimum horizontal span required
1296 * @param minSpanY The minimum vertical span required
1297 * @param spanX Horizontal span of the object.
1298 * @param spanY Vertical span of the object.
1299 * @param result Array in which to place the result, or null (in which case a new array will
1300 * be allocated)
1301 * @return The X, Y cell of a vacant area that can contain this object,
1302 * nearest the requested location.
1303 */
1304 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1305 int spanY, int[] result, int[] resultSpan) {
1306 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1307 result, resultSpan);
1308 }
1309
1310 /**
1311 * Find a vacant area that will fit the given bounds nearest the requested
1312 * cell location. Uses Euclidean distance to score multiple vacant areas.
1313 *
1314 * @param pixelX The X location at which you want to search for a vacant area.
1315 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001316 * @param spanX Horizontal span of the object.
1317 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001318 * @param ignoreOccupied If true, the result can be an occupied cell
1319 * @param result Array in which to place the result, or null (in which case a new array will
1320 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001321 * @return The X, Y cell of a vacant area that can contain this object,
1322 * nearest the requested location.
1323 */
Adam Cohendf035382011-04-11 17:22:04 -07001324 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1325 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001326 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001327 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001328 }
1329
1330 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1331 private void lazyInitTempRectStack() {
1332 if (mTempRectStack.isEmpty()) {
1333 for (int i = 0; i < mCountX * mCountY; i++) {
1334 mTempRectStack.push(new Rect());
1335 }
1336 }
1337 }
Adam Cohen482ed822012-03-02 14:15:13 -08001338
Adam Cohend41fbf52012-02-16 23:53:59 -08001339 private void recycleTempRects(Stack<Rect> used) {
1340 while (!used.isEmpty()) {
1341 mTempRectStack.push(used.pop());
1342 }
1343 }
1344
1345 /**
1346 * Find a vacant area that will fit the given bounds nearest the requested
1347 * cell location. Uses Euclidean distance to score multiple vacant areas.
1348 *
1349 * @param pixelX The X location at which you want to search for a vacant area.
1350 * @param pixelY The Y location at which you want to search for a vacant area.
1351 * @param minSpanX The minimum horizontal span required
1352 * @param minSpanY The minimum vertical span required
1353 * @param spanX Horizontal span of the object.
1354 * @param spanY Vertical span of the object.
1355 * @param ignoreOccupied If true, the result can be an occupied cell
1356 * @param result Array in which to place the result, or null (in which case a new array will
1357 * be allocated)
1358 * @return The X, Y cell of a vacant area that can contain this object,
1359 * nearest the requested location.
1360 */
1361 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001362 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1363 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001364 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001365 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001366 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001367
Adam Cohene3e27a82011-04-15 12:07:39 -07001368 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1369 // to the center of the item, but we are searching based on the top-left cell, so
1370 // we translate the point over to correspond to the top-left.
1371 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1372 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1373
Jeff Sharkey70864282009-04-07 21:08:40 -07001374 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001375 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001376 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001377 final Rect bestRect = new Rect(-1, -1, -1, -1);
1378 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001379
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001380 final int countX = mCountX;
1381 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001382
Adam Cohend41fbf52012-02-16 23:53:59 -08001383 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1384 spanX < minSpanX || spanY < minSpanY) {
1385 return bestXY;
1386 }
1387
1388 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001389 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001390 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1391 int ySize = -1;
1392 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001393 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001394 // First, let's see if this thing fits anywhere
1395 for (int i = 0; i < minSpanX; i++) {
1396 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001397 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001398 continue inner;
1399 }
Michael Jurkac28de512010-08-13 11:27:44 -07001400 }
1401 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001402 xSize = minSpanX;
1403 ySize = minSpanY;
1404
1405 // We know that the item will fit at _some_ acceptable size, now let's see
1406 // how big we can make it. We'll alternate between incrementing x and y spans
1407 // until we hit a limit.
1408 boolean incX = true;
1409 boolean hitMaxX = xSize >= spanX;
1410 boolean hitMaxY = ySize >= spanY;
1411 while (!(hitMaxX && hitMaxY)) {
1412 if (incX && !hitMaxX) {
1413 for (int j = 0; j < ySize; j++) {
1414 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1415 // We can't move out horizontally
1416 hitMaxX = true;
1417 }
1418 }
1419 if (!hitMaxX) {
1420 xSize++;
1421 }
1422 } else if (!hitMaxY) {
1423 for (int i = 0; i < xSize; i++) {
1424 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1425 // We can't move out vertically
1426 hitMaxY = true;
1427 }
1428 }
1429 if (!hitMaxY) {
1430 ySize++;
1431 }
1432 }
1433 hitMaxX |= xSize >= spanX;
1434 hitMaxY |= ySize >= spanY;
1435 incX = !incX;
1436 }
1437 incX = true;
1438 hitMaxX = xSize >= spanX;
1439 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001440 }
Winson Chung0be025d2011-05-23 17:45:09 -07001441 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001442 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443
Adam Cohend41fbf52012-02-16 23:53:59 -08001444 // We verify that the current rect is not a sub-rect of any of our previous
1445 // candidates. In this case, the current rect is disqualified in favour of the
1446 // containing rect.
1447 Rect currentRect = mTempRectStack.pop();
1448 currentRect.set(x, y, x + xSize, y + ySize);
1449 boolean contained = false;
1450 for (Rect r : validRegions) {
1451 if (r.contains(currentRect)) {
1452 contained = true;
1453 break;
1454 }
1455 }
1456 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001457 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1458 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001459
Adam Cohend41fbf52012-02-16 23:53:59 -08001460 if ((distance <= bestDistance && !contained) ||
1461 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001462 bestDistance = distance;
1463 bestXY[0] = x;
1464 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001465 if (resultSpan != null) {
1466 resultSpan[0] = xSize;
1467 resultSpan[1] = ySize;
1468 }
1469 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
1472 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001473 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001474 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475
Adam Cohenc0dcf592011-06-01 15:30:43 -07001476 // Return -1, -1 if no suitable location found
1477 if (bestDistance == Double.MAX_VALUE) {
1478 bestXY[0] = -1;
1479 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001480 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001481 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001482 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001484
Adam Cohen482ed822012-03-02 14:15:13 -08001485 /**
1486 * Find a vacant area that will fit the given bounds nearest the requested
1487 * cell location, and will also weigh in a suggested direction vector of the
1488 * desired location. This method computers distance based on unit grid distances,
1489 * not pixel distances.
1490 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001491 * @param cellX The X cell nearest to which you want to search for a vacant area.
1492 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001493 * @param spanX Horizontal span of the object.
1494 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001495 * @param direction The favored direction in which the views should move from x, y
1496 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1497 * matches exactly. Otherwise we find the best matching direction.
1498 * @param occoupied The array which represents which cells in the CellLayout are occupied
1499 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1500 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001501 * @param result Array in which to place the result, or null (in which case a new array will
1502 * be allocated)
1503 * @return The X, Y cell of a vacant area that can contain this object,
1504 * nearest the requested location.
1505 */
1506 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001507 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001508 // Keep track of best-scoring drop area
1509 final int[] bestXY = result != null ? result : new int[2];
1510 float bestDistance = Float.MAX_VALUE;
1511 int bestDirectionScore = Integer.MIN_VALUE;
1512
1513 final int countX = mCountX;
1514 final int countY = mCountY;
1515
1516 for (int y = 0; y < countY - (spanY - 1); y++) {
1517 inner:
1518 for (int x = 0; x < countX - (spanX - 1); x++) {
1519 // First, let's see if this thing fits anywhere
1520 for (int i = 0; i < spanX; i++) {
1521 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001522 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001523 continue inner;
1524 }
1525 }
1526 }
1527
1528 float distance = (float)
1529 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1530 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001531 computeDirectionVector(x - cellX, y - cellY, curDirection);
1532 // The direction score is just the dot product of the two candidate direction
1533 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001534 int curDirectionScore = direction[0] * curDirection[0] +
1535 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001536 boolean exactDirectionOnly = false;
1537 boolean directionMatches = direction[0] == curDirection[0] &&
1538 direction[0] == curDirection[0];
1539 if ((directionMatches || !exactDirectionOnly) &&
1540 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001541 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1542 bestDistance = distance;
1543 bestDirectionScore = curDirectionScore;
1544 bestXY[0] = x;
1545 bestXY[1] = y;
1546 }
1547 }
1548 }
1549
1550 // Return -1, -1 if no suitable location found
1551 if (bestDistance == Float.MAX_VALUE) {
1552 bestXY[0] = -1;
1553 bestXY[1] = -1;
1554 }
1555 return bestXY;
1556 }
1557
Adam Cohen47a876d2012-03-19 13:21:41 -07001558 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1559 int[] direction,boolean[][] occupied,
1560 boolean blockOccupied[][], int[] result) {
1561 // Keep track of best-scoring drop area
1562 final int[] bestXY = result != null ? result : new int[2];
1563 bestXY[0] = -1;
1564 bestXY[1] = -1;
1565 float bestDistance = Float.MAX_VALUE;
1566
1567 // We use this to march in a single direction
Adam Cohen5b53f292012-03-29 14:30:35 -07001568 if ((direction[0] != 0 && direction[1] != 0) ||
1569 (direction[0] == 0 && direction[1] == 0)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001570 return bestXY;
1571 }
1572
1573 // This will only incrememnet one of x or y based on the assertion above
1574 int x = cellX + direction[0];
1575 int y = cellY + direction[1];
1576 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
1577
1578 boolean fail = false;
1579 for (int i = 0; i < spanX; i++) {
1580 for (int j = 0; j < spanY; j++) {
1581 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1582 fail = true;
1583 }
1584 }
1585 }
1586 if (!fail) {
1587 float distance = (float)
1588 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1589 if (Float.compare(distance, bestDistance) < 0) {
1590 bestDistance = distance;
1591 bestXY[0] = x;
1592 bestXY[1] = y;
1593 }
1594 }
1595 x += direction[0];
1596 y += direction[1];
1597 }
1598 return bestXY;
1599 }
1600
Adam Cohen482ed822012-03-02 14:15:13 -08001601 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001602 int[] direction, ItemConfiguration currentState) {
1603 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001604 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001605 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001606 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1607
Adam Cohen8baab352012-03-20 17:39:21 -07001608 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001609
1610 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001611 c.x = mTempLocation[0];
1612 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001613 success = true;
1614
1615 }
Adam Cohen8baab352012-03-20 17:39:21 -07001616 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001617 return success;
1618 }
1619
Adam Cohen47a876d2012-03-19 13:21:41 -07001620 // This method looks in the specified direction to see if there is an additional view
1621 // immediately adjecent in that direction
1622 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
Adam Cohen19f37922012-03-21 11:59:11 -07001623 boolean[][] occupied, View dragView, ItemConfiguration currentState) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001624 boolean found = false;
1625
Michael Jurkaa52570f2012-03-20 03:18:20 -07001626 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001627 Rect r0 = new Rect(boundingRect);
1628 Rect r1 = new Rect();
1629
1630 int deltaX = 0;
1631 int deltaY = 0;
1632 if (direction[1] < 0) {
1633 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom);
1634 deltaY = -1;
1635 } else if (direction[1] > 0) {
1636 r0.set(r0.left, r0.top, r0.right, r0.bottom + 1);
1637 deltaY = 1;
1638 } else if (direction[0] < 0) {
1639 r0.set(r0.left - 1, r0.top, r0.right, r0.bottom);
1640 deltaX = -1;
1641 } else if (direction[0] > 0) {
1642 r0.set(r0.left, r0.top, r0.right + 1, r0.bottom);
1643 deltaX = 1;
1644 }
1645
1646 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001647 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen19f37922012-03-21 11:59:11 -07001648 if (views.contains(child) || child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001649 CellAndSpan c = currentState.map.get(child);
Adam Cohen47a876d2012-03-19 13:21:41 -07001650
Adam Cohen8baab352012-03-20 17:39:21 -07001651 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1652 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001653 if (Rect.intersects(r0, r1)) {
1654 if (!lp.canReorder) {
1655 return false;
1656 }
1657 boolean pushed = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001658 for (int x = c.x; x < c.x + c.spanX; x++) {
1659 for (int y = c.y; y < c.y + c.spanY; y++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001660 boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX
1661 && y - deltaY >= 0 && y - deltaY < mCountY;
1662 if (inBounds && occupied[x - deltaX][y - deltaY]) {
1663 pushed = true;
1664 }
1665 }
1666 }
1667 if (pushed) {
1668 views.add(child);
Adam Cohen8baab352012-03-20 17:39:21 -07001669 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001670 found = true;
1671 }
1672 }
1673 }
1674 return found;
1675 }
1676
Adam Cohen482ed822012-03-02 14:15:13 -08001677 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohen19f37922012-03-21 11:59:11 -07001678 int[] direction, boolean push, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001679 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001680
Adam Cohen8baab352012-03-20 17:39:21 -07001681 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001682 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001683 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001684 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001685 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001686 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001687 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001688 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001689 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001690 }
1691 }
Adam Cohen8baab352012-03-20 17:39:21 -07001692
1693 @SuppressWarnings("unchecked")
1694 ArrayList<View> dup = (ArrayList<View>) views.clone();
1695 // We try and expand the group of views in the direction vector passed, based on
1696 // whether they are physically adjacent, ie. based on "push mechanics".
Adam Cohen19f37922012-03-21 11:59:11 -07001697 while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied, dragView,
Adam Cohen8baab352012-03-20 17:39:21 -07001698 currentState)) {
1699 }
1700
1701 // Mark the occupied state as false for the group of views we want to move.
1702 for (View v: dup) {
1703 CellAndSpan c = currentState.map.get(v);
1704 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1705 }
1706
Adam Cohen47a876d2012-03-19 13:21:41 -07001707 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1708 int top = boundingRect.top;
1709 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001710 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
1711 // for tetris-style interlocking.
1712 for (View v: dup) {
1713 CellAndSpan c = currentState.map.get(v);
1714 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001715 }
1716
Adam Cohen482ed822012-03-02 14:15:13 -08001717 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1718
Adam Cohen8baab352012-03-20 17:39:21 -07001719 if (push) {
1720 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1721 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1722 } else {
1723 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1724 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1725 }
Adam Cohen482ed822012-03-02 14:15:13 -08001726
Adam Cohen8baab352012-03-20 17:39:21 -07001727 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001728 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001729 int deltaX = mTempLocation[0] - boundingRect.left;
1730 int deltaY = mTempLocation[1] - boundingRect.top;
1731 for (View v: dup) {
1732 CellAndSpan c = currentState.map.get(v);
1733 c.x += deltaX;
1734 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001735 }
1736 success = true;
1737 }
Adam Cohen8baab352012-03-20 17:39:21 -07001738
1739 // In either case, we set the occupied array as marked for the location of the views
1740 for (View v: dup) {
1741 CellAndSpan c = currentState.map.get(v);
1742 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001743 }
1744 return success;
1745 }
1746
1747 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1748 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1749 }
1750
1751 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001752 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001753 // Return early if get invalid cell positions
1754 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001755
Adam Cohen8baab352012-03-20 17:39:21 -07001756 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001757 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001758
Adam Cohen8baab352012-03-20 17:39:21 -07001759 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001760 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001761 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001762 if (c != null) {
1763 c.x = cellX;
1764 c.y = cellY;
1765 }
Adam Cohen482ed822012-03-02 14:15:13 -08001766 }
Adam Cohen482ed822012-03-02 14:15:13 -08001767 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1768 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001769 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001770 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001771 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001772 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001773 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001774 if (Rect.intersects(r0, r1)) {
1775 if (!lp.canReorder) {
1776 return false;
1777 }
1778 mIntersectingViews.add(child);
1779 }
1780 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001781
Adam Cohen8baab352012-03-20 17:39:21 -07001782 // We try to move the intersecting views as a block using the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001783 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1784 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001785 return true;
1786 }
1787 // Try the opposite direction
1788 direction[0] *= -1;
1789 direction[1] *= -1;
Adam Cohen19f37922012-03-21 11:59:11 -07001790 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1791 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001792 return true;
1793 }
1794 // Switch the direction back
1795 direction[0] *= -1;
1796 direction[1] *= -1;
1797
Adam Cohen8baab352012-03-20 17:39:21 -07001798 // Next we try moving the views as a block , but without requiring the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001799 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, ignoreView,
1800 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001801 return true;
1802 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001803
Adam Cohen482ed822012-03-02 14:15:13 -08001804 // Ok, they couldn't move as a block, let's move them individually
1805 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001806 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001807 return false;
1808 }
1809 }
1810 return true;
1811 }
1812
1813 /*
1814 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1815 * the provided point and the provided cell
1816 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001817 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001818 double angle = Math.atan(((float) deltaY) / deltaX);
1819
1820 result[0] = 0;
1821 result[1] = 0;
1822 if (Math.abs(Math.cos(angle)) > 0.5f) {
1823 result[0] = (int) Math.signum(deltaX);
1824 }
1825 if (Math.abs(Math.sin(angle)) > 0.5f) {
1826 result[1] = (int) Math.signum(deltaY);
1827 }
1828 }
1829
Adam Cohen8baab352012-03-20 17:39:21 -07001830 private void copyOccupiedArray(boolean[][] occupied) {
1831 for (int i = 0; i < mCountX; i++) {
1832 for (int j = 0; j < mCountY; j++) {
1833 occupied[i][j] = mOccupied[i][j];
1834 }
1835 }
1836 }
1837
Adam Cohen482ed822012-03-02 14:15:13 -08001838 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1839 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001840 // Copy the current state into the solution. This solution will be manipulated as necessary.
1841 copyCurrentStateToSolution(solution, false);
1842 // Copy the current occupied array into the temporary occupied array. This array will be
1843 // manipulated as necessary to find a solution.
1844 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001845
1846 // We find the nearest cell into which we would place the dragged item, assuming there's
1847 // nothing in its way.
1848 int result[] = new int[2];
1849 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1850
1851 boolean success = false;
1852 // First we try the exact nearest position of the item being dragged,
1853 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001854 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1855 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001856
1857 if (!success) {
1858 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1859 // x, then 1 in y etc.
1860 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1861 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1862 dragView, false, solution);
1863 } else if (spanY > minSpanY) {
1864 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1865 dragView, true, solution);
1866 }
1867 solution.isSolution = false;
1868 } else {
1869 solution.isSolution = true;
1870 solution.dragViewX = result[0];
1871 solution.dragViewY = result[1];
1872 solution.dragViewSpanX = spanX;
1873 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001874 }
1875 return solution;
1876 }
1877
1878 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001879 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001880 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001881 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001882 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001883 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001884 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001885 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001886 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001887 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001888 }
Adam Cohen8baab352012-03-20 17:39:21 -07001889 solution.map.put(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001890 }
1891 }
1892
1893 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1894 for (int i = 0; i < mCountX; i++) {
1895 for (int j = 0; j < mCountY; j++) {
1896 mTmpOccupied[i][j] = false;
1897 }
1898 }
1899
Michael Jurkaa52570f2012-03-20 03:18:20 -07001900 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001901 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001902 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001903 if (child == dragView) continue;
1904 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001905 CellAndSpan c = solution.map.get(child);
1906 if (c != null) {
1907 lp.tmpCellX = c.x;
1908 lp.tmpCellY = c.y;
1909 lp.cellHSpan = c.spanX;
1910 lp.cellVSpan = c.spanY;
1911 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001912 }
1913 }
1914 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1915 solution.dragViewSpanY, mTmpOccupied, true);
1916 }
1917
1918 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1919 commitDragView) {
1920
1921 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1922 for (int i = 0; i < mCountX; i++) {
1923 for (int j = 0; j < mCountY; j++) {
1924 occupied[i][j] = false;
1925 }
1926 }
1927
Michael Jurkaa52570f2012-03-20 03:18:20 -07001928 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001929 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001930 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001931 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001932 CellAndSpan c = solution.map.get(child);
1933 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07001934 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
1935 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001936 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001937 }
1938 }
1939 if (commitDragView) {
1940 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1941 solution.dragViewSpanY, occupied, true);
1942 }
1943 }
1944
Adam Cohen19f37922012-03-21 11:59:11 -07001945 // This method starts or changes the reorder hint animations
1946 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
1947 int childCount = mShortcutsAndWidgets.getChildCount();
1948 int timeForPriorAnimationToComplete = getMaxCompletionTime();
1949 for (int i = 0; i < childCount; i++) {
1950 View child = mShortcutsAndWidgets.getChildAt(i);
1951 if (child == dragView) continue;
1952 CellAndSpan c = solution.map.get(child);
1953 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1954 if (c != null) {
1955 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
1956 c.x, c.y, c.spanX, c.spanY);
1957 rha.animate(timeForPriorAnimationToComplete);
1958 }
1959 }
1960 }
1961
1962 // Class which represents the reorder hint animations. These animations show that an item is
1963 // in a temporary state, and hint at where the item will return to.
1964 class ReorderHintAnimation {
1965 View child;
1966 float deltaX;
1967 float deltaY;
1968 private static final int DURATION = 140;
1969 private int repeatCount;
1970 private boolean cancelOnCycleComplete = false;
1971 ValueAnimator va;
1972
1973 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
1974 int spanX, int spanY) {
1975 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1976 final int x0 = mTmpPoint[0];
1977 final int y0 = mTmpPoint[1];
1978 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1979 final int x1 = mTmpPoint[0];
1980 final int y1 = mTmpPoint[1];
1981 final int dX = x1 - x0;
1982 final int dY = y1 - y0;
1983 deltaX = 0;
1984 deltaY = 0;
1985 if (dX == dY && dX == 0) {
1986 } else {
1987 if (dY == 0) {
1988 deltaX = mReorderHintAnimationMagnitude;
1989 } else if (dX == 0) {
1990 deltaY = mReorderHintAnimationMagnitude;
1991 } else {
1992 double angle = Math.atan( (float) (dY) / dX);
1993 deltaX = (int) (Math.cos(angle) * mReorderHintAnimationMagnitude);
1994 deltaY = (int) (Math.sin(angle) * mReorderHintAnimationMagnitude);
1995 }
1996 }
1997 this.child = child;
1998 }
1999
2000 void animate(int delay) {
2001 if (mShakeAnimators.containsKey(child)) {
2002 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
2003 oldAnimation.completeAnimation();
2004 mShakeAnimators.remove(child);
2005 }
2006 if (deltaX == 0 && deltaY == 0) {
2007 return;
2008 }
2009 va = ValueAnimator.ofFloat(0f, 1f);
2010 va.setRepeatMode(ValueAnimator.REVERSE);
2011 va.setRepeatCount(ValueAnimator.INFINITE);
2012 va.setDuration(DURATION);
2013 va.addUpdateListener(new AnimatorUpdateListener() {
2014 @Override
2015 public void onAnimationUpdate(ValueAnimator animation) {
2016 float r = ((Float) animation.getAnimatedValue()).floatValue();
2017 float x = r * deltaX;
2018 float y = r * deltaY;
2019 child.setTranslationX(x);
2020 child.setTranslationY(y);
2021 }
2022 });
2023 va.addListener(new AnimatorListenerAdapter() {
2024 public void onAnimationRepeat(Animator animation) {
2025 repeatCount++;
2026 // We make sure to end only after a full period
2027 if (cancelOnCycleComplete && repeatCount % 2 == 0) {
2028 va.cancel();
2029 }
2030 }
2031 });
2032 va.setStartDelay(Math.max(REORDER_ANIMATION_DURATION, delay));
2033 mShakeAnimators.put(child, this);
2034 va.start();
2035 }
2036
2037
2038 private void completeAnimation() {
2039 cancelOnCycleComplete = true;
2040 }
2041
2042 // Returns the time required to complete the current oscillating animation
2043 private int completionTime() {
2044 if (repeatCount % 2 == 0) {
2045 return (int) (va.getDuration() - va.getCurrentPlayTime() + DURATION);
2046 } else {
2047 return (int) (va.getDuration() - va.getCurrentPlayTime());
2048 }
2049 }
2050 }
2051
2052 private void completeAndClearReorderHintAnimations() {
2053 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2054 a.completeAnimation();
2055 }
2056 mShakeAnimators.clear();
2057 }
2058
2059 private int getMaxCompletionTime() {
2060 int maxTime = 0;
2061 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2062 maxTime = Math.max(maxTime, a.completionTime());
2063 }
2064 return maxTime;
2065 }
2066
Adam Cohen482ed822012-03-02 14:15:13 -08002067 private void commitTempPlacement() {
2068 for (int i = 0; i < mCountX; i++) {
2069 for (int j = 0; j < mCountY; j++) {
2070 mOccupied[i][j] = mTmpOccupied[i][j];
2071 }
2072 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002073 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002074 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002075 View child = mShortcutsAndWidgets.getChildAt(i);
2076 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2077 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002078 // We do a null check here because the item info can be null in the case of the
2079 // AllApps button in the hotseat.
2080 if (info != null) {
2081 info.cellX = lp.cellX = lp.tmpCellX;
2082 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002083 info.spanX = lp.cellHSpan;
2084 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002085 }
Adam Cohen482ed822012-03-02 14:15:13 -08002086 }
Adam Cohen2acce882012-03-28 19:03:19 -07002087 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002088 }
2089
2090 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002091 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002092 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002093 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002094 lp.useTmpCoords = useTempCoords;
2095 }
2096 }
2097
Adam Cohen482ed822012-03-02 14:15:13 -08002098 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2099 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2100 int[] result = new int[2];
2101 int[] resultSpan = new int[2];
2102 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2103 resultSpan);
2104 if (result[0] >= 0 && result[1] >= 0) {
2105 copyCurrentStateToSolution(solution, false);
2106 solution.dragViewX = result[0];
2107 solution.dragViewY = result[1];
2108 solution.dragViewSpanX = resultSpan[0];
2109 solution.dragViewSpanY = resultSpan[1];
2110 solution.isSolution = true;
2111 } else {
2112 solution.isSolution = false;
2113 }
2114 return solution;
2115 }
2116
2117 public void prepareChildForDrag(View child) {
2118 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002119 }
2120
Adam Cohen19f37922012-03-21 11:59:11 -07002121 /* This seems like it should be obvious and straight-forward, but when the direction vector
2122 needs to match with the notion of the dragView pushing other views, we have to employ
2123 a slightly more subtle notion of the direction vector. The question is what two points is
2124 the vector between? The center of the dragView and its desired destination? Not quite, as
2125 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2126 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2127 or right, which helps make pushing feel right.
2128 */
2129 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2130 int spanY, View dragView, int[] resultDirection) {
2131 int[] targetDestination = new int[2];
2132
2133 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2134 Rect dragRect = new Rect();
2135 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2136 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2137
2138 Rect dropRegionRect = new Rect();
2139 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2140 dragView, dropRegionRect, mIntersectingViews);
2141
2142 int dropRegionSpanX = dropRegionRect.width();
2143 int dropRegionSpanY = dropRegionRect.height();
2144
2145 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2146 dropRegionRect.height(), dropRegionRect);
2147
2148 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2149 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2150
2151 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2152 deltaX = 0;
2153 }
2154 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2155 deltaY = 0;
2156 }
2157
2158 if (deltaX == 0 && deltaY == 0) {
2159 // No idea what to do, give a random direction.
2160 resultDirection[0] = 1;
2161 resultDirection[1] = 0;
2162 } else {
2163 computeDirectionVector(deltaX, deltaY, resultDirection);
2164 }
2165 }
2166
2167 // For a given cell and span, fetch the set of views intersecting the region.
2168 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2169 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2170 if (boundingRect != null) {
2171 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2172 }
2173 intersectingViews.clear();
2174 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2175 Rect r1 = new Rect();
2176 final int count = mShortcutsAndWidgets.getChildCount();
2177 for (int i = 0; i < count; i++) {
2178 View child = mShortcutsAndWidgets.getChildAt(i);
2179 if (child == dragView) continue;
2180 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2181 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2182 if (Rect.intersects(r0, r1)) {
2183 mIntersectingViews.add(child);
2184 if (boundingRect != null) {
2185 boundingRect.union(r1);
2186 }
2187 }
2188 }
2189 }
2190
2191 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2192 View dragView, int[] result) {
2193 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2194 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2195 mIntersectingViews);
2196 return !mIntersectingViews.isEmpty();
2197 }
2198
2199 void revertTempState() {
2200 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2201 final int count = mShortcutsAndWidgets.getChildCount();
2202 for (int i = 0; i < count; i++) {
2203 View child = mShortcutsAndWidgets.getChildAt(i);
2204 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2205 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2206 lp.tmpCellX = lp.cellX;
2207 lp.tmpCellY = lp.cellY;
2208 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2209 0, false, false);
2210 }
2211 }
2212 completeAndClearReorderHintAnimations();
2213 setItemPlacementDirty(false);
2214 }
2215
Adam Cohenbebf0422012-04-11 18:06:28 -07002216 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2217 View dragView, int[] direction, boolean commit) {
2218 int[] pixelXY = new int[2];
2219 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2220
2221 // First we determine if things have moved enough to cause a different layout
2222 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2223 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2224
2225 setUseTempCoords(true);
2226 if (swapSolution != null && swapSolution.isSolution) {
2227 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2228 // committing anything or animating anything as we just want to determine if a solution
2229 // exists
2230 copySolutionToTempState(swapSolution, dragView);
2231 setItemPlacementDirty(true);
2232 animateItemsToSolution(swapSolution, dragView, commit);
2233
2234 if (commit) {
2235 commitTempPlacement();
2236 completeAndClearReorderHintAnimations();
2237 setItemPlacementDirty(false);
2238 } else {
2239 beginOrAdjustHintAnimations(swapSolution, dragView,
2240 REORDER_ANIMATION_DURATION);
2241 }
2242 mShortcutsAndWidgets.requestLayout();
2243 }
2244 return swapSolution.isSolution;
2245 }
2246
Adam Cohen482ed822012-03-02 14:15:13 -08002247 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2248 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002249 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002250 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002251
2252 if (resultSpan == null) {
2253 resultSpan = new int[2];
2254 }
2255
Adam Cohen19f37922012-03-21 11:59:11 -07002256 // When we are checking drop validity or actually dropping, we don't recompute the
2257 // direction vector, since we want the solution to match the preview, and it's possible
2258 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002259 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2260 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002261 mDirectionVector[0] = mPreviousReorderDirection[0];
2262 mDirectionVector[1] = mPreviousReorderDirection[1];
2263 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002264 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2265 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2266 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002267 }
2268 } else {
2269 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2270 mPreviousReorderDirection[0] = mDirectionVector[0];
2271 mPreviousReorderDirection[1] = mDirectionVector[1];
2272 }
2273
Adam Cohen482ed822012-03-02 14:15:13 -08002274 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2275 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2276
2277 // We attempt the approach which doesn't shuffle views at all
2278 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2279 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2280
2281 ItemConfiguration finalSolution = null;
2282 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2283 finalSolution = swapSolution;
2284 } else if (noShuffleSolution.isSolution) {
2285 finalSolution = noShuffleSolution;
2286 }
2287
2288 boolean foundSolution = true;
2289 if (!DESTRUCTIVE_REORDER) {
2290 setUseTempCoords(true);
2291 }
2292
2293 if (finalSolution != null) {
2294 result[0] = finalSolution.dragViewX;
2295 result[1] = finalSolution.dragViewY;
2296 resultSpan[0] = finalSolution.dragViewSpanX;
2297 resultSpan[1] = finalSolution.dragViewSpanY;
2298
2299 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2300 // committing anything or animating anything as we just want to determine if a solution
2301 // exists
2302 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2303 if (!DESTRUCTIVE_REORDER) {
2304 copySolutionToTempState(finalSolution, dragView);
2305 }
2306 setItemPlacementDirty(true);
2307 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2308
Adam Cohen19f37922012-03-21 11:59:11 -07002309 if (!DESTRUCTIVE_REORDER &&
2310 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002311 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002312 completeAndClearReorderHintAnimations();
2313 setItemPlacementDirty(false);
2314 } else {
2315 beginOrAdjustHintAnimations(finalSolution, dragView,
2316 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002317 }
2318 }
2319 } else {
2320 foundSolution = false;
2321 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2322 }
2323
2324 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2325 setUseTempCoords(false);
2326 }
Adam Cohen482ed822012-03-02 14:15:13 -08002327
Michael Jurkaa52570f2012-03-20 03:18:20 -07002328 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002329 return result;
2330 }
2331
Adam Cohen19f37922012-03-21 11:59:11 -07002332 void setItemPlacementDirty(boolean dirty) {
2333 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002334 }
Adam Cohen19f37922012-03-21 11:59:11 -07002335 boolean isItemPlacementDirty() {
2336 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002337 }
2338
2339 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002340 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002341 boolean isSolution = false;
2342 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2343
2344 int area() {
2345 return dragViewSpanX * dragViewSpanY;
2346 }
Adam Cohen8baab352012-03-20 17:39:21 -07002347 }
2348
2349 private class CellAndSpan {
2350 int x, y;
2351 int spanX, spanY;
2352
2353 public CellAndSpan(int x, int y, int spanX, int spanY) {
2354 this.x = x;
2355 this.y = y;
2356 this.spanX = spanX;
2357 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002358 }
2359 }
2360
Adam Cohendf035382011-04-11 17:22:04 -07002361 /**
2362 * Find a vacant area that will fit the given bounds nearest the requested
2363 * cell location. Uses Euclidean distance to score multiple vacant areas.
2364 *
2365 * @param pixelX The X location at which you want to search for a vacant area.
2366 * @param pixelY The Y location at which you want to search for a vacant area.
2367 * @param spanX Horizontal span of the object.
2368 * @param spanY Vertical span of the object.
2369 * @param ignoreView Considers space occupied by this view as unoccupied
2370 * @param result Previously returned value to possibly recycle.
2371 * @return The X, Y cell of a vacant area that can contain this object,
2372 * nearest the requested location.
2373 */
2374 int[] findNearestVacantArea(
2375 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2376 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2377 }
2378
2379 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002380 * Find a vacant area that will fit the given bounds nearest the requested
2381 * cell location. Uses Euclidean distance to score multiple vacant areas.
2382 *
2383 * @param pixelX The X location at which you want to search for a vacant area.
2384 * @param pixelY The Y location at which you want to search for a vacant area.
2385 * @param minSpanX The minimum horizontal span required
2386 * @param minSpanY The minimum vertical span required
2387 * @param spanX Horizontal span of the object.
2388 * @param spanY Vertical span of the object.
2389 * @param ignoreView Considers space occupied by this view as unoccupied
2390 * @param result Previously returned value to possibly recycle.
2391 * @return The X, Y cell of a vacant area that can contain this object,
2392 * nearest the requested location.
2393 */
2394 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2395 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002396 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2397 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002398 }
2399
2400 /**
Adam Cohendf035382011-04-11 17:22:04 -07002401 * Find a starting cell position that will fit the given bounds nearest the requested
2402 * cell location. Uses Euclidean distance to score multiple vacant areas.
2403 *
2404 * @param pixelX The X location at which you want to search for a vacant area.
2405 * @param pixelY The Y location at which you want to search for a vacant area.
2406 * @param spanX Horizontal span of the object.
2407 * @param spanY Vertical span of the object.
2408 * @param ignoreView Considers space occupied by this view as unoccupied
2409 * @param result Previously returned value to possibly recycle.
2410 * @return The X, Y cell of a vacant area that can contain this object,
2411 * nearest the requested location.
2412 */
2413 int[] findNearestArea(
2414 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2415 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2416 }
2417
Michael Jurka0280c3b2010-09-17 15:00:07 -07002418 boolean existsEmptyCell() {
2419 return findCellForSpan(null, 1, 1);
2420 }
2421
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002423 * Finds the upper-left coordinate of the first rectangle in the grid that can
2424 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2425 * then this method will only return coordinates for rectangles that contain the cell
2426 * (intersectX, intersectY)
2427 *
2428 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2429 * can be found.
2430 * @param spanX The horizontal span of the cell we want to find.
2431 * @param spanY The vertical span of the cell we want to find.
2432 *
2433 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002434 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002435 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002436 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002437 }
2438
2439 /**
2440 * Like above, but ignores any cells occupied by the item "ignoreView"
2441 *
2442 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2443 * can be found.
2444 * @param spanX The horizontal span of the cell we want to find.
2445 * @param spanY The vertical span of the cell we want to find.
2446 * @param ignoreView The home screen item we should treat as not occupying any space
2447 * @return
2448 */
2449 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002450 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2451 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002452 }
2453
2454 /**
2455 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2456 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2457 *
2458 * @param spanX The horizontal span of the cell we want to find.
2459 * @param spanY The vertical span of the cell we want to find.
2460 * @param ignoreView The home screen item we should treat as not occupying any space
2461 * @param intersectX The X coordinate of the cell that we should try to overlap
2462 * @param intersectX The Y coordinate of the cell that we should try to overlap
2463 *
2464 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2465 */
2466 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2467 int intersectX, int intersectY) {
2468 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002469 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002470 }
2471
2472 /**
2473 * The superset of the above two methods
2474 */
2475 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002476 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002477 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002478 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002479
Michael Jurka28750fb2010-09-24 17:43:49 -07002480 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002481 while (true) {
2482 int startX = 0;
2483 if (intersectX >= 0) {
2484 startX = Math.max(startX, intersectX - (spanX - 1));
2485 }
2486 int endX = mCountX - (spanX - 1);
2487 if (intersectX >= 0) {
2488 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2489 }
2490 int startY = 0;
2491 if (intersectY >= 0) {
2492 startY = Math.max(startY, intersectY - (spanY - 1));
2493 }
2494 int endY = mCountY - (spanY - 1);
2495 if (intersectY >= 0) {
2496 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2497 }
2498
Winson Chungbbc60d82010-11-11 16:34:41 -08002499 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002500 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002501 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002502 for (int i = 0; i < spanX; i++) {
2503 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002504 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002505 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002506 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002507 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002508 continue inner;
2509 }
2510 }
2511 }
2512 if (cellXY != null) {
2513 cellXY[0] = x;
2514 cellXY[1] = y;
2515 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002516 foundCell = true;
2517 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002518 }
2519 }
2520 if (intersectX == -1 && intersectY == -1) {
2521 break;
2522 } else {
2523 // if we failed to find anything, try again but without any requirements of
2524 // intersecting
2525 intersectX = -1;
2526 intersectY = -1;
2527 continue;
2528 }
2529 }
2530
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002531 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002532 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002533 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002534 }
2535
2536 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002537 * A drag event has begun over this layout.
2538 * It may have begun over this layout (in which case onDragChild is called first),
2539 * or it may have begun on another layout.
2540 */
2541 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002542 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002543 mDragging = true;
2544 }
2545
2546 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002547 * Called when drag has left this CellLayout or has been completed (successfully or not)
2548 */
2549 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002550 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002551 // This can actually be called when we aren't in a drag, e.g. when adding a new
2552 // item to this layout via the customize drawer.
2553 // Guard against that case.
2554 if (mDragging) {
2555 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002556 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002557
2558 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002559 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002560 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2561 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002562 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002563 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002564 }
2565
2566 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002567 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002568 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002569 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002570 *
2571 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002572 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002573 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002574 if (child != null) {
2575 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002576 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002577 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 }
2580
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002582 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002583 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002584 * @param cellX X coordinate of upper left corner expressed as a cell position
2585 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002586 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002588 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002590 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591 final int cellWidth = mCellWidth;
2592 final int cellHeight = mCellHeight;
2593 final int widthGap = mWidthGap;
2594 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002595
Winson Chung4b825dcd2011-06-19 12:41:22 -07002596 final int hStartPadding = getPaddingLeft();
2597 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002599 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2600 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2601
2602 int x = hStartPadding + cellX * (cellWidth + widthGap);
2603 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002604
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002605 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002606 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002607
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002608 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002609 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002610 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002611 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002612 * @param width Width in pixels
2613 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002614 * @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 -08002615 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002616 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002617 return rectToCell(getResources(), width, height, result);
2618 }
2619
2620 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002621 // Always assume we're working with the smallest span to make sure we
2622 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002623 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2624 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002625 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002626
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002627 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002628 int spanX = (int) Math.ceil(width / (float) smallerSize);
2629 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002630
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002631 if (result == null) {
2632 return new int[] { spanX, spanY };
2633 }
2634 result[0] = spanX;
2635 result[1] = spanY;
2636 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002637 }
2638
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002639 public int[] cellSpansToSize(int hSpans, int vSpans) {
2640 int[] size = new int[2];
2641 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2642 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2643 return size;
2644 }
2645
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002646 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002647 * Calculate the grid spans needed to fit given item
2648 */
2649 public void calculateSpans(ItemInfo info) {
2650 final int minWidth;
2651 final int minHeight;
2652
2653 if (info instanceof LauncherAppWidgetInfo) {
2654 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2655 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2656 } else if (info instanceof PendingAddWidgetInfo) {
2657 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2658 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2659 } else {
2660 // It's not a widget, so it must be 1x1
2661 info.spanX = info.spanY = 1;
2662 return;
2663 }
2664 int[] spans = rectToCell(minWidth, minHeight, null);
2665 info.spanX = spans[0];
2666 info.spanY = spans[1];
2667 }
2668
2669 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002670 * Find the first vacant cell, if there is one.
2671 *
2672 * @param vacant Holds the x and y coordinate of the vacant cell
2673 * @param spanX Horizontal cell span.
2674 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002675 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002676 * @return True if a vacant cell was found
2677 */
2678 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679
Michael Jurka0280c3b2010-09-17 15:00:07 -07002680 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681 }
2682
2683 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2684 int xCount, int yCount, boolean[][] occupied) {
2685
Adam Cohen2801caf2011-05-13 20:57:39 -07002686 for (int y = 0; y < yCount; y++) {
2687 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002688 boolean available = !occupied[x][y];
2689out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2690 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2691 available = available && !occupied[i][j];
2692 if (!available) break out;
2693 }
2694 }
2695
2696 if (available) {
2697 vacant[0] = x;
2698 vacant[1] = y;
2699 return true;
2700 }
2701 }
2702 }
2703
2704 return false;
2705 }
2706
Michael Jurka0280c3b2010-09-17 15:00:07 -07002707 private void clearOccupiedCells() {
2708 for (int x = 0; x < mCountX; x++) {
2709 for (int y = 0; y < mCountY; y++) {
2710 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002711 }
2712 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002714
Adam Cohend41fbf52012-02-16 23:53:59 -08002715 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002716 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002717 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002718 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002719
Adam Cohend4844c32011-02-18 19:25:06 -08002720 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002721 markCellsAsOccupiedForView(view, mOccupied);
2722 }
2723 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002724 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002725 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002726 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002727 }
2728
Adam Cohend4844c32011-02-18 19:25:06 -08002729 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002730 markCellsAsUnoccupiedForView(view, mOccupied);
2731 }
2732 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002733 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002734 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002735 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002736 }
2737
Adam Cohen482ed822012-03-02 14:15:13 -08002738 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2739 boolean value) {
2740 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002741 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2742 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002743 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002744 }
2745 }
2746 }
2747
Adam Cohen2801caf2011-05-13 20:57:39 -07002748 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002749 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002750 (Math.max((mCountX - 1), 0) * mWidthGap);
2751 }
2752
2753 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002754 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002755 (Math.max((mCountY - 1), 0) * mHeightGap);
2756 }
2757
Michael Jurka66d72172011-04-12 16:29:25 -07002758 public boolean isOccupied(int x, int y) {
2759 if (x < mCountX && y < mCountY) {
2760 return mOccupied[x][y];
2761 } else {
2762 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2763 }
2764 }
2765
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766 @Override
2767 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2768 return new CellLayout.LayoutParams(getContext(), attrs);
2769 }
2770
2771 @Override
2772 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2773 return p instanceof CellLayout.LayoutParams;
2774 }
2775
2776 @Override
2777 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2778 return new CellLayout.LayoutParams(p);
2779 }
2780
Winson Chungaafa03c2010-06-11 17:34:16 -07002781 public static class CellLayoutAnimationController extends LayoutAnimationController {
2782 public CellLayoutAnimationController(Animation animation, float delay) {
2783 super(animation, delay);
2784 }
2785
2786 @Override
2787 protected long getDelayForView(View view) {
2788 return (int) (Math.random() * 150);
2789 }
2790 }
2791
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002792 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2793 /**
2794 * Horizontal location of the item in the grid.
2795 */
2796 @ViewDebug.ExportedProperty
2797 public int cellX;
2798
2799 /**
2800 * Vertical location of the item in the grid.
2801 */
2802 @ViewDebug.ExportedProperty
2803 public int cellY;
2804
2805 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002806 * Temporary horizontal location of the item in the grid during reorder
2807 */
2808 public int tmpCellX;
2809
2810 /**
2811 * Temporary vertical location of the item in the grid during reorder
2812 */
2813 public int tmpCellY;
2814
2815 /**
2816 * Indicates that the temporary coordinates should be used to layout the items
2817 */
2818 public boolean useTmpCoords;
2819
2820 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002821 * Number of cells spanned horizontally by the item.
2822 */
2823 @ViewDebug.ExportedProperty
2824 public int cellHSpan;
2825
2826 /**
2827 * Number of cells spanned vertically by the item.
2828 */
2829 @ViewDebug.ExportedProperty
2830 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002831
Adam Cohen1b607ed2011-03-03 17:26:50 -08002832 /**
2833 * Indicates whether the item will set its x, y, width and height parameters freely,
2834 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2835 */
Adam Cohend4844c32011-02-18 19:25:06 -08002836 public boolean isLockedToGrid = true;
2837
Adam Cohen482ed822012-03-02 14:15:13 -08002838 /**
2839 * Indicates whether this item can be reordered. Always true except in the case of the
2840 * the AllApps button.
2841 */
2842 public boolean canReorder = true;
2843
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002844 // X coordinate of the view in the layout.
2845 @ViewDebug.ExportedProperty
2846 int x;
2847 // Y coordinate of the view in the layout.
2848 @ViewDebug.ExportedProperty
2849 int y;
2850
Romain Guy84f296c2009-11-04 15:00:44 -08002851 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002852
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002853 public LayoutParams(Context c, AttributeSet attrs) {
2854 super(c, attrs);
2855 cellHSpan = 1;
2856 cellVSpan = 1;
2857 }
2858
2859 public LayoutParams(ViewGroup.LayoutParams source) {
2860 super(source);
2861 cellHSpan = 1;
2862 cellVSpan = 1;
2863 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002864
2865 public LayoutParams(LayoutParams source) {
2866 super(source);
2867 this.cellX = source.cellX;
2868 this.cellY = source.cellY;
2869 this.cellHSpan = source.cellHSpan;
2870 this.cellVSpan = source.cellVSpan;
2871 }
2872
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002873 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002874 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875 this.cellX = cellX;
2876 this.cellY = cellY;
2877 this.cellHSpan = cellHSpan;
2878 this.cellVSpan = cellVSpan;
2879 }
2880
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002881 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002882 if (isLockedToGrid) {
2883 final int myCellHSpan = cellHSpan;
2884 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002885 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2886 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002887
Adam Cohend4844c32011-02-18 19:25:06 -08002888 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2889 leftMargin - rightMargin;
2890 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2891 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002892 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2893 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002894 }
2895 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002896
Winson Chungaafa03c2010-06-11 17:34:16 -07002897 public String toString() {
2898 return "(" + this.cellX + ", " + this.cellY + ")";
2899 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002900
2901 public void setWidth(int width) {
2902 this.width = width;
2903 }
2904
2905 public int getWidth() {
2906 return width;
2907 }
2908
2909 public void setHeight(int height) {
2910 this.height = height;
2911 }
2912
2913 public int getHeight() {
2914 return height;
2915 }
2916
2917 public void setX(int x) {
2918 this.x = x;
2919 }
2920
2921 public int getX() {
2922 return x;
2923 }
2924
2925 public void setY(int y) {
2926 this.y = y;
2927 }
2928
2929 public int getY() {
2930 return y;
2931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 }
2933
Michael Jurka0280c3b2010-09-17 15:00:07 -07002934 // This class stores info for two purposes:
2935 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2936 // its spanX, spanY, and the screen it is on
2937 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2938 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2939 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002940 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002941 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002942 int cellX = -1;
2943 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 int spanX;
2945 int spanY;
2946 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002947 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 @Override
2950 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002951 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2952 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002953 }
2954 }
Michael Jurkad771c962011-08-09 15:00:48 -07002955
2956 public boolean lastDownOnOccupiedCell() {
2957 return mLastDownOnOccupiedCell;
2958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959}