blob: 4837ddd3a54c4c6a568dade16cb34ee3a39bb465 [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) {
Adam Cohenfe41ac62012-05-23 14:00:37 -07001945 deltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07001946 } else if (dX == 0) {
Adam Cohenfe41ac62012-05-23 14:00:37 -07001947 deltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07001948 } else {
1949 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfe41ac62012-05-23 14:00:37 -07001950 deltaX = (int) (- Math.signum(dX) *
1951 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
1952 deltaY = (int) (- Math.signum(dY) *
1953 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07001954 }
1955 }
Brandon Keely50e6e562012-05-08 16:28:49 -07001956 child.setPivotY(child.getMeasuredHeight() * 0.5f);
1957 child.setPivotX(child.getMeasuredWidth() * 0.5f);
Adam Cohen19f37922012-03-21 11:59:11 -07001958 this.child = child;
1959 }
1960
1961 void animate(int delay) {
1962 if (mShakeAnimators.containsKey(child)) {
1963 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
1964 oldAnimation.completeAnimation();
1965 mShakeAnimators.remove(child);
1966 }
1967 if (deltaX == 0 && deltaY == 0) {
1968 return;
1969 }
1970 va = ValueAnimator.ofFloat(0f, 1f);
1971 va.setRepeatMode(ValueAnimator.REVERSE);
1972 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07001973 va.setDuration(DURATION);
1974 va.setStartDelay((int) ((Math.max(REORDER_ANIMATION_DURATION, delay)
1975 + Math.random() * 60)));
Adam Cohen19f37922012-03-21 11:59:11 -07001976 va.addUpdateListener(new AnimatorUpdateListener() {
1977 @Override
1978 public void onAnimationUpdate(ValueAnimator animation) {
1979 float r = ((Float) animation.getAnimatedValue()).floatValue();
1980 float x = r * deltaX;
1981 float y = r * deltaY;
1982 child.setTranslationX(x);
1983 child.setTranslationY(y);
Brandon Keely50e6e562012-05-08 16:28:49 -07001984 float sf = 4.0f / child.getWidth();
1985 float s = 1.0f - r * sf;
1986 child.setScaleX(s);
1987 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07001988 }
1989 });
1990 va.addListener(new AnimatorListenerAdapter() {
1991 public void onAnimationRepeat(Animator animation) {
1992 repeatCount++;
1993 // We make sure to end only after a full period
1994 if (cancelOnCycleComplete && repeatCount % 2 == 0) {
1995 va.cancel();
1996 }
1997 }
1998 });
Adam Cohen19f37922012-03-21 11:59:11 -07001999 mShakeAnimators.put(child, this);
2000 va.start();
2001 }
2002
2003
2004 private void completeAnimation() {
2005 cancelOnCycleComplete = true;
2006 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002007 private void completeAnimationImmediately() {
2008 va.cancel();
2009
2010 AnimatorSet s = new AnimatorSet();
2011 s.playTogether(
2012 ObjectAnimator.ofFloat(child, "scaleX", 1f),
2013 ObjectAnimator.ofFloat(child, "scaleY", 1f),
2014 ObjectAnimator.ofFloat(child, "translationX", 0f),
Brandon Keelyfaf22e12012-05-10 16:48:50 -07002015 ObjectAnimator.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002016 );
2017 s.setDuration(REORDER_ANIMATION_DURATION);
2018 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2019 s.start();
2020 }
2021
Adam Cohen19f37922012-03-21 11:59:11 -07002022
2023 // Returns the time required to complete the current oscillating animation
2024 private int completionTime() {
2025 if (repeatCount % 2 == 0) {
2026 return (int) (va.getDuration() - va.getCurrentPlayTime() + DURATION);
2027 } else {
2028 return (int) (va.getDuration() - va.getCurrentPlayTime());
2029 }
2030 }
2031 }
2032
2033 private void completeAndClearReorderHintAnimations() {
2034 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002035 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002036 }
2037 mShakeAnimators.clear();
2038 }
2039
2040 private int getMaxCompletionTime() {
2041 int maxTime = 0;
2042 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2043 maxTime = Math.max(maxTime, a.completionTime());
2044 }
2045 return maxTime;
2046 }
2047
Adam Cohen482ed822012-03-02 14:15:13 -08002048 private void commitTempPlacement() {
2049 for (int i = 0; i < mCountX; i++) {
2050 for (int j = 0; j < mCountY; j++) {
2051 mOccupied[i][j] = mTmpOccupied[i][j];
2052 }
2053 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002054 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002055 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002056 View child = mShortcutsAndWidgets.getChildAt(i);
2057 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2058 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002059 // We do a null check here because the item info can be null in the case of the
2060 // AllApps button in the hotseat.
2061 if (info != null) {
2062 info.cellX = lp.cellX = lp.tmpCellX;
2063 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002064 info.spanX = lp.cellHSpan;
2065 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002066 }
Adam Cohen482ed822012-03-02 14:15:13 -08002067 }
Adam Cohen2acce882012-03-28 19:03:19 -07002068 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002069 }
2070
2071 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002072 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002073 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002074 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002075 lp.useTmpCoords = useTempCoords;
2076 }
2077 }
2078
Adam Cohen482ed822012-03-02 14:15:13 -08002079 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2080 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2081 int[] result = new int[2];
2082 int[] resultSpan = new int[2];
2083 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2084 resultSpan);
2085 if (result[0] >= 0 && result[1] >= 0) {
2086 copyCurrentStateToSolution(solution, false);
2087 solution.dragViewX = result[0];
2088 solution.dragViewY = result[1];
2089 solution.dragViewSpanX = resultSpan[0];
2090 solution.dragViewSpanY = resultSpan[1];
2091 solution.isSolution = true;
2092 } else {
2093 solution.isSolution = false;
2094 }
2095 return solution;
2096 }
2097
2098 public void prepareChildForDrag(View child) {
2099 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002100 }
2101
Adam Cohen19f37922012-03-21 11:59:11 -07002102 /* This seems like it should be obvious and straight-forward, but when the direction vector
2103 needs to match with the notion of the dragView pushing other views, we have to employ
2104 a slightly more subtle notion of the direction vector. The question is what two points is
2105 the vector between? The center of the dragView and its desired destination? Not quite, as
2106 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2107 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2108 or right, which helps make pushing feel right.
2109 */
2110 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2111 int spanY, View dragView, int[] resultDirection) {
2112 int[] targetDestination = new int[2];
2113
2114 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2115 Rect dragRect = new Rect();
2116 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2117 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2118
2119 Rect dropRegionRect = new Rect();
2120 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2121 dragView, dropRegionRect, mIntersectingViews);
2122
2123 int dropRegionSpanX = dropRegionRect.width();
2124 int dropRegionSpanY = dropRegionRect.height();
2125
2126 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2127 dropRegionRect.height(), dropRegionRect);
2128
2129 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2130 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2131
2132 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2133 deltaX = 0;
2134 }
2135 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2136 deltaY = 0;
2137 }
2138
2139 if (deltaX == 0 && deltaY == 0) {
2140 // No idea what to do, give a random direction.
2141 resultDirection[0] = 1;
2142 resultDirection[1] = 0;
2143 } else {
2144 computeDirectionVector(deltaX, deltaY, resultDirection);
2145 }
2146 }
2147
2148 // For a given cell and span, fetch the set of views intersecting the region.
2149 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2150 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2151 if (boundingRect != null) {
2152 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2153 }
2154 intersectingViews.clear();
2155 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2156 Rect r1 = new Rect();
2157 final int count = mShortcutsAndWidgets.getChildCount();
2158 for (int i = 0; i < count; i++) {
2159 View child = mShortcutsAndWidgets.getChildAt(i);
2160 if (child == dragView) continue;
2161 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2162 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2163 if (Rect.intersects(r0, r1)) {
2164 mIntersectingViews.add(child);
2165 if (boundingRect != null) {
2166 boundingRect.union(r1);
2167 }
2168 }
2169 }
2170 }
2171
2172 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2173 View dragView, int[] result) {
2174 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2175 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2176 mIntersectingViews);
2177 return !mIntersectingViews.isEmpty();
2178 }
2179
2180 void revertTempState() {
2181 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2182 final int count = mShortcutsAndWidgets.getChildCount();
2183 for (int i = 0; i < count; i++) {
2184 View child = mShortcutsAndWidgets.getChildAt(i);
2185 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2186 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2187 lp.tmpCellX = lp.cellX;
2188 lp.tmpCellY = lp.cellY;
2189 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2190 0, false, false);
2191 }
2192 }
2193 completeAndClearReorderHintAnimations();
2194 setItemPlacementDirty(false);
2195 }
2196
Adam Cohenbebf0422012-04-11 18:06:28 -07002197 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2198 View dragView, int[] direction, boolean commit) {
2199 int[] pixelXY = new int[2];
2200 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2201
2202 // First we determine if things have moved enough to cause a different layout
2203 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2204 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2205
2206 setUseTempCoords(true);
2207 if (swapSolution != null && swapSolution.isSolution) {
2208 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2209 // committing anything or animating anything as we just want to determine if a solution
2210 // exists
2211 copySolutionToTempState(swapSolution, dragView);
2212 setItemPlacementDirty(true);
2213 animateItemsToSolution(swapSolution, dragView, commit);
2214
2215 if (commit) {
2216 commitTempPlacement();
2217 completeAndClearReorderHintAnimations();
2218 setItemPlacementDirty(false);
2219 } else {
2220 beginOrAdjustHintAnimations(swapSolution, dragView,
2221 REORDER_ANIMATION_DURATION);
2222 }
2223 mShortcutsAndWidgets.requestLayout();
2224 }
2225 return swapSolution.isSolution;
2226 }
2227
Adam Cohen482ed822012-03-02 14:15:13 -08002228 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2229 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002230 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002231 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002232
2233 if (resultSpan == null) {
2234 resultSpan = new int[2];
2235 }
2236
Adam Cohen19f37922012-03-21 11:59:11 -07002237 // When we are checking drop validity or actually dropping, we don't recompute the
2238 // direction vector, since we want the solution to match the preview, and it's possible
2239 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002240 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2241 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002242 mDirectionVector[0] = mPreviousReorderDirection[0];
2243 mDirectionVector[1] = mPreviousReorderDirection[1];
2244 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002245 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2246 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2247 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002248 }
2249 } else {
2250 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2251 mPreviousReorderDirection[0] = mDirectionVector[0];
2252 mPreviousReorderDirection[1] = mDirectionVector[1];
2253 }
2254
Adam Cohen482ed822012-03-02 14:15:13 -08002255 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2256 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2257
2258 // We attempt the approach which doesn't shuffle views at all
2259 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2260 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2261
2262 ItemConfiguration finalSolution = null;
2263 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2264 finalSolution = swapSolution;
2265 } else if (noShuffleSolution.isSolution) {
2266 finalSolution = noShuffleSolution;
2267 }
2268
2269 boolean foundSolution = true;
2270 if (!DESTRUCTIVE_REORDER) {
2271 setUseTempCoords(true);
2272 }
2273
2274 if (finalSolution != null) {
2275 result[0] = finalSolution.dragViewX;
2276 result[1] = finalSolution.dragViewY;
2277 resultSpan[0] = finalSolution.dragViewSpanX;
2278 resultSpan[1] = finalSolution.dragViewSpanY;
2279
2280 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2281 // committing anything or animating anything as we just want to determine if a solution
2282 // exists
2283 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2284 if (!DESTRUCTIVE_REORDER) {
2285 copySolutionToTempState(finalSolution, dragView);
2286 }
2287 setItemPlacementDirty(true);
2288 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2289
Adam Cohen19f37922012-03-21 11:59:11 -07002290 if (!DESTRUCTIVE_REORDER &&
2291 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002292 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002293 completeAndClearReorderHintAnimations();
2294 setItemPlacementDirty(false);
2295 } else {
2296 beginOrAdjustHintAnimations(finalSolution, dragView,
2297 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002298 }
2299 }
2300 } else {
2301 foundSolution = false;
2302 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2303 }
2304
2305 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2306 setUseTempCoords(false);
2307 }
Adam Cohen482ed822012-03-02 14:15:13 -08002308
Michael Jurkaa52570f2012-03-20 03:18:20 -07002309 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002310 return result;
2311 }
2312
Adam Cohen19f37922012-03-21 11:59:11 -07002313 void setItemPlacementDirty(boolean dirty) {
2314 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002315 }
Adam Cohen19f37922012-03-21 11:59:11 -07002316 boolean isItemPlacementDirty() {
2317 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002318 }
2319
2320 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002321 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002322 boolean isSolution = false;
2323 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2324
2325 int area() {
2326 return dragViewSpanX * dragViewSpanY;
2327 }
Adam Cohen8baab352012-03-20 17:39:21 -07002328 }
2329
2330 private class CellAndSpan {
2331 int x, y;
2332 int spanX, spanY;
2333
2334 public CellAndSpan(int x, int y, int spanX, int spanY) {
2335 this.x = x;
2336 this.y = y;
2337 this.spanX = spanX;
2338 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002339 }
2340 }
2341
Adam Cohendf035382011-04-11 17:22:04 -07002342 /**
2343 * Find a vacant area that will fit the given bounds nearest the requested
2344 * cell location. Uses Euclidean distance to score multiple vacant areas.
2345 *
2346 * @param pixelX The X location at which you want to search for a vacant area.
2347 * @param pixelY The Y location at which you want to search for a vacant area.
2348 * @param spanX Horizontal span of the object.
2349 * @param spanY Vertical span of the object.
2350 * @param ignoreView Considers space occupied by this view as unoccupied
2351 * @param result Previously returned value to possibly recycle.
2352 * @return The X, Y cell of a vacant area that can contain this object,
2353 * nearest the requested location.
2354 */
2355 int[] findNearestVacantArea(
2356 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2357 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2358 }
2359
2360 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002361 * Find a vacant area that will fit the given bounds nearest the requested
2362 * cell location. Uses Euclidean distance to score multiple vacant areas.
2363 *
2364 * @param pixelX The X location at which you want to search for a vacant area.
2365 * @param pixelY The Y location at which you want to search for a vacant area.
2366 * @param minSpanX The minimum horizontal span required
2367 * @param minSpanY The minimum vertical span required
2368 * @param spanX Horizontal span of the object.
2369 * @param spanY Vertical span of the object.
2370 * @param ignoreView Considers space occupied by this view as unoccupied
2371 * @param result Previously returned value to possibly recycle.
2372 * @return The X, Y cell of a vacant area that can contain this object,
2373 * nearest the requested location.
2374 */
2375 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2376 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002377 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2378 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002379 }
2380
2381 /**
Adam Cohendf035382011-04-11 17:22:04 -07002382 * Find a starting cell position that will fit the given bounds nearest the requested
2383 * cell location. Uses Euclidean distance to score multiple vacant areas.
2384 *
2385 * @param pixelX The X location at which you want to search for a vacant area.
2386 * @param pixelY The Y location at which you want to search for a vacant area.
2387 * @param spanX Horizontal span of the object.
2388 * @param spanY Vertical span of the object.
2389 * @param ignoreView Considers space occupied by this view as unoccupied
2390 * @param result Previously returned value to possibly recycle.
2391 * @return The X, Y cell of a vacant area that can contain this object,
2392 * nearest the requested location.
2393 */
2394 int[] findNearestArea(
2395 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2396 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2397 }
2398
Michael Jurka0280c3b2010-09-17 15:00:07 -07002399 boolean existsEmptyCell() {
2400 return findCellForSpan(null, 1, 1);
2401 }
2402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002404 * Finds the upper-left coordinate of the first rectangle in the grid that can
2405 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2406 * then this method will only return coordinates for rectangles that contain the cell
2407 * (intersectX, intersectY)
2408 *
2409 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2410 * can be found.
2411 * @param spanX The horizontal span of the cell we want to find.
2412 * @param spanY The vertical span of the cell we want to find.
2413 *
2414 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002415 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002416 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002417 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002418 }
2419
2420 /**
2421 * Like above, but ignores any cells occupied by the item "ignoreView"
2422 *
2423 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2424 * can be found.
2425 * @param spanX The horizontal span of the cell we want to find.
2426 * @param spanY The vertical span of the cell we want to find.
2427 * @param ignoreView The home screen item we should treat as not occupying any space
2428 * @return
2429 */
2430 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002431 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2432 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002433 }
2434
2435 /**
2436 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2437 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2438 *
2439 * @param spanX The horizontal span of the cell we want to find.
2440 * @param spanY The vertical span of the cell we want to find.
2441 * @param ignoreView The home screen item we should treat as not occupying any space
2442 * @param intersectX The X coordinate of the cell that we should try to overlap
2443 * @param intersectX The Y coordinate of the cell that we should try to overlap
2444 *
2445 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2446 */
2447 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2448 int intersectX, int intersectY) {
2449 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002450 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002451 }
2452
2453 /**
2454 * The superset of the above two methods
2455 */
2456 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002457 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002458 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002459 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002460
Michael Jurka28750fb2010-09-24 17:43:49 -07002461 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002462 while (true) {
2463 int startX = 0;
2464 if (intersectX >= 0) {
2465 startX = Math.max(startX, intersectX - (spanX - 1));
2466 }
2467 int endX = mCountX - (spanX - 1);
2468 if (intersectX >= 0) {
2469 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2470 }
2471 int startY = 0;
2472 if (intersectY >= 0) {
2473 startY = Math.max(startY, intersectY - (spanY - 1));
2474 }
2475 int endY = mCountY - (spanY - 1);
2476 if (intersectY >= 0) {
2477 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2478 }
2479
Winson Chungbbc60d82010-11-11 16:34:41 -08002480 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002481 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002482 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002483 for (int i = 0; i < spanX; i++) {
2484 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002485 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002486 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002487 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002488 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002489 continue inner;
2490 }
2491 }
2492 }
2493 if (cellXY != null) {
2494 cellXY[0] = x;
2495 cellXY[1] = y;
2496 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002497 foundCell = true;
2498 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002499 }
2500 }
2501 if (intersectX == -1 && intersectY == -1) {
2502 break;
2503 } else {
2504 // if we failed to find anything, try again but without any requirements of
2505 // intersecting
2506 intersectX = -1;
2507 intersectY = -1;
2508 continue;
2509 }
2510 }
2511
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002512 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002513 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002514 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002515 }
2516
2517 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002518 * A drag event has begun over this layout.
2519 * It may have begun over this layout (in which case onDragChild is called first),
2520 * or it may have begun on another layout.
2521 */
2522 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002523 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002524 mDragging = true;
2525 }
2526
2527 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002528 * Called when drag has left this CellLayout or has been completed (successfully or not)
2529 */
2530 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002531 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002532 // This can actually be called when we aren't in a drag, e.g. when adding a new
2533 // item to this layout via the customize drawer.
2534 // Guard against that case.
2535 if (mDragging) {
2536 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002537 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002538
2539 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002540 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002541 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2542 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002543 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002544 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002545 }
2546
2547 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002548 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002549 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002550 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 *
2552 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002554 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002555 if (child != null) {
2556 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002557 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002558 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 }
2561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002562 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002564 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002565 * @param cellX X coordinate of upper left corner expressed as a cell position
2566 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002567 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002569 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002570 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002571 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002572 final int cellWidth = mCellWidth;
2573 final int cellHeight = mCellHeight;
2574 final int widthGap = mWidthGap;
2575 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002576
Winson Chung4b825dcd2011-06-19 12:41:22 -07002577 final int hStartPadding = getPaddingLeft();
2578 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002579
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2581 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2582
2583 int x = hStartPadding + cellX * (cellWidth + widthGap);
2584 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002585
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002586 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002588
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002590 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002592 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002593 * @param width Width in pixels
2594 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002595 * @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 -08002596 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002597 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002598 return rectToCell(getResources(), width, height, result);
2599 }
2600
2601 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602 // Always assume we're working with the smallest span to make sure we
2603 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002604 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2605 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002606 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002607
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002608 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002609 int spanX = (int) Math.ceil(width / (float) smallerSize);
2610 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002611
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002612 if (result == null) {
2613 return new int[] { spanX, spanY };
2614 }
2615 result[0] = spanX;
2616 result[1] = spanY;
2617 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002618 }
2619
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002620 public int[] cellSpansToSize(int hSpans, int vSpans) {
2621 int[] size = new int[2];
2622 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2623 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2624 return size;
2625 }
2626
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002627 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002628 * Calculate the grid spans needed to fit given item
2629 */
2630 public void calculateSpans(ItemInfo info) {
2631 final int minWidth;
2632 final int minHeight;
2633
2634 if (info instanceof LauncherAppWidgetInfo) {
2635 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2636 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2637 } else if (info instanceof PendingAddWidgetInfo) {
2638 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2639 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2640 } else {
2641 // It's not a widget, so it must be 1x1
2642 info.spanX = info.spanY = 1;
2643 return;
2644 }
2645 int[] spans = rectToCell(minWidth, minHeight, null);
2646 info.spanX = spans[0];
2647 info.spanY = spans[1];
2648 }
2649
2650 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002651 * Find the first vacant cell, if there is one.
2652 *
2653 * @param vacant Holds the x and y coordinate of the vacant cell
2654 * @param spanX Horizontal cell span.
2655 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002656 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002657 * @return True if a vacant cell was found
2658 */
2659 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002660
Michael Jurka0280c3b2010-09-17 15:00:07 -07002661 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002662 }
2663
2664 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2665 int xCount, int yCount, boolean[][] occupied) {
2666
Adam Cohen2801caf2011-05-13 20:57:39 -07002667 for (int y = 0; y < yCount; y++) {
2668 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002669 boolean available = !occupied[x][y];
2670out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2671 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2672 available = available && !occupied[i][j];
2673 if (!available) break out;
2674 }
2675 }
2676
2677 if (available) {
2678 vacant[0] = x;
2679 vacant[1] = y;
2680 return true;
2681 }
2682 }
2683 }
2684
2685 return false;
2686 }
2687
Michael Jurka0280c3b2010-09-17 15:00:07 -07002688 private void clearOccupiedCells() {
2689 for (int x = 0; x < mCountX; x++) {
2690 for (int y = 0; y < mCountY; y++) {
2691 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692 }
2693 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002695
Adam Cohend41fbf52012-02-16 23:53:59 -08002696 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002697 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002698 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002699 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002700
Adam Cohend4844c32011-02-18 19:25:06 -08002701 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002702 markCellsAsOccupiedForView(view, mOccupied);
2703 }
2704 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002705 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002706 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002707 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002708 }
2709
Adam Cohend4844c32011-02-18 19:25:06 -08002710 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002711 markCellsAsUnoccupiedForView(view, mOccupied);
2712 }
2713 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002714 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002715 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002716 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002717 }
2718
Adam Cohen482ed822012-03-02 14:15:13 -08002719 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2720 boolean value) {
2721 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002722 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2723 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002724 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002725 }
2726 }
2727 }
2728
Adam Cohen2801caf2011-05-13 20:57:39 -07002729 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002730 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002731 (Math.max((mCountX - 1), 0) * mWidthGap);
2732 }
2733
2734 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002735 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002736 (Math.max((mCountY - 1), 0) * mHeightGap);
2737 }
2738
Michael Jurka66d72172011-04-12 16:29:25 -07002739 public boolean isOccupied(int x, int y) {
2740 if (x < mCountX && y < mCountY) {
2741 return mOccupied[x][y];
2742 } else {
2743 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2744 }
2745 }
2746
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002747 @Override
2748 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2749 return new CellLayout.LayoutParams(getContext(), attrs);
2750 }
2751
2752 @Override
2753 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2754 return p instanceof CellLayout.LayoutParams;
2755 }
2756
2757 @Override
2758 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2759 return new CellLayout.LayoutParams(p);
2760 }
2761
Winson Chungaafa03c2010-06-11 17:34:16 -07002762 public static class CellLayoutAnimationController extends LayoutAnimationController {
2763 public CellLayoutAnimationController(Animation animation, float delay) {
2764 super(animation, delay);
2765 }
2766
2767 @Override
2768 protected long getDelayForView(View view) {
2769 return (int) (Math.random() * 150);
2770 }
2771 }
2772
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002773 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2774 /**
2775 * Horizontal location of the item in the grid.
2776 */
2777 @ViewDebug.ExportedProperty
2778 public int cellX;
2779
2780 /**
2781 * Vertical location of the item in the grid.
2782 */
2783 @ViewDebug.ExportedProperty
2784 public int cellY;
2785
2786 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002787 * Temporary horizontal location of the item in the grid during reorder
2788 */
2789 public int tmpCellX;
2790
2791 /**
2792 * Temporary vertical location of the item in the grid during reorder
2793 */
2794 public int tmpCellY;
2795
2796 /**
2797 * Indicates that the temporary coordinates should be used to layout the items
2798 */
2799 public boolean useTmpCoords;
2800
2801 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002802 * Number of cells spanned horizontally by the item.
2803 */
2804 @ViewDebug.ExportedProperty
2805 public int cellHSpan;
2806
2807 /**
2808 * Number of cells spanned vertically by the item.
2809 */
2810 @ViewDebug.ExportedProperty
2811 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002812
Adam Cohen1b607ed2011-03-03 17:26:50 -08002813 /**
2814 * Indicates whether the item will set its x, y, width and height parameters freely,
2815 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2816 */
Adam Cohend4844c32011-02-18 19:25:06 -08002817 public boolean isLockedToGrid = true;
2818
Adam Cohen482ed822012-03-02 14:15:13 -08002819 /**
2820 * Indicates whether this item can be reordered. Always true except in the case of the
2821 * the AllApps button.
2822 */
2823 public boolean canReorder = true;
2824
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002825 // X coordinate of the view in the layout.
2826 @ViewDebug.ExportedProperty
2827 int x;
2828 // Y coordinate of the view in the layout.
2829 @ViewDebug.ExportedProperty
2830 int y;
2831
Romain Guy84f296c2009-11-04 15:00:44 -08002832 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002833
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002834 public LayoutParams(Context c, AttributeSet attrs) {
2835 super(c, attrs);
2836 cellHSpan = 1;
2837 cellVSpan = 1;
2838 }
2839
2840 public LayoutParams(ViewGroup.LayoutParams source) {
2841 super(source);
2842 cellHSpan = 1;
2843 cellVSpan = 1;
2844 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002845
2846 public LayoutParams(LayoutParams source) {
2847 super(source);
2848 this.cellX = source.cellX;
2849 this.cellY = source.cellY;
2850 this.cellHSpan = source.cellHSpan;
2851 this.cellVSpan = source.cellVSpan;
2852 }
2853
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002854 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002855 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002856 this.cellX = cellX;
2857 this.cellY = cellY;
2858 this.cellHSpan = cellHSpan;
2859 this.cellVSpan = cellVSpan;
2860 }
2861
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002862 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002863 if (isLockedToGrid) {
2864 final int myCellHSpan = cellHSpan;
2865 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002866 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2867 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002868
Adam Cohend4844c32011-02-18 19:25:06 -08002869 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2870 leftMargin - rightMargin;
2871 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2872 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002873 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2874 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002875 }
2876 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002877
Winson Chungaafa03c2010-06-11 17:34:16 -07002878 public String toString() {
2879 return "(" + this.cellX + ", " + this.cellY + ")";
2880 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002881
2882 public void setWidth(int width) {
2883 this.width = width;
2884 }
2885
2886 public int getWidth() {
2887 return width;
2888 }
2889
2890 public void setHeight(int height) {
2891 this.height = height;
2892 }
2893
2894 public int getHeight() {
2895 return height;
2896 }
2897
2898 public void setX(int x) {
2899 this.x = x;
2900 }
2901
2902 public int getX() {
2903 return x;
2904 }
2905
2906 public void setY(int y) {
2907 this.y = y;
2908 }
2909
2910 public int getY() {
2911 return y;
2912 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002913 }
2914
Michael Jurka0280c3b2010-09-17 15:00:07 -07002915 // This class stores info for two purposes:
2916 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2917 // its spanX, spanY, and the screen it is on
2918 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2919 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2920 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002921 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002923 int cellX = -1;
2924 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002925 int spanX;
2926 int spanY;
2927 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002928 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002930 @Override
2931 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002932 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2933 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934 }
2935 }
Michael Jurkad771c962011-08-09 15:00:48 -07002936
2937 public boolean lastDownOnOccupiedCell() {
2938 return mLastDownOnOccupiedCell;
2939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002940}