blob: c7ad923104f1b68f6a2c09e63d865349f704c64a [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();
Adam Cohen19f37922012-03-21 11:59:11 -07001905 for (int i = 0; i < childCount; i++) {
1906 View child = mShortcutsAndWidgets.getChildAt(i);
1907 if (child == dragView) continue;
1908 CellAndSpan c = solution.map.get(child);
1909 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1910 if (c != null) {
1911 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
1912 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07001913 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001914 }
1915 }
1916 }
1917
1918 // Class which represents the reorder hint animations. These animations show that an item is
1919 // in a temporary state, and hint at where the item will return to.
1920 class ReorderHintAnimation {
1921 View child;
Adam Cohend024f982012-05-23 18:26:45 -07001922 float finalDeltaX;
1923 float finalDeltaY;
1924 float initDeltaX;
1925 float initDeltaY;
1926 float finalScale;
1927 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07001928 private static final int DURATION = 300;
Adam Cohen19f37922012-03-21 11:59:11 -07001929 ValueAnimator va;
1930
1931 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
1932 int spanX, int spanY) {
1933 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1934 final int x0 = mTmpPoint[0];
1935 final int y0 = mTmpPoint[1];
1936 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1937 final int x1 = mTmpPoint[0];
1938 final int y1 = mTmpPoint[1];
1939 final int dX = x1 - x0;
1940 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07001941 finalDeltaX = 0;
1942 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07001943 if (dX == dY && dX == 0) {
1944 } else {
1945 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07001946 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07001947 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07001948 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07001949 } else {
1950 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07001951 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07001952 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07001953 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07001954 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07001955 }
1956 }
Adam Cohend024f982012-05-23 18:26:45 -07001957 initDeltaX = child.getTranslationX();
1958 initDeltaY = child.getTranslationY();
1959 finalScale = 1.0f - 4.0f / child.getWidth();
1960 initScale = child.getScaleX();
1961
Brandon Keely50e6e562012-05-08 16:28:49 -07001962 child.setPivotY(child.getMeasuredHeight() * 0.5f);
1963 child.setPivotX(child.getMeasuredWidth() * 0.5f);
Adam Cohen19f37922012-03-21 11:59:11 -07001964 this.child = child;
1965 }
1966
Adam Cohend024f982012-05-23 18:26:45 -07001967 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07001968 if (mShakeAnimators.containsKey(child)) {
1969 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07001970 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07001971 mShakeAnimators.remove(child);
1972 }
Adam Cohend024f982012-05-23 18:26:45 -07001973 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07001974 return;
1975 }
1976 va = ValueAnimator.ofFloat(0f, 1f);
1977 va.setRepeatMode(ValueAnimator.REVERSE);
1978 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07001979 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07001980 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07001981 va.addUpdateListener(new AnimatorUpdateListener() {
1982 @Override
1983 public void onAnimationUpdate(ValueAnimator animation) {
1984 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07001985 float x = r * finalDeltaX + (1 - r) * initDeltaX;
1986 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07001987 child.setTranslationX(x);
1988 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07001989 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07001990 child.setScaleX(s);
1991 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07001992 }
1993 });
1994 va.addListener(new AnimatorListenerAdapter() {
1995 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07001996 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07001997 initDeltaX = 0;
1998 initDeltaY = 0;
1999 initScale = 1.0f;
Adam Cohen19f37922012-03-21 11:59:11 -07002000 }
2001 });
Adam Cohen19f37922012-03-21 11:59:11 -07002002 mShakeAnimators.put(child, this);
2003 va.start();
2004 }
2005
Adam Cohend024f982012-05-23 18:26:45 -07002006 private void cancel() {
2007 va.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002008 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002009 private void completeAnimationImmediately() {
2010 va.cancel();
2011
2012 AnimatorSet s = new AnimatorSet();
2013 s.playTogether(
2014 ObjectAnimator.ofFloat(child, "scaleX", 1f),
2015 ObjectAnimator.ofFloat(child, "scaleY", 1f),
2016 ObjectAnimator.ofFloat(child, "translationX", 0f),
Brandon Keelyfaf22e12012-05-10 16:48:50 -07002017 ObjectAnimator.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002018 );
2019 s.setDuration(REORDER_ANIMATION_DURATION);
2020 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2021 s.start();
2022 }
Adam Cohen19f37922012-03-21 11:59:11 -07002023 }
2024
2025 private void completeAndClearReorderHintAnimations() {
2026 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002027 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002028 }
2029 mShakeAnimators.clear();
2030 }
2031
Adam Cohen482ed822012-03-02 14:15:13 -08002032 private void commitTempPlacement() {
2033 for (int i = 0; i < mCountX; i++) {
2034 for (int j = 0; j < mCountY; j++) {
2035 mOccupied[i][j] = mTmpOccupied[i][j];
2036 }
2037 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002038 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002039 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002040 View child = mShortcutsAndWidgets.getChildAt(i);
2041 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2042 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002043 // We do a null check here because the item info can be null in the case of the
2044 // AllApps button in the hotseat.
2045 if (info != null) {
2046 info.cellX = lp.cellX = lp.tmpCellX;
2047 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002048 info.spanX = lp.cellHSpan;
2049 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002050 }
Adam Cohen482ed822012-03-02 14:15:13 -08002051 }
Adam Cohen2acce882012-03-28 19:03:19 -07002052 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002053 }
2054
2055 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002056 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002057 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002058 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002059 lp.useTmpCoords = useTempCoords;
2060 }
2061 }
2062
Adam Cohen482ed822012-03-02 14:15:13 -08002063 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2064 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2065 int[] result = new int[2];
2066 int[] resultSpan = new int[2];
2067 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2068 resultSpan);
2069 if (result[0] >= 0 && result[1] >= 0) {
2070 copyCurrentStateToSolution(solution, false);
2071 solution.dragViewX = result[0];
2072 solution.dragViewY = result[1];
2073 solution.dragViewSpanX = resultSpan[0];
2074 solution.dragViewSpanY = resultSpan[1];
2075 solution.isSolution = true;
2076 } else {
2077 solution.isSolution = false;
2078 }
2079 return solution;
2080 }
2081
2082 public void prepareChildForDrag(View child) {
2083 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002084 }
2085
Adam Cohen19f37922012-03-21 11:59:11 -07002086 /* This seems like it should be obvious and straight-forward, but when the direction vector
2087 needs to match with the notion of the dragView pushing other views, we have to employ
2088 a slightly more subtle notion of the direction vector. The question is what two points is
2089 the vector between? The center of the dragView and its desired destination? Not quite, as
2090 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2091 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2092 or right, which helps make pushing feel right.
2093 */
2094 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2095 int spanY, View dragView, int[] resultDirection) {
2096 int[] targetDestination = new int[2];
2097
2098 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2099 Rect dragRect = new Rect();
2100 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2101 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2102
2103 Rect dropRegionRect = new Rect();
2104 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2105 dragView, dropRegionRect, mIntersectingViews);
2106
2107 int dropRegionSpanX = dropRegionRect.width();
2108 int dropRegionSpanY = dropRegionRect.height();
2109
2110 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2111 dropRegionRect.height(), dropRegionRect);
2112
2113 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2114 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2115
2116 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2117 deltaX = 0;
2118 }
2119 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2120 deltaY = 0;
2121 }
2122
2123 if (deltaX == 0 && deltaY == 0) {
2124 // No idea what to do, give a random direction.
2125 resultDirection[0] = 1;
2126 resultDirection[1] = 0;
2127 } else {
2128 computeDirectionVector(deltaX, deltaY, resultDirection);
2129 }
2130 }
2131
2132 // For a given cell and span, fetch the set of views intersecting the region.
2133 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2134 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2135 if (boundingRect != null) {
2136 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2137 }
2138 intersectingViews.clear();
2139 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2140 Rect r1 = new Rect();
2141 final int count = mShortcutsAndWidgets.getChildCount();
2142 for (int i = 0; i < count; i++) {
2143 View child = mShortcutsAndWidgets.getChildAt(i);
2144 if (child == dragView) continue;
2145 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2146 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2147 if (Rect.intersects(r0, r1)) {
2148 mIntersectingViews.add(child);
2149 if (boundingRect != null) {
2150 boundingRect.union(r1);
2151 }
2152 }
2153 }
2154 }
2155
2156 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2157 View dragView, int[] result) {
2158 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2159 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2160 mIntersectingViews);
2161 return !mIntersectingViews.isEmpty();
2162 }
2163
2164 void revertTempState() {
2165 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2166 final int count = mShortcutsAndWidgets.getChildCount();
2167 for (int i = 0; i < count; i++) {
2168 View child = mShortcutsAndWidgets.getChildAt(i);
2169 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2170 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2171 lp.tmpCellX = lp.cellX;
2172 lp.tmpCellY = lp.cellY;
2173 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2174 0, false, false);
2175 }
2176 }
2177 completeAndClearReorderHintAnimations();
2178 setItemPlacementDirty(false);
2179 }
2180
Adam Cohenbebf0422012-04-11 18:06:28 -07002181 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2182 View dragView, int[] direction, boolean commit) {
2183 int[] pixelXY = new int[2];
2184 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2185
2186 // First we determine if things have moved enough to cause a different layout
2187 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2188 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2189
2190 setUseTempCoords(true);
2191 if (swapSolution != null && swapSolution.isSolution) {
2192 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2193 // committing anything or animating anything as we just want to determine if a solution
2194 // exists
2195 copySolutionToTempState(swapSolution, dragView);
2196 setItemPlacementDirty(true);
2197 animateItemsToSolution(swapSolution, dragView, commit);
2198
2199 if (commit) {
2200 commitTempPlacement();
2201 completeAndClearReorderHintAnimations();
2202 setItemPlacementDirty(false);
2203 } else {
2204 beginOrAdjustHintAnimations(swapSolution, dragView,
2205 REORDER_ANIMATION_DURATION);
2206 }
2207 mShortcutsAndWidgets.requestLayout();
2208 }
2209 return swapSolution.isSolution;
2210 }
2211
Adam Cohen482ed822012-03-02 14:15:13 -08002212 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2213 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002214 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002215 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002216
2217 if (resultSpan == null) {
2218 resultSpan = new int[2];
2219 }
2220
Adam Cohen19f37922012-03-21 11:59:11 -07002221 // When we are checking drop validity or actually dropping, we don't recompute the
2222 // direction vector, since we want the solution to match the preview, and it's possible
2223 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002224 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2225 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002226 mDirectionVector[0] = mPreviousReorderDirection[0];
2227 mDirectionVector[1] = mPreviousReorderDirection[1];
2228 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002229 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2230 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2231 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002232 }
2233 } else {
2234 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2235 mPreviousReorderDirection[0] = mDirectionVector[0];
2236 mPreviousReorderDirection[1] = mDirectionVector[1];
2237 }
2238
Adam Cohen482ed822012-03-02 14:15:13 -08002239 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2240 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2241
2242 // We attempt the approach which doesn't shuffle views at all
2243 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2244 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2245
2246 ItemConfiguration finalSolution = null;
2247 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2248 finalSolution = swapSolution;
2249 } else if (noShuffleSolution.isSolution) {
2250 finalSolution = noShuffleSolution;
2251 }
2252
2253 boolean foundSolution = true;
2254 if (!DESTRUCTIVE_REORDER) {
2255 setUseTempCoords(true);
2256 }
2257
2258 if (finalSolution != null) {
2259 result[0] = finalSolution.dragViewX;
2260 result[1] = finalSolution.dragViewY;
2261 resultSpan[0] = finalSolution.dragViewSpanX;
2262 resultSpan[1] = finalSolution.dragViewSpanY;
2263
2264 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2265 // committing anything or animating anything as we just want to determine if a solution
2266 // exists
2267 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2268 if (!DESTRUCTIVE_REORDER) {
2269 copySolutionToTempState(finalSolution, dragView);
2270 }
2271 setItemPlacementDirty(true);
2272 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2273
Adam Cohen19f37922012-03-21 11:59:11 -07002274 if (!DESTRUCTIVE_REORDER &&
2275 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002276 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002277 completeAndClearReorderHintAnimations();
2278 setItemPlacementDirty(false);
2279 } else {
2280 beginOrAdjustHintAnimations(finalSolution, dragView,
2281 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002282 }
2283 }
2284 } else {
2285 foundSolution = false;
2286 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2287 }
2288
2289 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2290 setUseTempCoords(false);
2291 }
Adam Cohen482ed822012-03-02 14:15:13 -08002292
Michael Jurkaa52570f2012-03-20 03:18:20 -07002293 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002294 return result;
2295 }
2296
Adam Cohen19f37922012-03-21 11:59:11 -07002297 void setItemPlacementDirty(boolean dirty) {
2298 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002299 }
Adam Cohen19f37922012-03-21 11:59:11 -07002300 boolean isItemPlacementDirty() {
2301 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002302 }
2303
2304 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002305 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002306 boolean isSolution = false;
2307 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2308
2309 int area() {
2310 return dragViewSpanX * dragViewSpanY;
2311 }
Adam Cohen8baab352012-03-20 17:39:21 -07002312 }
2313
2314 private class CellAndSpan {
2315 int x, y;
2316 int spanX, spanY;
2317
2318 public CellAndSpan(int x, int y, int spanX, int spanY) {
2319 this.x = x;
2320 this.y = y;
2321 this.spanX = spanX;
2322 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002323 }
2324 }
2325
Adam Cohendf035382011-04-11 17:22:04 -07002326 /**
2327 * Find a vacant area that will fit the given bounds nearest the requested
2328 * cell location. Uses Euclidean distance to score multiple vacant areas.
2329 *
2330 * @param pixelX The X location at which you want to search for a vacant area.
2331 * @param pixelY The Y location at which you want to search for a vacant area.
2332 * @param spanX Horizontal span of the object.
2333 * @param spanY Vertical span of the object.
2334 * @param ignoreView Considers space occupied by this view as unoccupied
2335 * @param result Previously returned value to possibly recycle.
2336 * @return The X, Y cell of a vacant area that can contain this object,
2337 * nearest the requested location.
2338 */
2339 int[] findNearestVacantArea(
2340 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2341 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2342 }
2343
2344 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002345 * Find a vacant area that will fit the given bounds nearest the requested
2346 * cell location. Uses Euclidean distance to score multiple vacant areas.
2347 *
2348 * @param pixelX The X location at which you want to search for a vacant area.
2349 * @param pixelY The Y location at which you want to search for a vacant area.
2350 * @param minSpanX The minimum horizontal span required
2351 * @param minSpanY The minimum vertical span required
2352 * @param spanX Horizontal span of the object.
2353 * @param spanY Vertical span of the object.
2354 * @param ignoreView Considers space occupied by this view as unoccupied
2355 * @param result Previously returned value to possibly recycle.
2356 * @return The X, Y cell of a vacant area that can contain this object,
2357 * nearest the requested location.
2358 */
2359 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2360 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002361 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2362 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002363 }
2364
2365 /**
Adam Cohendf035382011-04-11 17:22:04 -07002366 * Find a starting cell position that will fit the given bounds nearest the requested
2367 * cell location. Uses Euclidean distance to score multiple vacant areas.
2368 *
2369 * @param pixelX The X location at which you want to search for a vacant area.
2370 * @param pixelY The Y location at which you want to search for a vacant area.
2371 * @param spanX Horizontal span of the object.
2372 * @param spanY Vertical span of the object.
2373 * @param ignoreView Considers space occupied by this view as unoccupied
2374 * @param result Previously returned value to possibly recycle.
2375 * @return The X, Y cell of a vacant area that can contain this object,
2376 * nearest the requested location.
2377 */
2378 int[] findNearestArea(
2379 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2380 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2381 }
2382
Michael Jurka0280c3b2010-09-17 15:00:07 -07002383 boolean existsEmptyCell() {
2384 return findCellForSpan(null, 1, 1);
2385 }
2386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002387 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002388 * Finds the upper-left coordinate of the first rectangle in the grid that can
2389 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2390 * then this method will only return coordinates for rectangles that contain the cell
2391 * (intersectX, intersectY)
2392 *
2393 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2394 * can be found.
2395 * @param spanX The horizontal span of the cell we want to find.
2396 * @param spanY The vertical span of the cell we want to find.
2397 *
2398 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002399 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002400 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002401 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002402 }
2403
2404 /**
2405 * Like above, but ignores any cells occupied by the item "ignoreView"
2406 *
2407 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2408 * can be found.
2409 * @param spanX The horizontal span of the cell we want to find.
2410 * @param spanY The vertical span of the cell we want to find.
2411 * @param ignoreView The home screen item we should treat as not occupying any space
2412 * @return
2413 */
2414 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002415 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2416 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002417 }
2418
2419 /**
2420 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2421 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2422 *
2423 * @param spanX The horizontal span of the cell we want to find.
2424 * @param spanY The vertical span of the cell we want to find.
2425 * @param ignoreView The home screen item we should treat as not occupying any space
2426 * @param intersectX The X coordinate of the cell that we should try to overlap
2427 * @param intersectX The Y coordinate of the cell that we should try to overlap
2428 *
2429 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2430 */
2431 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2432 int intersectX, int intersectY) {
2433 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002434 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002435 }
2436
2437 /**
2438 * The superset of the above two methods
2439 */
2440 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002441 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002442 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002443 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002444
Michael Jurka28750fb2010-09-24 17:43:49 -07002445 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002446 while (true) {
2447 int startX = 0;
2448 if (intersectX >= 0) {
2449 startX = Math.max(startX, intersectX - (spanX - 1));
2450 }
2451 int endX = mCountX - (spanX - 1);
2452 if (intersectX >= 0) {
2453 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2454 }
2455 int startY = 0;
2456 if (intersectY >= 0) {
2457 startY = Math.max(startY, intersectY - (spanY - 1));
2458 }
2459 int endY = mCountY - (spanY - 1);
2460 if (intersectY >= 0) {
2461 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2462 }
2463
Winson Chungbbc60d82010-11-11 16:34:41 -08002464 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002465 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002466 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002467 for (int i = 0; i < spanX; i++) {
2468 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002469 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002470 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002471 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002472 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002473 continue inner;
2474 }
2475 }
2476 }
2477 if (cellXY != null) {
2478 cellXY[0] = x;
2479 cellXY[1] = y;
2480 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002481 foundCell = true;
2482 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002483 }
2484 }
2485 if (intersectX == -1 && intersectY == -1) {
2486 break;
2487 } else {
2488 // if we failed to find anything, try again but without any requirements of
2489 // intersecting
2490 intersectX = -1;
2491 intersectY = -1;
2492 continue;
2493 }
2494 }
2495
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002496 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002497 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002498 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002499 }
2500
2501 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002502 * A drag event has begun over this layout.
2503 * It may have begun over this layout (in which case onDragChild is called first),
2504 * or it may have begun on another layout.
2505 */
2506 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002507 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002508 mDragging = true;
2509 }
2510
2511 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002512 * Called when drag has left this CellLayout or has been completed (successfully or not)
2513 */
2514 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002515 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002516 // This can actually be called when we aren't in a drag, e.g. when adding a new
2517 // item to this layout via the customize drawer.
2518 // Guard against that case.
2519 if (mDragging) {
2520 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002521 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002522
2523 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002524 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002525 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2526 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002527 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002528 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002529 }
2530
2531 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002532 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002533 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002534 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 *
2536 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002538 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002539 if (child != null) {
2540 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002541 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002542 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002544 }
2545
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002546 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002548 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 * @param cellX X coordinate of upper left corner expressed as a cell position
2550 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002551 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002553 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002555 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 final int cellWidth = mCellWidth;
2557 final int cellHeight = mCellHeight;
2558 final int widthGap = mWidthGap;
2559 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002560
Winson Chung4b825dcd2011-06-19 12:41:22 -07002561 final int hStartPadding = getPaddingLeft();
2562 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002563
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2565 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2566
2567 int x = hStartPadding + cellX * (cellWidth + widthGap);
2568 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002569
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002570 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002571 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002572
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002573 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002574 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002575 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002576 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 * @param width Width in pixels
2578 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002579 * @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 -08002580 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002581 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002582 return rectToCell(getResources(), width, height, result);
2583 }
2584
2585 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002586 // Always assume we're working with the smallest span to make sure we
2587 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002588 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2589 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002590 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002593 int spanX = (int) Math.ceil(width / (float) smallerSize);
2594 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002595
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002596 if (result == null) {
2597 return new int[] { spanX, spanY };
2598 }
2599 result[0] = spanX;
2600 result[1] = spanY;
2601 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602 }
2603
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002604 public int[] cellSpansToSize(int hSpans, int vSpans) {
2605 int[] size = new int[2];
2606 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2607 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2608 return size;
2609 }
2610
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002611 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002612 * Calculate the grid spans needed to fit given item
2613 */
2614 public void calculateSpans(ItemInfo info) {
2615 final int minWidth;
2616 final int minHeight;
2617
2618 if (info instanceof LauncherAppWidgetInfo) {
2619 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2620 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2621 } else if (info instanceof PendingAddWidgetInfo) {
2622 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2623 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2624 } else {
2625 // It's not a widget, so it must be 1x1
2626 info.spanX = info.spanY = 1;
2627 return;
2628 }
2629 int[] spans = rectToCell(minWidth, minHeight, null);
2630 info.spanX = spans[0];
2631 info.spanY = spans[1];
2632 }
2633
2634 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002635 * Find the first vacant cell, if there is one.
2636 *
2637 * @param vacant Holds the x and y coordinate of the vacant cell
2638 * @param spanX Horizontal cell span.
2639 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002640 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002641 * @return True if a vacant cell was found
2642 */
2643 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002644
Michael Jurka0280c3b2010-09-17 15:00:07 -07002645 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002646 }
2647
2648 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2649 int xCount, int yCount, boolean[][] occupied) {
2650
Adam Cohen2801caf2011-05-13 20:57:39 -07002651 for (int y = 0; y < yCount; y++) {
2652 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002653 boolean available = !occupied[x][y];
2654out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2655 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2656 available = available && !occupied[i][j];
2657 if (!available) break out;
2658 }
2659 }
2660
2661 if (available) {
2662 vacant[0] = x;
2663 vacant[1] = y;
2664 return true;
2665 }
2666 }
2667 }
2668
2669 return false;
2670 }
2671
Michael Jurka0280c3b2010-09-17 15:00:07 -07002672 private void clearOccupiedCells() {
2673 for (int x = 0; x < mCountX; x++) {
2674 for (int y = 0; y < mCountY; y++) {
2675 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002676 }
2677 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679
Adam Cohend41fbf52012-02-16 23:53:59 -08002680 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002681 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002682 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002684
Adam Cohend4844c32011-02-18 19:25:06 -08002685 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002686 markCellsAsOccupiedForView(view, mOccupied);
2687 }
2688 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002689 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002690 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002691 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002692 }
2693
Adam Cohend4844c32011-02-18 19:25:06 -08002694 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002695 markCellsAsUnoccupiedForView(view, mOccupied);
2696 }
2697 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002698 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002699 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002700 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002701 }
2702
Adam Cohen482ed822012-03-02 14:15:13 -08002703 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2704 boolean value) {
2705 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002706 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2707 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002708 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002709 }
2710 }
2711 }
2712
Adam Cohen2801caf2011-05-13 20:57:39 -07002713 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002714 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002715 (Math.max((mCountX - 1), 0) * mWidthGap);
2716 }
2717
2718 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002719 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002720 (Math.max((mCountY - 1), 0) * mHeightGap);
2721 }
2722
Michael Jurka66d72172011-04-12 16:29:25 -07002723 public boolean isOccupied(int x, int y) {
2724 if (x < mCountX && y < mCountY) {
2725 return mOccupied[x][y];
2726 } else {
2727 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2728 }
2729 }
2730
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002731 @Override
2732 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2733 return new CellLayout.LayoutParams(getContext(), attrs);
2734 }
2735
2736 @Override
2737 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2738 return p instanceof CellLayout.LayoutParams;
2739 }
2740
2741 @Override
2742 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2743 return new CellLayout.LayoutParams(p);
2744 }
2745
Winson Chungaafa03c2010-06-11 17:34:16 -07002746 public static class CellLayoutAnimationController extends LayoutAnimationController {
2747 public CellLayoutAnimationController(Animation animation, float delay) {
2748 super(animation, delay);
2749 }
2750
2751 @Override
2752 protected long getDelayForView(View view) {
2753 return (int) (Math.random() * 150);
2754 }
2755 }
2756
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002757 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2758 /**
2759 * Horizontal location of the item in the grid.
2760 */
2761 @ViewDebug.ExportedProperty
2762 public int cellX;
2763
2764 /**
2765 * Vertical location of the item in the grid.
2766 */
2767 @ViewDebug.ExportedProperty
2768 public int cellY;
2769
2770 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002771 * Temporary horizontal location of the item in the grid during reorder
2772 */
2773 public int tmpCellX;
2774
2775 /**
2776 * Temporary vertical location of the item in the grid during reorder
2777 */
2778 public int tmpCellY;
2779
2780 /**
2781 * Indicates that the temporary coordinates should be used to layout the items
2782 */
2783 public boolean useTmpCoords;
2784
2785 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002786 * Number of cells spanned horizontally by the item.
2787 */
2788 @ViewDebug.ExportedProperty
2789 public int cellHSpan;
2790
2791 /**
2792 * Number of cells spanned vertically by the item.
2793 */
2794 @ViewDebug.ExportedProperty
2795 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002796
Adam Cohen1b607ed2011-03-03 17:26:50 -08002797 /**
2798 * Indicates whether the item will set its x, y, width and height parameters freely,
2799 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2800 */
Adam Cohend4844c32011-02-18 19:25:06 -08002801 public boolean isLockedToGrid = true;
2802
Adam Cohen482ed822012-03-02 14:15:13 -08002803 /**
2804 * Indicates whether this item can be reordered. Always true except in the case of the
2805 * the AllApps button.
2806 */
2807 public boolean canReorder = true;
2808
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002809 // X coordinate of the view in the layout.
2810 @ViewDebug.ExportedProperty
2811 int x;
2812 // Y coordinate of the view in the layout.
2813 @ViewDebug.ExportedProperty
2814 int y;
2815
Romain Guy84f296c2009-11-04 15:00:44 -08002816 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002817
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002818 public LayoutParams(Context c, AttributeSet attrs) {
2819 super(c, attrs);
2820 cellHSpan = 1;
2821 cellVSpan = 1;
2822 }
2823
2824 public LayoutParams(ViewGroup.LayoutParams source) {
2825 super(source);
2826 cellHSpan = 1;
2827 cellVSpan = 1;
2828 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002829
2830 public LayoutParams(LayoutParams source) {
2831 super(source);
2832 this.cellX = source.cellX;
2833 this.cellY = source.cellY;
2834 this.cellHSpan = source.cellHSpan;
2835 this.cellVSpan = source.cellVSpan;
2836 }
2837
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002839 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840 this.cellX = cellX;
2841 this.cellY = cellY;
2842 this.cellHSpan = cellHSpan;
2843 this.cellVSpan = cellVSpan;
2844 }
2845
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002846 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002847 if (isLockedToGrid) {
2848 final int myCellHSpan = cellHSpan;
2849 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002850 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2851 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002852
Adam Cohend4844c32011-02-18 19:25:06 -08002853 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2854 leftMargin - rightMargin;
2855 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2856 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002857 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2858 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002859 }
2860 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002861
Winson Chungaafa03c2010-06-11 17:34:16 -07002862 public String toString() {
2863 return "(" + this.cellX + ", " + this.cellY + ")";
2864 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002865
2866 public void setWidth(int width) {
2867 this.width = width;
2868 }
2869
2870 public int getWidth() {
2871 return width;
2872 }
2873
2874 public void setHeight(int height) {
2875 this.height = height;
2876 }
2877
2878 public int getHeight() {
2879 return height;
2880 }
2881
2882 public void setX(int x) {
2883 this.x = x;
2884 }
2885
2886 public int getX() {
2887 return x;
2888 }
2889
2890 public void setY(int y) {
2891 this.y = y;
2892 }
2893
2894 public int getY() {
2895 return y;
2896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002897 }
2898
Michael Jurka0280c3b2010-09-17 15:00:07 -07002899 // This class stores info for two purposes:
2900 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2901 // its spanX, spanY, and the screen it is on
2902 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2903 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2904 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002905 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002906 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002907 int cellX = -1;
2908 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002909 int spanX;
2910 int spanY;
2911 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002912 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002914 @Override
2915 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002916 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2917 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002918 }
2919 }
Michael Jurkad771c962011-08-09 15:00:48 -07002920
2921 public boolean lastDownOnOccupiedCell() {
2922 return mLastDownOnOccupiedCell;
2923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002924}