blob: e4b5af3b52a7e2cdf7330f8e795b671e7918e08c [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];
83 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070084 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070085
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080087 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070088 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Michael Jurkadee05892010-07-27 10:01:56 -070090 private OnTouchListener mInterceptTouchListener;
91
Adam Cohen69ce2e52011-07-03 19:25:21 -070092 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070093 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070094
Adam Cohenb5ba0972011-09-07 18:02:31 -070095 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070096 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070097 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070098
Michael Jurka33945b22010-12-21 18:19:38 -080099 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -0800100 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700101 private Drawable mOverScrollForegroundDrawable;
102 private Drawable mOverScrollLeft;
103 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700104 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700105 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700106 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700107
Michael Jurka33945b22010-12-21 18:19:38 -0800108 // If we're actively dragging something over this screen, mIsDragOverlapping is true
109 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700110 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700111
Winson Chung150fbab2010-09-29 17:14:26 -0700112 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700113 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800114 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700115 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700116 private InterruptibleInOutAnimator[] mDragOutlineAnims =
117 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700118
119 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700120 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700121 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700122
Patrick Dubroy96864c32011-03-10 17:17:23 -0800123 private BubbleTextView mPressedOrFocusedIcon;
124
Adam Cohen482ed822012-03-02 14:15:13 -0800125 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
126 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700127 private HashMap<View, ReorderHintAnimation>
128 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
129
130 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700131
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700132 // When a drag operation is in progress, holds the nearest cell to the touch point
133 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Joe Onorato4be866d2010-10-10 11:26:02 -0700135 private boolean mDragging = false;
136
Patrick Dubroyce34a972010-10-19 10:34:32 -0700137 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700138 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700139
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800140 private boolean mIsHotseat = false;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800141
Adam Cohen482ed822012-03-02 14:15:13 -0800142 public static final int MODE_DRAG_OVER = 0;
143 public static final int MODE_ON_DROP = 1;
144 public static final int MODE_ON_DROP_EXTERNAL = 2;
145 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700146 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800147 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
148
Adam Cohena897f392012-04-27 18:12:05 -0700149 static final int LANDSCAPE = 0;
150 static final int PORTRAIT = 1;
151
Brandon Keely50e6e562012-05-08 16:28:49 -0700152 private static final float REORDER_HINT_MAGNITUDE = 0.10f;
Adam Cohen19f37922012-03-21 11:59:11 -0700153 private static final int REORDER_ANIMATION_DURATION = 150;
154 private float mReorderHintAnimationMagnitude;
155
Adam Cohen482ed822012-03-02 14:15:13 -0800156 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
157 private Rect mOccupiedRect = new Rect();
158 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700159 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700160 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700161 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800162
Romain Guy8a0bff52012-05-06 13:14:33 -0700163 private final static PorterDuffXfermode sAddBlendMode =
164 new PorterDuffXfermode(PorterDuff.Mode.ADD);
165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 public CellLayout(Context context) {
167 this(context, null);
168 }
169
170 public CellLayout(Context context, AttributeSet attrs) {
171 this(context, attrs, 0);
172 }
173
174 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
175 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700176 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700177
178 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
179 // the user where a dragged item will land when dropped.
180 setWillNotDraw(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700181 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
184
Adam Cohenf4bd5792012-04-27 11:35:29 -0700185 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
186 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700187 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
188 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700189 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700190 mCountX = LauncherModel.getCellCountX();
191 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700192 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800193 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700194 mPreviousReorderDirection[0] = INVALID_DIRECTION;
195 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
197 a.recycle();
198
199 setAlwaysDrawnWithCacheEnabled(false);
200
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700201 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700202
Winson Chung967289b2011-06-30 18:09:30 -0700203 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700204 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800205
Adam Cohenb5ba0972011-09-07 18:02:31 -0700206 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
207 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
208 mForegroundPadding =
209 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800210
Adam Cohen19f37922012-03-21 11:59:11 -0700211 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
212 res.getDimensionPixelSize(R.dimen.app_icon_size));
213
Winson Chungb26f3d62011-06-02 10:49:29 -0700214 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700215 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700216
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700217 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700218
Patrick Dubroyce34a972010-10-19 10:34:32 -0700219 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700220
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221
Winson Chungb8c69f32011-10-19 21:36:08 -0700222 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700223 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800224 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700225 }
226
227 // When dragging things around the home screens, we show a green outline of
228 // where the item will land. The outlines gradually fade out, leaving a trail
229 // behind the drag path.
230 // Set up all the animations that are used to implement this fading.
231 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700232 final float fromAlphaValue = 0;
233 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700234
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700235 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700236
237 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700238 final InterruptibleInOutAnimator anim =
239 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700240 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700241 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700242 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700243 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700244 final Bitmap outline = (Bitmap)anim.getTag();
245
246 // If an animation is started and then stopped very quickly, we can still
247 // get spurious updates we've cleared the tag. Guard against this.
248 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700249 @SuppressWarnings("all") // suppress dead code warning
250 final boolean debug = false;
251 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700252 Object val = animation.getAnimatedValue();
253 Log.d(TAG, "anim " + thisIndex + " update: " + val +
254 ", isStopped " + anim.isStopped());
255 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700256 // Try to prevent it from continuing to run
257 animation.cancel();
258 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700259 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800260 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700261 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700262 }
263 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700264 // The animation holds a reference to the drag outline bitmap as long is it's
265 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700266 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700267 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700269 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700270 anim.setTag(null);
271 }
272 }
273 });
274 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700275 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700276
Michael Jurka18014792010-10-14 09:01:34 -0700277 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700278 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800279
Michael Jurkaa52570f2012-03-20 03:18:20 -0700280 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
281 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
282 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700283 }
284
Michael Jurkaf6440da2011-04-05 14:50:34 -0700285 static int widthInPortrait(Resources r, int numCells) {
286 // We use this method from Workspace to figure out how many rows/columns Launcher should
287 // have. We ignore the left/right padding on CellLayout because it turns out in our design
288 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700289 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700290 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
291 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700292
Winson Chung4b825dcd2011-06-19 12:41:22 -0700293 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700294 }
295
Michael Jurkaf6440da2011-04-05 14:50:34 -0700296 static int heightInLandscape(Resources r, int numCells) {
297 // We use this method from Workspace to figure out how many rows/columns Launcher should
298 // have. We ignore the left/right padding on CellLayout because it turns out in our design
299 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700300 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700301 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
302 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700303
Winson Chung4b825dcd2011-06-19 12:41:22 -0700304 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700305 }
306
Adam Cohen2801caf2011-05-13 20:57:39 -0700307 public void enableHardwareLayers() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700308 mShortcutsAndWidgets.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700309 }
310
311 public void setGridSize(int x, int y) {
312 mCountX = x;
313 mCountY = y;
314 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800315 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700316 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700317 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700318 }
319
Patrick Dubroy96864c32011-03-10 17:17:23 -0800320 private void invalidateBubbleTextView(BubbleTextView icon) {
321 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700322 invalidate(icon.getLeft() + getPaddingLeft() - padding,
323 icon.getTop() + getPaddingTop() - padding,
324 icon.getRight() + getPaddingLeft() + padding,
325 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800326 }
327
Adam Cohenb5ba0972011-09-07 18:02:31 -0700328 void setOverScrollAmount(float r, boolean left) {
329 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
330 mOverScrollForegroundDrawable = mOverScrollLeft;
331 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
332 mOverScrollForegroundDrawable = mOverScrollRight;
333 }
334
335 mForegroundAlpha = (int) Math.round((r * 255));
336 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
337 invalidate();
338 }
339
Patrick Dubroy96864c32011-03-10 17:17:23 -0800340 void setPressedOrFocusedIcon(BubbleTextView icon) {
341 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
342 // requires an expanded clip rect (due to the glow's blur radius)
343 BubbleTextView oldIcon = mPressedOrFocusedIcon;
344 mPressedOrFocusedIcon = icon;
345 if (oldIcon != null) {
346 invalidateBubbleTextView(oldIcon);
347 }
348 if (mPressedOrFocusedIcon != null) {
349 invalidateBubbleTextView(mPressedOrFocusedIcon);
350 }
351 }
352
Michael Jurka33945b22010-12-21 18:19:38 -0800353 void setIsDragOverlapping(boolean isDragOverlapping) {
354 if (mIsDragOverlapping != isDragOverlapping) {
355 mIsDragOverlapping = isDragOverlapping;
356 invalidate();
357 }
358 }
359
360 boolean getIsDragOverlapping() {
361 return mIsDragOverlapping;
362 }
363
Adam Cohenebea84d2011-11-09 17:20:41 -0800364 protected void setOverscrollTransformsDirty(boolean dirty) {
365 mScrollingTransformsDirty = dirty;
366 }
367
368 protected void resetOverscrollTransforms() {
369 if (mScrollingTransformsDirty) {
370 setOverscrollTransformsDirty(false);
371 setTranslationX(0);
372 setRotationY(0);
373 // It doesn't matter if we pass true or false here, the important thing is that we
374 // pass 0, which results in the overscroll drawable not being drawn any more.
375 setOverScrollAmount(0, false);
376 setPivotX(getMeasuredWidth() / 2);
377 setPivotY(getMeasuredHeight() / 2);
378 }
379 }
380
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700381 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700382 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700383 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
384 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
385 // When we're small, we are either drawn normally or in the "accepts drops" state (during
386 // a drag). However, we also drag the mini hover background *over* one of those two
387 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700388 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700389 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800390
391 if (mIsDragOverlapping) {
392 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700393 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700394 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700395 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700396 }
Michael Jurka33945b22010-12-21 18:19:38 -0800397
398 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
399 bg.setBounds(mBackgroundRect);
400 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700401 }
Romain Guya6abce82009-11-10 02:54:41 -0800402
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700403 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700404 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700405 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700406 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800407 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700408 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700409 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800410 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700411 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700412 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800413
414 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
415 // requires an expanded clip rect (due to the glow's blur radius)
416 if (mPressedOrFocusedIcon != null) {
417 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
418 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
419 if (b != null) {
420 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700421 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
422 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800423 null);
424 }
425 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700426
Adam Cohen482ed822012-03-02 14:15:13 -0800427 if (DEBUG_VISUALIZE_OCCUPIED) {
428 int[] pt = new int[2];
429 ColorDrawable cd = new ColorDrawable(Color.RED);
430 cd.setBounds(0, 0, 80, 80);
431 for (int i = 0; i < mCountX; i++) {
432 for (int j = 0; j < mCountY; j++) {
433 if (mOccupied[i][j]) {
434 cellToPoint(i, j, pt);
435 canvas.save();
436 canvas.translate(pt[0], pt[1]);
437 cd.draw(canvas);
438 canvas.restore();
439 }
440 }
441 }
442 }
443
Andrew Flynn850d2e72012-04-26 16:51:20 -0700444 int previewOffset = FolderRingAnimator.sPreviewSize;
445
Adam Cohen69ce2e52011-07-03 19:25:21 -0700446 // The folder outer / inner ring image(s)
447 for (int i = 0; i < mFolderOuterRings.size(); i++) {
448 FolderRingAnimator fra = mFolderOuterRings.get(i);
449
450 // Draw outer ring
451 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
452 int width = (int) fra.getOuterRingSize();
453 int height = width;
454 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
455
456 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700457 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700458
459 canvas.save();
460 canvas.translate(centerX - width / 2, centerY - height / 2);
461 d.setBounds(0, 0, width, height);
462 d.draw(canvas);
463 canvas.restore();
464
465 // Draw inner ring
466 d = FolderRingAnimator.sSharedInnerRingDrawable;
467 width = (int) fra.getInnerRingSize();
468 height = width;
469 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
470
471 centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700472 centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700473 canvas.save();
474 canvas.translate(centerX - width / 2, centerY - width / 2);
475 d.setBounds(0, 0, width, height);
476 d.draw(canvas);
477 canvas.restore();
478 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700479
480 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
481 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
482 int width = d.getIntrinsicWidth();
483 int height = d.getIntrinsicHeight();
484
485 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
486 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700487 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohenc51934b2011-07-26 21:07:43 -0700488
489 canvas.save();
490 canvas.translate(centerX - width / 2, centerY - width / 2);
491 d.setBounds(0, 0, width, height);
492 d.draw(canvas);
493 canvas.restore();
494 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700495 }
496
Adam Cohenb5ba0972011-09-07 18:02:31 -0700497 @Override
498 protected void dispatchDraw(Canvas canvas) {
499 super.dispatchDraw(canvas);
500 if (mForegroundAlpha > 0) {
501 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
502 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700503 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700504 mOverScrollForegroundDrawable.draw(canvas);
505 p.setXfermode(null);
506 }
507 }
508
Adam Cohen69ce2e52011-07-03 19:25:21 -0700509 public void showFolderAccept(FolderRingAnimator fra) {
510 mFolderOuterRings.add(fra);
511 }
512
513 public void hideFolderAccept(FolderRingAnimator fra) {
514 if (mFolderOuterRings.contains(fra)) {
515 mFolderOuterRings.remove(fra);
516 }
517 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700518 }
519
Adam Cohenc51934b2011-07-26 21:07:43 -0700520 public void setFolderLeaveBehindCell(int x, int y) {
521 mFolderLeaveBehindCell[0] = x;
522 mFolderLeaveBehindCell[1] = y;
523 invalidate();
524 }
525
526 public void clearFolderLeaveBehind() {
527 mFolderLeaveBehindCell[0] = -1;
528 mFolderLeaveBehindCell[1] = -1;
529 invalidate();
530 }
531
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700532 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700533 public boolean shouldDelayChildPressedState() {
534 return false;
535 }
536
537 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700538 public void cancelLongPress() {
539 super.cancelLongPress();
540
541 // Cancel long press for all children
542 final int count = getChildCount();
543 for (int i = 0; i < count; i++) {
544 final View child = getChildAt(i);
545 child.cancelLongPress();
546 }
547 }
548
Michael Jurkadee05892010-07-27 10:01:56 -0700549 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
550 mInterceptTouchListener = listener;
551 }
552
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700554 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 }
556
557 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700558 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 }
560
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800561 public void setIsHotseat(boolean isHotseat) {
562 mIsHotseat = isHotseat;
563 }
564
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800565 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700566 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700567 final LayoutParams lp = params;
568
Andrew Flynnde38e422012-05-08 11:22:15 -0700569 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800570 if (child instanceof BubbleTextView) {
571 BubbleTextView bubbleChild = (BubbleTextView) child;
572
Andrew Flynnde38e422012-05-08 11:22:15 -0700573 Resources res = getResources();
574 if (mIsHotseat) {
575 bubbleChild.setTextColor(res.getColor(android.R.color.transparent));
576 } else {
577 bubbleChild.setTextColor(res.getColor(R.color.workspace_icon_text_color));
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800578 }
579 }
580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 // Generate an id for each view, this assumes we have at most 256x256 cells
582 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700583 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700584 // If the horizontal or vertical span is set to -1, it is taken to
585 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700586 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
587 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588
Winson Chungaafa03c2010-06-11 17:34:16 -0700589 child.setId(childId);
590
Michael Jurkaa52570f2012-03-20 03:18:20 -0700591 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700592
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700593 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700594
Winson Chungaafa03c2010-06-11 17:34:16 -0700595 return true;
596 }
597 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700599
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700601 public void removeAllViews() {
602 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700603 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700604 }
605
606 @Override
607 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700608 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700609 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700610 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700611 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700612 }
613
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700614 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700615 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700616 }
617
Michael Jurka0280c3b2010-09-17 15:00:07 -0700618 @Override
619 public void removeView(View view) {
620 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700621 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700622 }
623
624 @Override
625 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700626 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
627 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700628 }
629
630 @Override
631 public void removeViewInLayout(View view) {
632 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700633 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700634 }
635
636 @Override
637 public void removeViews(int start, int count) {
638 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700639 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700640 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700641 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700642 }
643
644 @Override
645 public void removeViewsInLayout(int start, int count) {
646 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700647 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700648 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700649 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800650 }
651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 @Override
653 protected void onAttachedToWindow() {
654 super.onAttachedToWindow();
655 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
656 }
657
Michael Jurkaaf442092010-06-10 17:01:57 -0700658 public void setTagToCellInfoForPoint(int touchX, int touchY) {
659 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800660 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700661 final int x = touchX + getScrollX();
662 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700663 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700664
665 boolean found = false;
666 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700667 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800668 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700669
Adam Cohen1b607ed2011-03-03 17:26:50 -0800670 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
671 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700672 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700673
Winson Chungeecf02d2012-03-02 17:14:58 -0800674 float scale = child.getScaleX();
675 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
676 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700677 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
678 // offset that by this CellLayout's padding to test an (x,y) point that is relative
679 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700680 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800681 frame.inset((int) (frame.width() * (1f - scale) / 2),
682 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700683
Michael Jurkaaf442092010-06-10 17:01:57 -0700684 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700685 cellInfo.cell = child;
686 cellInfo.cellX = lp.cellX;
687 cellInfo.cellY = lp.cellY;
688 cellInfo.spanX = lp.cellHSpan;
689 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700690 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700691 break;
692 }
693 }
694 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700695
Michael Jurkad771c962011-08-09 15:00:48 -0700696 mLastDownOnOccupiedCell = found;
697
Michael Jurkaaf442092010-06-10 17:01:57 -0700698 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700699 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700700 pointToCellExact(x, y, cellXY);
701
Michael Jurkaaf442092010-06-10 17:01:57 -0700702 cellInfo.cell = null;
703 cellInfo.cellX = cellXY[0];
704 cellInfo.cellY = cellXY[1];
705 cellInfo.spanX = 1;
706 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700707 }
708 setTag(cellInfo);
709 }
710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 @Override
712 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700713 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
714 // even in the case where we return early. Not clearing here was causing bugs whereby on
715 // long-press we'd end up picking up an item from a previous drag operation.
716 final int action = ev.getAction();
717
718 if (action == MotionEvent.ACTION_DOWN) {
719 clearTagCellInfo();
720 }
721
Michael Jurkadee05892010-07-27 10:01:56 -0700722 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
723 return true;
724 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725
726 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700727 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800729
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 return false;
731 }
732
Adam Cohenc1997fd2011-08-15 18:26:39 -0700733 private void clearTagCellInfo() {
734 final CellInfo cellInfo = mCellInfo;
735 cellInfo.cell = null;
736 cellInfo.cellX = -1;
737 cellInfo.cellY = -1;
738 cellInfo.spanX = 0;
739 cellInfo.spanY = 0;
740 setTag(cellInfo);
741 }
742
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700744 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 }
746
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700747 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700748 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 * @param x X coordinate of the point
750 * @param y Y coordinate of the point
751 * @param result Array of 2 ints to hold the x and y coordinate of the cell
752 */
753 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700754 final int hStartPadding = getPaddingLeft();
755 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756
757 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
758 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
759
Adam Cohend22015c2010-07-26 22:02:18 -0700760 final int xAxis = mCountX;
761 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762
763 if (result[0] < 0) result[0] = 0;
764 if (result[0] >= xAxis) result[0] = xAxis - 1;
765 if (result[1] < 0) result[1] = 0;
766 if (result[1] >= yAxis) result[1] = yAxis - 1;
767 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700768
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 /**
770 * Given a point, return the cell that most closely encloses that point
771 * @param x X coordinate of the point
772 * @param y Y coordinate of the point
773 * @param result Array of 2 ints to hold the x and y coordinate of the cell
774 */
775 void pointToCellRounded(int x, int y, int[] result) {
776 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
777 }
778
779 /**
780 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700781 *
782 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700784 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 * @param result Array of 2 ints to hold the x and y coordinate of the point
786 */
787 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700788 final int hStartPadding = getPaddingLeft();
789 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790
791 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
792 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
793 }
794
Adam Cohene3e27a82011-04-15 12:07:39 -0700795 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800796 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700797 *
798 * @param cellX X coordinate of the cell
799 * @param cellY Y coordinate of the cell
800 *
801 * @param result Array of 2 ints to hold the x and y coordinate of the point
802 */
803 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700804 regionToCenterPoint(cellX, cellY, 1, 1, result);
805 }
806
807 /**
808 * Given a cell coordinate and span return the point that represents the center of the regio
809 *
810 * @param cellX X coordinate of the cell
811 * @param cellY Y coordinate of the cell
812 *
813 * @param result Array of 2 ints to hold the x and y coordinate of the point
814 */
815 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700816 final int hStartPadding = getPaddingLeft();
817 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700818 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
819 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
820 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
821 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700822 }
823
Adam Cohen19f37922012-03-21 11:59:11 -0700824 /**
825 * Given a cell coordinate and span fills out a corresponding pixel rect
826 *
827 * @param cellX X coordinate of the cell
828 * @param cellY Y coordinate of the cell
829 * @param result Rect in which to write the result
830 */
831 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
832 final int hStartPadding = getPaddingLeft();
833 final int vStartPadding = getPaddingTop();
834 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
835 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
836 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
837 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
838 }
839
Adam Cohen482ed822012-03-02 14:15:13 -0800840 public float getDistanceFromCell(float x, float y, int[] cell) {
841 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
842 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
843 Math.pow(y - mTmpPoint[1], 2));
844 return distance;
845 }
846
Romain Guy84f296c2009-11-04 15:00:44 -0800847 int getCellWidth() {
848 return mCellWidth;
849 }
850
851 int getCellHeight() {
852 return mCellHeight;
853 }
854
Adam Cohend4844c32011-02-18 19:25:06 -0800855 int getWidthGap() {
856 return mWidthGap;
857 }
858
859 int getHeightGap() {
860 return mHeightGap;
861 }
862
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700863 Rect getContentRect(Rect r) {
864 if (r == null) {
865 r = new Rect();
866 }
867 int left = getPaddingLeft();
868 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700869 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
870 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700871 r.set(left, top, right, bottom);
872 return r;
873 }
874
Adam Cohena897f392012-04-27 18:12:05 -0700875 static void getMetrics(Rect metrics, Resources res, int measureWidth, int measureHeight,
876 int countX, int countY, int orientation) {
877 int numWidthGaps = countX - 1;
878 int numHeightGaps = countY - 1;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700879
880 int widthGap;
881 int heightGap;
882 int cellWidth;
883 int cellHeight;
884 int paddingLeft;
885 int paddingRight;
886 int paddingTop;
887 int paddingBottom;
888
Adam Cohena897f392012-04-27 18:12:05 -0700889 int maxGap = res.getDimensionPixelSize(R.dimen.workspace_max_gap);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700890 if (orientation == LANDSCAPE) {
891 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_land);
892 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_land);
893 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_land);
894 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
895 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_land);
896 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_land);
897 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_land);
898 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_land);
899 } else {
900 // PORTRAIT
901 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_port);
902 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_port);
903 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
904 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_port);
905 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_port);
906 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_port);
907 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_port);
908 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_port);
909 }
910
911 if (widthGap < 0 || heightGap < 0) {
912 int hSpace = measureWidth - paddingLeft - paddingRight;
913 int vSpace = measureHeight - paddingTop - paddingBottom;
Adam Cohena897f392012-04-27 18:12:05 -0700914 int hFreeSpace = hSpace - (countX * cellWidth);
915 int vFreeSpace = vSpace - (countY * cellHeight);
916 widthGap = Math.min(maxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
917 heightGap = Math.min(maxGap, numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700918 }
919 metrics.set(cellWidth, cellHeight, widthGap, heightGap);
920 }
921
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 @Override
923 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700925 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
926
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
928 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700929
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
931 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
932 }
933
Adam Cohend22015c2010-07-26 22:02:18 -0700934 int numWidthGaps = mCountX - 1;
935 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936
Adam Cohen234c4cd2011-07-17 21:03:04 -0700937 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Michael Jurkadd13e3d2012-05-01 12:38:17 -0700938 int hSpace = widthSpecSize - getPaddingLeft() - getPaddingRight();
939 int vSpace = heightSpecSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700940 int hFreeSpace = hSpace - (mCountX * mCellWidth);
941 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700942 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
943 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700944 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700945 } else {
946 mWidthGap = mOriginalWidthGap;
947 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700948 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700949
Michael Jurka8c920dd2011-01-20 14:16:56 -0800950 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
951 int newWidth = widthSpecSize;
952 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700953 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700954 newWidth = getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700955 ((mCountX - 1) * mWidthGap);
Michael Jurka8b805b12012-04-18 14:23:14 -0700956 newHeight = getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700957 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700958 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700959 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800960
961 int count = getChildCount();
962 for (int i = 0; i < count; i++) {
963 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700964 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
965 getPaddingRight(), MeasureSpec.EXACTLY);
966 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
967 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800968 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
969 }
970 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 }
972
973 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700974 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800977 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700978 child.layout(getPaddingLeft(), getPaddingTop(),
979 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 }
981 }
982
983 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700984 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
985 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700986 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700987 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
988 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -0700989 }
990
991 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700993 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 }
995
996 @Override
997 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700998 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
1000
Michael Jurka5f1c5092010-09-03 14:15:02 -07001001 public float getBackgroundAlpha() {
1002 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001003 }
1004
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001005 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001006 if (mBackgroundAlphaMultiplier != multiplier) {
1007 mBackgroundAlphaMultiplier = multiplier;
1008 invalidate();
1009 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001010 }
1011
Adam Cohenddb82192010-11-10 16:32:54 -08001012 public float getBackgroundAlphaMultiplier() {
1013 return mBackgroundAlphaMultiplier;
1014 }
1015
Michael Jurka5f1c5092010-09-03 14:15:02 -07001016 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001017 if (mBackgroundAlpha != alpha) {
1018 mBackgroundAlpha = alpha;
1019 invalidate();
1020 }
Michael Jurkadee05892010-07-27 10:01:56 -07001021 }
1022
Michael Jurkaa52570f2012-03-20 03:18:20 -07001023 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001024 final int childCount = getChildCount();
1025 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001026 getChildAt(i).setAlpha(alpha);
1027 }
1028 }
1029
Michael Jurkaa52570f2012-03-20 03:18:20 -07001030 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1031 if (getChildCount() > 0) {
1032 return (ShortcutAndWidgetContainer) getChildAt(0);
1033 }
1034 return null;
1035 }
1036
Patrick Dubroy440c3602010-07-13 17:50:32 -07001037 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001038 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001039 }
1040
Adam Cohen76fc0852011-06-17 13:26:23 -07001041 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001042 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001043 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001044 boolean[][] occupied = mOccupied;
1045 if (!permanent) {
1046 occupied = mTmpOccupied;
1047 }
1048
Adam Cohen19f37922012-03-21 11:59:11 -07001049 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001050 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1051 final ItemInfo info = (ItemInfo) child.getTag();
1052
1053 // We cancel any existing animations
1054 if (mReorderAnimators.containsKey(lp)) {
1055 mReorderAnimators.get(lp).cancel();
1056 mReorderAnimators.remove(lp);
1057 }
1058
Adam Cohen482ed822012-03-02 14:15:13 -08001059 final int oldX = lp.x;
1060 final int oldY = lp.y;
1061 if (adjustOccupied) {
1062 occupied[lp.cellX][lp.cellY] = false;
1063 occupied[cellX][cellY] = true;
1064 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001065 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001066 if (permanent) {
1067 lp.cellX = info.cellX = cellX;
1068 lp.cellY = info.cellY = cellY;
1069 } else {
1070 lp.tmpCellX = cellX;
1071 lp.tmpCellY = cellY;
1072 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001073 clc.setupLp(lp);
1074 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001075 final int newX = lp.x;
1076 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001077
Adam Cohen76fc0852011-06-17 13:26:23 -07001078 lp.x = oldX;
1079 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001080
Adam Cohen482ed822012-03-02 14:15:13 -08001081 // Exit early if we're not actually moving the view
1082 if (oldX == newX && oldY == newY) {
1083 lp.isLockedToGrid = true;
1084 return true;
1085 }
1086
1087 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
1088 va.setDuration(duration);
1089 mReorderAnimators.put(lp, va);
1090
1091 va.addUpdateListener(new AnimatorUpdateListener() {
1092 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001093 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001094 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001095 lp.x = (int) ((1 - r) * oldX + r * newX);
1096 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001097 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001098 }
1099 });
Adam Cohen482ed822012-03-02 14:15:13 -08001100 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001101 boolean cancelled = false;
1102 public void onAnimationEnd(Animator animation) {
1103 // If the animation was cancelled, it means that another animation
1104 // has interrupted this one, and we don't want to lock the item into
1105 // place just yet.
1106 if (!cancelled) {
1107 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001108 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001109 }
1110 if (mReorderAnimators.containsKey(lp)) {
1111 mReorderAnimators.remove(lp);
1112 }
1113 }
1114 public void onAnimationCancel(Animator animation) {
1115 cancelled = true;
1116 }
1117 });
Adam Cohen482ed822012-03-02 14:15:13 -08001118 va.setStartDelay(delay);
1119 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001120 return true;
1121 }
1122 return false;
1123 }
1124
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001125 /**
1126 * Estimate where the top left cell of the dragged item will land if it is dropped.
1127 *
1128 * @param originX The X value of the top left corner of the item
1129 * @param originY The Y value of the top left corner of the item
1130 * @param spanX The number of horizontal cells that the item spans
1131 * @param spanY The number of vertical cells that the item spans
1132 * @param result The estimated drop cell X and Y.
1133 */
1134 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001135 final int countX = mCountX;
1136 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001137
Michael Jurkaa63c4522010-08-19 13:52:27 -07001138 // pointToCellRounded takes the top left of a cell but will pad that with
1139 // cellWidth/2 and cellHeight/2 when finding the matching cell
1140 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001141
1142 // If the item isn't fully on this screen, snap to the edges
1143 int rightOverhang = result[0] + spanX - countX;
1144 if (rightOverhang > 0) {
1145 result[0] -= rightOverhang; // Snap to right
1146 }
1147 result[0] = Math.max(0, result[0]); // Snap to left
1148 int bottomOverhang = result[1] + spanY - countY;
1149 if (bottomOverhang > 0) {
1150 result[1] -= bottomOverhang; // Snap to bottom
1151 }
1152 result[1] = Math.max(0, result[1]); // Snap to top
1153 }
1154
Adam Cohen482ed822012-03-02 14:15:13 -08001155 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1156 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001157 final int oldDragCellX = mDragCell[0];
1158 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001159
Winson Chungb8c69f32011-10-19 21:36:08 -07001160 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001161 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1162 } else {
1163 mDragCenter.set(originX, originY);
1164 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001165
Adam Cohen2801caf2011-05-13 20:57:39 -07001166 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001167 return;
1168 }
1169
Adam Cohen482ed822012-03-02 14:15:13 -08001170 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1171 mDragCell[0] = cellX;
1172 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001173 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001174 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001175 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001176
Joe Onorato4be866d2010-10-10 11:26:02 -07001177 int left = topLeft[0];
1178 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001179
Winson Chungb8c69f32011-10-19 21:36:08 -07001180 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001181 // When drawing the drag outline, it did not account for margin offsets
1182 // added by the view's parent.
1183 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1184 left += lp.leftMargin;
1185 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001186
Adam Cohen99e8b402011-03-25 19:23:43 -07001187 // Offsets due to the size difference between the View and the dragOutline.
1188 // There is a size difference to account for the outer blur, which may lie
1189 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001190 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001191 // We center about the x axis
1192 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1193 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001194 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001195 if (dragOffset != null && dragRegion != null) {
1196 // Center the drag region *horizontally* in the cell and apply a drag
1197 // outline offset
1198 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1199 - dragRegion.width()) / 2;
1200 top += dragOffset.y;
1201 } else {
1202 // Center the drag outline in the cell
1203 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1204 - dragOutline.getWidth()) / 2;
1205 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1206 - dragOutline.getHeight()) / 2;
1207 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001208 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001209 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001210 mDragOutlineAnims[oldIndex].animateOut();
1211 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001212 Rect r = mDragOutlines[mDragOutlineCurrent];
1213 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1214 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001215 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001216 }
Winson Chung150fbab2010-09-29 17:14:26 -07001217
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001218 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1219 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001220 }
1221 }
1222
Adam Cohene0310962011-04-18 16:15:31 -07001223 public void clearDragOutlines() {
1224 final int oldIndex = mDragOutlineCurrent;
1225 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001226 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001227 }
1228
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001230 * Find a vacant area that will fit the given bounds nearest the requested
1231 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001232 *
Romain Guy51afc022009-05-04 18:03:43 -07001233 * @param pixelX The X location at which you want to search for a vacant area.
1234 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001235 * @param spanX Horizontal span of the object.
1236 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001237 * @param result Array in which to place the result, or null (in which case a new array will
1238 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001239 * @return The X, Y cell of a vacant area that can contain this object,
1240 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001242 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1243 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001244 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001245 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001246
Michael Jurka6a1435d2010-09-27 17:35:12 -07001247 /**
1248 * Find a vacant area that will fit the given bounds nearest the requested
1249 * cell location. Uses Euclidean distance to score multiple vacant areas.
1250 *
1251 * @param pixelX The X location at which you want to search for a vacant area.
1252 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001253 * @param minSpanX The minimum horizontal span required
1254 * @param minSpanY The minimum vertical span required
1255 * @param spanX Horizontal span of the object.
1256 * @param spanY Vertical span of the object.
1257 * @param result Array in which to place the result, or null (in which case a new array will
1258 * be allocated)
1259 * @return The X, Y cell of a vacant area that can contain this object,
1260 * nearest the requested location.
1261 */
1262 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1263 int spanY, int[] result, int[] resultSpan) {
1264 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1265 result, resultSpan);
1266 }
1267
1268 /**
1269 * Find a vacant area that will fit the given bounds nearest the requested
1270 * cell location. Uses Euclidean distance to score multiple vacant areas.
1271 *
1272 * @param pixelX The X location at which you want to search for a vacant area.
1273 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001274 * @param spanX Horizontal span of the object.
1275 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001276 * @param ignoreOccupied If true, the result can be an occupied cell
1277 * @param result Array in which to place the result, or null (in which case a new array will
1278 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001279 * @return The X, Y cell of a vacant area that can contain this object,
1280 * nearest the requested location.
1281 */
Adam Cohendf035382011-04-11 17:22:04 -07001282 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1283 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001284 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001285 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001286 }
1287
1288 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1289 private void lazyInitTempRectStack() {
1290 if (mTempRectStack.isEmpty()) {
1291 for (int i = 0; i < mCountX * mCountY; i++) {
1292 mTempRectStack.push(new Rect());
1293 }
1294 }
1295 }
Adam Cohen482ed822012-03-02 14:15:13 -08001296
Adam Cohend41fbf52012-02-16 23:53:59 -08001297 private void recycleTempRects(Stack<Rect> used) {
1298 while (!used.isEmpty()) {
1299 mTempRectStack.push(used.pop());
1300 }
1301 }
1302
1303 /**
1304 * Find a vacant area that will fit the given bounds nearest the requested
1305 * cell location. Uses Euclidean distance to score multiple vacant areas.
1306 *
1307 * @param pixelX The X location at which you want to search for a vacant area.
1308 * @param pixelY The Y location at which you want to search for a vacant area.
1309 * @param minSpanX The minimum horizontal span required
1310 * @param minSpanY The minimum vertical span required
1311 * @param spanX Horizontal span of the object.
1312 * @param spanY Vertical span of the object.
1313 * @param ignoreOccupied If true, the result can be an occupied cell
1314 * @param result Array in which to place the result, or null (in which case a new array will
1315 * be allocated)
1316 * @return The X, Y cell of a vacant area that can contain this object,
1317 * nearest the requested location.
1318 */
1319 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001320 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1321 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001322 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001323 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001324 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001325
Adam Cohene3e27a82011-04-15 12:07:39 -07001326 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1327 // to the center of the item, but we are searching based on the top-left cell, so
1328 // we translate the point over to correspond to the top-left.
1329 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1330 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1331
Jeff Sharkey70864282009-04-07 21:08:40 -07001332 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001333 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001334 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001335 final Rect bestRect = new Rect(-1, -1, -1, -1);
1336 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001337
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001338 final int countX = mCountX;
1339 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001340
Adam Cohend41fbf52012-02-16 23:53:59 -08001341 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1342 spanX < minSpanX || spanY < minSpanY) {
1343 return bestXY;
1344 }
1345
1346 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001347 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001348 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1349 int ySize = -1;
1350 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001351 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001352 // First, let's see if this thing fits anywhere
1353 for (int i = 0; i < minSpanX; i++) {
1354 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001355 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001356 continue inner;
1357 }
Michael Jurkac28de512010-08-13 11:27:44 -07001358 }
1359 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001360 xSize = minSpanX;
1361 ySize = minSpanY;
1362
1363 // We know that the item will fit at _some_ acceptable size, now let's see
1364 // how big we can make it. We'll alternate between incrementing x and y spans
1365 // until we hit a limit.
1366 boolean incX = true;
1367 boolean hitMaxX = xSize >= spanX;
1368 boolean hitMaxY = ySize >= spanY;
1369 while (!(hitMaxX && hitMaxY)) {
1370 if (incX && !hitMaxX) {
1371 for (int j = 0; j < ySize; j++) {
1372 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1373 // We can't move out horizontally
1374 hitMaxX = true;
1375 }
1376 }
1377 if (!hitMaxX) {
1378 xSize++;
1379 }
1380 } else if (!hitMaxY) {
1381 for (int i = 0; i < xSize; i++) {
1382 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1383 // We can't move out vertically
1384 hitMaxY = true;
1385 }
1386 }
1387 if (!hitMaxY) {
1388 ySize++;
1389 }
1390 }
1391 hitMaxX |= xSize >= spanX;
1392 hitMaxY |= ySize >= spanY;
1393 incX = !incX;
1394 }
1395 incX = true;
1396 hitMaxX = xSize >= spanX;
1397 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001398 }
Winson Chung0be025d2011-05-23 17:45:09 -07001399 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001400 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401
Adam Cohend41fbf52012-02-16 23:53:59 -08001402 // We verify that the current rect is not a sub-rect of any of our previous
1403 // candidates. In this case, the current rect is disqualified in favour of the
1404 // containing rect.
1405 Rect currentRect = mTempRectStack.pop();
1406 currentRect.set(x, y, x + xSize, y + ySize);
1407 boolean contained = false;
1408 for (Rect r : validRegions) {
1409 if (r.contains(currentRect)) {
1410 contained = true;
1411 break;
1412 }
1413 }
1414 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001415 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1416 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001417
Adam Cohend41fbf52012-02-16 23:53:59 -08001418 if ((distance <= bestDistance && !contained) ||
1419 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001420 bestDistance = distance;
1421 bestXY[0] = x;
1422 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001423 if (resultSpan != null) {
1424 resultSpan[0] = xSize;
1425 resultSpan[1] = ySize;
1426 }
1427 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001428 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
1430 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001431 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001432 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433
Adam Cohenc0dcf592011-06-01 15:30:43 -07001434 // Return -1, -1 if no suitable location found
1435 if (bestDistance == Double.MAX_VALUE) {
1436 bestXY[0] = -1;
1437 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001438 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001439 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001440 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001442
Adam Cohen482ed822012-03-02 14:15:13 -08001443 /**
1444 * Find a vacant area that will fit the given bounds nearest the requested
1445 * cell location, and will also weigh in a suggested direction vector of the
1446 * desired location. This method computers distance based on unit grid distances,
1447 * not pixel distances.
1448 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001449 * @param cellX The X cell nearest to which you want to search for a vacant area.
1450 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001451 * @param spanX Horizontal span of the object.
1452 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001453 * @param direction The favored direction in which the views should move from x, y
1454 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1455 * matches exactly. Otherwise we find the best matching direction.
1456 * @param occoupied The array which represents which cells in the CellLayout are occupied
1457 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1458 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001459 * @param result Array in which to place the result, or null (in which case a new array will
1460 * be allocated)
1461 * @return The X, Y cell of a vacant area that can contain this object,
1462 * nearest the requested location.
1463 */
1464 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001465 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001466 // Keep track of best-scoring drop area
1467 final int[] bestXY = result != null ? result : new int[2];
1468 float bestDistance = Float.MAX_VALUE;
1469 int bestDirectionScore = Integer.MIN_VALUE;
1470
1471 final int countX = mCountX;
1472 final int countY = mCountY;
1473
1474 for (int y = 0; y < countY - (spanY - 1); y++) {
1475 inner:
1476 for (int x = 0; x < countX - (spanX - 1); x++) {
1477 // First, let's see if this thing fits anywhere
1478 for (int i = 0; i < spanX; i++) {
1479 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001480 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001481 continue inner;
1482 }
1483 }
1484 }
1485
1486 float distance = (float)
1487 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1488 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001489 computeDirectionVector(x - cellX, y - cellY, curDirection);
1490 // The direction score is just the dot product of the two candidate direction
1491 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001492 int curDirectionScore = direction[0] * curDirection[0] +
1493 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001494 boolean exactDirectionOnly = false;
1495 boolean directionMatches = direction[0] == curDirection[0] &&
1496 direction[0] == curDirection[0];
1497 if ((directionMatches || !exactDirectionOnly) &&
1498 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001499 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1500 bestDistance = distance;
1501 bestDirectionScore = curDirectionScore;
1502 bestXY[0] = x;
1503 bestXY[1] = y;
1504 }
1505 }
1506 }
1507
1508 // Return -1, -1 if no suitable location found
1509 if (bestDistance == Float.MAX_VALUE) {
1510 bestXY[0] = -1;
1511 bestXY[1] = -1;
1512 }
1513 return bestXY;
1514 }
1515
Adam Cohen47a876d2012-03-19 13:21:41 -07001516 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1517 int[] direction,boolean[][] occupied,
1518 boolean blockOccupied[][], int[] result) {
1519 // Keep track of best-scoring drop area
1520 final int[] bestXY = result != null ? result : new int[2];
1521 bestXY[0] = -1;
1522 bestXY[1] = -1;
1523 float bestDistance = Float.MAX_VALUE;
1524
1525 // We use this to march in a single direction
Adam Cohen5b53f292012-03-29 14:30:35 -07001526 if ((direction[0] != 0 && direction[1] != 0) ||
1527 (direction[0] == 0 && direction[1] == 0)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001528 return bestXY;
1529 }
1530
1531 // This will only incrememnet one of x or y based on the assertion above
1532 int x = cellX + direction[0];
1533 int y = cellY + direction[1];
1534 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
1535
1536 boolean fail = false;
1537 for (int i = 0; i < spanX; i++) {
1538 for (int j = 0; j < spanY; j++) {
1539 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1540 fail = true;
1541 }
1542 }
1543 }
1544 if (!fail) {
1545 float distance = (float)
1546 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1547 if (Float.compare(distance, bestDistance) < 0) {
1548 bestDistance = distance;
1549 bestXY[0] = x;
1550 bestXY[1] = y;
1551 }
1552 }
1553 x += direction[0];
1554 y += direction[1];
1555 }
1556 return bestXY;
1557 }
1558
Adam Cohen482ed822012-03-02 14:15:13 -08001559 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001560 int[] direction, ItemConfiguration currentState) {
1561 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001562 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001563 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001564 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1565
Adam Cohen8baab352012-03-20 17:39:21 -07001566 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001567
1568 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001569 c.x = mTempLocation[0];
1570 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001571 success = true;
1572
1573 }
Adam Cohen8baab352012-03-20 17:39:21 -07001574 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001575 return success;
1576 }
1577
Adam Cohen47a876d2012-03-19 13:21:41 -07001578 // This method looks in the specified direction to see if there is an additional view
1579 // immediately adjecent in that direction
1580 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
Adam Cohen19f37922012-03-21 11:59:11 -07001581 boolean[][] occupied, View dragView, ItemConfiguration currentState) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001582 boolean found = false;
1583
Michael Jurkaa52570f2012-03-20 03:18:20 -07001584 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001585 Rect r0 = new Rect(boundingRect);
1586 Rect r1 = new Rect();
1587
1588 int deltaX = 0;
1589 int deltaY = 0;
1590 if (direction[1] < 0) {
1591 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom);
1592 deltaY = -1;
1593 } else if (direction[1] > 0) {
1594 r0.set(r0.left, r0.top, r0.right, r0.bottom + 1);
1595 deltaY = 1;
1596 } else if (direction[0] < 0) {
1597 r0.set(r0.left - 1, r0.top, r0.right, r0.bottom);
1598 deltaX = -1;
1599 } else if (direction[0] > 0) {
1600 r0.set(r0.left, r0.top, r0.right + 1, r0.bottom);
1601 deltaX = 1;
1602 }
1603
1604 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001605 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen19f37922012-03-21 11:59:11 -07001606 if (views.contains(child) || child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001607 CellAndSpan c = currentState.map.get(child);
Adam Cohen47a876d2012-03-19 13:21:41 -07001608
Adam Cohen8baab352012-03-20 17:39:21 -07001609 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1610 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001611 if (Rect.intersects(r0, r1)) {
1612 if (!lp.canReorder) {
1613 return false;
1614 }
1615 boolean pushed = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001616 for (int x = c.x; x < c.x + c.spanX; x++) {
1617 for (int y = c.y; y < c.y + c.spanY; y++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001618 boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX
1619 && y - deltaY >= 0 && y - deltaY < mCountY;
1620 if (inBounds && occupied[x - deltaX][y - deltaY]) {
1621 pushed = true;
1622 }
1623 }
1624 }
1625 if (pushed) {
1626 views.add(child);
Adam Cohen8baab352012-03-20 17:39:21 -07001627 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001628 found = true;
1629 }
1630 }
1631 }
1632 return found;
1633 }
1634
Adam Cohen482ed822012-03-02 14:15:13 -08001635 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohen19f37922012-03-21 11:59:11 -07001636 int[] direction, boolean push, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001637 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001638
Adam Cohen8baab352012-03-20 17:39:21 -07001639 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001640 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001641 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001642 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001643 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001644 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001645 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001646 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001647 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001648 }
1649 }
Adam Cohen8baab352012-03-20 17:39:21 -07001650
1651 @SuppressWarnings("unchecked")
1652 ArrayList<View> dup = (ArrayList<View>) views.clone();
1653 // We try and expand the group of views in the direction vector passed, based on
1654 // whether they are physically adjacent, ie. based on "push mechanics".
Adam Cohen19f37922012-03-21 11:59:11 -07001655 while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied, dragView,
Adam Cohen8baab352012-03-20 17:39:21 -07001656 currentState)) {
1657 }
1658
1659 // Mark the occupied state as false for the group of views we want to move.
1660 for (View v: dup) {
1661 CellAndSpan c = currentState.map.get(v);
1662 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1663 }
1664
Adam Cohen47a876d2012-03-19 13:21:41 -07001665 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1666 int top = boundingRect.top;
1667 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001668 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
1669 // for tetris-style interlocking.
1670 for (View v: dup) {
1671 CellAndSpan c = currentState.map.get(v);
1672 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001673 }
1674
Adam Cohen482ed822012-03-02 14:15:13 -08001675 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1676
Adam Cohen8baab352012-03-20 17:39:21 -07001677 if (push) {
1678 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1679 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1680 } else {
1681 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1682 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1683 }
Adam Cohen482ed822012-03-02 14:15:13 -08001684
Adam Cohen8baab352012-03-20 17:39:21 -07001685 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001686 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001687 int deltaX = mTempLocation[0] - boundingRect.left;
1688 int deltaY = mTempLocation[1] - boundingRect.top;
1689 for (View v: dup) {
1690 CellAndSpan c = currentState.map.get(v);
1691 c.x += deltaX;
1692 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001693 }
1694 success = true;
1695 }
Adam Cohen8baab352012-03-20 17:39:21 -07001696
1697 // In either case, we set the occupied array as marked for the location of the views
1698 for (View v: dup) {
1699 CellAndSpan c = currentState.map.get(v);
1700 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001701 }
1702 return success;
1703 }
1704
1705 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1706 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1707 }
1708
1709 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001710 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001711 // Return early if get invalid cell positions
1712 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001713
Adam Cohen8baab352012-03-20 17:39:21 -07001714 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001715 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001716
Adam Cohen8baab352012-03-20 17:39:21 -07001717 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001718 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001719 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001720 if (c != null) {
1721 c.x = cellX;
1722 c.y = cellY;
1723 }
Adam Cohen482ed822012-03-02 14:15:13 -08001724 }
Adam Cohen482ed822012-03-02 14:15:13 -08001725 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1726 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001727 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001728 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001729 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001730 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001731 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001732 if (Rect.intersects(r0, r1)) {
1733 if (!lp.canReorder) {
1734 return false;
1735 }
1736 mIntersectingViews.add(child);
1737 }
1738 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001739
Adam Cohen8baab352012-03-20 17:39:21 -07001740 // We try to move the intersecting views as a block using the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001741 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1742 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001743 return true;
1744 }
1745 // Try the opposite direction
1746 direction[0] *= -1;
1747 direction[1] *= -1;
Adam Cohen19f37922012-03-21 11:59:11 -07001748 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1749 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001750 return true;
1751 }
1752 // Switch the direction back
1753 direction[0] *= -1;
1754 direction[1] *= -1;
1755
Adam Cohen8baab352012-03-20 17:39:21 -07001756 // Next we try moving the views as a block , but without requiring the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001757 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, ignoreView,
1758 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001759 return true;
1760 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001761
Adam Cohen482ed822012-03-02 14:15:13 -08001762 // Ok, they couldn't move as a block, let's move them individually
1763 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001764 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001765 return false;
1766 }
1767 }
1768 return true;
1769 }
1770
1771 /*
1772 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1773 * the provided point and the provided cell
1774 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001775 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001776 double angle = Math.atan(((float) deltaY) / deltaX);
1777
1778 result[0] = 0;
1779 result[1] = 0;
1780 if (Math.abs(Math.cos(angle)) > 0.5f) {
1781 result[0] = (int) Math.signum(deltaX);
1782 }
1783 if (Math.abs(Math.sin(angle)) > 0.5f) {
1784 result[1] = (int) Math.signum(deltaY);
1785 }
1786 }
1787
Adam Cohen8baab352012-03-20 17:39:21 -07001788 private void copyOccupiedArray(boolean[][] occupied) {
1789 for (int i = 0; i < mCountX; i++) {
1790 for (int j = 0; j < mCountY; j++) {
1791 occupied[i][j] = mOccupied[i][j];
1792 }
1793 }
1794 }
1795
Adam Cohen482ed822012-03-02 14:15:13 -08001796 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1797 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001798 // Copy the current state into the solution. This solution will be manipulated as necessary.
1799 copyCurrentStateToSolution(solution, false);
1800 // Copy the current occupied array into the temporary occupied array. This array will be
1801 // manipulated as necessary to find a solution.
1802 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001803
1804 // We find the nearest cell into which we would place the dragged item, assuming there's
1805 // nothing in its way.
1806 int result[] = new int[2];
1807 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1808
1809 boolean success = false;
1810 // First we try the exact nearest position of the item being dragged,
1811 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001812 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1813 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001814
1815 if (!success) {
1816 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1817 // x, then 1 in y etc.
1818 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1819 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1820 dragView, false, solution);
1821 } else if (spanY > minSpanY) {
1822 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1823 dragView, true, solution);
1824 }
1825 solution.isSolution = false;
1826 } else {
1827 solution.isSolution = true;
1828 solution.dragViewX = result[0];
1829 solution.dragViewY = result[1];
1830 solution.dragViewSpanX = spanX;
1831 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001832 }
1833 return solution;
1834 }
1835
1836 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001837 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001838 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001839 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001840 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001841 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001842 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001843 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001844 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001845 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001846 }
Adam Cohen8baab352012-03-20 17:39:21 -07001847 solution.map.put(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001848 }
1849 }
1850
1851 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1852 for (int i = 0; i < mCountX; i++) {
1853 for (int j = 0; j < mCountY; j++) {
1854 mTmpOccupied[i][j] = false;
1855 }
1856 }
1857
Michael Jurkaa52570f2012-03-20 03:18:20 -07001858 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001859 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001860 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001861 if (child == dragView) continue;
1862 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001863 CellAndSpan c = solution.map.get(child);
1864 if (c != null) {
1865 lp.tmpCellX = c.x;
1866 lp.tmpCellY = c.y;
1867 lp.cellHSpan = c.spanX;
1868 lp.cellVSpan = c.spanY;
1869 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001870 }
1871 }
1872 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1873 solution.dragViewSpanY, mTmpOccupied, true);
1874 }
1875
1876 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1877 commitDragView) {
1878
1879 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1880 for (int i = 0; i < mCountX; i++) {
1881 for (int j = 0; j < mCountY; j++) {
1882 occupied[i][j] = false;
1883 }
1884 }
1885
Michael Jurkaa52570f2012-03-20 03:18:20 -07001886 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001887 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001888 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001889 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001890 CellAndSpan c = solution.map.get(child);
1891 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07001892 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
1893 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001894 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001895 }
1896 }
1897 if (commitDragView) {
1898 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1899 solution.dragViewSpanY, occupied, true);
1900 }
1901 }
1902
Adam Cohen19f37922012-03-21 11:59:11 -07001903 // This method starts or changes the reorder hint animations
1904 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
1905 int childCount = mShortcutsAndWidgets.getChildCount();
1906 int timeForPriorAnimationToComplete = getMaxCompletionTime();
1907 for (int i = 0; i < childCount; i++) {
1908 View child = mShortcutsAndWidgets.getChildAt(i);
1909 if (child == dragView) continue;
1910 CellAndSpan c = solution.map.get(child);
1911 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1912 if (c != null) {
1913 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
1914 c.x, c.y, c.spanX, c.spanY);
1915 rha.animate(timeForPriorAnimationToComplete);
1916 }
1917 }
1918 }
1919
1920 // Class which represents the reorder hint animations. These animations show that an item is
1921 // in a temporary state, and hint at where the item will return to.
1922 class ReorderHintAnimation {
1923 View child;
1924 float deltaX;
1925 float deltaY;
Brandon Keely50e6e562012-05-08 16:28:49 -07001926 private static final int DURATION = 300;
Adam Cohen19f37922012-03-21 11:59:11 -07001927 private int repeatCount;
1928 private boolean cancelOnCycleComplete = false;
1929 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;
1941 deltaX = 0;
1942 deltaY = 0;
1943 if (dX == dY && dX == 0) {
1944 } else {
1945 if (dY == 0) {
1946 deltaX = mReorderHintAnimationMagnitude;
1947 } else if (dX == 0) {
1948 deltaY = mReorderHintAnimationMagnitude;
1949 } else {
1950 double angle = Math.atan( (float) (dY) / dX);
1951 deltaX = (int) (Math.cos(angle) * mReorderHintAnimationMagnitude);
1952 deltaY = (int) (Math.sin(angle) * mReorderHintAnimationMagnitude);
1953 }
1954 }
Brandon Keely50e6e562012-05-08 16:28:49 -07001955 child.setPivotY(child.getMeasuredHeight() * 0.5f);
1956 child.setPivotX(child.getMeasuredWidth() * 0.5f);
Adam Cohen19f37922012-03-21 11:59:11 -07001957 this.child = child;
1958 }
1959
1960 void animate(int delay) {
1961 if (mShakeAnimators.containsKey(child)) {
1962 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
1963 oldAnimation.completeAnimation();
1964 mShakeAnimators.remove(child);
1965 }
1966 if (deltaX == 0 && deltaY == 0) {
1967 return;
1968 }
1969 va = ValueAnimator.ofFloat(0f, 1f);
1970 va.setRepeatMode(ValueAnimator.REVERSE);
1971 va.setRepeatCount(ValueAnimator.INFINITE);
Brandon Keely50e6e562012-05-08 16:28:49 -07001972 va.setDuration((int) (DURATION * (1.0f + Math.random()*.08f)));
Adam Cohen19f37922012-03-21 11:59:11 -07001973 va.addUpdateListener(new AnimatorUpdateListener() {
1974 @Override
1975 public void onAnimationUpdate(ValueAnimator animation) {
1976 float r = ((Float) animation.getAnimatedValue()).floatValue();
1977 float x = r * deltaX;
1978 float y = r * deltaY;
1979 child.setTranslationX(x);
1980 child.setTranslationY(y);
Brandon Keely50e6e562012-05-08 16:28:49 -07001981 float sf = 4.0f / child.getWidth();
1982 float s = 1.0f - r * sf;
1983 child.setScaleX(s);
1984 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07001985 }
1986 });
1987 va.addListener(new AnimatorListenerAdapter() {
1988 public void onAnimationRepeat(Animator animation) {
1989 repeatCount++;
1990 // We make sure to end only after a full period
1991 if (cancelOnCycleComplete && repeatCount % 2 == 0) {
1992 va.cancel();
1993 }
1994 }
1995 });
1996 va.setStartDelay(Math.max(REORDER_ANIMATION_DURATION, delay));
1997 mShakeAnimators.put(child, this);
1998 va.start();
1999 }
2000
2001
2002 private void completeAnimation() {
2003 cancelOnCycleComplete = true;
2004 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002005 private void completeAnimationImmediately() {
2006 va.cancel();
2007
2008 AnimatorSet s = new AnimatorSet();
2009 s.playTogether(
2010 ObjectAnimator.ofFloat(child, "scaleX", 1f),
2011 ObjectAnimator.ofFloat(child, "scaleY", 1f),
2012 ObjectAnimator.ofFloat(child, "translationX", 0f),
Brandon Keelyfaf22e12012-05-10 16:48:50 -07002013 ObjectAnimator.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002014 );
2015 s.setDuration(REORDER_ANIMATION_DURATION);
2016 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2017 s.start();
2018 }
2019
Adam Cohen19f37922012-03-21 11:59:11 -07002020
2021 // Returns the time required to complete the current oscillating animation
2022 private int completionTime() {
2023 if (repeatCount % 2 == 0) {
2024 return (int) (va.getDuration() - va.getCurrentPlayTime() + DURATION);
2025 } else {
2026 return (int) (va.getDuration() - va.getCurrentPlayTime());
2027 }
2028 }
2029 }
2030
2031 private void completeAndClearReorderHintAnimations() {
2032 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002033 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002034 }
2035 mShakeAnimators.clear();
2036 }
2037
2038 private int getMaxCompletionTime() {
2039 int maxTime = 0;
2040 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2041 maxTime = Math.max(maxTime, a.completionTime());
2042 }
2043 return maxTime;
2044 }
2045
Adam Cohen482ed822012-03-02 14:15:13 -08002046 private void commitTempPlacement() {
2047 for (int i = 0; i < mCountX; i++) {
2048 for (int j = 0; j < mCountY; j++) {
2049 mOccupied[i][j] = mTmpOccupied[i][j];
2050 }
2051 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002052 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002053 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002054 View child = mShortcutsAndWidgets.getChildAt(i);
2055 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2056 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002057 // We do a null check here because the item info can be null in the case of the
2058 // AllApps button in the hotseat.
2059 if (info != null) {
2060 info.cellX = lp.cellX = lp.tmpCellX;
2061 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002062 info.spanX = lp.cellHSpan;
2063 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002064 }
Adam Cohen482ed822012-03-02 14:15:13 -08002065 }
Adam Cohen2acce882012-03-28 19:03:19 -07002066 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002067 }
2068
2069 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002070 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002071 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002072 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002073 lp.useTmpCoords = useTempCoords;
2074 }
2075 }
2076
Adam Cohen482ed822012-03-02 14:15:13 -08002077 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2078 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2079 int[] result = new int[2];
2080 int[] resultSpan = new int[2];
2081 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2082 resultSpan);
2083 if (result[0] >= 0 && result[1] >= 0) {
2084 copyCurrentStateToSolution(solution, false);
2085 solution.dragViewX = result[0];
2086 solution.dragViewY = result[1];
2087 solution.dragViewSpanX = resultSpan[0];
2088 solution.dragViewSpanY = resultSpan[1];
2089 solution.isSolution = true;
2090 } else {
2091 solution.isSolution = false;
2092 }
2093 return solution;
2094 }
2095
2096 public void prepareChildForDrag(View child) {
2097 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002098 }
2099
Adam Cohen19f37922012-03-21 11:59:11 -07002100 /* This seems like it should be obvious and straight-forward, but when the direction vector
2101 needs to match with the notion of the dragView pushing other views, we have to employ
2102 a slightly more subtle notion of the direction vector. The question is what two points is
2103 the vector between? The center of the dragView and its desired destination? Not quite, as
2104 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2105 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2106 or right, which helps make pushing feel right.
2107 */
2108 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2109 int spanY, View dragView, int[] resultDirection) {
2110 int[] targetDestination = new int[2];
2111
2112 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2113 Rect dragRect = new Rect();
2114 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2115 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2116
2117 Rect dropRegionRect = new Rect();
2118 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2119 dragView, dropRegionRect, mIntersectingViews);
2120
2121 int dropRegionSpanX = dropRegionRect.width();
2122 int dropRegionSpanY = dropRegionRect.height();
2123
2124 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2125 dropRegionRect.height(), dropRegionRect);
2126
2127 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2128 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2129
2130 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2131 deltaX = 0;
2132 }
2133 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2134 deltaY = 0;
2135 }
2136
2137 if (deltaX == 0 && deltaY == 0) {
2138 // No idea what to do, give a random direction.
2139 resultDirection[0] = 1;
2140 resultDirection[1] = 0;
2141 } else {
2142 computeDirectionVector(deltaX, deltaY, resultDirection);
2143 }
2144 }
2145
2146 // For a given cell and span, fetch the set of views intersecting the region.
2147 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2148 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2149 if (boundingRect != null) {
2150 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2151 }
2152 intersectingViews.clear();
2153 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2154 Rect r1 = new Rect();
2155 final int count = mShortcutsAndWidgets.getChildCount();
2156 for (int i = 0; i < count; i++) {
2157 View child = mShortcutsAndWidgets.getChildAt(i);
2158 if (child == dragView) continue;
2159 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2160 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2161 if (Rect.intersects(r0, r1)) {
2162 mIntersectingViews.add(child);
2163 if (boundingRect != null) {
2164 boundingRect.union(r1);
2165 }
2166 }
2167 }
2168 }
2169
2170 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2171 View dragView, int[] result) {
2172 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2173 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2174 mIntersectingViews);
2175 return !mIntersectingViews.isEmpty();
2176 }
2177
2178 void revertTempState() {
2179 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2180 final int count = mShortcutsAndWidgets.getChildCount();
2181 for (int i = 0; i < count; i++) {
2182 View child = mShortcutsAndWidgets.getChildAt(i);
2183 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2184 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2185 lp.tmpCellX = lp.cellX;
2186 lp.tmpCellY = lp.cellY;
2187 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2188 0, false, false);
2189 }
2190 }
2191 completeAndClearReorderHintAnimations();
2192 setItemPlacementDirty(false);
2193 }
2194
Adam Cohenbebf0422012-04-11 18:06:28 -07002195 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2196 View dragView, int[] direction, boolean commit) {
2197 int[] pixelXY = new int[2];
2198 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2199
2200 // First we determine if things have moved enough to cause a different layout
2201 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2202 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2203
2204 setUseTempCoords(true);
2205 if (swapSolution != null && swapSolution.isSolution) {
2206 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2207 // committing anything or animating anything as we just want to determine if a solution
2208 // exists
2209 copySolutionToTempState(swapSolution, dragView);
2210 setItemPlacementDirty(true);
2211 animateItemsToSolution(swapSolution, dragView, commit);
2212
2213 if (commit) {
2214 commitTempPlacement();
2215 completeAndClearReorderHintAnimations();
2216 setItemPlacementDirty(false);
2217 } else {
2218 beginOrAdjustHintAnimations(swapSolution, dragView,
2219 REORDER_ANIMATION_DURATION);
2220 }
2221 mShortcutsAndWidgets.requestLayout();
2222 }
2223 return swapSolution.isSolution;
2224 }
2225
Adam Cohen482ed822012-03-02 14:15:13 -08002226 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2227 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002228 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002229 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002230
2231 if (resultSpan == null) {
2232 resultSpan = new int[2];
2233 }
2234
Adam Cohen19f37922012-03-21 11:59:11 -07002235 // When we are checking drop validity or actually dropping, we don't recompute the
2236 // direction vector, since we want the solution to match the preview, and it's possible
2237 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002238 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2239 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002240 mDirectionVector[0] = mPreviousReorderDirection[0];
2241 mDirectionVector[1] = mPreviousReorderDirection[1];
2242 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002243 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2244 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2245 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002246 }
2247 } else {
2248 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2249 mPreviousReorderDirection[0] = mDirectionVector[0];
2250 mPreviousReorderDirection[1] = mDirectionVector[1];
2251 }
2252
Adam Cohen482ed822012-03-02 14:15:13 -08002253 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2254 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2255
2256 // We attempt the approach which doesn't shuffle views at all
2257 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2258 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2259
2260 ItemConfiguration finalSolution = null;
2261 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2262 finalSolution = swapSolution;
2263 } else if (noShuffleSolution.isSolution) {
2264 finalSolution = noShuffleSolution;
2265 }
2266
2267 boolean foundSolution = true;
2268 if (!DESTRUCTIVE_REORDER) {
2269 setUseTempCoords(true);
2270 }
2271
2272 if (finalSolution != null) {
2273 result[0] = finalSolution.dragViewX;
2274 result[1] = finalSolution.dragViewY;
2275 resultSpan[0] = finalSolution.dragViewSpanX;
2276 resultSpan[1] = finalSolution.dragViewSpanY;
2277
2278 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2279 // committing anything or animating anything as we just want to determine if a solution
2280 // exists
2281 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2282 if (!DESTRUCTIVE_REORDER) {
2283 copySolutionToTempState(finalSolution, dragView);
2284 }
2285 setItemPlacementDirty(true);
2286 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2287
Adam Cohen19f37922012-03-21 11:59:11 -07002288 if (!DESTRUCTIVE_REORDER &&
2289 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002290 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002291 completeAndClearReorderHintAnimations();
2292 setItemPlacementDirty(false);
2293 } else {
2294 beginOrAdjustHintAnimations(finalSolution, dragView,
2295 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002296 }
2297 }
2298 } else {
2299 foundSolution = false;
2300 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2301 }
2302
2303 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2304 setUseTempCoords(false);
2305 }
Adam Cohen482ed822012-03-02 14:15:13 -08002306
Michael Jurkaa52570f2012-03-20 03:18:20 -07002307 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002308 return result;
2309 }
2310
Adam Cohen19f37922012-03-21 11:59:11 -07002311 void setItemPlacementDirty(boolean dirty) {
2312 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002313 }
Adam Cohen19f37922012-03-21 11:59:11 -07002314 boolean isItemPlacementDirty() {
2315 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002316 }
2317
2318 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002319 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002320 boolean isSolution = false;
2321 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2322
2323 int area() {
2324 return dragViewSpanX * dragViewSpanY;
2325 }
Adam Cohen8baab352012-03-20 17:39:21 -07002326 }
2327
2328 private class CellAndSpan {
2329 int x, y;
2330 int spanX, spanY;
2331
2332 public CellAndSpan(int x, int y, int spanX, int spanY) {
2333 this.x = x;
2334 this.y = y;
2335 this.spanX = spanX;
2336 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002337 }
2338 }
2339
Adam Cohendf035382011-04-11 17:22:04 -07002340 /**
2341 * Find a vacant area that will fit the given bounds nearest the requested
2342 * cell location. Uses Euclidean distance to score multiple vacant areas.
2343 *
2344 * @param pixelX The X location at which you want to search for a vacant area.
2345 * @param pixelY The Y location at which you want to search for a vacant area.
2346 * @param spanX Horizontal span of the object.
2347 * @param spanY Vertical span of the object.
2348 * @param ignoreView Considers space occupied by this view as unoccupied
2349 * @param result Previously returned value to possibly recycle.
2350 * @return The X, Y cell of a vacant area that can contain this object,
2351 * nearest the requested location.
2352 */
2353 int[] findNearestVacantArea(
2354 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2355 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2356 }
2357
2358 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002359 * Find a vacant area that will fit the given bounds nearest the requested
2360 * cell location. Uses Euclidean distance to score multiple vacant areas.
2361 *
2362 * @param pixelX The X location at which you want to search for a vacant area.
2363 * @param pixelY The Y location at which you want to search for a vacant area.
2364 * @param minSpanX The minimum horizontal span required
2365 * @param minSpanY The minimum vertical span required
2366 * @param spanX Horizontal span of the object.
2367 * @param spanY Vertical span of the object.
2368 * @param ignoreView Considers space occupied by this view as unoccupied
2369 * @param result Previously returned value to possibly recycle.
2370 * @return The X, Y cell of a vacant area that can contain this object,
2371 * nearest the requested location.
2372 */
2373 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2374 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002375 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2376 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002377 }
2378
2379 /**
Adam Cohendf035382011-04-11 17:22:04 -07002380 * Find a starting cell position that will fit the given bounds nearest the requested
2381 * cell location. Uses Euclidean distance to score multiple vacant areas.
2382 *
2383 * @param pixelX The X location at which you want to search for a vacant area.
2384 * @param pixelY The Y location at which you want to search for a vacant area.
2385 * @param spanX Horizontal span of the object.
2386 * @param spanY Vertical span of the object.
2387 * @param ignoreView Considers space occupied by this view as unoccupied
2388 * @param result Previously returned value to possibly recycle.
2389 * @return The X, Y cell of a vacant area that can contain this object,
2390 * nearest the requested location.
2391 */
2392 int[] findNearestArea(
2393 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2394 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2395 }
2396
Michael Jurka0280c3b2010-09-17 15:00:07 -07002397 boolean existsEmptyCell() {
2398 return findCellForSpan(null, 1, 1);
2399 }
2400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002402 * Finds the upper-left coordinate of the first rectangle in the grid that can
2403 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2404 * then this method will only return coordinates for rectangles that contain the cell
2405 * (intersectX, intersectY)
2406 *
2407 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2408 * can be found.
2409 * @param spanX The horizontal span of the cell we want to find.
2410 * @param spanY The vertical span of the cell we want to find.
2411 *
2412 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002413 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002414 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002415 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002416 }
2417
2418 /**
2419 * Like above, but ignores any cells occupied by the item "ignoreView"
2420 *
2421 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2422 * can be found.
2423 * @param spanX The horizontal span of the cell we want to find.
2424 * @param spanY The vertical span of the cell we want to find.
2425 * @param ignoreView The home screen item we should treat as not occupying any space
2426 * @return
2427 */
2428 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002429 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2430 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002431 }
2432
2433 /**
2434 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2435 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2436 *
2437 * @param spanX The horizontal span of the cell we want to find.
2438 * @param spanY The vertical span of the cell we want to find.
2439 * @param ignoreView The home screen item we should treat as not occupying any space
2440 * @param intersectX The X coordinate of the cell that we should try to overlap
2441 * @param intersectX The Y coordinate of the cell that we should try to overlap
2442 *
2443 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2444 */
2445 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2446 int intersectX, int intersectY) {
2447 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002448 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002449 }
2450
2451 /**
2452 * The superset of the above two methods
2453 */
2454 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002455 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002456 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002457 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002458
Michael Jurka28750fb2010-09-24 17:43:49 -07002459 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002460 while (true) {
2461 int startX = 0;
2462 if (intersectX >= 0) {
2463 startX = Math.max(startX, intersectX - (spanX - 1));
2464 }
2465 int endX = mCountX - (spanX - 1);
2466 if (intersectX >= 0) {
2467 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2468 }
2469 int startY = 0;
2470 if (intersectY >= 0) {
2471 startY = Math.max(startY, intersectY - (spanY - 1));
2472 }
2473 int endY = mCountY - (spanY - 1);
2474 if (intersectY >= 0) {
2475 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2476 }
2477
Winson Chungbbc60d82010-11-11 16:34:41 -08002478 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002479 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002480 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002481 for (int i = 0; i < spanX; i++) {
2482 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002483 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002484 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002485 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002486 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002487 continue inner;
2488 }
2489 }
2490 }
2491 if (cellXY != null) {
2492 cellXY[0] = x;
2493 cellXY[1] = y;
2494 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002495 foundCell = true;
2496 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002497 }
2498 }
2499 if (intersectX == -1 && intersectY == -1) {
2500 break;
2501 } else {
2502 // if we failed to find anything, try again but without any requirements of
2503 // intersecting
2504 intersectX = -1;
2505 intersectY = -1;
2506 continue;
2507 }
2508 }
2509
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002510 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002511 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002512 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002513 }
2514
2515 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002516 * A drag event has begun over this layout.
2517 * It may have begun over this layout (in which case onDragChild is called first),
2518 * or it may have begun on another layout.
2519 */
2520 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002521 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002522 mDragging = true;
2523 }
2524
2525 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002526 * Called when drag has left this CellLayout or has been completed (successfully or not)
2527 */
2528 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002529 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002530 // This can actually be called when we aren't in a drag, e.g. when adding a new
2531 // item to this layout via the customize drawer.
2532 // Guard against that case.
2533 if (mDragging) {
2534 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002535 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002536
2537 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002538 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002539 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2540 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002541 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002542 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002543 }
2544
2545 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002546 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002547 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002548 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 *
2550 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002552 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002553 if (child != null) {
2554 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002555 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002556 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 }
2559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002562 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 * @param cellX X coordinate of upper left corner expressed as a cell position
2564 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002565 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002567 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002569 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002570 final int cellWidth = mCellWidth;
2571 final int cellHeight = mCellHeight;
2572 final int widthGap = mWidthGap;
2573 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002574
Winson Chung4b825dcd2011-06-19 12:41:22 -07002575 final int hStartPadding = getPaddingLeft();
2576 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002578 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2579 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2580
2581 int x = hStartPadding + cellX * (cellWidth + widthGap);
2582 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002583
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002584 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002585 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002588 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002590 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591 * @param width Width in pixels
2592 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002593 * @param result An array of length 2 in which to store the result (may be null).
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002594 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002595 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002596 return rectToCell(getResources(), width, height, result);
2597 }
2598
2599 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002600 // Always assume we're working with the smallest span to make sure we
2601 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002602 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2603 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002604 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002605
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002606 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002607 int spanX = (int) Math.ceil(width / (float) smallerSize);
2608 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002609
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002610 if (result == null) {
2611 return new int[] { spanX, spanY };
2612 }
2613 result[0] = spanX;
2614 result[1] = spanY;
2615 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002616 }
2617
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002618 public int[] cellSpansToSize(int hSpans, int vSpans) {
2619 int[] size = new int[2];
2620 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2621 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2622 return size;
2623 }
2624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002625 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002626 * Calculate the grid spans needed to fit given item
2627 */
2628 public void calculateSpans(ItemInfo info) {
2629 final int minWidth;
2630 final int minHeight;
2631
2632 if (info instanceof LauncherAppWidgetInfo) {
2633 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2634 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2635 } else if (info instanceof PendingAddWidgetInfo) {
2636 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2637 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2638 } else {
2639 // It's not a widget, so it must be 1x1
2640 info.spanX = info.spanY = 1;
2641 return;
2642 }
2643 int[] spans = rectToCell(minWidth, minHeight, null);
2644 info.spanX = spans[0];
2645 info.spanY = spans[1];
2646 }
2647
2648 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002649 * Find the first vacant cell, if there is one.
2650 *
2651 * @param vacant Holds the x and y coordinate of the vacant cell
2652 * @param spanX Horizontal cell span.
2653 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002654 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002655 * @return True if a vacant cell was found
2656 */
2657 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658
Michael Jurka0280c3b2010-09-17 15:00:07 -07002659 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002660 }
2661
2662 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2663 int xCount, int yCount, boolean[][] occupied) {
2664
Adam Cohen2801caf2011-05-13 20:57:39 -07002665 for (int y = 0; y < yCount; y++) {
2666 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002667 boolean available = !occupied[x][y];
2668out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2669 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2670 available = available && !occupied[i][j];
2671 if (!available) break out;
2672 }
2673 }
2674
2675 if (available) {
2676 vacant[0] = x;
2677 vacant[1] = y;
2678 return true;
2679 }
2680 }
2681 }
2682
2683 return false;
2684 }
2685
Michael Jurka0280c3b2010-09-17 15:00:07 -07002686 private void clearOccupiedCells() {
2687 for (int x = 0; x < mCountX; x++) {
2688 for (int y = 0; y < mCountY; y++) {
2689 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002690 }
2691 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002692 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002693
Adam Cohend41fbf52012-02-16 23:53:59 -08002694 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002695 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002696 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002698
Adam Cohend4844c32011-02-18 19:25:06 -08002699 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002700 markCellsAsOccupiedForView(view, mOccupied);
2701 }
2702 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002703 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002704 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002705 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002706 }
2707
Adam Cohend4844c32011-02-18 19:25:06 -08002708 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002709 markCellsAsUnoccupiedForView(view, mOccupied);
2710 }
2711 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002712 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002713 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002714 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002715 }
2716
Adam Cohen482ed822012-03-02 14:15:13 -08002717 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2718 boolean value) {
2719 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002720 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2721 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002722 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002723 }
2724 }
2725 }
2726
Adam Cohen2801caf2011-05-13 20:57:39 -07002727 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002728 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002729 (Math.max((mCountX - 1), 0) * mWidthGap);
2730 }
2731
2732 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002733 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002734 (Math.max((mCountY - 1), 0) * mHeightGap);
2735 }
2736
Michael Jurka66d72172011-04-12 16:29:25 -07002737 public boolean isOccupied(int x, int y) {
2738 if (x < mCountX && y < mCountY) {
2739 return mOccupied[x][y];
2740 } else {
2741 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2742 }
2743 }
2744
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002745 @Override
2746 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2747 return new CellLayout.LayoutParams(getContext(), attrs);
2748 }
2749
2750 @Override
2751 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2752 return p instanceof CellLayout.LayoutParams;
2753 }
2754
2755 @Override
2756 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2757 return new CellLayout.LayoutParams(p);
2758 }
2759
Winson Chungaafa03c2010-06-11 17:34:16 -07002760 public static class CellLayoutAnimationController extends LayoutAnimationController {
2761 public CellLayoutAnimationController(Animation animation, float delay) {
2762 super(animation, delay);
2763 }
2764
2765 @Override
2766 protected long getDelayForView(View view) {
2767 return (int) (Math.random() * 150);
2768 }
2769 }
2770
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002771 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2772 /**
2773 * Horizontal location of the item in the grid.
2774 */
2775 @ViewDebug.ExportedProperty
2776 public int cellX;
2777
2778 /**
2779 * Vertical location of the item in the grid.
2780 */
2781 @ViewDebug.ExportedProperty
2782 public int cellY;
2783
2784 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002785 * Temporary horizontal location of the item in the grid during reorder
2786 */
2787 public int tmpCellX;
2788
2789 /**
2790 * Temporary vertical location of the item in the grid during reorder
2791 */
2792 public int tmpCellY;
2793
2794 /**
2795 * Indicates that the temporary coordinates should be used to layout the items
2796 */
2797 public boolean useTmpCoords;
2798
2799 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002800 * Number of cells spanned horizontally by the item.
2801 */
2802 @ViewDebug.ExportedProperty
2803 public int cellHSpan;
2804
2805 /**
2806 * Number of cells spanned vertically by the item.
2807 */
2808 @ViewDebug.ExportedProperty
2809 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002810
Adam Cohen1b607ed2011-03-03 17:26:50 -08002811 /**
2812 * Indicates whether the item will set its x, y, width and height parameters freely,
2813 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2814 */
Adam Cohend4844c32011-02-18 19:25:06 -08002815 public boolean isLockedToGrid = true;
2816
Adam Cohen482ed822012-03-02 14:15:13 -08002817 /**
2818 * Indicates whether this item can be reordered. Always true except in the case of the
2819 * the AllApps button.
2820 */
2821 public boolean canReorder = true;
2822
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823 // X coordinate of the view in the layout.
2824 @ViewDebug.ExportedProperty
2825 int x;
2826 // Y coordinate of the view in the layout.
2827 @ViewDebug.ExportedProperty
2828 int y;
2829
Romain Guy84f296c2009-11-04 15:00:44 -08002830 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002831
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002832 public LayoutParams(Context c, AttributeSet attrs) {
2833 super(c, attrs);
2834 cellHSpan = 1;
2835 cellVSpan = 1;
2836 }
2837
2838 public LayoutParams(ViewGroup.LayoutParams source) {
2839 super(source);
2840 cellHSpan = 1;
2841 cellVSpan = 1;
2842 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002843
2844 public LayoutParams(LayoutParams source) {
2845 super(source);
2846 this.cellX = source.cellX;
2847 this.cellY = source.cellY;
2848 this.cellHSpan = source.cellHSpan;
2849 this.cellVSpan = source.cellVSpan;
2850 }
2851
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002852 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002853 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002854 this.cellX = cellX;
2855 this.cellY = cellY;
2856 this.cellHSpan = cellHSpan;
2857 this.cellVSpan = cellVSpan;
2858 }
2859
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002860 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002861 if (isLockedToGrid) {
2862 final int myCellHSpan = cellHSpan;
2863 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002864 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2865 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002866
Adam Cohend4844c32011-02-18 19:25:06 -08002867 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2868 leftMargin - rightMargin;
2869 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2870 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002871 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2872 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002873 }
2874 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002875
Winson Chungaafa03c2010-06-11 17:34:16 -07002876 public String toString() {
2877 return "(" + this.cellX + ", " + this.cellY + ")";
2878 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002879
2880 public void setWidth(int width) {
2881 this.width = width;
2882 }
2883
2884 public int getWidth() {
2885 return width;
2886 }
2887
2888 public void setHeight(int height) {
2889 this.height = height;
2890 }
2891
2892 public int getHeight() {
2893 return height;
2894 }
2895
2896 public void setX(int x) {
2897 this.x = x;
2898 }
2899
2900 public int getX() {
2901 return x;
2902 }
2903
2904 public void setY(int y) {
2905 this.y = y;
2906 }
2907
2908 public int getY() {
2909 return y;
2910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002911 }
2912
Michael Jurka0280c3b2010-09-17 15:00:07 -07002913 // This class stores info for two purposes:
2914 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2915 // its spanX, spanY, and the screen it is on
2916 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2917 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2918 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002919 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002921 int cellX = -1;
2922 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002923 int spanX;
2924 int spanY;
2925 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002926 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002928 @Override
2929 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002930 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2931 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 }
2933 }
Michael Jurkad771c962011-08-09 15:00:48 -07002934
2935 public boolean lastDownOnOccupiedCell() {
2936 return mLastDownOnOccupiedCell;
2937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938}