blob: 5591c9acc0d897050075cd97d273698a619079d7 [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;
Brandon Keely50e6e562012-05-08 16:28:49 -070020import android.animation.AnimatorSet;
21import android.animation.ObjectAnimator;
Joe Onorato4be866d2010-10-10 11:26:02 -070022import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070023import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070024import android.animation.ValueAnimator;
25import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040027import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070029import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080031import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070033import android.graphics.Point;
34import android.graphics.PointF;
Adam Cohenb5ba0972011-09-07 18:02:31 -070035import android.graphics.PorterDuff;
36import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080038import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070039import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070040import android.graphics.drawable.NinePatchDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070042import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Adam Cohen66396872011-04-15 17:50:36 -070051import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070052import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070053
Adam Cohen69ce2e52011-07-03 19:25:21 -070054import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070056import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080057import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070058
Michael Jurkabdb5c532011-02-01 15:05:06 -080059public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070060 static final String TAG = "CellLayout";
61
Adam Cohen2acce882012-03-28 19:03:19 -070062 private Launcher mLauncher;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 private int mCellWidth;
64 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070065
Adam Cohend22015c2010-07-26 22:02:18 -070066 private int mCountX;
67 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
Adam Cohen234c4cd2011-07-17 21:03:04 -070069 private int mOriginalWidthGap;
70 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 private int mWidthGap;
72 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070073 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080074 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
76 private final Rect mRect = new Rect();
77 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070078
Patrick Dubroyde7658b2010-09-27 11:15:43 -070079 // These are temporary variables to prevent having to allocate a new object just to
80 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070081 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070082 private final int[] mTmpPoint = new int[2];
Adam Cohen69ce2e52011-07-03 19:25:21 -070083 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070084
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080086 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070087 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Michael Jurkadee05892010-07-27 10:01:56 -070089 private OnTouchListener mInterceptTouchListener;
90
Adam Cohen69ce2e52011-07-03 19:25:21 -070091 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070092 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070093
Adam Cohenb5ba0972011-09-07 18:02:31 -070094 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070095 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070096 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070097
Michael Jurka33945b22010-12-21 18:19:38 -080098 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080099 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700100 private Drawable mOverScrollForegroundDrawable;
101 private Drawable mOverScrollLeft;
102 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700103 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700104 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700105 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700106
Michael Jurka33945b22010-12-21 18:19:38 -0800107 // If we're actively dragging something over this screen, mIsDragOverlapping is true
108 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700109 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700110
Winson Chung150fbab2010-09-29 17:14:26 -0700111 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700112 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800113 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700114 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700115 private InterruptibleInOutAnimator[] mDragOutlineAnims =
116 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700117
118 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700119 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700120 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700121
Patrick Dubroy96864c32011-03-10 17:17:23 -0800122 private BubbleTextView mPressedOrFocusedIcon;
123
Adam Cohen482ed822012-03-02 14:15:13 -0800124 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
125 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700126 private HashMap<View, ReorderHintAnimation>
127 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
128
129 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700130
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700131 // When a drag operation is in progress, holds the nearest cell to the touch point
132 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Joe Onorato4be866d2010-10-10 11:26:02 -0700134 private boolean mDragging = false;
135
Patrick Dubroyce34a972010-10-19 10:34:32 -0700136 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700137 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700138
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800139 private boolean mIsHotseat = false;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800140
Adam Cohen482ed822012-03-02 14:15:13 -0800141 public static final int MODE_DRAG_OVER = 0;
142 public static final int MODE_ON_DROP = 1;
143 public static final int MODE_ON_DROP_EXTERNAL = 2;
144 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700145 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800146 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
147
Adam Cohena897f392012-04-27 18:12:05 -0700148 static final int LANDSCAPE = 0;
149 static final int PORTRAIT = 1;
150
Adam Cohen7bdfc972012-05-22 16:50:35 -0700151 private static final float REORDER_HINT_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700152 private static final int REORDER_ANIMATION_DURATION = 150;
153 private float mReorderHintAnimationMagnitude;
154
Adam Cohen482ed822012-03-02 14:15:13 -0800155 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
156 private Rect mOccupiedRect = new Rect();
157 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700158 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700159 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700160 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800161
Romain Guy8a0bff52012-05-06 13:14:33 -0700162 private final static PorterDuffXfermode sAddBlendMode =
163 new PorterDuffXfermode(PorterDuff.Mode.ADD);
164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 public CellLayout(Context context) {
166 this(context, null);
167 }
168
169 public CellLayout(Context context, AttributeSet attrs) {
170 this(context, attrs, 0);
171 }
172
173 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
174 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700175 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700176
177 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
178 // the user where a dragged item will land when dropped.
179 setWillNotDraw(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700180 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
183
Adam Cohenf4bd5792012-04-27 11:35:29 -0700184 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
185 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700186 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
187 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700188 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700189 mCountX = LauncherModel.getCellCountX();
190 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700191 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800192 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700193 mPreviousReorderDirection[0] = INVALID_DIRECTION;
194 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
196 a.recycle();
197
198 setAlwaysDrawnWithCacheEnabled(false);
199
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700200 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700201
Winson Chung967289b2011-06-30 18:09:30 -0700202 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700203 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800204
Adam Cohenb5ba0972011-09-07 18:02:31 -0700205 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
206 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
207 mForegroundPadding =
208 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800209
Adam Cohen19f37922012-03-21 11:59:11 -0700210 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
211 res.getDimensionPixelSize(R.dimen.app_icon_size));
212
Winson Chungb26f3d62011-06-02 10:49:29 -0700213 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700214 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700215
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700216 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700217
Patrick Dubroyce34a972010-10-19 10:34:32 -0700218 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700219
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700220
Winson Chungb8c69f32011-10-19 21:36:08 -0700221 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700222 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800223 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700224 }
225
226 // When dragging things around the home screens, we show a green outline of
227 // where the item will land. The outlines gradually fade out, leaving a trail
228 // behind the drag path.
229 // Set up all the animations that are used to implement this fading.
230 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700231 final float fromAlphaValue = 0;
232 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700233
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700234 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700235
236 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700237 final InterruptibleInOutAnimator anim =
238 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700239 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700240 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700241 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700242 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700243 final Bitmap outline = (Bitmap)anim.getTag();
244
245 // If an animation is started and then stopped very quickly, we can still
246 // get spurious updates we've cleared the tag. Guard against this.
247 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700248 @SuppressWarnings("all") // suppress dead code warning
249 final boolean debug = false;
250 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700251 Object val = animation.getAnimatedValue();
252 Log.d(TAG, "anim " + thisIndex + " update: " + val +
253 ", isStopped " + anim.isStopped());
254 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700255 // Try to prevent it from continuing to run
256 animation.cancel();
257 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700258 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800259 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700260 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700261 }
262 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700263 // The animation holds a reference to the drag outline bitmap as long is it's
264 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700265 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700266 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700267 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700268 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700269 anim.setTag(null);
270 }
271 }
272 });
273 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700274 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700275
Michael Jurka18014792010-10-14 09:01:34 -0700276 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700277 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800278
Michael Jurkaa52570f2012-03-20 03:18:20 -0700279 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
280 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
281 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700282 }
283
Michael Jurkaf6440da2011-04-05 14:50:34 -0700284 static int widthInPortrait(Resources r, int numCells) {
285 // We use this method from Workspace to figure out how many rows/columns Launcher should
286 // have. We ignore the left/right padding on CellLayout because it turns out in our design
287 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700288 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700289 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
290 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700291
Winson Chung4b825dcd2011-06-19 12:41:22 -0700292 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700293 }
294
Michael Jurkaf6440da2011-04-05 14:50:34 -0700295 static int heightInLandscape(Resources r, int numCells) {
296 // We use this method from Workspace to figure out how many rows/columns Launcher should
297 // have. We ignore the left/right padding on CellLayout because it turns out in our design
298 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700299 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700300 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
301 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700302
Winson Chung4b825dcd2011-06-19 12:41:22 -0700303 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700304 }
305
Adam Cohen2801caf2011-05-13 20:57:39 -0700306 public void enableHardwareLayers() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700307 mShortcutsAndWidgets.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700308 }
309
310 public void setGridSize(int x, int y) {
311 mCountX = x;
312 mCountY = y;
313 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800314 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700315 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700316 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700317 }
318
Patrick Dubroy96864c32011-03-10 17:17:23 -0800319 private void invalidateBubbleTextView(BubbleTextView icon) {
320 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700321 invalidate(icon.getLeft() + getPaddingLeft() - padding,
322 icon.getTop() + getPaddingTop() - padding,
323 icon.getRight() + getPaddingLeft() + padding,
324 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800325 }
326
Adam Cohenb5ba0972011-09-07 18:02:31 -0700327 void setOverScrollAmount(float r, boolean left) {
328 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
329 mOverScrollForegroundDrawable = mOverScrollLeft;
330 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
331 mOverScrollForegroundDrawable = mOverScrollRight;
332 }
333
334 mForegroundAlpha = (int) Math.round((r * 255));
335 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
336 invalidate();
337 }
338
Patrick Dubroy96864c32011-03-10 17:17:23 -0800339 void setPressedOrFocusedIcon(BubbleTextView icon) {
340 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
341 // requires an expanded clip rect (due to the glow's blur radius)
342 BubbleTextView oldIcon = mPressedOrFocusedIcon;
343 mPressedOrFocusedIcon = icon;
344 if (oldIcon != null) {
345 invalidateBubbleTextView(oldIcon);
346 }
347 if (mPressedOrFocusedIcon != null) {
348 invalidateBubbleTextView(mPressedOrFocusedIcon);
349 }
350 }
351
Michael Jurka33945b22010-12-21 18:19:38 -0800352 void setIsDragOverlapping(boolean isDragOverlapping) {
353 if (mIsDragOverlapping != isDragOverlapping) {
354 mIsDragOverlapping = isDragOverlapping;
355 invalidate();
356 }
357 }
358
359 boolean getIsDragOverlapping() {
360 return mIsDragOverlapping;
361 }
362
Adam Cohenebea84d2011-11-09 17:20:41 -0800363 protected void setOverscrollTransformsDirty(boolean dirty) {
364 mScrollingTransformsDirty = dirty;
365 }
366
367 protected void resetOverscrollTransforms() {
368 if (mScrollingTransformsDirty) {
369 setOverscrollTransformsDirty(false);
370 setTranslationX(0);
371 setRotationY(0);
372 // It doesn't matter if we pass true or false here, the important thing is that we
373 // pass 0, which results in the overscroll drawable not being drawn any more.
374 setOverScrollAmount(0, false);
375 setPivotX(getMeasuredWidth() / 2);
376 setPivotY(getMeasuredHeight() / 2);
377 }
378 }
379
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700380 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700381 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700382 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
383 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
384 // When we're small, we are either drawn normally or in the "accepts drops" state (during
385 // a drag). However, we also drag the mini hover background *over* one of those two
386 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700387 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700388 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800389
390 if (mIsDragOverlapping) {
391 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700392 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700393 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700394 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700395 }
Michael Jurka33945b22010-12-21 18:19:38 -0800396
397 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
398 bg.setBounds(mBackgroundRect);
399 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700400 }
Romain Guya6abce82009-11-10 02:54:41 -0800401
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700402 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700403 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700404 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700405 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800406 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700407 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700408 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800409 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700410 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700411 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800412
413 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
414 // requires an expanded clip rect (due to the glow's blur radius)
415 if (mPressedOrFocusedIcon != null) {
416 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
417 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
418 if (b != null) {
419 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700420 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
421 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800422 null);
423 }
424 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700425
Adam Cohen482ed822012-03-02 14:15:13 -0800426 if (DEBUG_VISUALIZE_OCCUPIED) {
427 int[] pt = new int[2];
428 ColorDrawable cd = new ColorDrawable(Color.RED);
429 cd.setBounds(0, 0, 80, 80);
430 for (int i = 0; i < mCountX; i++) {
431 for (int j = 0; j < mCountY; j++) {
432 if (mOccupied[i][j]) {
433 cellToPoint(i, j, pt);
434 canvas.save();
435 canvas.translate(pt[0], pt[1]);
436 cd.draw(canvas);
437 canvas.restore();
438 }
439 }
440 }
441 }
442
Andrew Flynn850d2e72012-04-26 16:51:20 -0700443 int previewOffset = FolderRingAnimator.sPreviewSize;
444
Adam Cohen69ce2e52011-07-03 19:25:21 -0700445 // The folder outer / inner ring image(s)
446 for (int i = 0; i < mFolderOuterRings.size(); i++) {
447 FolderRingAnimator fra = mFolderOuterRings.get(i);
448
449 // Draw outer ring
450 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
451 int width = (int) fra.getOuterRingSize();
452 int height = width;
453 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
454
455 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700456 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700457
458 canvas.save();
459 canvas.translate(centerX - width / 2, centerY - height / 2);
460 d.setBounds(0, 0, width, height);
461 d.draw(canvas);
462 canvas.restore();
463
464 // Draw inner ring
465 d = FolderRingAnimator.sSharedInnerRingDrawable;
466 width = (int) fra.getInnerRingSize();
467 height = width;
468 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
469
470 centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700471 centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700472 canvas.save();
473 canvas.translate(centerX - width / 2, centerY - width / 2);
474 d.setBounds(0, 0, width, height);
475 d.draw(canvas);
476 canvas.restore();
477 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700478
479 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
480 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
481 int width = d.getIntrinsicWidth();
482 int height = d.getIntrinsicHeight();
483
484 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
485 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700486 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohenc51934b2011-07-26 21:07:43 -0700487
488 canvas.save();
489 canvas.translate(centerX - width / 2, centerY - width / 2);
490 d.setBounds(0, 0, width, height);
491 d.draw(canvas);
492 canvas.restore();
493 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700494 }
495
Adam Cohenb5ba0972011-09-07 18:02:31 -0700496 @Override
497 protected void dispatchDraw(Canvas canvas) {
498 super.dispatchDraw(canvas);
499 if (mForegroundAlpha > 0) {
500 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
501 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700502 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700503 mOverScrollForegroundDrawable.draw(canvas);
504 p.setXfermode(null);
505 }
506 }
507
Adam Cohen69ce2e52011-07-03 19:25:21 -0700508 public void showFolderAccept(FolderRingAnimator fra) {
509 mFolderOuterRings.add(fra);
510 }
511
512 public void hideFolderAccept(FolderRingAnimator fra) {
513 if (mFolderOuterRings.contains(fra)) {
514 mFolderOuterRings.remove(fra);
515 }
516 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700517 }
518
Adam Cohenc51934b2011-07-26 21:07:43 -0700519 public void setFolderLeaveBehindCell(int x, int y) {
520 mFolderLeaveBehindCell[0] = x;
521 mFolderLeaveBehindCell[1] = y;
522 invalidate();
523 }
524
525 public void clearFolderLeaveBehind() {
526 mFolderLeaveBehindCell[0] = -1;
527 mFolderLeaveBehindCell[1] = -1;
528 invalidate();
529 }
530
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700531 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700532 public boolean shouldDelayChildPressedState() {
533 return false;
534 }
535
536 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700537 public void cancelLongPress() {
538 super.cancelLongPress();
539
540 // Cancel long press for all children
541 final int count = getChildCount();
542 for (int i = 0; i < count; i++) {
543 final View child = getChildAt(i);
544 child.cancelLongPress();
545 }
546 }
547
Michael Jurkadee05892010-07-27 10:01:56 -0700548 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
549 mInterceptTouchListener = listener;
550 }
551
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700553 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 }
555
556 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700557 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 }
559
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800560 public void setIsHotseat(boolean isHotseat) {
561 mIsHotseat = isHotseat;
562 }
563
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800564 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700565 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700566 final LayoutParams lp = params;
567
Andrew Flynnde38e422012-05-08 11:22:15 -0700568 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800569 if (child instanceof BubbleTextView) {
570 BubbleTextView bubbleChild = (BubbleTextView) child;
571
Andrew Flynnde38e422012-05-08 11:22:15 -0700572 Resources res = getResources();
573 if (mIsHotseat) {
574 bubbleChild.setTextColor(res.getColor(android.R.color.transparent));
575 } else {
576 bubbleChild.setTextColor(res.getColor(R.color.workspace_icon_text_color));
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800577 }
578 }
579
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 // Generate an id for each view, this assumes we have at most 256x256 cells
581 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700582 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700583 // If the horizontal or vertical span is set to -1, it is taken to
584 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700585 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
586 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587
Winson Chungaafa03c2010-06-11 17:34:16 -0700588 child.setId(childId);
589
Michael Jurkaa52570f2012-03-20 03:18:20 -0700590 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700591
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700592 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700593
Winson Chungaafa03c2010-06-11 17:34:16 -0700594 return true;
595 }
596 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700598
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700600 public void removeAllViews() {
601 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700602 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700603 }
604
605 @Override
606 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700607 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700608 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700609 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700610 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700611 }
612
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700613 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700614 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700615 }
616
Michael Jurka0280c3b2010-09-17 15:00:07 -0700617 @Override
618 public void removeView(View view) {
619 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700620 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700621 }
622
623 @Override
624 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700625 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
626 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700627 }
628
629 @Override
630 public void removeViewInLayout(View view) {
631 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700632 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700633 }
634
635 @Override
636 public void removeViews(int start, int count) {
637 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700638 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700639 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700640 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700641 }
642
643 @Override
644 public void removeViewsInLayout(int start, int count) {
645 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700646 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700647 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700648 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800649 }
650
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 @Override
652 protected void onAttachedToWindow() {
653 super.onAttachedToWindow();
654 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
655 }
656
Michael Jurkaaf442092010-06-10 17:01:57 -0700657 public void setTagToCellInfoForPoint(int touchX, int touchY) {
658 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800659 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700660 final int x = touchX + getScrollX();
661 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700662 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700663
664 boolean found = false;
665 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700666 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800667 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700668
Adam Cohen1b607ed2011-03-03 17:26:50 -0800669 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
670 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700671 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700672
Winson Chungeecf02d2012-03-02 17:14:58 -0800673 float scale = child.getScaleX();
674 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
675 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700676 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
677 // offset that by this CellLayout's padding to test an (x,y) point that is relative
678 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700679 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800680 frame.inset((int) (frame.width() * (1f - scale) / 2),
681 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700682
Michael Jurkaaf442092010-06-10 17:01:57 -0700683 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700684 cellInfo.cell = child;
685 cellInfo.cellX = lp.cellX;
686 cellInfo.cellY = lp.cellY;
687 cellInfo.spanX = lp.cellHSpan;
688 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700689 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700690 break;
691 }
692 }
693 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700694
Michael Jurkad771c962011-08-09 15:00:48 -0700695 mLastDownOnOccupiedCell = found;
696
Michael Jurkaaf442092010-06-10 17:01:57 -0700697 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700698 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700699 pointToCellExact(x, y, cellXY);
700
Michael Jurkaaf442092010-06-10 17:01:57 -0700701 cellInfo.cell = null;
702 cellInfo.cellX = cellXY[0];
703 cellInfo.cellY = cellXY[1];
704 cellInfo.spanX = 1;
705 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700706 }
707 setTag(cellInfo);
708 }
709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 @Override
711 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700712 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
713 // even in the case where we return early. Not clearing here was causing bugs whereby on
714 // long-press we'd end up picking up an item from a previous drag operation.
715 final int action = ev.getAction();
716
717 if (action == MotionEvent.ACTION_DOWN) {
718 clearTagCellInfo();
719 }
720
Michael Jurkadee05892010-07-27 10:01:56 -0700721 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
722 return true;
723 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724
725 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700726 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800728
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 return false;
730 }
731
Adam Cohenc1997fd2011-08-15 18:26:39 -0700732 private void clearTagCellInfo() {
733 final CellInfo cellInfo = mCellInfo;
734 cellInfo.cell = null;
735 cellInfo.cellX = -1;
736 cellInfo.cellY = -1;
737 cellInfo.spanX = 0;
738 cellInfo.spanY = 0;
739 setTag(cellInfo);
740 }
741
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700743 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
745
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700746 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700747 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 * @param x X coordinate of the point
749 * @param y Y coordinate of the point
750 * @param result Array of 2 ints to hold the x and y coordinate of the cell
751 */
752 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700753 final int hStartPadding = getPaddingLeft();
754 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755
756 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
757 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
758
Adam Cohend22015c2010-07-26 22:02:18 -0700759 final int xAxis = mCountX;
760 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761
762 if (result[0] < 0) result[0] = 0;
763 if (result[0] >= xAxis) result[0] = xAxis - 1;
764 if (result[1] < 0) result[1] = 0;
765 if (result[1] >= yAxis) result[1] = yAxis - 1;
766 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700767
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 /**
769 * Given a point, return the cell that most closely encloses that point
770 * @param x X coordinate of the point
771 * @param y Y coordinate of the point
772 * @param result Array of 2 ints to hold the x and y coordinate of the cell
773 */
774 void pointToCellRounded(int x, int y, int[] result) {
775 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
776 }
777
778 /**
779 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700780 *
781 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700783 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 * @param result Array of 2 ints to hold the x and y coordinate of the point
785 */
786 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700787 final int hStartPadding = getPaddingLeft();
788 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789
790 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
791 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
792 }
793
Adam Cohene3e27a82011-04-15 12:07:39 -0700794 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800795 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700796 *
797 * @param cellX X coordinate of the cell
798 * @param cellY Y coordinate of the cell
799 *
800 * @param result Array of 2 ints to hold the x and y coordinate of the point
801 */
802 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700803 regionToCenterPoint(cellX, cellY, 1, 1, result);
804 }
805
806 /**
807 * Given a cell coordinate and span return the point that represents the center of the regio
808 *
809 * @param cellX X coordinate of the cell
810 * @param cellY Y coordinate of the cell
811 *
812 * @param result Array of 2 ints to hold the x and y coordinate of the point
813 */
814 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700815 final int hStartPadding = getPaddingLeft();
816 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700817 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
818 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
819 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
820 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700821 }
822
Adam Cohen19f37922012-03-21 11:59:11 -0700823 /**
824 * Given a cell coordinate and span fills out a corresponding pixel rect
825 *
826 * @param cellX X coordinate of the cell
827 * @param cellY Y coordinate of the cell
828 * @param result Rect in which to write the result
829 */
830 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
831 final int hStartPadding = getPaddingLeft();
832 final int vStartPadding = getPaddingTop();
833 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
834 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
835 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
836 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
837 }
838
Adam Cohen482ed822012-03-02 14:15:13 -0800839 public float getDistanceFromCell(float x, float y, int[] cell) {
840 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
841 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
842 Math.pow(y - mTmpPoint[1], 2));
843 return distance;
844 }
845
Romain Guy84f296c2009-11-04 15:00:44 -0800846 int getCellWidth() {
847 return mCellWidth;
848 }
849
850 int getCellHeight() {
851 return mCellHeight;
852 }
853
Adam Cohend4844c32011-02-18 19:25:06 -0800854 int getWidthGap() {
855 return mWidthGap;
856 }
857
858 int getHeightGap() {
859 return mHeightGap;
860 }
861
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700862 Rect getContentRect(Rect r) {
863 if (r == null) {
864 r = new Rect();
865 }
866 int left = getPaddingLeft();
867 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700868 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
869 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700870 r.set(left, top, right, bottom);
871 return r;
872 }
873
Adam Cohena897f392012-04-27 18:12:05 -0700874 static void getMetrics(Rect metrics, Resources res, int measureWidth, int measureHeight,
875 int countX, int countY, int orientation) {
876 int numWidthGaps = countX - 1;
877 int numHeightGaps = countY - 1;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700878
879 int widthGap;
880 int heightGap;
881 int cellWidth;
882 int cellHeight;
883 int paddingLeft;
884 int paddingRight;
885 int paddingTop;
886 int paddingBottom;
887
Adam Cohena897f392012-04-27 18:12:05 -0700888 int maxGap = res.getDimensionPixelSize(R.dimen.workspace_max_gap);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700889 if (orientation == LANDSCAPE) {
890 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_land);
891 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_land);
892 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_land);
893 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
894 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_land);
895 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_land);
896 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_land);
897 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_land);
898 } else {
899 // PORTRAIT
900 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_port);
901 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_port);
902 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
903 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_port);
904 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_port);
905 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_port);
906 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_port);
907 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_port);
908 }
909
910 if (widthGap < 0 || heightGap < 0) {
911 int hSpace = measureWidth - paddingLeft - paddingRight;
912 int vSpace = measureHeight - paddingTop - paddingBottom;
Adam Cohena897f392012-04-27 18:12:05 -0700913 int hFreeSpace = hSpace - (countX * cellWidth);
914 int vFreeSpace = vSpace - (countY * cellHeight);
915 widthGap = Math.min(maxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
916 heightGap = Math.min(maxGap, numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700917 }
918 metrics.set(cellWidth, cellHeight, widthGap, heightGap);
919 }
920
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 @Override
922 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700924 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
925
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
927 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700928
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
930 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
931 }
932
Adam Cohend22015c2010-07-26 22:02:18 -0700933 int numWidthGaps = mCountX - 1;
934 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935
Adam Cohen234c4cd2011-07-17 21:03:04 -0700936 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Michael Jurkadd13e3d2012-05-01 12:38:17 -0700937 int hSpace = widthSpecSize - getPaddingLeft() - getPaddingRight();
938 int vSpace = heightSpecSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700939 int hFreeSpace = hSpace - (mCountX * mCellWidth);
940 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700941 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
942 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700943 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700944 } else {
945 mWidthGap = mOriginalWidthGap;
946 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700947 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700948
Michael Jurka8c920dd2011-01-20 14:16:56 -0800949 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
950 int newWidth = widthSpecSize;
951 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700952 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700953 newWidth = getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700954 ((mCountX - 1) * mWidthGap);
Michael Jurka8b805b12012-04-18 14:23:14 -0700955 newHeight = getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700956 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700957 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700958 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800959
960 int count = getChildCount();
961 for (int i = 0; i < count; i++) {
962 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700963 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
964 getPaddingRight(), MeasureSpec.EXACTLY);
965 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
966 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800967 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
968 }
969 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 }
971
972 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700973 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800976 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700977 child.layout(getPaddingLeft(), getPaddingTop(),
978 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 }
980 }
981
982 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700983 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
984 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700985 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700986 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
987 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -0700988 }
989
990 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700992 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
994
995 @Override
996 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700997 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 }
999
Michael Jurka5f1c5092010-09-03 14:15:02 -07001000 public float getBackgroundAlpha() {
1001 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001002 }
1003
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001004 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001005 if (mBackgroundAlphaMultiplier != multiplier) {
1006 mBackgroundAlphaMultiplier = multiplier;
1007 invalidate();
1008 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001009 }
1010
Adam Cohenddb82192010-11-10 16:32:54 -08001011 public float getBackgroundAlphaMultiplier() {
1012 return mBackgroundAlphaMultiplier;
1013 }
1014
Michael Jurka5f1c5092010-09-03 14:15:02 -07001015 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001016 if (mBackgroundAlpha != alpha) {
1017 mBackgroundAlpha = alpha;
1018 invalidate();
1019 }
Michael Jurkadee05892010-07-27 10:01:56 -07001020 }
1021
Michael Jurkaa52570f2012-03-20 03:18:20 -07001022 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001023 final int childCount = getChildCount();
1024 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001025 getChildAt(i).setAlpha(alpha);
1026 }
1027 }
1028
Michael Jurkaa52570f2012-03-20 03:18:20 -07001029 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1030 if (getChildCount() > 0) {
1031 return (ShortcutAndWidgetContainer) getChildAt(0);
1032 }
1033 return null;
1034 }
1035
Patrick Dubroy440c3602010-07-13 17:50:32 -07001036 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001037 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001038 }
1039
Adam Cohen76fc0852011-06-17 13:26:23 -07001040 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001041 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001042 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001043 boolean[][] occupied = mOccupied;
1044 if (!permanent) {
1045 occupied = mTmpOccupied;
1046 }
1047
Adam Cohen19f37922012-03-21 11:59:11 -07001048 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001049 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1050 final ItemInfo info = (ItemInfo) child.getTag();
1051
1052 // We cancel any existing animations
1053 if (mReorderAnimators.containsKey(lp)) {
1054 mReorderAnimators.get(lp).cancel();
1055 mReorderAnimators.remove(lp);
1056 }
1057
Adam Cohen482ed822012-03-02 14:15:13 -08001058 final int oldX = lp.x;
1059 final int oldY = lp.y;
1060 if (adjustOccupied) {
1061 occupied[lp.cellX][lp.cellY] = false;
1062 occupied[cellX][cellY] = true;
1063 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001064 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001065 if (permanent) {
1066 lp.cellX = info.cellX = cellX;
1067 lp.cellY = info.cellY = cellY;
1068 } else {
1069 lp.tmpCellX = cellX;
1070 lp.tmpCellY = cellY;
1071 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001072 clc.setupLp(lp);
1073 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001074 final int newX = lp.x;
1075 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001076
Adam Cohen76fc0852011-06-17 13:26:23 -07001077 lp.x = oldX;
1078 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001079
Adam Cohen482ed822012-03-02 14:15:13 -08001080 // Exit early if we're not actually moving the view
1081 if (oldX == newX && oldY == newY) {
1082 lp.isLockedToGrid = true;
1083 return true;
1084 }
1085
1086 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
1087 va.setDuration(duration);
1088 mReorderAnimators.put(lp, va);
1089
1090 va.addUpdateListener(new AnimatorUpdateListener() {
1091 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001092 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001093 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001094 lp.x = (int) ((1 - r) * oldX + r * newX);
1095 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001096 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001097 }
1098 });
Adam Cohen482ed822012-03-02 14:15:13 -08001099 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001100 boolean cancelled = false;
1101 public void onAnimationEnd(Animator animation) {
1102 // If the animation was cancelled, it means that another animation
1103 // has interrupted this one, and we don't want to lock the item into
1104 // place just yet.
1105 if (!cancelled) {
1106 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001107 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001108 }
1109 if (mReorderAnimators.containsKey(lp)) {
1110 mReorderAnimators.remove(lp);
1111 }
1112 }
1113 public void onAnimationCancel(Animator animation) {
1114 cancelled = true;
1115 }
1116 });
Adam Cohen482ed822012-03-02 14:15:13 -08001117 va.setStartDelay(delay);
1118 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001119 return true;
1120 }
1121 return false;
1122 }
1123
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001124 /**
1125 * Estimate where the top left cell of the dragged item will land if it is dropped.
1126 *
1127 * @param originX The X value of the top left corner of the item
1128 * @param originY The Y value of the top left corner of the item
1129 * @param spanX The number of horizontal cells that the item spans
1130 * @param spanY The number of vertical cells that the item spans
1131 * @param result The estimated drop cell X and Y.
1132 */
1133 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001134 final int countX = mCountX;
1135 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001136
Michael Jurkaa63c4522010-08-19 13:52:27 -07001137 // pointToCellRounded takes the top left of a cell but will pad that with
1138 // cellWidth/2 and cellHeight/2 when finding the matching cell
1139 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001140
1141 // If the item isn't fully on this screen, snap to the edges
1142 int rightOverhang = result[0] + spanX - countX;
1143 if (rightOverhang > 0) {
1144 result[0] -= rightOverhang; // Snap to right
1145 }
1146 result[0] = Math.max(0, result[0]); // Snap to left
1147 int bottomOverhang = result[1] + spanY - countY;
1148 if (bottomOverhang > 0) {
1149 result[1] -= bottomOverhang; // Snap to bottom
1150 }
1151 result[1] = Math.max(0, result[1]); // Snap to top
1152 }
1153
Adam Cohen482ed822012-03-02 14:15:13 -08001154 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1155 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001156 final int oldDragCellX = mDragCell[0];
1157 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001158
Winson Chungb8c69f32011-10-19 21:36:08 -07001159 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001160 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1161 } else {
1162 mDragCenter.set(originX, originY);
1163 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001164
Adam Cohen2801caf2011-05-13 20:57:39 -07001165 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001166 return;
1167 }
1168
Adam Cohen482ed822012-03-02 14:15:13 -08001169 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1170 mDragCell[0] = cellX;
1171 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001172 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001173 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001174 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001175
Joe Onorato4be866d2010-10-10 11:26:02 -07001176 int left = topLeft[0];
1177 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001178
Winson Chungb8c69f32011-10-19 21:36:08 -07001179 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001180 // When drawing the drag outline, it did not account for margin offsets
1181 // added by the view's parent.
1182 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1183 left += lp.leftMargin;
1184 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001185
Adam Cohen99e8b402011-03-25 19:23:43 -07001186 // Offsets due to the size difference between the View and the dragOutline.
1187 // There is a size difference to account for the outer blur, which may lie
1188 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001189 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001190 // We center about the x axis
1191 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1192 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001193 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001194 if (dragOffset != null && dragRegion != null) {
1195 // Center the drag region *horizontally* in the cell and apply a drag
1196 // outline offset
1197 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1198 - dragRegion.width()) / 2;
1199 top += dragOffset.y;
1200 } else {
1201 // Center the drag outline in the cell
1202 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1203 - dragOutline.getWidth()) / 2;
1204 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1205 - dragOutline.getHeight()) / 2;
1206 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001207 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001208 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001209 mDragOutlineAnims[oldIndex].animateOut();
1210 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001211 Rect r = mDragOutlines[mDragOutlineCurrent];
1212 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1213 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001214 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001215 }
Winson Chung150fbab2010-09-29 17:14:26 -07001216
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001217 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1218 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001219 }
1220 }
1221
Adam Cohene0310962011-04-18 16:15:31 -07001222 public void clearDragOutlines() {
1223 final int oldIndex = mDragOutlineCurrent;
1224 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001225 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001226 }
1227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001229 * Find a vacant area that will fit the given bounds nearest the requested
1230 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001231 *
Romain Guy51afc022009-05-04 18:03:43 -07001232 * @param pixelX The X location at which you want to search for a vacant area.
1233 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001234 * @param spanX Horizontal span of the object.
1235 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001236 * @param result Array in which to place the result, or null (in which case a new array will
1237 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001238 * @return The X, Y cell of a vacant area that can contain this object,
1239 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001241 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1242 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001243 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001244 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001245
Michael Jurka6a1435d2010-09-27 17:35:12 -07001246 /**
1247 * Find a vacant area that will fit the given bounds nearest the requested
1248 * cell location. Uses Euclidean distance to score multiple vacant areas.
1249 *
1250 * @param pixelX The X location at which you want to search for a vacant area.
1251 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001252 * @param minSpanX The minimum horizontal span required
1253 * @param minSpanY The minimum vertical span required
1254 * @param spanX Horizontal span of the object.
1255 * @param spanY Vertical span of the object.
1256 * @param result Array in which to place the result, or null (in which case a new array will
1257 * be allocated)
1258 * @return The X, Y cell of a vacant area that can contain this object,
1259 * nearest the requested location.
1260 */
1261 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1262 int spanY, int[] result, int[] resultSpan) {
1263 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1264 result, resultSpan);
1265 }
1266
1267 /**
1268 * Find a vacant area that will fit the given bounds nearest the requested
1269 * cell location. Uses Euclidean distance to score multiple vacant areas.
1270 *
1271 * @param pixelX The X location at which you want to search for a vacant area.
1272 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001273 * @param spanX Horizontal span of the object.
1274 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001275 * @param ignoreOccupied If true, the result can be an occupied cell
1276 * @param result Array in which to place the result, or null (in which case a new array will
1277 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001278 * @return The X, Y cell of a vacant area that can contain this object,
1279 * nearest the requested location.
1280 */
Adam Cohendf035382011-04-11 17:22:04 -07001281 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1282 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001283 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001284 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001285 }
1286
1287 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1288 private void lazyInitTempRectStack() {
1289 if (mTempRectStack.isEmpty()) {
1290 for (int i = 0; i < mCountX * mCountY; i++) {
1291 mTempRectStack.push(new Rect());
1292 }
1293 }
1294 }
Adam Cohen482ed822012-03-02 14:15:13 -08001295
Adam Cohend41fbf52012-02-16 23:53:59 -08001296 private void recycleTempRects(Stack<Rect> used) {
1297 while (!used.isEmpty()) {
1298 mTempRectStack.push(used.pop());
1299 }
1300 }
1301
1302 /**
1303 * Find a vacant area that will fit the given bounds nearest the requested
1304 * cell location. Uses Euclidean distance to score multiple vacant areas.
1305 *
1306 * @param pixelX The X location at which you want to search for a vacant area.
1307 * @param pixelY The Y location at which you want to search for a vacant area.
1308 * @param minSpanX The minimum horizontal span required
1309 * @param minSpanY The minimum vertical span required
1310 * @param spanX Horizontal span of the object.
1311 * @param spanY Vertical span of the object.
1312 * @param ignoreOccupied If true, the result can be an occupied cell
1313 * @param result Array in which to place the result, or null (in which case a new array will
1314 * be allocated)
1315 * @return The X, Y cell of a vacant area that can contain this object,
1316 * nearest the requested location.
1317 */
1318 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001319 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1320 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001321 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001322 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001323 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001324
Adam Cohene3e27a82011-04-15 12:07:39 -07001325 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1326 // to the center of the item, but we are searching based on the top-left cell, so
1327 // we translate the point over to correspond to the top-left.
1328 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1329 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1330
Jeff Sharkey70864282009-04-07 21:08:40 -07001331 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001332 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001333 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001334 final Rect bestRect = new Rect(-1, -1, -1, -1);
1335 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001336
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001337 final int countX = mCountX;
1338 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001339
Adam Cohend41fbf52012-02-16 23:53:59 -08001340 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1341 spanX < minSpanX || spanY < minSpanY) {
1342 return bestXY;
1343 }
1344
1345 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001346 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001347 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1348 int ySize = -1;
1349 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001350 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001351 // First, let's see if this thing fits anywhere
1352 for (int i = 0; i < minSpanX; i++) {
1353 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001354 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001355 continue inner;
1356 }
Michael Jurkac28de512010-08-13 11:27:44 -07001357 }
1358 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001359 xSize = minSpanX;
1360 ySize = minSpanY;
1361
1362 // We know that the item will fit at _some_ acceptable size, now let's see
1363 // how big we can make it. We'll alternate between incrementing x and y spans
1364 // until we hit a limit.
1365 boolean incX = true;
1366 boolean hitMaxX = xSize >= spanX;
1367 boolean hitMaxY = ySize >= spanY;
1368 while (!(hitMaxX && hitMaxY)) {
1369 if (incX && !hitMaxX) {
1370 for (int j = 0; j < ySize; j++) {
1371 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1372 // We can't move out horizontally
1373 hitMaxX = true;
1374 }
1375 }
1376 if (!hitMaxX) {
1377 xSize++;
1378 }
1379 } else if (!hitMaxY) {
1380 for (int i = 0; i < xSize; i++) {
1381 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1382 // We can't move out vertically
1383 hitMaxY = true;
1384 }
1385 }
1386 if (!hitMaxY) {
1387 ySize++;
1388 }
1389 }
1390 hitMaxX |= xSize >= spanX;
1391 hitMaxY |= ySize >= spanY;
1392 incX = !incX;
1393 }
1394 incX = true;
1395 hitMaxX = xSize >= spanX;
1396 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001397 }
Winson Chung0be025d2011-05-23 17:45:09 -07001398 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001399 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400
Adam Cohend41fbf52012-02-16 23:53:59 -08001401 // We verify that the current rect is not a sub-rect of any of our previous
1402 // candidates. In this case, the current rect is disqualified in favour of the
1403 // containing rect.
1404 Rect currentRect = mTempRectStack.pop();
1405 currentRect.set(x, y, x + xSize, y + ySize);
1406 boolean contained = false;
1407 for (Rect r : validRegions) {
1408 if (r.contains(currentRect)) {
1409 contained = true;
1410 break;
1411 }
1412 }
1413 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001414 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1415 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001416
Adam Cohend41fbf52012-02-16 23:53:59 -08001417 if ((distance <= bestDistance && !contained) ||
1418 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001419 bestDistance = distance;
1420 bestXY[0] = x;
1421 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001422 if (resultSpan != null) {
1423 resultSpan[0] = xSize;
1424 resultSpan[1] = ySize;
1425 }
1426 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
1429 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001430 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001431 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432
Adam Cohenc0dcf592011-06-01 15:30:43 -07001433 // Return -1, -1 if no suitable location found
1434 if (bestDistance == Double.MAX_VALUE) {
1435 bestXY[0] = -1;
1436 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001437 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001438 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001439 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001441
Adam Cohen482ed822012-03-02 14:15:13 -08001442 /**
1443 * Find a vacant area that will fit the given bounds nearest the requested
1444 * cell location, and will also weigh in a suggested direction vector of the
1445 * desired location. This method computers distance based on unit grid distances,
1446 * not pixel distances.
1447 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001448 * @param cellX The X cell nearest to which you want to search for a vacant area.
1449 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001450 * @param spanX Horizontal span of the object.
1451 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001452 * @param direction The favored direction in which the views should move from x, y
1453 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1454 * matches exactly. Otherwise we find the best matching direction.
1455 * @param occoupied The array which represents which cells in the CellLayout are occupied
1456 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1457 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001458 * @param result Array in which to place the result, or null (in which case a new array will
1459 * be allocated)
1460 * @return The X, Y cell of a vacant area that can contain this object,
1461 * nearest the requested location.
1462 */
1463 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001464 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001465 // Keep track of best-scoring drop area
1466 final int[] bestXY = result != null ? result : new int[2];
1467 float bestDistance = Float.MAX_VALUE;
1468 int bestDirectionScore = Integer.MIN_VALUE;
1469
1470 final int countX = mCountX;
1471 final int countY = mCountY;
1472
1473 for (int y = 0; y < countY - (spanY - 1); y++) {
1474 inner:
1475 for (int x = 0; x < countX - (spanX - 1); x++) {
1476 // First, let's see if this thing fits anywhere
1477 for (int i = 0; i < spanX; i++) {
1478 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001479 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001480 continue inner;
1481 }
1482 }
1483 }
1484
1485 float distance = (float)
1486 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1487 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001488 computeDirectionVector(x - cellX, y - cellY, curDirection);
1489 // The direction score is just the dot product of the two candidate direction
1490 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001491 int curDirectionScore = direction[0] * curDirection[0] +
1492 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001493 boolean exactDirectionOnly = false;
1494 boolean directionMatches = direction[0] == curDirection[0] &&
1495 direction[0] == curDirection[0];
1496 if ((directionMatches || !exactDirectionOnly) &&
1497 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001498 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1499 bestDistance = distance;
1500 bestDirectionScore = curDirectionScore;
1501 bestXY[0] = x;
1502 bestXY[1] = y;
1503 }
1504 }
1505 }
1506
1507 // Return -1, -1 if no suitable location found
1508 if (bestDistance == Float.MAX_VALUE) {
1509 bestXY[0] = -1;
1510 bestXY[1] = -1;
1511 }
1512 return bestXY;
1513 }
1514
Adam Cohen47a876d2012-03-19 13:21:41 -07001515 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1516 int[] direction,boolean[][] occupied,
1517 boolean blockOccupied[][], int[] result) {
1518 // Keep track of best-scoring drop area
1519 final int[] bestXY = result != null ? result : new int[2];
1520 bestXY[0] = -1;
1521 bestXY[1] = -1;
1522 float bestDistance = Float.MAX_VALUE;
1523
1524 // We use this to march in a single direction
Adam Cohen5b53f292012-03-29 14:30:35 -07001525 if ((direction[0] != 0 && direction[1] != 0) ||
1526 (direction[0] == 0 && direction[1] == 0)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001527 return bestXY;
1528 }
1529
1530 // This will only incrememnet one of x or y based on the assertion above
1531 int x = cellX + direction[0];
1532 int y = cellY + direction[1];
1533 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
1534
1535 boolean fail = false;
1536 for (int i = 0; i < spanX; i++) {
1537 for (int j = 0; j < spanY; j++) {
1538 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1539 fail = true;
1540 }
1541 }
1542 }
1543 if (!fail) {
1544 float distance = (float)
1545 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1546 if (Float.compare(distance, bestDistance) < 0) {
1547 bestDistance = distance;
1548 bestXY[0] = x;
1549 bestXY[1] = y;
1550 }
1551 }
1552 x += direction[0];
1553 y += direction[1];
1554 }
1555 return bestXY;
1556 }
1557
Adam Cohen482ed822012-03-02 14:15:13 -08001558 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001559 int[] direction, ItemConfiguration currentState) {
1560 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001561 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001562 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001563 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1564
Adam Cohen8baab352012-03-20 17:39:21 -07001565 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001566
1567 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001568 c.x = mTempLocation[0];
1569 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001570 success = true;
1571
1572 }
Adam Cohen8baab352012-03-20 17:39:21 -07001573 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001574 return success;
1575 }
1576
Adam Cohen47a876d2012-03-19 13:21:41 -07001577 // This method looks in the specified direction to see if there is an additional view
1578 // immediately adjecent in that direction
1579 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
Adam Cohen19f37922012-03-21 11:59:11 -07001580 boolean[][] occupied, View dragView, ItemConfiguration currentState) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001581 boolean found = false;
1582
Michael Jurkaa52570f2012-03-20 03:18:20 -07001583 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001584 Rect r0 = new Rect(boundingRect);
1585 Rect r1 = new Rect();
1586
1587 int deltaX = 0;
1588 int deltaY = 0;
1589 if (direction[1] < 0) {
1590 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom);
1591 deltaY = -1;
1592 } else if (direction[1] > 0) {
1593 r0.set(r0.left, r0.top, r0.right, r0.bottom + 1);
1594 deltaY = 1;
1595 } else if (direction[0] < 0) {
1596 r0.set(r0.left - 1, r0.top, r0.right, r0.bottom);
1597 deltaX = -1;
1598 } else if (direction[0] > 0) {
1599 r0.set(r0.left, r0.top, r0.right + 1, r0.bottom);
1600 deltaX = 1;
1601 }
1602
1603 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001604 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen19f37922012-03-21 11:59:11 -07001605 if (views.contains(child) || child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001606 CellAndSpan c = currentState.map.get(child);
Adam Cohen47a876d2012-03-19 13:21:41 -07001607
Adam Cohen8baab352012-03-20 17:39:21 -07001608 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1609 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001610 if (Rect.intersects(r0, r1)) {
1611 if (!lp.canReorder) {
1612 return false;
1613 }
1614 boolean pushed = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001615 for (int x = c.x; x < c.x + c.spanX; x++) {
1616 for (int y = c.y; y < c.y + c.spanY; y++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001617 boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX
1618 && y - deltaY >= 0 && y - deltaY < mCountY;
1619 if (inBounds && occupied[x - deltaX][y - deltaY]) {
1620 pushed = true;
1621 }
1622 }
1623 }
1624 if (pushed) {
1625 views.add(child);
Adam Cohen8baab352012-03-20 17:39:21 -07001626 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001627 found = true;
1628 }
1629 }
1630 }
1631 return found;
1632 }
1633
Adam Cohen482ed822012-03-02 14:15:13 -08001634 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohen19f37922012-03-21 11:59:11 -07001635 int[] direction, boolean push, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001636 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001637
Adam Cohen8baab352012-03-20 17:39:21 -07001638 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001639 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001640 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001641 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001642 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001643 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001644 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001645 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001646 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001647 }
1648 }
Adam Cohen8baab352012-03-20 17:39:21 -07001649
1650 @SuppressWarnings("unchecked")
1651 ArrayList<View> dup = (ArrayList<View>) views.clone();
1652 // We try and expand the group of views in the direction vector passed, based on
1653 // whether they are physically adjacent, ie. based on "push mechanics".
Adam Cohen19f37922012-03-21 11:59:11 -07001654 while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied, dragView,
Adam Cohen8baab352012-03-20 17:39:21 -07001655 currentState)) {
1656 }
1657
1658 // Mark the occupied state as false for the group of views we want to move.
1659 for (View v: dup) {
1660 CellAndSpan c = currentState.map.get(v);
1661 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1662 }
1663
Adam Cohen47a876d2012-03-19 13:21:41 -07001664 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1665 int top = boundingRect.top;
1666 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001667 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
1668 // for tetris-style interlocking.
1669 for (View v: dup) {
1670 CellAndSpan c = currentState.map.get(v);
1671 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001672 }
1673
Adam Cohen482ed822012-03-02 14:15:13 -08001674 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1675
Adam Cohen8baab352012-03-20 17:39:21 -07001676 if (push) {
1677 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1678 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1679 } else {
1680 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1681 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1682 }
Adam Cohen482ed822012-03-02 14:15:13 -08001683
Adam Cohen8baab352012-03-20 17:39:21 -07001684 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001685 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001686 int deltaX = mTempLocation[0] - boundingRect.left;
1687 int deltaY = mTempLocation[1] - boundingRect.top;
1688 for (View v: dup) {
1689 CellAndSpan c = currentState.map.get(v);
1690 c.x += deltaX;
1691 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001692 }
1693 success = true;
1694 }
Adam Cohen8baab352012-03-20 17:39:21 -07001695
1696 // In either case, we set the occupied array as marked for the location of the views
1697 for (View v: dup) {
1698 CellAndSpan c = currentState.map.get(v);
1699 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001700 }
1701 return success;
1702 }
1703
1704 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1705 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1706 }
1707
1708 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001709 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001710 // Return early if get invalid cell positions
1711 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001712
Adam Cohen8baab352012-03-20 17:39:21 -07001713 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001714 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001715
Adam Cohen8baab352012-03-20 17:39:21 -07001716 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001717 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001718 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001719 if (c != null) {
1720 c.x = cellX;
1721 c.y = cellY;
1722 }
Adam Cohen482ed822012-03-02 14:15:13 -08001723 }
Adam Cohen482ed822012-03-02 14:15:13 -08001724 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1725 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001726 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001727 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001728 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001729 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001730 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001731 if (Rect.intersects(r0, r1)) {
1732 if (!lp.canReorder) {
1733 return false;
1734 }
1735 mIntersectingViews.add(child);
1736 }
1737 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001738
Adam Cohen8baab352012-03-20 17:39:21 -07001739 // We try to move the intersecting views as a block using the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001740 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1741 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001742 return true;
1743 }
1744 // Try the opposite direction
1745 direction[0] *= -1;
1746 direction[1] *= -1;
Adam Cohen19f37922012-03-21 11:59:11 -07001747 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1748 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001749 return true;
1750 }
1751 // Switch the direction back
1752 direction[0] *= -1;
1753 direction[1] *= -1;
1754
Adam Cohen8baab352012-03-20 17:39:21 -07001755 // Next we try moving the views as a block , but without requiring the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001756 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, ignoreView,
1757 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001758 return true;
1759 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001760
Adam Cohen482ed822012-03-02 14:15:13 -08001761 // Ok, they couldn't move as a block, let's move them individually
1762 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001763 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001764 return false;
1765 }
1766 }
1767 return true;
1768 }
1769
1770 /*
1771 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1772 * the provided point and the provided cell
1773 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001774 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001775 double angle = Math.atan(((float) deltaY) / deltaX);
1776
1777 result[0] = 0;
1778 result[1] = 0;
1779 if (Math.abs(Math.cos(angle)) > 0.5f) {
1780 result[0] = (int) Math.signum(deltaX);
1781 }
1782 if (Math.abs(Math.sin(angle)) > 0.5f) {
1783 result[1] = (int) Math.signum(deltaY);
1784 }
1785 }
1786
Adam Cohen8baab352012-03-20 17:39:21 -07001787 private void copyOccupiedArray(boolean[][] occupied) {
1788 for (int i = 0; i < mCountX; i++) {
1789 for (int j = 0; j < mCountY; j++) {
1790 occupied[i][j] = mOccupied[i][j];
1791 }
1792 }
1793 }
1794
Adam Cohen482ed822012-03-02 14:15:13 -08001795 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1796 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001797 // Copy the current state into the solution. This solution will be manipulated as necessary.
1798 copyCurrentStateToSolution(solution, false);
1799 // Copy the current occupied array into the temporary occupied array. This array will be
1800 // manipulated as necessary to find a solution.
1801 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001802
1803 // We find the nearest cell into which we would place the dragged item, assuming there's
1804 // nothing in its way.
1805 int result[] = new int[2];
1806 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1807
1808 boolean success = false;
1809 // First we try the exact nearest position of the item being dragged,
1810 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001811 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1812 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001813
1814 if (!success) {
1815 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1816 // x, then 1 in y etc.
1817 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1818 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1819 dragView, false, solution);
1820 } else if (spanY > minSpanY) {
1821 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1822 dragView, true, solution);
1823 }
1824 solution.isSolution = false;
1825 } else {
1826 solution.isSolution = true;
1827 solution.dragViewX = result[0];
1828 solution.dragViewY = result[1];
1829 solution.dragViewSpanX = spanX;
1830 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001831 }
1832 return solution;
1833 }
1834
1835 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001836 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001837 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001838 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001839 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001840 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001841 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001842 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001843 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001844 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001845 }
Adam Cohen8baab352012-03-20 17:39:21 -07001846 solution.map.put(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001847 }
1848 }
1849
1850 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1851 for (int i = 0; i < mCountX; i++) {
1852 for (int j = 0; j < mCountY; j++) {
1853 mTmpOccupied[i][j] = false;
1854 }
1855 }
1856
Michael Jurkaa52570f2012-03-20 03:18:20 -07001857 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001858 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001859 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001860 if (child == dragView) continue;
1861 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001862 CellAndSpan c = solution.map.get(child);
1863 if (c != null) {
1864 lp.tmpCellX = c.x;
1865 lp.tmpCellY = c.y;
1866 lp.cellHSpan = c.spanX;
1867 lp.cellVSpan = c.spanY;
1868 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001869 }
1870 }
1871 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1872 solution.dragViewSpanY, mTmpOccupied, true);
1873 }
1874
1875 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1876 commitDragView) {
1877
1878 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1879 for (int i = 0; i < mCountX; i++) {
1880 for (int j = 0; j < mCountY; j++) {
1881 occupied[i][j] = false;
1882 }
1883 }
1884
Michael Jurkaa52570f2012-03-20 03:18:20 -07001885 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001886 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001887 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001888 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001889 CellAndSpan c = solution.map.get(child);
1890 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07001891 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
1892 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001893 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001894 }
1895 }
1896 if (commitDragView) {
1897 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1898 solution.dragViewSpanY, occupied, true);
1899 }
1900 }
1901
Adam Cohen19f37922012-03-21 11:59:11 -07001902 // This method starts or changes the reorder hint animations
1903 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
1904 int childCount = mShortcutsAndWidgets.getChildCount();
1905 int timeForPriorAnimationToComplete = getMaxCompletionTime();
1906 for (int i = 0; i < childCount; i++) {
1907 View child = mShortcutsAndWidgets.getChildAt(i);
1908 if (child == dragView) continue;
1909 CellAndSpan c = solution.map.get(child);
1910 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1911 if (c != null) {
1912 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
1913 c.x, c.y, c.spanX, c.spanY);
1914 rha.animate(timeForPriorAnimationToComplete);
1915 }
1916 }
1917 }
1918
1919 // Class which represents the reorder hint animations. These animations show that an item is
1920 // in a temporary state, and hint at where the item will return to.
1921 class ReorderHintAnimation {
1922 View child;
1923 float deltaX;
1924 float deltaY;
Brandon Keely50e6e562012-05-08 16:28:49 -07001925 private static final int DURATION = 300;
Adam Cohen19f37922012-03-21 11:59:11 -07001926 private int repeatCount;
1927 private boolean cancelOnCycleComplete = false;
1928 ValueAnimator va;
1929
1930 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
1931 int spanX, int spanY) {
1932 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1933 final int x0 = mTmpPoint[0];
1934 final int y0 = mTmpPoint[1];
1935 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1936 final int x1 = mTmpPoint[0];
1937 final int y1 = mTmpPoint[1];
1938 final int dX = x1 - x0;
1939 final int dY = y1 - y0;
1940 deltaX = 0;
1941 deltaY = 0;
1942 if (dX == dY && dX == 0) {
1943 } else {
1944 if (dY == 0) {
1945 deltaX = mReorderHintAnimationMagnitude;
1946 } else if (dX == 0) {
1947 deltaY = mReorderHintAnimationMagnitude;
1948 } else {
1949 double angle = Math.atan( (float) (dY) / dX);
1950 deltaX = (int) (Math.cos(angle) * mReorderHintAnimationMagnitude);
1951 deltaY = (int) (Math.sin(angle) * mReorderHintAnimationMagnitude);
1952 }
1953 }
Brandon Keely50e6e562012-05-08 16:28:49 -07001954 child.setPivotY(child.getMeasuredHeight() * 0.5f);
1955 child.setPivotX(child.getMeasuredWidth() * 0.5f);
Adam Cohen19f37922012-03-21 11:59:11 -07001956 this.child = child;
1957 }
1958
1959 void animate(int delay) {
1960 if (mShakeAnimators.containsKey(child)) {
1961 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
1962 oldAnimation.completeAnimation();
1963 mShakeAnimators.remove(child);
1964 }
1965 if (deltaX == 0 && deltaY == 0) {
1966 return;
1967 }
1968 va = ValueAnimator.ofFloat(0f, 1f);
1969 va.setRepeatMode(ValueAnimator.REVERSE);
1970 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07001971 va.setDuration(DURATION);
1972 va.setStartDelay((int) ((Math.max(REORDER_ANIMATION_DURATION, delay)
1973 + Math.random() * 60)));
Adam Cohen19f37922012-03-21 11:59:11 -07001974 va.addUpdateListener(new AnimatorUpdateListener() {
1975 @Override
1976 public void onAnimationUpdate(ValueAnimator animation) {
1977 float r = ((Float) animation.getAnimatedValue()).floatValue();
1978 float x = r * deltaX;
1979 float y = r * deltaY;
1980 child.setTranslationX(x);
1981 child.setTranslationY(y);
Brandon Keely50e6e562012-05-08 16:28:49 -07001982 float sf = 4.0f / child.getWidth();
1983 float s = 1.0f - r * sf;
1984 child.setScaleX(s);
1985 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07001986 }
1987 });
1988 va.addListener(new AnimatorListenerAdapter() {
1989 public void onAnimationRepeat(Animator animation) {
1990 repeatCount++;
1991 // We make sure to end only after a full period
1992 if (cancelOnCycleComplete && repeatCount % 2 == 0) {
1993 va.cancel();
1994 }
1995 }
1996 });
Adam Cohen19f37922012-03-21 11:59:11 -07001997 mShakeAnimators.put(child, this);
1998 va.start();
1999 }
2000
2001
2002 private void completeAnimation() {
2003 cancelOnCycleComplete = true;
2004 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002005 private void completeAnimationImmediately() {
2006 va.cancel();
2007
2008 AnimatorSet s = new AnimatorSet();
2009 s.playTogether(
2010 ObjectAnimator.ofFloat(child, "scaleX", 1f),
2011 ObjectAnimator.ofFloat(child, "scaleY", 1f),
2012 ObjectAnimator.ofFloat(child, "translationX", 0f),
Brandon Keelyfaf22e12012-05-10 16:48:50 -07002013 ObjectAnimator.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002014 );
2015 s.setDuration(REORDER_ANIMATION_DURATION);
2016 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2017 s.start();
2018 }
2019
Adam Cohen19f37922012-03-21 11:59:11 -07002020
2021 // Returns the time required to complete the current oscillating animation
2022 private int completionTime() {
2023 if (repeatCount % 2 == 0) {
2024 return (int) (va.getDuration() - va.getCurrentPlayTime() + DURATION);
2025 } else {
2026 return (int) (va.getDuration() - va.getCurrentPlayTime());
2027 }
2028 }
2029 }
2030
2031 private void completeAndClearReorderHintAnimations() {
2032 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002033 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002034 }
2035 mShakeAnimators.clear();
2036 }
2037
2038 private int getMaxCompletionTime() {
2039 int maxTime = 0;
2040 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2041 maxTime = Math.max(maxTime, a.completionTime());
2042 }
2043 return maxTime;
2044 }
2045
Adam Cohen482ed822012-03-02 14:15:13 -08002046 private void commitTempPlacement() {
2047 for (int i = 0; i < mCountX; i++) {
2048 for (int j = 0; j < mCountY; j++) {
2049 mOccupied[i][j] = mTmpOccupied[i][j];
2050 }
2051 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002052 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002053 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002054 View child = mShortcutsAndWidgets.getChildAt(i);
2055 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2056 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002057 // We do a null check here because the item info can be null in the case of the
2058 // AllApps button in the hotseat.
2059 if (info != null) {
2060 info.cellX = lp.cellX = lp.tmpCellX;
2061 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002062 info.spanX = lp.cellHSpan;
2063 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002064 }
Adam Cohen482ed822012-03-02 14:15:13 -08002065 }
Adam Cohen2acce882012-03-28 19:03:19 -07002066 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002067 }
2068
2069 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002070 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002071 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002072 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002073 lp.useTmpCoords = useTempCoords;
2074 }
2075 }
2076
Adam Cohen482ed822012-03-02 14:15:13 -08002077 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2078 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2079 int[] result = new int[2];
2080 int[] resultSpan = new int[2];
2081 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2082 resultSpan);
2083 if (result[0] >= 0 && result[1] >= 0) {
2084 copyCurrentStateToSolution(solution, false);
2085 solution.dragViewX = result[0];
2086 solution.dragViewY = result[1];
2087 solution.dragViewSpanX = resultSpan[0];
2088 solution.dragViewSpanY = resultSpan[1];
2089 solution.isSolution = true;
2090 } else {
2091 solution.isSolution = false;
2092 }
2093 return solution;
2094 }
2095
2096 public void prepareChildForDrag(View child) {
2097 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002098 }
2099
Adam Cohen19f37922012-03-21 11:59:11 -07002100 /* This seems like it should be obvious and straight-forward, but when the direction vector
2101 needs to match with the notion of the dragView pushing other views, we have to employ
2102 a slightly more subtle notion of the direction vector. The question is what two points is
2103 the vector between? The center of the dragView and its desired destination? Not quite, as
2104 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2105 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2106 or right, which helps make pushing feel right.
2107 */
2108 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2109 int spanY, View dragView, int[] resultDirection) {
2110 int[] targetDestination = new int[2];
2111
2112 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2113 Rect dragRect = new Rect();
2114 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2115 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2116
2117 Rect dropRegionRect = new Rect();
2118 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2119 dragView, dropRegionRect, mIntersectingViews);
2120
2121 int dropRegionSpanX = dropRegionRect.width();
2122 int dropRegionSpanY = dropRegionRect.height();
2123
2124 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2125 dropRegionRect.height(), dropRegionRect);
2126
2127 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2128 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2129
2130 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2131 deltaX = 0;
2132 }
2133 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2134 deltaY = 0;
2135 }
2136
2137 if (deltaX == 0 && deltaY == 0) {
2138 // No idea what to do, give a random direction.
2139 resultDirection[0] = 1;
2140 resultDirection[1] = 0;
2141 } else {
2142 computeDirectionVector(deltaX, deltaY, resultDirection);
2143 }
2144 }
2145
2146 // For a given cell and span, fetch the set of views intersecting the region.
2147 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2148 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2149 if (boundingRect != null) {
2150 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2151 }
2152 intersectingViews.clear();
2153 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2154 Rect r1 = new Rect();
2155 final int count = mShortcutsAndWidgets.getChildCount();
2156 for (int i = 0; i < count; i++) {
2157 View child = mShortcutsAndWidgets.getChildAt(i);
2158 if (child == dragView) continue;
2159 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2160 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2161 if (Rect.intersects(r0, r1)) {
2162 mIntersectingViews.add(child);
2163 if (boundingRect != null) {
2164 boundingRect.union(r1);
2165 }
2166 }
2167 }
2168 }
2169
2170 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2171 View dragView, int[] result) {
2172 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2173 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2174 mIntersectingViews);
2175 return !mIntersectingViews.isEmpty();
2176 }
2177
2178 void revertTempState() {
2179 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2180 final int count = mShortcutsAndWidgets.getChildCount();
2181 for (int i = 0; i < count; i++) {
2182 View child = mShortcutsAndWidgets.getChildAt(i);
2183 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2184 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2185 lp.tmpCellX = lp.cellX;
2186 lp.tmpCellY = lp.cellY;
2187 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2188 0, false, false);
2189 }
2190 }
2191 completeAndClearReorderHintAnimations();
2192 setItemPlacementDirty(false);
2193 }
2194
Adam Cohenbebf0422012-04-11 18:06:28 -07002195 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2196 View dragView, int[] direction, boolean commit) {
2197 int[] pixelXY = new int[2];
2198 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2199
2200 // First we determine if things have moved enough to cause a different layout
2201 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2202 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2203
2204 setUseTempCoords(true);
2205 if (swapSolution != null && swapSolution.isSolution) {
2206 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2207 // committing anything or animating anything as we just want to determine if a solution
2208 // exists
2209 copySolutionToTempState(swapSolution, dragView);
2210 setItemPlacementDirty(true);
2211 animateItemsToSolution(swapSolution, dragView, commit);
2212
2213 if (commit) {
2214 commitTempPlacement();
2215 completeAndClearReorderHintAnimations();
2216 setItemPlacementDirty(false);
2217 } else {
2218 beginOrAdjustHintAnimations(swapSolution, dragView,
2219 REORDER_ANIMATION_DURATION);
2220 }
2221 mShortcutsAndWidgets.requestLayout();
2222 }
2223 return swapSolution.isSolution;
2224 }
2225
Adam Cohen482ed822012-03-02 14:15:13 -08002226 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2227 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002228 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002229 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002230
2231 if (resultSpan == null) {
2232 resultSpan = new int[2];
2233 }
2234
Adam Cohen19f37922012-03-21 11:59:11 -07002235 // When we are checking drop validity or actually dropping, we don't recompute the
2236 // direction vector, since we want the solution to match the preview, and it's possible
2237 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002238 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2239 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002240 mDirectionVector[0] = mPreviousReorderDirection[0];
2241 mDirectionVector[1] = mPreviousReorderDirection[1];
2242 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002243 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2244 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2245 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002246 }
2247 } else {
2248 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2249 mPreviousReorderDirection[0] = mDirectionVector[0];
2250 mPreviousReorderDirection[1] = mDirectionVector[1];
2251 }
2252
Adam Cohen482ed822012-03-02 14:15:13 -08002253 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2254 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2255
2256 // We attempt the approach which doesn't shuffle views at all
2257 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2258 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2259
2260 ItemConfiguration finalSolution = null;
2261 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2262 finalSolution = swapSolution;
2263 } else if (noShuffleSolution.isSolution) {
2264 finalSolution = noShuffleSolution;
2265 }
2266
2267 boolean foundSolution = true;
2268 if (!DESTRUCTIVE_REORDER) {
2269 setUseTempCoords(true);
2270 }
2271
2272 if (finalSolution != null) {
2273 result[0] = finalSolution.dragViewX;
2274 result[1] = finalSolution.dragViewY;
2275 resultSpan[0] = finalSolution.dragViewSpanX;
2276 resultSpan[1] = finalSolution.dragViewSpanY;
2277
2278 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2279 // committing anything or animating anything as we just want to determine if a solution
2280 // exists
2281 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2282 if (!DESTRUCTIVE_REORDER) {
2283 copySolutionToTempState(finalSolution, dragView);
2284 }
2285 setItemPlacementDirty(true);
2286 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2287
Adam Cohen19f37922012-03-21 11:59:11 -07002288 if (!DESTRUCTIVE_REORDER &&
2289 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002290 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002291 completeAndClearReorderHintAnimations();
2292 setItemPlacementDirty(false);
2293 } else {
2294 beginOrAdjustHintAnimations(finalSolution, dragView,
2295 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002296 }
2297 }
2298 } else {
2299 foundSolution = false;
2300 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2301 }
2302
2303 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2304 setUseTempCoords(false);
2305 }
Adam Cohen482ed822012-03-02 14:15:13 -08002306
Michael Jurkaa52570f2012-03-20 03:18:20 -07002307 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002308 return result;
2309 }
2310
Adam Cohen19f37922012-03-21 11:59:11 -07002311 void setItemPlacementDirty(boolean dirty) {
2312 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002313 }
Adam Cohen19f37922012-03-21 11:59:11 -07002314 boolean isItemPlacementDirty() {
2315 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002316 }
2317
2318 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002319 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002320 boolean isSolution = false;
2321 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2322
2323 int area() {
2324 return dragViewSpanX * dragViewSpanY;
2325 }
Adam Cohen8baab352012-03-20 17:39:21 -07002326 }
2327
2328 private class CellAndSpan {
2329 int x, y;
2330 int spanX, spanY;
2331
2332 public CellAndSpan(int x, int y, int spanX, int spanY) {
2333 this.x = x;
2334 this.y = y;
2335 this.spanX = spanX;
2336 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002337 }
2338 }
2339
Adam Cohendf035382011-04-11 17:22:04 -07002340 /**
2341 * Find a vacant area that will fit the given bounds nearest the requested
2342 * cell location. Uses Euclidean distance to score multiple vacant areas.
2343 *
2344 * @param pixelX The X location at which you want to search for a vacant area.
2345 * @param pixelY The Y location at which you want to search for a vacant area.
2346 * @param spanX Horizontal span of the object.
2347 * @param spanY Vertical span of the object.
2348 * @param ignoreView Considers space occupied by this view as unoccupied
2349 * @param result Previously returned value to possibly recycle.
2350 * @return The X, Y cell of a vacant area that can contain this object,
2351 * nearest the requested location.
2352 */
2353 int[] findNearestVacantArea(
2354 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2355 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2356 }
2357
2358 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002359 * Find a vacant area that will fit the given bounds nearest the requested
2360 * cell location. Uses Euclidean distance to score multiple vacant areas.
2361 *
2362 * @param pixelX The X location at which you want to search for a vacant area.
2363 * @param pixelY The Y location at which you want to search for a vacant area.
2364 * @param minSpanX The minimum horizontal span required
2365 * @param minSpanY The minimum vertical span required
2366 * @param spanX Horizontal span of the object.
2367 * @param spanY Vertical span of the object.
2368 * @param ignoreView Considers space occupied by this view as unoccupied
2369 * @param result Previously returned value to possibly recycle.
2370 * @return The X, Y cell of a vacant area that can contain this object,
2371 * nearest the requested location.
2372 */
2373 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2374 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002375 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2376 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002377 }
2378
2379 /**
Adam Cohendf035382011-04-11 17:22:04 -07002380 * Find a starting cell position 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 spanX Horizontal span of the object.
2386 * @param spanY Vertical span of the object.
2387 * @param ignoreView Considers space occupied by this view as unoccupied
2388 * @param result Previously returned value to possibly recycle.
2389 * @return The X, Y cell of a vacant area that can contain this object,
2390 * nearest the requested location.
2391 */
2392 int[] findNearestArea(
2393 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2394 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2395 }
2396
Michael Jurka0280c3b2010-09-17 15:00:07 -07002397 boolean existsEmptyCell() {
2398 return findCellForSpan(null, 1, 1);
2399 }
2400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002402 * Finds the upper-left coordinate of the first rectangle in the grid that can
2403 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2404 * then this method will only return coordinates for rectangles that contain the cell
2405 * (intersectX, intersectY)
2406 *
2407 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2408 * can be found.
2409 * @param spanX The horizontal span of the cell we want to find.
2410 * @param spanY The vertical span of the cell we want to find.
2411 *
2412 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002413 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002414 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002415 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002416 }
2417
2418 /**
2419 * Like above, but ignores any cells occupied by the item "ignoreView"
2420 *
2421 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2422 * can be found.
2423 * @param spanX The horizontal span of the cell we want to find.
2424 * @param spanY The vertical span of the cell we want to find.
2425 * @param ignoreView The home screen item we should treat as not occupying any space
2426 * @return
2427 */
2428 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002429 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2430 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002431 }
2432
2433 /**
2434 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2435 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2436 *
2437 * @param spanX The horizontal span of the cell we want to find.
2438 * @param spanY The vertical span of the cell we want to find.
2439 * @param ignoreView The home screen item we should treat as not occupying any space
2440 * @param intersectX The X coordinate of the cell that we should try to overlap
2441 * @param intersectX The Y coordinate of the cell that we should try to overlap
2442 *
2443 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2444 */
2445 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2446 int intersectX, int intersectY) {
2447 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002448 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002449 }
2450
2451 /**
2452 * The superset of the above two methods
2453 */
2454 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002455 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002456 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002457 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002458
Michael Jurka28750fb2010-09-24 17:43:49 -07002459 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002460 while (true) {
2461 int startX = 0;
2462 if (intersectX >= 0) {
2463 startX = Math.max(startX, intersectX - (spanX - 1));
2464 }
2465 int endX = mCountX - (spanX - 1);
2466 if (intersectX >= 0) {
2467 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2468 }
2469 int startY = 0;
2470 if (intersectY >= 0) {
2471 startY = Math.max(startY, intersectY - (spanY - 1));
2472 }
2473 int endY = mCountY - (spanY - 1);
2474 if (intersectY >= 0) {
2475 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2476 }
2477
Winson Chungbbc60d82010-11-11 16:34:41 -08002478 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002479 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002480 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002481 for (int i = 0; i < spanX; i++) {
2482 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002483 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002484 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002485 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002486 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002487 continue inner;
2488 }
2489 }
2490 }
2491 if (cellXY != null) {
2492 cellXY[0] = x;
2493 cellXY[1] = y;
2494 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002495 foundCell = true;
2496 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002497 }
2498 }
2499 if (intersectX == -1 && intersectY == -1) {
2500 break;
2501 } else {
2502 // if we failed to find anything, try again but without any requirements of
2503 // intersecting
2504 intersectX = -1;
2505 intersectY = -1;
2506 continue;
2507 }
2508 }
2509
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002510 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002511 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002512 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002513 }
2514
2515 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002516 * A drag event has begun over this layout.
2517 * It may have begun over this layout (in which case onDragChild is called first),
2518 * or it may have begun on another layout.
2519 */
2520 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002521 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002522 mDragging = true;
2523 }
2524
2525 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002526 * Called when drag has left this CellLayout or has been completed (successfully or not)
2527 */
2528 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002529 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002530 // This can actually be called when we aren't in a drag, e.g. when adding a new
2531 // item to this layout via the customize drawer.
2532 // Guard against that case.
2533 if (mDragging) {
2534 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002535 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002536
2537 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002538 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002539 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2540 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002541 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002542 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002543 }
2544
2545 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002546 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002547 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002548 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 *
2550 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002552 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002553 if (child != null) {
2554 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002555 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002556 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 }
2559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002562 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 * @param cellX X coordinate of upper left corner expressed as a cell position
2564 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002565 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002567 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002569 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002570 final int cellWidth = mCellWidth;
2571 final int cellHeight = mCellHeight;
2572 final int widthGap = mWidthGap;
2573 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002574
Winson Chung4b825dcd2011-06-19 12:41:22 -07002575 final int hStartPadding = getPaddingLeft();
2576 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002578 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2579 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2580
2581 int x = hStartPadding + cellX * (cellWidth + widthGap);
2582 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002583
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002584 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002585 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002588 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002590 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591 * @param width Width in pixels
2592 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002593 * @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 -08002594 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002595 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002596 return rectToCell(getResources(), width, height, result);
2597 }
2598
2599 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002600 // Always assume we're working with the smallest span to make sure we
2601 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002602 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2603 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002604 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002605
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002606 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002607 int spanX = (int) Math.ceil(width / (float) smallerSize);
2608 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002609
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002610 if (result == null) {
2611 return new int[] { spanX, spanY };
2612 }
2613 result[0] = spanX;
2614 result[1] = spanY;
2615 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002616 }
2617
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002618 public int[] cellSpansToSize(int hSpans, int vSpans) {
2619 int[] size = new int[2];
2620 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2621 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2622 return size;
2623 }
2624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002625 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002626 * Calculate the grid spans needed to fit given item
2627 */
2628 public void calculateSpans(ItemInfo info) {
2629 final int minWidth;
2630 final int minHeight;
2631
2632 if (info instanceof LauncherAppWidgetInfo) {
2633 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2634 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2635 } else if (info instanceof PendingAddWidgetInfo) {
2636 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2637 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2638 } else {
2639 // It's not a widget, so it must be 1x1
2640 info.spanX = info.spanY = 1;
2641 return;
2642 }
2643 int[] spans = rectToCell(minWidth, minHeight, null);
2644 info.spanX = spans[0];
2645 info.spanY = spans[1];
2646 }
2647
2648 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002649 * Find the first vacant cell, if there is one.
2650 *
2651 * @param vacant Holds the x and y coordinate of the vacant cell
2652 * @param spanX Horizontal cell span.
2653 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002654 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002655 * @return True if a vacant cell was found
2656 */
2657 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658
Michael Jurka0280c3b2010-09-17 15:00:07 -07002659 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002660 }
2661
2662 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2663 int xCount, int yCount, boolean[][] occupied) {
2664
Adam Cohen2801caf2011-05-13 20:57:39 -07002665 for (int y = 0; y < yCount; y++) {
2666 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002667 boolean available = !occupied[x][y];
2668out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2669 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2670 available = available && !occupied[i][j];
2671 if (!available) break out;
2672 }
2673 }
2674
2675 if (available) {
2676 vacant[0] = x;
2677 vacant[1] = y;
2678 return true;
2679 }
2680 }
2681 }
2682
2683 return false;
2684 }
2685
Michael Jurka0280c3b2010-09-17 15:00:07 -07002686 private void clearOccupiedCells() {
2687 for (int x = 0; x < mCountX; x++) {
2688 for (int y = 0; y < mCountY; y++) {
2689 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002690 }
2691 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002692 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002693
Adam Cohend41fbf52012-02-16 23:53:59 -08002694 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002695 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002696 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002698
Adam Cohend4844c32011-02-18 19:25:06 -08002699 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002700 markCellsAsOccupiedForView(view, mOccupied);
2701 }
2702 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002703 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002704 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002705 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002706 }
2707
Adam Cohend4844c32011-02-18 19:25:06 -08002708 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002709 markCellsAsUnoccupiedForView(view, mOccupied);
2710 }
2711 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002712 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002713 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002714 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002715 }
2716
Adam Cohen482ed822012-03-02 14:15:13 -08002717 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2718 boolean value) {
2719 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002720 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2721 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002722 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002723 }
2724 }
2725 }
2726
Adam Cohen2801caf2011-05-13 20:57:39 -07002727 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002728 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002729 (Math.max((mCountX - 1), 0) * mWidthGap);
2730 }
2731
2732 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002733 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002734 (Math.max((mCountY - 1), 0) * mHeightGap);
2735 }
2736
Michael Jurka66d72172011-04-12 16:29:25 -07002737 public boolean isOccupied(int x, int y) {
2738 if (x < mCountX && y < mCountY) {
2739 return mOccupied[x][y];
2740 } else {
2741 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2742 }
2743 }
2744
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002745 @Override
2746 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2747 return new CellLayout.LayoutParams(getContext(), attrs);
2748 }
2749
2750 @Override
2751 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2752 return p instanceof CellLayout.LayoutParams;
2753 }
2754
2755 @Override
2756 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2757 return new CellLayout.LayoutParams(p);
2758 }
2759
Winson Chungaafa03c2010-06-11 17:34:16 -07002760 public static class CellLayoutAnimationController extends LayoutAnimationController {
2761 public CellLayoutAnimationController(Animation animation, float delay) {
2762 super(animation, delay);
2763 }
2764
2765 @Override
2766 protected long getDelayForView(View view) {
2767 return (int) (Math.random() * 150);
2768 }
2769 }
2770
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002771 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2772 /**
2773 * Horizontal location of the item in the grid.
2774 */
2775 @ViewDebug.ExportedProperty
2776 public int cellX;
2777
2778 /**
2779 * Vertical location of the item in the grid.
2780 */
2781 @ViewDebug.ExportedProperty
2782 public int cellY;
2783
2784 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002785 * Temporary horizontal location of the item in the grid during reorder
2786 */
2787 public int tmpCellX;
2788
2789 /**
2790 * Temporary vertical location of the item in the grid during reorder
2791 */
2792 public int tmpCellY;
2793
2794 /**
2795 * Indicates that the temporary coordinates should be used to layout the items
2796 */
2797 public boolean useTmpCoords;
2798
2799 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002800 * Number of cells spanned horizontally by the item.
2801 */
2802 @ViewDebug.ExportedProperty
2803 public int cellHSpan;
2804
2805 /**
2806 * Number of cells spanned vertically by the item.
2807 */
2808 @ViewDebug.ExportedProperty
2809 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002810
Adam Cohen1b607ed2011-03-03 17:26:50 -08002811 /**
2812 * Indicates whether the item will set its x, y, width and height parameters freely,
2813 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2814 */
Adam Cohend4844c32011-02-18 19:25:06 -08002815 public boolean isLockedToGrid = true;
2816
Adam Cohen482ed822012-03-02 14:15:13 -08002817 /**
2818 * Indicates whether this item can be reordered. Always true except in the case of the
2819 * the AllApps button.
2820 */
2821 public boolean canReorder = true;
2822
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823 // X coordinate of the view in the layout.
2824 @ViewDebug.ExportedProperty
2825 int x;
2826 // Y coordinate of the view in the layout.
2827 @ViewDebug.ExportedProperty
2828 int y;
2829
Romain Guy84f296c2009-11-04 15:00:44 -08002830 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002831
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002832 public LayoutParams(Context c, AttributeSet attrs) {
2833 super(c, attrs);
2834 cellHSpan = 1;
2835 cellVSpan = 1;
2836 }
2837
2838 public LayoutParams(ViewGroup.LayoutParams source) {
2839 super(source);
2840 cellHSpan = 1;
2841 cellVSpan = 1;
2842 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002843
2844 public LayoutParams(LayoutParams source) {
2845 super(source);
2846 this.cellX = source.cellX;
2847 this.cellY = source.cellY;
2848 this.cellHSpan = source.cellHSpan;
2849 this.cellVSpan = source.cellVSpan;
2850 }
2851
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002852 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002853 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002854 this.cellX = cellX;
2855 this.cellY = cellY;
2856 this.cellHSpan = cellHSpan;
2857 this.cellVSpan = cellVSpan;
2858 }
2859
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002860 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002861 if (isLockedToGrid) {
2862 final int myCellHSpan = cellHSpan;
2863 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002864 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2865 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002866
Adam Cohend4844c32011-02-18 19:25:06 -08002867 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2868 leftMargin - rightMargin;
2869 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2870 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002871 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2872 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002873 }
2874 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002875
Winson Chungaafa03c2010-06-11 17:34:16 -07002876 public String toString() {
2877 return "(" + this.cellX + ", " + this.cellY + ")";
2878 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002879
2880 public void setWidth(int width) {
2881 this.width = width;
2882 }
2883
2884 public int getWidth() {
2885 return width;
2886 }
2887
2888 public void setHeight(int height) {
2889 this.height = height;
2890 }
2891
2892 public int getHeight() {
2893 return height;
2894 }
2895
2896 public void setX(int x) {
2897 this.x = x;
2898 }
2899
2900 public int getX() {
2901 return x;
2902 }
2903
2904 public void setY(int y) {
2905 this.y = y;
2906 }
2907
2908 public int getY() {
2909 return y;
2910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002911 }
2912
Michael Jurka0280c3b2010-09-17 15:00:07 -07002913 // This class stores info for two purposes:
2914 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2915 // its spanX, spanY, and the screen it is on
2916 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2917 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2918 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002919 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002921 int cellX = -1;
2922 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002923 int spanX;
2924 int spanY;
2925 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002926 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002928 @Override
2929 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002930 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2931 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 }
2933 }
Michael Jurkad771c962011-08-09 15:00:48 -07002934
2935 public boolean lastDownOnOccupiedCell() {
2936 return mLastDownOnOccupiedCell;
2937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938}