blob: a05dd90c050bbb4c5b2b1a68783e86a1c23bffb7 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Brandon Keely50e6e562012-05-08 16:28:49 -070021import android.animation.AnimatorSet;
Chet Haase00397b12010-10-07 11:13:10 -070022import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070023import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070028import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080030import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070032import android.graphics.Point;
Adam Cohenb5ba0972011-09-07 18:02:31 -070033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080036import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070037import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070038import android.graphics.drawable.NinePatchDrawable;
Adam Cohen1462de32012-07-24 22:34:36 -070039import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070042import android.util.SparseArray;
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
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.R;
52import com.android.launcher3.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 Cohenf3900c22012-11-16 18:28:11 -080056import java.util.Collections;
57import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070058import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080059import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070060
Michael Jurkabdb5c532011-02-01 15:05:06 -080061public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070062 static final String TAG = "CellLayout";
63
Adam Cohen2acce882012-03-28 19:03:19 -070064 private Launcher mLauncher;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 private int mCellWidth;
66 private int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070067 private int mFixedCellWidth;
68 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070069
Adam Cohend22015c2010-07-26 22:02:18 -070070 private int mCountX;
71 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
Adam Cohen234c4cd2011-07-17 21:03:04 -070073 private int mOriginalWidthGap;
74 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075 private int mWidthGap;
76 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070077 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080078 private boolean mScrollingTransformsDirty = false;
Adam Cohen917e3882013-10-31 15:03:35 -070079 private boolean mDropPending = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
81 private final Rect mRect = new Rect();
82 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070083
Patrick Dubroyde7658b2010-09-27 11:15:43 -070084 // These are temporary variables to prevent having to allocate a new object just to
85 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070086 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070087 private final int[] mTmpPoint = new int[2];
Adam Cohen69ce2e52011-07-03 19:25:21 -070088 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070089
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080091 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070092 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093
Michael Jurkadee05892010-07-27 10:01:56 -070094 private OnTouchListener mInterceptTouchListener;
95
Adam Cohen69ce2e52011-07-03 19:25:21 -070096 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070097 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070098
Adam Cohen02dcfcc2013-10-01 12:37:33 -070099 private float FOREGROUND_ALPHA_DAMPER = 0.65f;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700100 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700101 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700102 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -0700103
Michael Jurka33945b22010-12-21 18:19:38 -0800104 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -0800105 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700106 private Drawable mOverScrollForegroundDrawable;
107 private Drawable mOverScrollLeft;
108 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700109 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700110 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700111 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700112
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700113 // These values allow a fixed measurement to be set on the CellLayout.
114 private int mFixedWidth = -1;
115 private int mFixedHeight = -1;
116
Michael Jurka33945b22010-12-21 18:19:38 -0800117 // If we're actively dragging something over this screen, mIsDragOverlapping is true
118 private boolean mIsDragOverlapping = false;
Adam Cohendedbd962013-07-11 14:21:49 -0700119 boolean mUseActiveGlowBackground = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700120
Winson Chung150fbab2010-09-29 17:14:26 -0700121 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700122 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800123 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700124 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700125 private InterruptibleInOutAnimator[] mDragOutlineAnims =
126 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700127
128 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700129 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700130 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700131
Patrick Dubroy96864c32011-03-10 17:17:23 -0800132 private BubbleTextView mPressedOrFocusedIcon;
133
Adam Cohen482ed822012-03-02 14:15:13 -0800134 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
135 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800136 private HashMap<View, ReorderPreviewAnimation>
137 mShakeAnimators = new HashMap<View, ReorderPreviewAnimation>();
Adam Cohen19f37922012-03-21 11:59:11 -0700138
139 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700140
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700141 // When a drag operation is in progress, holds the nearest cell to the touch point
142 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Joe Onorato4be866d2010-10-10 11:26:02 -0700144 private boolean mDragging = false;
145
Patrick Dubroyce34a972010-10-19 10:34:32 -0700146 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700147 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700148
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800149 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700150 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800151
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800152 public static final int MODE_SHOW_REORDER_HINT = 0;
153 public static final int MODE_DRAG_OVER = 1;
154 public static final int MODE_ON_DROP = 2;
155 public static final int MODE_ON_DROP_EXTERNAL = 3;
156 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700157 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800158 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
159
Adam Cohena897f392012-04-27 18:12:05 -0700160 static final int LANDSCAPE = 0;
161 static final int PORTRAIT = 1;
162
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800163 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700164 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800165 private float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700166
Adam Cohen482ed822012-03-02 14:15:13 -0800167 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
168 private Rect mOccupiedRect = new Rect();
169 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700170 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700171 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700172 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800173
Winson Chung3a6e7f32013-10-09 15:50:52 -0700174 private Rect mTempRect = new Rect();
175
Romain Guy8a0bff52012-05-06 13:14:33 -0700176 private final static PorterDuffXfermode sAddBlendMode =
177 new PorterDuffXfermode(PorterDuff.Mode.ADD);
Michael Jurkaca993832012-06-29 15:17:04 -0700178 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 public CellLayout(Context context) {
181 this(context, null);
182 }
183
184 public CellLayout(Context context, AttributeSet attrs) {
185 this(context, attrs, 0);
186 }
187
188 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
189 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700190 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700191
192 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
193 // the user where a dragged item will land when dropped.
194 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800195 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700196 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700197
Winson Chung892c74d2013-08-22 16:15:50 -0700198 LauncherAppState app = LauncherAppState.getInstance();
199 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
201
Winson Chung11a1a532013-09-13 11:14:45 -0700202 mCellWidth = mCellHeight = -1;
203 mFixedCellHeight = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700204 mWidthGap = mOriginalWidthGap = 0;
205 mHeightGap = mOriginalHeightGap = 0;
206 mMaxGap = Integer.MAX_VALUE;
Winson Chung892c74d2013-08-22 16:15:50 -0700207 mCountX = (int) grid.numColumns;
208 mCountY = (int) grid.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700209 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800210 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700211 mPreviousReorderDirection[0] = INVALID_DIRECTION;
212 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
214 a.recycle();
215
216 setAlwaysDrawnWithCacheEnabled(false);
217
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700218 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700219 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700220
Adam Cohen410f3cd2013-09-22 12:09:32 -0700221 mNormalBackground = res.getDrawable(R.drawable.screenpanel);
222 mActiveGlowBackground = res.getDrawable(R.drawable.screenpanel_hover);
Michael Jurka33945b22010-12-21 18:19:38 -0800223
Adam Cohenb5ba0972011-09-07 18:02:31 -0700224 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
225 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
226 mForegroundPadding =
227 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800228
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800229 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700230 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700231
Winson Chungb26f3d62011-06-02 10:49:29 -0700232 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700233 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700234
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700235 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700236 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700237 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700238 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800239 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700240 }
241
242 // When dragging things around the home screens, we show a green outline of
243 // where the item will land. The outlines gradually fade out, leaving a trail
244 // behind the drag path.
245 // Set up all the animations that are used to implement this fading.
246 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700247 final float fromAlphaValue = 0;
248 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700249
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700250 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700251
252 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700253 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100254 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700255 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700256 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700257 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700258 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700259 final Bitmap outline = (Bitmap)anim.getTag();
260
261 // If an animation is started and then stopped very quickly, we can still
262 // get spurious updates we've cleared the tag. Guard against this.
263 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700264 @SuppressWarnings("all") // suppress dead code warning
265 final boolean debug = false;
266 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700267 Object val = animation.getAnimatedValue();
268 Log.d(TAG, "anim " + thisIndex + " update: " + val +
269 ", isStopped " + anim.isStopped());
270 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700271 // Try to prevent it from continuing to run
272 animation.cancel();
273 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700274 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800275 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700276 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700277 }
278 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700279 // The animation holds a reference to the drag outline bitmap as long is it's
280 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700281 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700282 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700283 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700284 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700285 anim.setTag(null);
286 }
287 }
288 });
289 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700290 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700291
Michael Jurka18014792010-10-14 09:01:34 -0700292 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700293 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800294
Michael Jurkaa52570f2012-03-20 03:18:20 -0700295 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700296 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700297 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700298
Michael Jurkaa52570f2012-03-20 03:18:20 -0700299 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700300 }
301
Chris Craik01f2d7f2013-10-01 14:41:56 -0700302 public void enableHardwareLayer(boolean hasLayer) {
303 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700304 }
305
306 public void buildHardwareLayer() {
307 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700308 }
309
Adam Cohen307fe232012-08-16 17:55:58 -0700310 public float getChildrenScale() {
311 return mIsHotseat ? mHotseatScale : 1.0f;
312 }
313
Winson Chung5f8afe62013-08-12 16:19:28 -0700314 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700315 mFixedCellWidth = mCellWidth = width;
316 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700317 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
318 mCountX, mCountY);
319 }
320
Adam Cohen2801caf2011-05-13 20:57:39 -0700321 public void setGridSize(int x, int y) {
322 mCountX = x;
323 mCountY = y;
324 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800325 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700326 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700327 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700328 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700329 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700330 }
331
Adam Cohen2374abf2013-04-16 14:56:57 -0700332 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
333 public void setInvertIfRtl(boolean invert) {
334 mShortcutsAndWidgets.setInvertIfRtl(invert);
335 }
336
Adam Cohen917e3882013-10-31 15:03:35 -0700337 public void setDropPending(boolean pending) {
338 mDropPending = pending;
339 }
340
341 public boolean isDropPending() {
342 return mDropPending;
343 }
344
Patrick Dubroy96864c32011-03-10 17:17:23 -0800345 private void invalidateBubbleTextView(BubbleTextView icon) {
346 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700347 invalidate(icon.getLeft() + getPaddingLeft() - padding,
348 icon.getTop() + getPaddingTop() - padding,
349 icon.getRight() + getPaddingLeft() + padding,
350 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800351 }
352
Adam Cohenb5ba0972011-09-07 18:02:31 -0700353 void setOverScrollAmount(float r, boolean left) {
354 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
355 mOverScrollForegroundDrawable = mOverScrollLeft;
356 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
357 mOverScrollForegroundDrawable = mOverScrollRight;
358 }
359
Adam Cohen02dcfcc2013-10-01 12:37:33 -0700360 r *= FOREGROUND_ALPHA_DAMPER;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700361 mForegroundAlpha = (int) Math.round((r * 255));
362 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
363 invalidate();
364 }
365
Patrick Dubroy96864c32011-03-10 17:17:23 -0800366 void setPressedOrFocusedIcon(BubbleTextView icon) {
367 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
368 // requires an expanded clip rect (due to the glow's blur radius)
369 BubbleTextView oldIcon = mPressedOrFocusedIcon;
370 mPressedOrFocusedIcon = icon;
371 if (oldIcon != null) {
372 invalidateBubbleTextView(oldIcon);
373 }
374 if (mPressedOrFocusedIcon != null) {
375 invalidateBubbleTextView(mPressedOrFocusedIcon);
376 }
377 }
378
Michael Jurka33945b22010-12-21 18:19:38 -0800379 void setIsDragOverlapping(boolean isDragOverlapping) {
380 if (mIsDragOverlapping != isDragOverlapping) {
381 mIsDragOverlapping = isDragOverlapping;
Adam Cohendedbd962013-07-11 14:21:49 -0700382 setUseActiveGlowBackground(mIsDragOverlapping);
Michael Jurka33945b22010-12-21 18:19:38 -0800383 invalidate();
384 }
385 }
386
Adam Cohendedbd962013-07-11 14:21:49 -0700387 void setUseActiveGlowBackground(boolean use) {
388 mUseActiveGlowBackground = use;
389 }
390
Michael Jurka33945b22010-12-21 18:19:38 -0800391 boolean getIsDragOverlapping() {
392 return mIsDragOverlapping;
393 }
394
Adam Cohenebea84d2011-11-09 17:20:41 -0800395 protected void setOverscrollTransformsDirty(boolean dirty) {
396 mScrollingTransformsDirty = dirty;
397 }
398
399 protected void resetOverscrollTransforms() {
400 if (mScrollingTransformsDirty) {
401 setOverscrollTransformsDirty(false);
402 setTranslationX(0);
403 setRotationY(0);
404 // It doesn't matter if we pass true or false here, the important thing is that we
405 // pass 0, which results in the overscroll drawable not being drawn any more.
406 setOverScrollAmount(0, false);
407 setPivotX(getMeasuredWidth() / 2);
408 setPivotY(getMeasuredHeight() / 2);
409 }
410 }
411
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700412 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700413 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700414 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
415 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
416 // When we're small, we are either drawn normally or in the "accepts drops" state (during
417 // a drag). However, we also drag the mini hover background *over* one of those two
418 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700419 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700420 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800421
Adam Cohendedbd962013-07-11 14:21:49 -0700422 if (mUseActiveGlowBackground) {
Michael Jurka33945b22010-12-21 18:19:38 -0800423 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700424 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700425 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700426 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700427 }
Michael Jurka33945b22010-12-21 18:19:38 -0800428
429 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
430 bg.setBounds(mBackgroundRect);
431 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700432 }
Romain Guya6abce82009-11-10 02:54:41 -0800433
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700434 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700435 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700436 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700437 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800438 final Rect r = mDragOutlines[i];
Winson Chung3a6e7f32013-10-09 15:50:52 -0700439 mTempRect.set(r);
440 Utilities.scaleRectAboutCenter(mTempRect, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700441 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700442 paint.setAlpha((int)(alpha + .5f));
Winson Chung3a6e7f32013-10-09 15:50:52 -0700443 canvas.drawBitmap(b, null, mTempRect, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700444 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700445 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800446
447 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
448 // requires an expanded clip rect (due to the glow's blur radius)
449 if (mPressedOrFocusedIcon != null) {
450 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
451 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
452 if (b != null) {
Winson Chung3a6e7f32013-10-09 15:50:52 -0700453 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
454 (mCountX * mCellWidth);
455 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
456 int top = getPaddingTop();
Patrick Dubroy96864c32011-03-10 17:17:23 -0800457 canvas.drawBitmap(b,
Winson Chung3a6e7f32013-10-09 15:50:52 -0700458 mPressedOrFocusedIcon.getLeft() + left - padding,
459 mPressedOrFocusedIcon.getTop() + top - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800460 null);
461 }
462 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700463
Adam Cohen482ed822012-03-02 14:15:13 -0800464 if (DEBUG_VISUALIZE_OCCUPIED) {
465 int[] pt = new int[2];
466 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700467 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800468 for (int i = 0; i < mCountX; i++) {
469 for (int j = 0; j < mCountY; j++) {
470 if (mOccupied[i][j]) {
471 cellToPoint(i, j, pt);
472 canvas.save();
473 canvas.translate(pt[0], pt[1]);
474 cd.draw(canvas);
475 canvas.restore();
476 }
477 }
478 }
479 }
480
Andrew Flynn850d2e72012-04-26 16:51:20 -0700481 int previewOffset = FolderRingAnimator.sPreviewSize;
482
Adam Cohen69ce2e52011-07-03 19:25:21 -0700483 // The folder outer / inner ring image(s)
Winson Chung5f8afe62013-08-12 16:19:28 -0700484 LauncherAppState app = LauncherAppState.getInstance();
485 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700486 for (int i = 0; i < mFolderOuterRings.size(); i++) {
487 FolderRingAnimator fra = mFolderOuterRings.get(i);
488
Adam Cohen5108bc02013-09-20 17:04:51 -0700489 Drawable d;
490 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700491 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700492 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700493
Winson Chung89f97052013-09-20 11:32:26 -0700494 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700495 int centerX = mTempLocation[0] + mCellWidth / 2;
496 int centerY = mTempLocation[1] + previewOffset / 2 +
497 child.getPaddingTop() + grid.folderBackgroundOffset;
498
Adam Cohen5108bc02013-09-20 17:04:51 -0700499 // Draw outer ring, if it exists
500 if (FolderIcon.HAS_OUTER_RING) {
501 d = FolderRingAnimator.sSharedOuterRingDrawable;
502 width = (int) (fra.getOuterRingSize() * getChildrenScale());
503 height = width;
504 canvas.save();
505 canvas.translate(centerX - width / 2, centerY - height / 2);
506 d.setBounds(0, 0, width, height);
507 d.draw(canvas);
508 canvas.restore();
509 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700510
Winson Chung89f97052013-09-20 11:32:26 -0700511 // Draw inner ring
512 d = FolderRingAnimator.sSharedInnerRingDrawable;
513 width = (int) (fra.getInnerRingSize() * getChildrenScale());
514 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700515 canvas.save();
516 canvas.translate(centerX - width / 2, centerY - width / 2);
517 d.setBounds(0, 0, width, height);
518 d.draw(canvas);
519 canvas.restore();
520 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700521 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700522
523 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
524 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
525 int width = d.getIntrinsicWidth();
526 int height = d.getIntrinsicHeight();
527
528 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700529 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700530 if (child != null) {
531 int centerX = mTempLocation[0] + mCellWidth / 2;
532 int centerY = mTempLocation[1] + previewOffset / 2 +
533 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700534
Winson Chung89f97052013-09-20 11:32:26 -0700535 canvas.save();
536 canvas.translate(centerX - width / 2, centerY - width / 2);
537 d.setBounds(0, 0, width, height);
538 d.draw(canvas);
539 canvas.restore();
540 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700541 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700542 }
543
Adam Cohenb5ba0972011-09-07 18:02:31 -0700544 @Override
545 protected void dispatchDraw(Canvas canvas) {
546 super.dispatchDraw(canvas);
547 if (mForegroundAlpha > 0) {
548 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700549 mOverScrollForegroundDrawable.draw(canvas);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700550 }
551 }
552
Adam Cohen69ce2e52011-07-03 19:25:21 -0700553 public void showFolderAccept(FolderRingAnimator fra) {
554 mFolderOuterRings.add(fra);
555 }
556
557 public void hideFolderAccept(FolderRingAnimator fra) {
558 if (mFolderOuterRings.contains(fra)) {
559 mFolderOuterRings.remove(fra);
560 }
561 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700562 }
563
Adam Cohenc51934b2011-07-26 21:07:43 -0700564 public void setFolderLeaveBehindCell(int x, int y) {
565 mFolderLeaveBehindCell[0] = x;
566 mFolderLeaveBehindCell[1] = y;
567 invalidate();
568 }
569
570 public void clearFolderLeaveBehind() {
571 mFolderLeaveBehindCell[0] = -1;
572 mFolderLeaveBehindCell[1] = -1;
573 invalidate();
574 }
575
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700576 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700577 public boolean shouldDelayChildPressedState() {
578 return false;
579 }
580
Adam Cohen1462de32012-07-24 22:34:36 -0700581 public void restoreInstanceState(SparseArray<Parcelable> states) {
582 dispatchRestoreInstanceState(states);
583 }
584
Michael Jurkae6235dd2011-10-04 15:02:05 -0700585 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700586 public void cancelLongPress() {
587 super.cancelLongPress();
588
589 // Cancel long press for all children
590 final int count = getChildCount();
591 for (int i = 0; i < count; i++) {
592 final View child = getChildAt(i);
593 child.cancelLongPress();
594 }
595 }
596
Michael Jurkadee05892010-07-27 10:01:56 -0700597 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
598 mInterceptTouchListener = listener;
599 }
600
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700602 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
604
605 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700606 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 }
608
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800609 public void setIsHotseat(boolean isHotseat) {
610 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700611 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800612 }
613
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800614 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700615 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700616 final LayoutParams lp = params;
617
Andrew Flynnde38e422012-05-08 11:22:15 -0700618 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800619 if (child instanceof BubbleTextView) {
620 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700621 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800622 }
623
Adam Cohen307fe232012-08-16 17:55:58 -0700624 child.setScaleX(getChildrenScale());
625 child.setScaleY(getChildrenScale());
626
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 // Generate an id for each view, this assumes we have at most 256x256 cells
628 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700629 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700630 // If the horizontal or vertical span is set to -1, it is taken to
631 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700632 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
633 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634
Winson Chungaafa03c2010-06-11 17:34:16 -0700635 child.setId(childId);
636
Michael Jurkaa52570f2012-03-20 03:18:20 -0700637 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700638
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700639 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700640
Winson Chungaafa03c2010-06-11 17:34:16 -0700641 return true;
642 }
643 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700645
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700647 public void removeAllViews() {
648 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700649 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700650 }
651
652 @Override
653 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700654 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700655 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700656 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700657 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700658 }
659
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700660 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700661 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700662 }
663
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 @Override
665 public void removeView(View view) {
666 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700667 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 }
669
670 @Override
671 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700672 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
673 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 }
675
676 @Override
677 public void removeViewInLayout(View view) {
678 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700679 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 }
681
682 @Override
683 public void removeViews(int start, int count) {
684 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700685 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700686 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700687 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700688 }
689
690 @Override
691 public void removeViewsInLayout(int start, int count) {
692 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700693 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700694 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700695 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800696 }
697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 @Override
699 protected void onAttachedToWindow() {
700 super.onAttachedToWindow();
Adam Cohendcd297f2013-06-18 13:13:40 -0700701 if (getParent() instanceof Workspace) {
702 Workspace workspace = (Workspace) getParent();
703 mCellInfo.screenId = workspace.getIdForScreen(this);
704 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 }
706
Michael Jurkaaf442092010-06-10 17:01:57 -0700707 public void setTagToCellInfoForPoint(int touchX, int touchY) {
708 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800709 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700710 final int x = touchX + getScrollX();
711 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700712 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700713
714 boolean found = false;
715 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700716 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800717 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700718
Adam Cohen1b607ed2011-03-03 17:26:50 -0800719 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
720 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700721 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700722
Winson Chungeecf02d2012-03-02 17:14:58 -0800723 float scale = child.getScaleX();
724 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
725 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700726 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
727 // offset that by this CellLayout's padding to test an (x,y) point that is relative
728 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700729 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800730 frame.inset((int) (frame.width() * (1f - scale) / 2),
731 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700732
Michael Jurkaaf442092010-06-10 17:01:57 -0700733 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700734 cellInfo.cell = child;
735 cellInfo.cellX = lp.cellX;
736 cellInfo.cellY = lp.cellY;
737 cellInfo.spanX = lp.cellHSpan;
738 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700739 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700740 break;
741 }
742 }
743 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700744
Michael Jurkad771c962011-08-09 15:00:48 -0700745 mLastDownOnOccupiedCell = found;
746
Michael Jurkaaf442092010-06-10 17:01:57 -0700747 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700748 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700749 pointToCellExact(x, y, cellXY);
750
Michael Jurkaaf442092010-06-10 17:01:57 -0700751 cellInfo.cell = null;
752 cellInfo.cellX = cellXY[0];
753 cellInfo.cellY = cellXY[1];
754 cellInfo.spanX = 1;
755 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700756 }
757 setTag(cellInfo);
758 }
759
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 @Override
761 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700762 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
763 // even in the case where we return early. Not clearing here was causing bugs whereby on
764 // long-press we'd end up picking up an item from a previous drag operation.
765 final int action = ev.getAction();
766
767 if (action == MotionEvent.ACTION_DOWN) {
768 clearTagCellInfo();
769 }
770
Michael Jurkadee05892010-07-27 10:01:56 -0700771 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
772 return true;
773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774
775 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700776 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800778
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 return false;
780 }
781
Adam Cohenc1997fd2011-08-15 18:26:39 -0700782 private void clearTagCellInfo() {
783 final CellInfo cellInfo = mCellInfo;
784 cellInfo.cell = null;
785 cellInfo.cellX = -1;
786 cellInfo.cellY = -1;
787 cellInfo.spanX = 0;
788 cellInfo.spanY = 0;
789 setTag(cellInfo);
790 }
791
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700793 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 }
795
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700796 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700797 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 * @param x X coordinate of the point
799 * @param y Y coordinate of the point
800 * @param result Array of 2 ints to hold the x and y coordinate of the cell
801 */
802 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700803 final int hStartPadding = getPaddingLeft();
804 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805
806 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
807 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
808
Adam Cohend22015c2010-07-26 22:02:18 -0700809 final int xAxis = mCountX;
810 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811
812 if (result[0] < 0) result[0] = 0;
813 if (result[0] >= xAxis) result[0] = xAxis - 1;
814 if (result[1] < 0) result[1] = 0;
815 if (result[1] >= yAxis) result[1] = yAxis - 1;
816 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700817
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 /**
819 * Given a point, return the cell that most closely encloses that point
820 * @param x X coordinate of the point
821 * @param y Y coordinate of the point
822 * @param result Array of 2 ints to hold the x and y coordinate of the cell
823 */
824 void pointToCellRounded(int x, int y, int[] result) {
825 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
826 }
827
828 /**
829 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700830 *
831 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700833 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 * @param result Array of 2 ints to hold the x and y coordinate of the point
835 */
836 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700837 final int hStartPadding = getPaddingLeft();
838 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839
840 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
841 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
842 }
843
Adam Cohene3e27a82011-04-15 12:07:39 -0700844 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800845 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700846 *
847 * @param cellX X coordinate of the cell
848 * @param cellY Y coordinate of the cell
849 *
850 * @param result Array of 2 ints to hold the x and y coordinate of the point
851 */
852 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700853 regionToCenterPoint(cellX, cellY, 1, 1, result);
854 }
855
856 /**
857 * Given a cell coordinate and span return the point that represents the center of the regio
858 *
859 * @param cellX X coordinate of the cell
860 * @param cellY Y coordinate of the cell
861 *
862 * @param result Array of 2 ints to hold the x and y coordinate of the point
863 */
864 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700865 final int hStartPadding = getPaddingLeft();
866 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700867 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
868 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
869 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
870 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700871 }
872
Adam Cohen19f37922012-03-21 11:59:11 -0700873 /**
874 * Given a cell coordinate and span fills out a corresponding pixel rect
875 *
876 * @param cellX X coordinate of the cell
877 * @param cellY Y coordinate of the cell
878 * @param result Rect in which to write the result
879 */
880 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
881 final int hStartPadding = getPaddingLeft();
882 final int vStartPadding = getPaddingTop();
883 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
884 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
885 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
886 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
887 }
888
Adam Cohen482ed822012-03-02 14:15:13 -0800889 public float getDistanceFromCell(float x, float y, int[] cell) {
890 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
891 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
892 Math.pow(y - mTmpPoint[1], 2));
893 return distance;
894 }
895
Romain Guy84f296c2009-11-04 15:00:44 -0800896 int getCellWidth() {
897 return mCellWidth;
898 }
899
900 int getCellHeight() {
901 return mCellHeight;
902 }
903
Adam Cohend4844c32011-02-18 19:25:06 -0800904 int getWidthGap() {
905 return mWidthGap;
906 }
907
908 int getHeightGap() {
909 return mHeightGap;
910 }
911
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700912 Rect getContentRect(Rect r) {
913 if (r == null) {
914 r = new Rect();
915 }
916 int left = getPaddingLeft();
917 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700918 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
919 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700920 r.set(left, top, right, bottom);
921 return r;
922 }
923
Winson Chungfe411c82013-09-26 16:07:17 -0700924 /** Return a rect that has the cellWidth/cellHeight (left, top), and
925 * widthGap/heightGap (right, bottom) */
Winson Chung66700732013-08-20 16:56:15 -0700926 static void getMetrics(Rect metrics, int paddedMeasureWidth,
927 int paddedMeasureHeight, int countX, int countY) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700928 LauncherAppState app = LauncherAppState.getInstance();
929 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -0700930 metrics.set(grid.calculateCellWidth(paddedMeasureWidth, countX),
931 grid.calculateCellHeight(paddedMeasureHeight, countY), 0, 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700932 }
933
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700934 public void setFixedSize(int width, int height) {
935 mFixedWidth = width;
936 mFixedHeight = height;
937 }
938
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 @Override
940 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700941 LauncherAppState app = LauncherAppState.getInstance();
942 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
943
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700946 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
947 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700948 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
949 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700950 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700951 int cw = grid.calculateCellWidth(childWidthSize, mCountX);
952 int ch = grid.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700953 if (cw != mCellWidth || ch != mCellHeight) {
954 mCellWidth = cw;
955 mCellHeight = ch;
956 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
957 mHeightGap, mCountX, mCountY);
958 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700959 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700960
Winson Chung2d75f122013-09-23 16:53:31 -0700961 int newWidth = childWidthSize;
962 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700963 if (mFixedWidth > 0 && mFixedHeight > 0) {
964 newWidth = mFixedWidth;
965 newHeight = mFixedHeight;
966 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
968 }
969
Adam Cohend22015c2010-07-26 22:02:18 -0700970 int numWidthGaps = mCountX - 1;
971 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972
Adam Cohen234c4cd2011-07-17 21:03:04 -0700973 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700974 int hSpace = childWidthSize;
975 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700976 int hFreeSpace = hSpace - (mCountX * mCellWidth);
977 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700978 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
979 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700980 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
981 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700982 } else {
983 mWidthGap = mOriginalWidthGap;
984 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700985 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800986 int count = getChildCount();
Winson Chung5f8afe62013-08-12 16:19:28 -0700987 int maxWidth = 0;
988 int maxHeight = 0;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800989 for (int i = 0; i < count; i++) {
990 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -0700991 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth,
992 MeasureSpec.EXACTLY);
993 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
994 MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800995 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700996 maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
997 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
Michael Jurka8c920dd2011-01-20 14:16:56 -0800998 }
Winson Chung2d75f122013-09-23 16:53:31 -0700999 if (mFixedWidth > 0 && mFixedHeight > 0) {
1000 setMeasuredDimension(maxWidth, maxHeight);
1001 } else {
1002 setMeasuredDimension(widthSize, heightSize);
1003 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 }
1005
1006 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001007 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -07001008 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
1009 (mCountX * mCellWidth);
1010 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
1011 int top = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001014 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -07001015 child.layout(left, top,
1016 left + r - l,
1017 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 }
1019 }
1020
1021 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001022 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1023 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -07001024
1025 // Expand the background drawing bounds by the padding baked into the background drawable
1026 Rect padding = new Rect();
1027 mNormalBackground.getPadding(padding);
1028 mBackgroundRect.set(-padding.left, -padding.top, w + padding.right, h + padding.bottom);
1029
Adam Cohenb5ba0972011-09-07 18:02:31 -07001030 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001031 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001032 }
1033
1034 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001036 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 }
1038
1039 @Override
1040 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001041 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 }
1043
Michael Jurka5f1c5092010-09-03 14:15:02 -07001044 public float getBackgroundAlpha() {
1045 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001046 }
1047
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001048 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001049 if (mBackgroundAlphaMultiplier != multiplier) {
1050 mBackgroundAlphaMultiplier = multiplier;
1051 invalidate();
1052 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001053 }
1054
Adam Cohenddb82192010-11-10 16:32:54 -08001055 public float getBackgroundAlphaMultiplier() {
1056 return mBackgroundAlphaMultiplier;
1057 }
1058
Michael Jurka5f1c5092010-09-03 14:15:02 -07001059 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001060 if (mBackgroundAlpha != alpha) {
1061 mBackgroundAlpha = alpha;
1062 invalidate();
1063 }
Michael Jurkadee05892010-07-27 10:01:56 -07001064 }
1065
Michael Jurkaa52570f2012-03-20 03:18:20 -07001066 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001067 final int childCount = getChildCount();
1068 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001069 getChildAt(i).setAlpha(alpha);
1070 }
1071 }
1072
Michael Jurkaa52570f2012-03-20 03:18:20 -07001073 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1074 if (getChildCount() > 0) {
1075 return (ShortcutAndWidgetContainer) getChildAt(0);
1076 }
1077 return null;
1078 }
1079
Patrick Dubroy440c3602010-07-13 17:50:32 -07001080 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001081 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001082 }
1083
Adam Cohen76fc0852011-06-17 13:26:23 -07001084 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001085 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001086 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001087 boolean[][] occupied = mOccupied;
1088 if (!permanent) {
1089 occupied = mTmpOccupied;
1090 }
1091
Adam Cohen19f37922012-03-21 11:59:11 -07001092 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001093 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1094 final ItemInfo info = (ItemInfo) child.getTag();
1095
1096 // We cancel any existing animations
1097 if (mReorderAnimators.containsKey(lp)) {
1098 mReorderAnimators.get(lp).cancel();
1099 mReorderAnimators.remove(lp);
1100 }
1101
Adam Cohen482ed822012-03-02 14:15:13 -08001102 final int oldX = lp.x;
1103 final int oldY = lp.y;
1104 if (adjustOccupied) {
1105 occupied[lp.cellX][lp.cellY] = false;
1106 occupied[cellX][cellY] = true;
1107 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001108 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001109 if (permanent) {
1110 lp.cellX = info.cellX = cellX;
1111 lp.cellY = info.cellY = cellY;
1112 } else {
1113 lp.tmpCellX = cellX;
1114 lp.tmpCellY = cellY;
1115 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001116 clc.setupLp(lp);
1117 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001118 final int newX = lp.x;
1119 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001120
Adam Cohen76fc0852011-06-17 13:26:23 -07001121 lp.x = oldX;
1122 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001123
Adam Cohen482ed822012-03-02 14:15:13 -08001124 // Exit early if we're not actually moving the view
1125 if (oldX == newX && oldY == newY) {
1126 lp.isLockedToGrid = true;
1127 return true;
1128 }
1129
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001130 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001131 va.setDuration(duration);
1132 mReorderAnimators.put(lp, va);
1133
1134 va.addUpdateListener(new AnimatorUpdateListener() {
1135 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001136 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001137 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001138 lp.x = (int) ((1 - r) * oldX + r * newX);
1139 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001140 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001141 }
1142 });
Adam Cohen482ed822012-03-02 14:15:13 -08001143 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001144 boolean cancelled = false;
1145 public void onAnimationEnd(Animator animation) {
1146 // If the animation was cancelled, it means that another animation
1147 // has interrupted this one, and we don't want to lock the item into
1148 // place just yet.
1149 if (!cancelled) {
1150 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001151 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001152 }
1153 if (mReorderAnimators.containsKey(lp)) {
1154 mReorderAnimators.remove(lp);
1155 }
1156 }
1157 public void onAnimationCancel(Animator animation) {
1158 cancelled = true;
1159 }
1160 });
Adam Cohen482ed822012-03-02 14:15:13 -08001161 va.setStartDelay(delay);
1162 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001163 return true;
1164 }
1165 return false;
1166 }
1167
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001168 /**
1169 * Estimate where the top left cell of the dragged item will land if it is dropped.
1170 *
1171 * @param originX The X value of the top left corner of the item
1172 * @param originY The Y value of the top left corner of the item
1173 * @param spanX The number of horizontal cells that the item spans
1174 * @param spanY The number of vertical cells that the item spans
1175 * @param result The estimated drop cell X and Y.
1176 */
1177 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001178 final int countX = mCountX;
1179 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001180
Michael Jurkaa63c4522010-08-19 13:52:27 -07001181 // pointToCellRounded takes the top left of a cell but will pad that with
1182 // cellWidth/2 and cellHeight/2 when finding the matching cell
1183 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001184
1185 // If the item isn't fully on this screen, snap to the edges
1186 int rightOverhang = result[0] + spanX - countX;
1187 if (rightOverhang > 0) {
1188 result[0] -= rightOverhang; // Snap to right
1189 }
1190 result[0] = Math.max(0, result[0]); // Snap to left
1191 int bottomOverhang = result[1] + spanY - countY;
1192 if (bottomOverhang > 0) {
1193 result[1] -= bottomOverhang; // Snap to bottom
1194 }
1195 result[1] = Math.max(0, result[1]); // Snap to top
1196 }
1197
Adam Cohen482ed822012-03-02 14:15:13 -08001198 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1199 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001200 final int oldDragCellX = mDragCell[0];
1201 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001202
Adam Cohen2801caf2011-05-13 20:57:39 -07001203 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001204 return;
1205 }
1206
Adam Cohen482ed822012-03-02 14:15:13 -08001207 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1208 mDragCell[0] = cellX;
1209 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001210 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001211 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001212 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001213
Joe Onorato4be866d2010-10-10 11:26:02 -07001214 int left = topLeft[0];
1215 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001216
Winson Chungb8c69f32011-10-19 21:36:08 -07001217 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001218 // When drawing the drag outline, it did not account for margin offsets
1219 // added by the view's parent.
1220 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1221 left += lp.leftMargin;
1222 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001223
Adam Cohen99e8b402011-03-25 19:23:43 -07001224 // Offsets due to the size difference between the View and the dragOutline.
1225 // There is a size difference to account for the outer blur, which may lie
1226 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001227 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001228 // We center about the x axis
1229 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1230 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001231 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001232 if (dragOffset != null && dragRegion != null) {
1233 // Center the drag region *horizontally* in the cell and apply a drag
1234 // outline offset
1235 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1236 - dragRegion.width()) / 2;
Winson Chung69737c32013-10-08 17:00:19 -07001237 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1238 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1239 top += dragOffset.y + cellPaddingY;
Winson Chungb8c69f32011-10-19 21:36:08 -07001240 } else {
1241 // Center the drag outline in the cell
1242 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1243 - dragOutline.getWidth()) / 2;
1244 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1245 - dragOutline.getHeight()) / 2;
1246 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001247 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001248 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001249 mDragOutlineAnims[oldIndex].animateOut();
1250 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001251 Rect r = mDragOutlines[mDragOutlineCurrent];
1252 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1253 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001254 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001255 }
Winson Chung150fbab2010-09-29 17:14:26 -07001256
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001257 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1258 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001259 }
1260 }
1261
Adam Cohene0310962011-04-18 16:15:31 -07001262 public void clearDragOutlines() {
1263 final int oldIndex = mDragOutlineCurrent;
1264 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001265 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001266 }
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001269 * Find a vacant area that will fit the given bounds nearest the requested
1270 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001271 *
Romain Guy51afc022009-05-04 18:03:43 -07001272 * @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.
Jeff Sharkey70864282009-04-07 21:08:40 -07001274 * @param spanX Horizontal span of the object.
1275 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001276 * @param result Array in which to place the result, or null (in which case a new array will
1277 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001278 * @return The X, Y cell of a vacant area that can contain this object,
1279 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001281 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1282 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001283 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001284 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001285
Michael Jurka6a1435d2010-09-27 17:35:12 -07001286 /**
1287 * Find a vacant area that will fit the given bounds nearest the requested
1288 * cell location. Uses Euclidean distance to score multiple vacant areas.
1289 *
1290 * @param pixelX The X location at which you want to search for a vacant area.
1291 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001292 * @param minSpanX The minimum horizontal span required
1293 * @param minSpanY The minimum vertical span required
1294 * @param spanX Horizontal span of the object.
1295 * @param spanY Vertical span of the object.
1296 * @param result Array in which to place the result, or null (in which case a new array will
1297 * be allocated)
1298 * @return The X, Y cell of a vacant area that can contain this object,
1299 * nearest the requested location.
1300 */
1301 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1302 int spanY, int[] result, int[] resultSpan) {
1303 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1304 result, resultSpan);
1305 }
1306
1307 /**
1308 * Find a vacant area that will fit the given bounds nearest the requested
1309 * cell location. Uses Euclidean distance to score multiple vacant areas.
1310 *
1311 * @param pixelX The X location at which you want to search for a vacant area.
1312 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001313 * @param spanX Horizontal span of the object.
1314 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001315 * @param ignoreOccupied If true, the result can be an occupied cell
1316 * @param result Array in which to place the result, or null (in which case a new array will
1317 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001318 * @return The X, Y cell of a vacant area that can contain this object,
1319 * nearest the requested location.
1320 */
Adam Cohendf035382011-04-11 17:22:04 -07001321 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1322 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001323 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001324 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001325 }
1326
1327 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1328 private void lazyInitTempRectStack() {
1329 if (mTempRectStack.isEmpty()) {
1330 for (int i = 0; i < mCountX * mCountY; i++) {
1331 mTempRectStack.push(new Rect());
1332 }
1333 }
1334 }
Adam Cohen482ed822012-03-02 14:15:13 -08001335
Adam Cohend41fbf52012-02-16 23:53:59 -08001336 private void recycleTempRects(Stack<Rect> used) {
1337 while (!used.isEmpty()) {
1338 mTempRectStack.push(used.pop());
1339 }
1340 }
1341
1342 /**
1343 * Find a vacant area that will fit the given bounds nearest the requested
1344 * cell location. Uses Euclidean distance to score multiple vacant areas.
1345 *
1346 * @param pixelX The X location at which you want to search for a vacant area.
1347 * @param pixelY The Y location at which you want to search for a vacant area.
1348 * @param minSpanX The minimum horizontal span required
1349 * @param minSpanY The minimum vertical span required
1350 * @param spanX Horizontal span of the object.
1351 * @param spanY Vertical span of the object.
1352 * @param ignoreOccupied If true, the result can be an occupied cell
1353 * @param result Array in which to place the result, or null (in which case a new array will
1354 * be allocated)
1355 * @return The X, Y cell of a vacant area that can contain this object,
1356 * nearest the requested location.
1357 */
1358 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001359 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1360 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001361 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001362 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001363 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001364
Adam Cohene3e27a82011-04-15 12:07:39 -07001365 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1366 // to the center of the item, but we are searching based on the top-left cell, so
1367 // we translate the point over to correspond to the top-left.
1368 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1369 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1370
Jeff Sharkey70864282009-04-07 21:08:40 -07001371 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001372 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001373 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001374 final Rect bestRect = new Rect(-1, -1, -1, -1);
1375 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001376
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001377 final int countX = mCountX;
1378 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001379
Adam Cohend41fbf52012-02-16 23:53:59 -08001380 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1381 spanX < minSpanX || spanY < minSpanY) {
1382 return bestXY;
1383 }
1384
1385 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001386 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001387 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1388 int ySize = -1;
1389 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001390 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001391 // First, let's see if this thing fits anywhere
1392 for (int i = 0; i < minSpanX; i++) {
1393 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001394 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001395 continue inner;
1396 }
Michael Jurkac28de512010-08-13 11:27:44 -07001397 }
1398 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001399 xSize = minSpanX;
1400 ySize = minSpanY;
1401
1402 // We know that the item will fit at _some_ acceptable size, now let's see
1403 // how big we can make it. We'll alternate between incrementing x and y spans
1404 // until we hit a limit.
1405 boolean incX = true;
1406 boolean hitMaxX = xSize >= spanX;
1407 boolean hitMaxY = ySize >= spanY;
1408 while (!(hitMaxX && hitMaxY)) {
1409 if (incX && !hitMaxX) {
1410 for (int j = 0; j < ySize; j++) {
1411 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1412 // We can't move out horizontally
1413 hitMaxX = true;
1414 }
1415 }
1416 if (!hitMaxX) {
1417 xSize++;
1418 }
1419 } else if (!hitMaxY) {
1420 for (int i = 0; i < xSize; i++) {
1421 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1422 // We can't move out vertically
1423 hitMaxY = true;
1424 }
1425 }
1426 if (!hitMaxY) {
1427 ySize++;
1428 }
1429 }
1430 hitMaxX |= xSize >= spanX;
1431 hitMaxY |= ySize >= spanY;
1432 incX = !incX;
1433 }
1434 incX = true;
1435 hitMaxX = xSize >= spanX;
1436 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001437 }
Winson Chung0be025d2011-05-23 17:45:09 -07001438 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001439 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440
Adam Cohend41fbf52012-02-16 23:53:59 -08001441 // We verify that the current rect is not a sub-rect of any of our previous
1442 // candidates. In this case, the current rect is disqualified in favour of the
1443 // containing rect.
1444 Rect currentRect = mTempRectStack.pop();
1445 currentRect.set(x, y, x + xSize, y + ySize);
1446 boolean contained = false;
1447 for (Rect r : validRegions) {
1448 if (r.contains(currentRect)) {
1449 contained = true;
1450 break;
1451 }
1452 }
1453 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001454 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1455 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001456
Adam Cohend41fbf52012-02-16 23:53:59 -08001457 if ((distance <= bestDistance && !contained) ||
1458 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001459 bestDistance = distance;
1460 bestXY[0] = x;
1461 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001462 if (resultSpan != null) {
1463 resultSpan[0] = xSize;
1464 resultSpan[1] = ySize;
1465 }
1466 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 }
1469 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001470 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001471 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472
Adam Cohenc0dcf592011-06-01 15:30:43 -07001473 // Return -1, -1 if no suitable location found
1474 if (bestDistance == Double.MAX_VALUE) {
1475 bestXY[0] = -1;
1476 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001477 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001478 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001479 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001481
Adam Cohen482ed822012-03-02 14:15:13 -08001482 /**
1483 * Find a vacant area that will fit the given bounds nearest the requested
1484 * cell location, and will also weigh in a suggested direction vector of the
1485 * desired location. This method computers distance based on unit grid distances,
1486 * not pixel distances.
1487 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001488 * @param cellX The X cell nearest to which you want to search for a vacant area.
1489 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001490 * @param spanX Horizontal span of the object.
1491 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001492 * @param direction The favored direction in which the views should move from x, y
1493 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1494 * matches exactly. Otherwise we find the best matching direction.
1495 * @param occoupied The array which represents which cells in the CellLayout are occupied
1496 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001497 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001498 * @param result Array in which to place the result, or null (in which case a new array will
1499 * be allocated)
1500 * @return The X, Y cell of a vacant area that can contain this object,
1501 * nearest the requested location.
1502 */
1503 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001504 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001505 // Keep track of best-scoring drop area
1506 final int[] bestXY = result != null ? result : new int[2];
1507 float bestDistance = Float.MAX_VALUE;
1508 int bestDirectionScore = Integer.MIN_VALUE;
1509
1510 final int countX = mCountX;
1511 final int countY = mCountY;
1512
1513 for (int y = 0; y < countY - (spanY - 1); y++) {
1514 inner:
1515 for (int x = 0; x < countX - (spanX - 1); x++) {
1516 // First, let's see if this thing fits anywhere
1517 for (int i = 0; i < spanX; i++) {
1518 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001519 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001520 continue inner;
1521 }
1522 }
1523 }
1524
1525 float distance = (float)
1526 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1527 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001528 computeDirectionVector(x - cellX, y - cellY, curDirection);
1529 // The direction score is just the dot product of the two candidate direction
1530 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001531 int curDirectionScore = direction[0] * curDirection[0] +
1532 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001533 boolean exactDirectionOnly = false;
1534 boolean directionMatches = direction[0] == curDirection[0] &&
1535 direction[0] == curDirection[0];
1536 if ((directionMatches || !exactDirectionOnly) &&
1537 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001538 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1539 bestDistance = distance;
1540 bestDirectionScore = curDirectionScore;
1541 bestXY[0] = x;
1542 bestXY[1] = y;
1543 }
1544 }
1545 }
1546
1547 // Return -1, -1 if no suitable location found
1548 if (bestDistance == Float.MAX_VALUE) {
1549 bestXY[0] = -1;
1550 bestXY[1] = -1;
1551 }
1552 return bestXY;
1553 }
1554
1555 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001556 int[] direction, ItemConfiguration currentState) {
1557 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001558 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001559 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001560 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1561
Adam Cohen8baab352012-03-20 17:39:21 -07001562 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001563
1564 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001565 c.x = mTempLocation[0];
1566 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001567 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001568 }
Adam Cohen8baab352012-03-20 17:39:21 -07001569 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001570 return success;
1571 }
1572
Adam Cohenf3900c22012-11-16 18:28:11 -08001573 /**
1574 * This helper class defines a cluster of views. It helps with defining complex edges
1575 * of the cluster and determining how those edges interact with other views. The edges
1576 * essentially define a fine-grained boundary around the cluster of views -- like a more
1577 * precise version of a bounding box.
1578 */
1579 private class ViewCluster {
1580 final static int LEFT = 0;
1581 final static int TOP = 1;
1582 final static int RIGHT = 2;
1583 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001584
Adam Cohenf3900c22012-11-16 18:28:11 -08001585 ArrayList<View> views;
1586 ItemConfiguration config;
1587 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001588
Adam Cohenf3900c22012-11-16 18:28:11 -08001589 int[] leftEdge = new int[mCountY];
1590 int[] rightEdge = new int[mCountY];
1591 int[] topEdge = new int[mCountX];
1592 int[] bottomEdge = new int[mCountX];
1593 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1594
1595 @SuppressWarnings("unchecked")
1596 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1597 this.views = (ArrayList<View>) views.clone();
1598 this.config = config;
1599 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001600 }
1601
Adam Cohenf3900c22012-11-16 18:28:11 -08001602 void resetEdges() {
1603 for (int i = 0; i < mCountX; i++) {
1604 topEdge[i] = -1;
1605 bottomEdge[i] = -1;
1606 }
1607 for (int i = 0; i < mCountY; i++) {
1608 leftEdge[i] = -1;
1609 rightEdge[i] = -1;
1610 }
1611 leftEdgeDirty = true;
1612 rightEdgeDirty = true;
1613 bottomEdgeDirty = true;
1614 topEdgeDirty = true;
1615 boundingRectDirty = true;
1616 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001617
Adam Cohenf3900c22012-11-16 18:28:11 -08001618 void computeEdge(int which, int[] edge) {
1619 int count = views.size();
1620 for (int i = 0; i < count; i++) {
1621 CellAndSpan cs = config.map.get(views.get(i));
1622 switch (which) {
1623 case LEFT:
1624 int left = cs.x;
1625 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1626 if (left < edge[j] || edge[j] < 0) {
1627 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001628 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001629 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001630 break;
1631 case RIGHT:
1632 int right = cs.x + cs.spanX;
1633 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1634 if (right > edge[j]) {
1635 edge[j] = right;
1636 }
1637 }
1638 break;
1639 case TOP:
1640 int top = cs.y;
1641 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1642 if (top < edge[j] || edge[j] < 0) {
1643 edge[j] = top;
1644 }
1645 }
1646 break;
1647 case BOTTOM:
1648 int bottom = cs.y + cs.spanY;
1649 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1650 if (bottom > edge[j]) {
1651 edge[j] = bottom;
1652 }
1653 }
1654 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001655 }
1656 }
1657 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001658
1659 boolean isViewTouchingEdge(View v, int whichEdge) {
1660 CellAndSpan cs = config.map.get(v);
1661
1662 int[] edge = getEdge(whichEdge);
1663
1664 switch (whichEdge) {
1665 case LEFT:
1666 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1667 if (edge[i] == cs.x + cs.spanX) {
1668 return true;
1669 }
1670 }
1671 break;
1672 case RIGHT:
1673 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1674 if (edge[i] == cs.x) {
1675 return true;
1676 }
1677 }
1678 break;
1679 case TOP:
1680 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1681 if (edge[i] == cs.y + cs.spanY) {
1682 return true;
1683 }
1684 }
1685 break;
1686 case BOTTOM:
1687 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1688 if (edge[i] == cs.y) {
1689 return true;
1690 }
1691 }
1692 break;
1693 }
1694 return false;
1695 }
1696
1697 void shift(int whichEdge, int delta) {
1698 for (View v: views) {
1699 CellAndSpan c = config.map.get(v);
1700 switch (whichEdge) {
1701 case LEFT:
1702 c.x -= delta;
1703 break;
1704 case RIGHT:
1705 c.x += delta;
1706 break;
1707 case TOP:
1708 c.y -= delta;
1709 break;
1710 case BOTTOM:
1711 default:
1712 c.y += delta;
1713 break;
1714 }
1715 }
1716 resetEdges();
1717 }
1718
1719 public void addView(View v) {
1720 views.add(v);
1721 resetEdges();
1722 }
1723
1724 public Rect getBoundingRect() {
1725 if (boundingRectDirty) {
1726 boolean first = true;
1727 for (View v: views) {
1728 CellAndSpan c = config.map.get(v);
1729 if (first) {
1730 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1731 first = false;
1732 } else {
1733 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1734 }
1735 }
1736 }
1737 return boundingRect;
1738 }
1739
1740 public int[] getEdge(int which) {
1741 switch (which) {
1742 case LEFT:
1743 return getLeftEdge();
1744 case RIGHT:
1745 return getRightEdge();
1746 case TOP:
1747 return getTopEdge();
1748 case BOTTOM:
1749 default:
1750 return getBottomEdge();
1751 }
1752 }
1753
1754 public int[] getLeftEdge() {
1755 if (leftEdgeDirty) {
1756 computeEdge(LEFT, leftEdge);
1757 }
1758 return leftEdge;
1759 }
1760
1761 public int[] getRightEdge() {
1762 if (rightEdgeDirty) {
1763 computeEdge(RIGHT, rightEdge);
1764 }
1765 return rightEdge;
1766 }
1767
1768 public int[] getTopEdge() {
1769 if (topEdgeDirty) {
1770 computeEdge(TOP, topEdge);
1771 }
1772 return topEdge;
1773 }
1774
1775 public int[] getBottomEdge() {
1776 if (bottomEdgeDirty) {
1777 computeEdge(BOTTOM, bottomEdge);
1778 }
1779 return bottomEdge;
1780 }
1781
1782 PositionComparator comparator = new PositionComparator();
1783 class PositionComparator implements Comparator<View> {
1784 int whichEdge = 0;
1785 public int compare(View left, View right) {
1786 CellAndSpan l = config.map.get(left);
1787 CellAndSpan r = config.map.get(right);
1788 switch (whichEdge) {
1789 case LEFT:
1790 return (r.x + r.spanX) - (l.x + l.spanX);
1791 case RIGHT:
1792 return l.x - r.x;
1793 case TOP:
1794 return (r.y + r.spanY) - (l.y + l.spanY);
1795 case BOTTOM:
1796 default:
1797 return l.y - r.y;
1798 }
1799 }
1800 }
1801
1802 public void sortConfigurationForEdgePush(int edge) {
1803 comparator.whichEdge = edge;
1804 Collections.sort(config.sortedViews, comparator);
1805 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001806 }
1807
Adam Cohenf3900c22012-11-16 18:28:11 -08001808 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1809 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001810
Adam Cohenf3900c22012-11-16 18:28:11 -08001811 ViewCluster cluster = new ViewCluster(views, currentState);
1812 Rect clusterRect = cluster.getBoundingRect();
1813 int whichEdge;
1814 int pushDistance;
1815 boolean fail = false;
1816
1817 // Determine the edge of the cluster that will be leading the push and how far
1818 // the cluster must be shifted.
1819 if (direction[0] < 0) {
1820 whichEdge = ViewCluster.LEFT;
1821 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001822 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001823 whichEdge = ViewCluster.RIGHT;
1824 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1825 } else if (direction[1] < 0) {
1826 whichEdge = ViewCluster.TOP;
1827 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1828 } else {
1829 whichEdge = ViewCluster.BOTTOM;
1830 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001831 }
1832
Adam Cohenf3900c22012-11-16 18:28:11 -08001833 // Break early for invalid push distance.
1834 if (pushDistance <= 0) {
1835 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001836 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001837
1838 // Mark the occupied state as false for the group of views we want to move.
1839 for (View v: views) {
1840 CellAndSpan c = currentState.map.get(v);
1841 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1842 }
1843
1844 // We save the current configuration -- if we fail to find a solution we will revert
1845 // to the initial state. The process of finding a solution modifies the configuration
1846 // in place, hence the need for revert in the failure case.
1847 currentState.save();
1848
1849 // The pushing algorithm is simplified by considering the views in the order in which
1850 // they would be pushed by the cluster. For example, if the cluster is leading with its
1851 // left edge, we consider sort the views by their right edge, from right to left.
1852 cluster.sortConfigurationForEdgePush(whichEdge);
1853
1854 while (pushDistance > 0 && !fail) {
1855 for (View v: currentState.sortedViews) {
1856 // For each view that isn't in the cluster, we see if the leading edge of the
1857 // cluster is contacting the edge of that view. If so, we add that view to the
1858 // cluster.
1859 if (!cluster.views.contains(v) && v != dragView) {
1860 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1861 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1862 if (!lp.canReorder) {
1863 // The push solution includes the all apps button, this is not viable.
1864 fail = true;
1865 break;
1866 }
1867 cluster.addView(v);
1868 CellAndSpan c = currentState.map.get(v);
1869
1870 // Adding view to cluster, mark it as not occupied.
1871 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1872 }
1873 }
1874 }
1875 pushDistance--;
1876
1877 // The cluster has been completed, now we move the whole thing over in the appropriate
1878 // direction.
1879 cluster.shift(whichEdge, 1);
1880 }
1881
1882 boolean foundSolution = false;
1883 clusterRect = cluster.getBoundingRect();
1884
1885 // Due to the nature of the algorithm, the only check required to verify a valid solution
1886 // is to ensure that completed shifted cluster lies completely within the cell layout.
1887 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1888 clusterRect.bottom <= mCountY) {
1889 foundSolution = true;
1890 } else {
1891 currentState.restore();
1892 }
1893
1894 // In either case, we set the occupied array as marked for the location of the views
1895 for (View v: cluster.views) {
1896 CellAndSpan c = currentState.map.get(v);
1897 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1898 }
1899
1900 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001901 }
1902
Adam Cohen482ed822012-03-02 14:15:13 -08001903 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001904 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001905 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001906
Adam Cohen8baab352012-03-20 17:39:21 -07001907 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001908 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001909 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001910 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001911 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001912 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001913 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001914 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001915 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001916 }
1917 }
Adam Cohen8baab352012-03-20 17:39:21 -07001918
Adam Cohen8baab352012-03-20 17:39:21 -07001919 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001920 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001921 CellAndSpan c = currentState.map.get(v);
1922 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1923 }
1924
Adam Cohen47a876d2012-03-19 13:21:41 -07001925 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1926 int top = boundingRect.top;
1927 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001928 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001929 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001930 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001931 CellAndSpan c = currentState.map.get(v);
1932 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001933 }
1934
Adam Cohen482ed822012-03-02 14:15:13 -08001935 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1936
Adam Cohenf3900c22012-11-16 18:28:11 -08001937 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1938 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001939
Adam Cohen8baab352012-03-20 17:39:21 -07001940 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001941 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001942 int deltaX = mTempLocation[0] - boundingRect.left;
1943 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001944 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001945 CellAndSpan c = currentState.map.get(v);
1946 c.x += deltaX;
1947 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001948 }
1949 success = true;
1950 }
Adam Cohen8baab352012-03-20 17:39:21 -07001951
1952 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001953 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001954 CellAndSpan c = currentState.map.get(v);
1955 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001956 }
1957 return success;
1958 }
1959
1960 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1961 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1962 }
1963
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001964 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1965 // to push items in each of the cardinal directions, in an order based on the direction vector
1966 // passed.
1967 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1968 int[] direction, View ignoreView, ItemConfiguration solution) {
1969 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001970 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001971 // separately in each of the components.
1972 int temp = direction[1];
1973 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001974
1975 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001976 ignoreView, solution)) {
1977 return true;
1978 }
1979 direction[1] = temp;
1980 temp = direction[0];
1981 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001982
1983 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001984 ignoreView, solution)) {
1985 return true;
1986 }
1987 // Revert the direction
1988 direction[0] = temp;
1989
1990 // Now we try pushing in each component of the opposite direction
1991 direction[0] *= -1;
1992 direction[1] *= -1;
1993 temp = direction[1];
1994 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001995 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001996 ignoreView, solution)) {
1997 return true;
1998 }
1999
2000 direction[1] = temp;
2001 temp = direction[0];
2002 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002003 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002004 ignoreView, solution)) {
2005 return true;
2006 }
2007 // revert the direction
2008 direction[0] = temp;
2009 direction[0] *= -1;
2010 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002011
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002012 } else {
2013 // If the direction vector has a single non-zero component, we push first in the
2014 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002015 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002016 ignoreView, solution)) {
2017 return true;
2018 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002019 // Then we try the opposite direction
2020 direction[0] *= -1;
2021 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002022 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002023 ignoreView, solution)) {
2024 return true;
2025 }
2026 // Switch the direction back
2027 direction[0] *= -1;
2028 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002029
2030 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002031 // to find a solution by pushing along the perpendicular axis.
2032
2033 // Swap the components
2034 int temp = direction[1];
2035 direction[1] = direction[0];
2036 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002037 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002038 ignoreView, solution)) {
2039 return true;
2040 }
2041
2042 // Then we try the opposite direction
2043 direction[0] *= -1;
2044 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002045 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002046 ignoreView, solution)) {
2047 return true;
2048 }
2049 // Switch the direction back
2050 direction[0] *= -1;
2051 direction[1] *= -1;
2052
2053 // Swap the components back
2054 temp = direction[1];
2055 direction[1] = direction[0];
2056 direction[0] = temp;
2057 }
2058 return false;
2059 }
2060
Adam Cohen482ed822012-03-02 14:15:13 -08002061 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002062 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002063 // Return early if get invalid cell positions
2064 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002065
Adam Cohen8baab352012-03-20 17:39:21 -07002066 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002067 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002068
Adam Cohen8baab352012-03-20 17:39:21 -07002069 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002070 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002071 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002072 if (c != null) {
2073 c.x = cellX;
2074 c.y = cellY;
2075 }
Adam Cohen482ed822012-03-02 14:15:13 -08002076 }
Adam Cohen482ed822012-03-02 14:15:13 -08002077 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2078 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002079 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002080 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002081 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002082 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002083 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002084 if (Rect.intersects(r0, r1)) {
2085 if (!lp.canReorder) {
2086 return false;
2087 }
2088 mIntersectingViews.add(child);
2089 }
2090 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002091
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002092 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
2093
Winson Chung5f8afe62013-08-12 16:19:28 -07002094 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002095 // we try to find a solution such that no displaced item travels through another item
2096 // without also displacing that item.
2097 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002098 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002099 return true;
2100 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002101
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002102 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002103 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002104 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002105 return true;
2106 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002107
Adam Cohen482ed822012-03-02 14:15:13 -08002108 // Ok, they couldn't move as a block, let's move them individually
2109 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002110 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002111 return false;
2112 }
2113 }
2114 return true;
2115 }
2116
2117 /*
2118 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2119 * the provided point and the provided cell
2120 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002121 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002122 double angle = Math.atan(((float) deltaY) / deltaX);
2123
2124 result[0] = 0;
2125 result[1] = 0;
2126 if (Math.abs(Math.cos(angle)) > 0.5f) {
2127 result[0] = (int) Math.signum(deltaX);
2128 }
2129 if (Math.abs(Math.sin(angle)) > 0.5f) {
2130 result[1] = (int) Math.signum(deltaY);
2131 }
2132 }
2133
Adam Cohen8baab352012-03-20 17:39:21 -07002134 private void copyOccupiedArray(boolean[][] occupied) {
2135 for (int i = 0; i < mCountX; i++) {
2136 for (int j = 0; j < mCountY; j++) {
2137 occupied[i][j] = mOccupied[i][j];
2138 }
2139 }
2140 }
2141
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002142 ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
2143 int spanX, int spanY, int[] direction, View dragView, boolean decX,
2144 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002145 // Copy the current state into the solution. This solution will be manipulated as necessary.
2146 copyCurrentStateToSolution(solution, false);
2147 // Copy the current occupied array into the temporary occupied array. This array will be
2148 // manipulated as necessary to find a solution.
2149 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002150
2151 // We find the nearest cell into which we would place the dragged item, assuming there's
2152 // nothing in its way.
2153 int result[] = new int[2];
2154 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2155
2156 boolean success = false;
2157 // First we try the exact nearest position of the item being dragged,
2158 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002159 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2160 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002161
2162 if (!success) {
2163 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2164 // x, then 1 in y etc.
2165 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002166 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
2167 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002168 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002169 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
2170 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002171 }
2172 solution.isSolution = false;
2173 } else {
2174 solution.isSolution = true;
2175 solution.dragViewX = result[0];
2176 solution.dragViewY = result[1];
2177 solution.dragViewSpanX = spanX;
2178 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002179 }
2180 return solution;
2181 }
2182
2183 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002184 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002185 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002186 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002187 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002188 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002189 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002190 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002191 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002192 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002193 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002194 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002195 }
2196 }
2197
2198 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2199 for (int i = 0; i < mCountX; i++) {
2200 for (int j = 0; j < mCountY; j++) {
2201 mTmpOccupied[i][j] = false;
2202 }
2203 }
2204
Michael Jurkaa52570f2012-03-20 03:18:20 -07002205 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002206 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002207 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002208 if (child == dragView) continue;
2209 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002210 CellAndSpan c = solution.map.get(child);
2211 if (c != null) {
2212 lp.tmpCellX = c.x;
2213 lp.tmpCellY = c.y;
2214 lp.cellHSpan = c.spanX;
2215 lp.cellVSpan = c.spanY;
2216 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002217 }
2218 }
2219 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2220 solution.dragViewSpanY, mTmpOccupied, true);
2221 }
2222
2223 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2224 commitDragView) {
2225
2226 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2227 for (int i = 0; i < mCountX; i++) {
2228 for (int j = 0; j < mCountY; j++) {
2229 occupied[i][j] = false;
2230 }
2231 }
2232
Michael Jurkaa52570f2012-03-20 03:18:20 -07002233 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002234 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002235 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002236 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002237 CellAndSpan c = solution.map.get(child);
2238 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002239 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2240 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002241 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002242 }
2243 }
2244 if (commitDragView) {
2245 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2246 solution.dragViewSpanY, occupied, true);
2247 }
2248 }
2249
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002250
2251 // This method starts or changes the reorder preview animations
2252 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2253 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002254 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002255 for (int i = 0; i < childCount; i++) {
2256 View child = mShortcutsAndWidgets.getChildAt(i);
2257 if (child == dragView) continue;
2258 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002259 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2260 != null && !solution.intersectingViews.contains(child);
2261
Adam Cohen19f37922012-03-21 11:59:11 -07002262 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002263 if (c != null && !skip) {
2264 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2265 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002266 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002267 }
2268 }
2269 }
2270
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002271 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002272 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002273 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002274 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002275 float finalDeltaX;
2276 float finalDeltaY;
2277 float initDeltaX;
2278 float initDeltaY;
2279 float finalScale;
2280 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002281 int mode;
2282 boolean repeating = false;
2283 private static final int PREVIEW_DURATION = 300;
2284 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2285
2286 public static final int MODE_HINT = 0;
2287 public static final int MODE_PREVIEW = 1;
2288
Adam Cohene7587d22012-05-24 18:50:02 -07002289 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002290
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002291 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2292 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002293 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2294 final int x0 = mTmpPoint[0];
2295 final int y0 = mTmpPoint[1];
2296 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2297 final int x1 = mTmpPoint[0];
2298 final int y1 = mTmpPoint[1];
2299 final int dX = x1 - x0;
2300 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002301 finalDeltaX = 0;
2302 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002303 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002304 if (dX == dY && dX == 0) {
2305 } else {
2306 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002307 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002308 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002309 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002310 } else {
2311 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002312 finalDeltaX = (int) (- dir * Math.signum(dX) *
2313 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2314 finalDeltaY = (int) (- dir * Math.signum(dY) *
2315 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002316 }
2317 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002318 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002319 initDeltaX = child.getTranslationX();
2320 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002321 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002322 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002323 this.child = child;
2324 }
2325
Adam Cohend024f982012-05-23 18:26:45 -07002326 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002327 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002328 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002329 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002330 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002331 if (finalDeltaX == 0 && finalDeltaY == 0) {
2332 completeAnimationImmediately();
2333 return;
2334 }
Adam Cohen19f37922012-03-21 11:59:11 -07002335 }
Adam Cohend024f982012-05-23 18:26:45 -07002336 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002337 return;
2338 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002339 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002340 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002341 va.setRepeatMode(ValueAnimator.REVERSE);
2342 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002343 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002344 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002345 va.addUpdateListener(new AnimatorUpdateListener() {
2346 @Override
2347 public void onAnimationUpdate(ValueAnimator animation) {
2348 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002349 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2350 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2351 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002352 child.setTranslationX(x);
2353 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002354 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002355 child.setScaleX(s);
2356 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002357 }
2358 });
2359 va.addListener(new AnimatorListenerAdapter() {
2360 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002361 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002362 initDeltaX = 0;
2363 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002364 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002365 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002366 }
2367 });
Adam Cohen19f37922012-03-21 11:59:11 -07002368 mShakeAnimators.put(child, this);
2369 va.start();
2370 }
2371
Adam Cohend024f982012-05-23 18:26:45 -07002372 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002373 if (a != null) {
2374 a.cancel();
2375 }
Adam Cohen19f37922012-03-21 11:59:11 -07002376 }
Adam Cohene7587d22012-05-24 18:50:02 -07002377
Brandon Keely50e6e562012-05-08 16:28:49 -07002378 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002379 if (a != null) {
2380 a.cancel();
2381 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002382
Michael Jurka2ecf9952012-06-18 12:52:28 -07002383 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002384 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002385 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002386 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2387 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002388 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2389 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002390 );
2391 s.setDuration(REORDER_ANIMATION_DURATION);
2392 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2393 s.start();
2394 }
Adam Cohen19f37922012-03-21 11:59:11 -07002395 }
2396
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002397 private void completeAndClearReorderPreviewAnimations() {
2398 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002399 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002400 }
2401 mShakeAnimators.clear();
2402 }
2403
Adam Cohen482ed822012-03-02 14:15:13 -08002404 private void commitTempPlacement() {
2405 for (int i = 0; i < mCountX; i++) {
2406 for (int j = 0; j < mCountY; j++) {
2407 mOccupied[i][j] = mTmpOccupied[i][j];
2408 }
2409 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002410 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002411 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002412 View child = mShortcutsAndWidgets.getChildAt(i);
2413 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2414 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002415 // We do a null check here because the item info can be null in the case of the
2416 // AllApps button in the hotseat.
2417 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002418 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2419 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2420 info.requiresDbUpdate = true;
2421 }
Adam Cohen2acce882012-03-28 19:03:19 -07002422 info.cellX = lp.cellX = lp.tmpCellX;
2423 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002424 info.spanX = lp.cellHSpan;
2425 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002426 }
Adam Cohen482ed822012-03-02 14:15:13 -08002427 }
Adam Cohen2acce882012-03-28 19:03:19 -07002428 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002429 }
2430
2431 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002432 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002433 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002434 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002435 lp.useTmpCoords = useTempCoords;
2436 }
2437 }
2438
Adam Cohen482ed822012-03-02 14:15:13 -08002439 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2440 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2441 int[] result = new int[2];
2442 int[] resultSpan = new int[2];
2443 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2444 resultSpan);
2445 if (result[0] >= 0 && result[1] >= 0) {
2446 copyCurrentStateToSolution(solution, false);
2447 solution.dragViewX = result[0];
2448 solution.dragViewY = result[1];
2449 solution.dragViewSpanX = resultSpan[0];
2450 solution.dragViewSpanY = resultSpan[1];
2451 solution.isSolution = true;
2452 } else {
2453 solution.isSolution = false;
2454 }
2455 return solution;
2456 }
2457
2458 public void prepareChildForDrag(View child) {
2459 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002460 }
2461
Adam Cohen19f37922012-03-21 11:59:11 -07002462 /* This seems like it should be obvious and straight-forward, but when the direction vector
2463 needs to match with the notion of the dragView pushing other views, we have to employ
2464 a slightly more subtle notion of the direction vector. The question is what two points is
2465 the vector between? The center of the dragView and its desired destination? Not quite, as
2466 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2467 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2468 or right, which helps make pushing feel right.
2469 */
2470 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2471 int spanY, View dragView, int[] resultDirection) {
2472 int[] targetDestination = new int[2];
2473
2474 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2475 Rect dragRect = new Rect();
2476 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2477 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2478
2479 Rect dropRegionRect = new Rect();
2480 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2481 dragView, dropRegionRect, mIntersectingViews);
2482
2483 int dropRegionSpanX = dropRegionRect.width();
2484 int dropRegionSpanY = dropRegionRect.height();
2485
2486 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2487 dropRegionRect.height(), dropRegionRect);
2488
2489 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2490 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2491
2492 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2493 deltaX = 0;
2494 }
2495 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2496 deltaY = 0;
2497 }
2498
2499 if (deltaX == 0 && deltaY == 0) {
2500 // No idea what to do, give a random direction.
2501 resultDirection[0] = 1;
2502 resultDirection[1] = 0;
2503 } else {
2504 computeDirectionVector(deltaX, deltaY, resultDirection);
2505 }
2506 }
2507
2508 // For a given cell and span, fetch the set of views intersecting the region.
2509 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2510 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2511 if (boundingRect != null) {
2512 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2513 }
2514 intersectingViews.clear();
2515 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2516 Rect r1 = new Rect();
2517 final int count = mShortcutsAndWidgets.getChildCount();
2518 for (int i = 0; i < count; i++) {
2519 View child = mShortcutsAndWidgets.getChildAt(i);
2520 if (child == dragView) continue;
2521 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2522 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2523 if (Rect.intersects(r0, r1)) {
2524 mIntersectingViews.add(child);
2525 if (boundingRect != null) {
2526 boundingRect.union(r1);
2527 }
2528 }
2529 }
2530 }
2531
2532 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2533 View dragView, int[] result) {
2534 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2535 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2536 mIntersectingViews);
2537 return !mIntersectingViews.isEmpty();
2538 }
2539
2540 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002541 completeAndClearReorderPreviewAnimations();
2542 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2543 final int count = mShortcutsAndWidgets.getChildCount();
2544 for (int i = 0; i < count; i++) {
2545 View child = mShortcutsAndWidgets.getChildAt(i);
2546 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2547 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2548 lp.tmpCellX = lp.cellX;
2549 lp.tmpCellY = lp.cellY;
2550 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2551 0, false, false);
2552 }
Adam Cohen19f37922012-03-21 11:59:11 -07002553 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002554 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002555 }
Adam Cohen19f37922012-03-21 11:59:11 -07002556 }
2557
Adam Cohenbebf0422012-04-11 18:06:28 -07002558 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2559 View dragView, int[] direction, boolean commit) {
2560 int[] pixelXY = new int[2];
2561 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2562
2563 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002564 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002565 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2566
2567 setUseTempCoords(true);
2568 if (swapSolution != null && swapSolution.isSolution) {
2569 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2570 // committing anything or animating anything as we just want to determine if a solution
2571 // exists
2572 copySolutionToTempState(swapSolution, dragView);
2573 setItemPlacementDirty(true);
2574 animateItemsToSolution(swapSolution, dragView, commit);
2575
2576 if (commit) {
2577 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002578 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002579 setItemPlacementDirty(false);
2580 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002581 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2582 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002583 }
2584 mShortcutsAndWidgets.requestLayout();
2585 }
2586 return swapSolution.isSolution;
2587 }
2588
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002589 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002590 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002591 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002592 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002593
2594 if (resultSpan == null) {
2595 resultSpan = new int[2];
2596 }
2597
Adam Cohen19f37922012-03-21 11:59:11 -07002598 // When we are checking drop validity or actually dropping, we don't recompute the
2599 // direction vector, since we want the solution to match the preview, and it's possible
2600 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002601 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2602 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002603 mDirectionVector[0] = mPreviousReorderDirection[0];
2604 mDirectionVector[1] = mPreviousReorderDirection[1];
2605 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002606 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2607 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2608 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002609 }
2610 } else {
2611 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2612 mPreviousReorderDirection[0] = mDirectionVector[0];
2613 mPreviousReorderDirection[1] = mDirectionVector[1];
2614 }
2615
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002616 // Find a solution involving pushing / displacing any items in the way
2617 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002618 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2619
2620 // We attempt the approach which doesn't shuffle views at all
2621 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2622 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2623
2624 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002625
2626 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2627 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002628 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2629 finalSolution = swapSolution;
2630 } else if (noShuffleSolution.isSolution) {
2631 finalSolution = noShuffleSolution;
2632 }
2633
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002634 if (mode == MODE_SHOW_REORDER_HINT) {
2635 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2636 ReorderPreviewAnimation.MODE_HINT);
2637 if (finalSolution != null) {
2638 result[0] = finalSolution.dragViewX;
2639 result[1] = finalSolution.dragViewY;
2640 resultSpan[0] = finalSolution.dragViewSpanX;
2641 resultSpan[1] = finalSolution.dragViewSpanY;
2642 } else {
2643 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2644 }
2645 return result;
2646 }
2647
Adam Cohen482ed822012-03-02 14:15:13 -08002648 boolean foundSolution = true;
2649 if (!DESTRUCTIVE_REORDER) {
2650 setUseTempCoords(true);
2651 }
2652
2653 if (finalSolution != null) {
2654 result[0] = finalSolution.dragViewX;
2655 result[1] = finalSolution.dragViewY;
2656 resultSpan[0] = finalSolution.dragViewSpanX;
2657 resultSpan[1] = finalSolution.dragViewSpanY;
2658
2659 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2660 // committing anything or animating anything as we just want to determine if a solution
2661 // exists
2662 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2663 if (!DESTRUCTIVE_REORDER) {
2664 copySolutionToTempState(finalSolution, dragView);
2665 }
2666 setItemPlacementDirty(true);
2667 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2668
Adam Cohen19f37922012-03-21 11:59:11 -07002669 if (!DESTRUCTIVE_REORDER &&
2670 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002671 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002672 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002673 setItemPlacementDirty(false);
2674 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002675 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2676 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002677 }
2678 }
2679 } else {
2680 foundSolution = false;
2681 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2682 }
2683
2684 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2685 setUseTempCoords(false);
2686 }
Adam Cohen482ed822012-03-02 14:15:13 -08002687
Michael Jurkaa52570f2012-03-20 03:18:20 -07002688 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002689 return result;
2690 }
2691
Adam Cohen19f37922012-03-21 11:59:11 -07002692 void setItemPlacementDirty(boolean dirty) {
2693 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002694 }
Adam Cohen19f37922012-03-21 11:59:11 -07002695 boolean isItemPlacementDirty() {
2696 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002697 }
2698
2699 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002700 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002701 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2702 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002703 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002704 boolean isSolution = false;
2705 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2706
Adam Cohenf3900c22012-11-16 18:28:11 -08002707 void save() {
2708 // Copy current state into savedMap
2709 for (View v: map.keySet()) {
2710 map.get(v).copy(savedMap.get(v));
2711 }
2712 }
2713
2714 void restore() {
2715 // Restore current state from savedMap
2716 for (View v: savedMap.keySet()) {
2717 savedMap.get(v).copy(map.get(v));
2718 }
2719 }
2720
2721 void add(View v, CellAndSpan cs) {
2722 map.put(v, cs);
2723 savedMap.put(v, new CellAndSpan());
2724 sortedViews.add(v);
2725 }
2726
Adam Cohen482ed822012-03-02 14:15:13 -08002727 int area() {
2728 return dragViewSpanX * dragViewSpanY;
2729 }
Adam Cohen8baab352012-03-20 17:39:21 -07002730 }
2731
2732 private class CellAndSpan {
2733 int x, y;
2734 int spanX, spanY;
2735
Adam Cohenf3900c22012-11-16 18:28:11 -08002736 public CellAndSpan() {
2737 }
2738
2739 public void copy(CellAndSpan copy) {
2740 copy.x = x;
2741 copy.y = y;
2742 copy.spanX = spanX;
2743 copy.spanY = spanY;
2744 }
2745
Adam Cohen8baab352012-03-20 17:39:21 -07002746 public CellAndSpan(int x, int y, int spanX, int spanY) {
2747 this.x = x;
2748 this.y = y;
2749 this.spanX = spanX;
2750 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002751 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002752
2753 public String toString() {
2754 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2755 }
2756
Adam Cohen482ed822012-03-02 14:15:13 -08002757 }
2758
Adam Cohendf035382011-04-11 17:22:04 -07002759 /**
2760 * Find a vacant area that will fit the given bounds nearest the requested
2761 * cell location. Uses Euclidean distance to score multiple vacant areas.
2762 *
2763 * @param pixelX The X location at which you want to search for a vacant area.
2764 * @param pixelY The Y location at which you want to search for a vacant area.
2765 * @param spanX Horizontal span of the object.
2766 * @param spanY Vertical span of the object.
2767 * @param ignoreView Considers space occupied by this view as unoccupied
2768 * @param result Previously returned value to possibly recycle.
2769 * @return The X, Y cell of a vacant area that can contain this object,
2770 * nearest the requested location.
2771 */
2772 int[] findNearestVacantArea(
2773 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2774 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2775 }
2776
2777 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002778 * Find a vacant area that will fit the given bounds nearest the requested
2779 * cell location. Uses Euclidean distance to score multiple vacant areas.
2780 *
2781 * @param pixelX The X location at which you want to search for a vacant area.
2782 * @param pixelY The Y location at which you want to search for a vacant area.
2783 * @param minSpanX The minimum horizontal span required
2784 * @param minSpanY The minimum vertical span required
2785 * @param spanX Horizontal span of the object.
2786 * @param spanY Vertical span of the object.
2787 * @param ignoreView Considers space occupied by this view as unoccupied
2788 * @param result Previously returned value to possibly recycle.
2789 * @return The X, Y cell of a vacant area that can contain this object,
2790 * nearest the requested location.
2791 */
2792 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2793 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002794 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2795 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002796 }
2797
2798 /**
Adam Cohendf035382011-04-11 17:22:04 -07002799 * Find a starting cell position that will fit the given bounds nearest the requested
2800 * cell location. Uses Euclidean distance to score multiple vacant areas.
2801 *
2802 * @param pixelX The X location at which you want to search for a vacant area.
2803 * @param pixelY The Y location at which you want to search for a vacant area.
2804 * @param spanX Horizontal span of the object.
2805 * @param spanY Vertical span of the object.
2806 * @param ignoreView Considers space occupied by this view as unoccupied
2807 * @param result Previously returned value to possibly recycle.
2808 * @return The X, Y cell of a vacant area that can contain this object,
2809 * nearest the requested location.
2810 */
2811 int[] findNearestArea(
2812 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2813 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2814 }
2815
Michael Jurka0280c3b2010-09-17 15:00:07 -07002816 boolean existsEmptyCell() {
2817 return findCellForSpan(null, 1, 1);
2818 }
2819
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002820 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002821 * Finds the upper-left coordinate of the first rectangle in the grid that can
2822 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2823 * then this method will only return coordinates for rectangles that contain the cell
2824 * (intersectX, intersectY)
2825 *
2826 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2827 * can be found.
2828 * @param spanX The horizontal span of the cell we want to find.
2829 * @param spanY The vertical span of the cell we want to find.
2830 *
2831 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002832 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002833 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002834 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002835 }
2836
2837 /**
2838 * Like above, but ignores any cells occupied by the item "ignoreView"
2839 *
2840 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2841 * can be found.
2842 * @param spanX The horizontal span of the cell we want to find.
2843 * @param spanY The vertical span of the cell we want to find.
2844 * @param ignoreView The home screen item we should treat as not occupying any space
2845 * @return
2846 */
2847 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002848 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2849 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002850 }
2851
2852 /**
2853 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2854 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2855 *
2856 * @param spanX The horizontal span of the cell we want to find.
2857 * @param spanY The vertical span of the cell we want to find.
2858 * @param ignoreView The home screen item we should treat as not occupying any space
2859 * @param intersectX The X coordinate of the cell that we should try to overlap
2860 * @param intersectX The Y coordinate of the cell that we should try to overlap
2861 *
2862 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2863 */
2864 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2865 int intersectX, int intersectY) {
2866 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002867 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002868 }
2869
2870 /**
2871 * The superset of the above two methods
2872 */
2873 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002874 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002875 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002876 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002877
Michael Jurka28750fb2010-09-24 17:43:49 -07002878 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002879 while (true) {
2880 int startX = 0;
2881 if (intersectX >= 0) {
2882 startX = Math.max(startX, intersectX - (spanX - 1));
2883 }
2884 int endX = mCountX - (spanX - 1);
2885 if (intersectX >= 0) {
2886 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2887 }
2888 int startY = 0;
2889 if (intersectY >= 0) {
2890 startY = Math.max(startY, intersectY - (spanY - 1));
2891 }
2892 int endY = mCountY - (spanY - 1);
2893 if (intersectY >= 0) {
2894 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2895 }
2896
Winson Chungbbc60d82010-11-11 16:34:41 -08002897 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002898 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002899 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002900 for (int i = 0; i < spanX; i++) {
2901 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002902 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002903 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002904 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002905 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002906 continue inner;
2907 }
2908 }
2909 }
2910 if (cellXY != null) {
2911 cellXY[0] = x;
2912 cellXY[1] = y;
2913 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002914 foundCell = true;
2915 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002916 }
2917 }
2918 if (intersectX == -1 && intersectY == -1) {
2919 break;
2920 } else {
2921 // if we failed to find anything, try again but without any requirements of
2922 // intersecting
2923 intersectX = -1;
2924 intersectY = -1;
2925 continue;
2926 }
2927 }
2928
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002929 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002930 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002931 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002932 }
2933
2934 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002935 * A drag event has begun over this layout.
2936 * It may have begun over this layout (in which case onDragChild is called first),
2937 * or it may have begun on another layout.
2938 */
2939 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002940 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002941 mDragging = true;
2942 }
2943
2944 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002945 * Called when drag has left this CellLayout or has been completed (successfully or not)
2946 */
2947 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002948 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002949 // This can actually be called when we aren't in a drag, e.g. when adding a new
2950 // item to this layout via the customize drawer.
2951 // Guard against that case.
2952 if (mDragging) {
2953 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002954 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002955
2956 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002957 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002958 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2959 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002960 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002961 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002962 }
2963
2964 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002965 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002966 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002967 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 *
2969 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002970 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002971 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002972 if (child != null) {
2973 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002974 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002975 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 }
2978
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002980 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002981 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002982 * @param cellX X coordinate of upper left corner expressed as a cell position
2983 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002984 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002986 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002987 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002988 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989 final int cellWidth = mCellWidth;
2990 final int cellHeight = mCellHeight;
2991 final int widthGap = mWidthGap;
2992 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002993
Winson Chung4b825dcd2011-06-19 12:41:22 -07002994 final int hStartPadding = getPaddingLeft();
2995 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002996
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002997 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2998 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2999
3000 int x = hStartPadding + cellX * (cellWidth + widthGap);
3001 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07003002
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07003003 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003004 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07003007 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003008 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07003009 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003010 * @param width Width in pixels
3011 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07003012 * @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 -08003013 */
Winson Chung66700732013-08-20 16:56:15 -07003014 public static int[] rectToCell(int width, int height, int[] result) {
Winson Chung5f8afe62013-08-12 16:19:28 -07003015 LauncherAppState app = LauncherAppState.getInstance();
3016 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -07003017 Rect padding = grid.getWorkspacePadding(grid.isLandscape ?
3018 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
Winson Chung5f8afe62013-08-12 16:19:28 -07003019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020 // Always assume we're working with the smallest span to make sure we
3021 // reserve enough space in both orientations.
Winson Chung66700732013-08-20 16:56:15 -07003022 int parentWidth = grid.calculateCellWidth(grid.widthPx
3023 - padding.left - padding.right, (int) grid.numColumns);
3024 int parentHeight = grid.calculateCellHeight(grid.heightPx
3025 - padding.top - padding.bottom, (int) grid.numRows);
3026 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04003027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07003029 int spanX = (int) Math.ceil(width / (float) smallerSize);
3030 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04003031
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07003032 if (result == null) {
3033 return new int[] { spanX, spanY };
3034 }
3035 result[0] = spanX;
3036 result[1] = spanY;
3037 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038 }
3039
Michael Jurkaf12c75c2011-01-25 22:41:40 -08003040 public int[] cellSpansToSize(int hSpans, int vSpans) {
3041 int[] size = new int[2];
3042 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
3043 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
3044 return size;
3045 }
3046
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003047 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08003048 * Calculate the grid spans needed to fit given item
3049 */
3050 public void calculateSpans(ItemInfo info) {
3051 final int minWidth;
3052 final int minHeight;
3053
3054 if (info instanceof LauncherAppWidgetInfo) {
3055 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3056 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3057 } else if (info instanceof PendingAddWidgetInfo) {
3058 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3059 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3060 } else {
3061 // It's not a widget, so it must be 1x1
3062 info.spanX = info.spanY = 1;
3063 return;
3064 }
3065 int[] spans = rectToCell(minWidth, minHeight, null);
3066 info.spanX = spans[0];
3067 info.spanY = spans[1];
3068 }
3069
3070 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003071 * Find the first vacant cell, if there is one.
3072 *
3073 * @param vacant Holds the x and y coordinate of the vacant cell
3074 * @param spanX Horizontal cell span.
3075 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003076 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003077 * @return True if a vacant cell was found
3078 */
3079 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003080
Michael Jurka0280c3b2010-09-17 15:00:07 -07003081 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 }
3083
3084 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3085 int xCount, int yCount, boolean[][] occupied) {
3086
Adam Cohen2801caf2011-05-13 20:57:39 -07003087 for (int y = 0; y < yCount; y++) {
3088 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003089 boolean available = !occupied[x][y];
3090out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3091 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3092 available = available && !occupied[i][j];
3093 if (!available) break out;
3094 }
3095 }
3096
3097 if (available) {
3098 vacant[0] = x;
3099 vacant[1] = y;
3100 return true;
3101 }
3102 }
3103 }
3104
3105 return false;
3106 }
3107
Michael Jurka0280c3b2010-09-17 15:00:07 -07003108 private void clearOccupiedCells() {
3109 for (int x = 0; x < mCountX; x++) {
3110 for (int y = 0; y < mCountY; y++) {
3111 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003112 }
3113 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003115
Adam Cohend41fbf52012-02-16 23:53:59 -08003116 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003117 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003118 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003119 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120
Adam Cohend4844c32011-02-18 19:25:06 -08003121 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003122 markCellsAsOccupiedForView(view, mOccupied);
3123 }
3124 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003125 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003126 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003127 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003128 }
3129
Adam Cohend4844c32011-02-18 19:25:06 -08003130 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003131 markCellsAsUnoccupiedForView(view, mOccupied);
3132 }
3133 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003134 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003135 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003136 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003137 }
3138
Adam Cohen482ed822012-03-02 14:15:13 -08003139 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3140 boolean value) {
3141 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003142 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3143 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003144 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 }
3146 }
3147 }
3148
Adam Cohen2801caf2011-05-13 20:57:39 -07003149 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003150 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003151 (Math.max((mCountX - 1), 0) * mWidthGap);
3152 }
3153
3154 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003155 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003156 (Math.max((mCountY - 1), 0) * mHeightGap);
3157 }
3158
Michael Jurka66d72172011-04-12 16:29:25 -07003159 public boolean isOccupied(int x, int y) {
3160 if (x < mCountX && y < mCountY) {
3161 return mOccupied[x][y];
3162 } else {
3163 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3164 }
3165 }
3166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167 @Override
3168 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3169 return new CellLayout.LayoutParams(getContext(), attrs);
3170 }
3171
3172 @Override
3173 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3174 return p instanceof CellLayout.LayoutParams;
3175 }
3176
3177 @Override
3178 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3179 return new CellLayout.LayoutParams(p);
3180 }
3181
Winson Chungaafa03c2010-06-11 17:34:16 -07003182 public static class CellLayoutAnimationController extends LayoutAnimationController {
3183 public CellLayoutAnimationController(Animation animation, float delay) {
3184 super(animation, delay);
3185 }
3186
3187 @Override
3188 protected long getDelayForView(View view) {
3189 return (int) (Math.random() * 150);
3190 }
3191 }
3192
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003193 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3194 /**
3195 * Horizontal location of the item in the grid.
3196 */
3197 @ViewDebug.ExportedProperty
3198 public int cellX;
3199
3200 /**
3201 * Vertical location of the item in the grid.
3202 */
3203 @ViewDebug.ExportedProperty
3204 public int cellY;
3205
3206 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003207 * Temporary horizontal location of the item in the grid during reorder
3208 */
3209 public int tmpCellX;
3210
3211 /**
3212 * Temporary vertical location of the item in the grid during reorder
3213 */
3214 public int tmpCellY;
3215
3216 /**
3217 * Indicates that the temporary coordinates should be used to layout the items
3218 */
3219 public boolean useTmpCoords;
3220
3221 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003222 * Number of cells spanned horizontally by the item.
3223 */
3224 @ViewDebug.ExportedProperty
3225 public int cellHSpan;
3226
3227 /**
3228 * Number of cells spanned vertically by the item.
3229 */
3230 @ViewDebug.ExportedProperty
3231 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003232
Adam Cohen1b607ed2011-03-03 17:26:50 -08003233 /**
3234 * Indicates whether the item will set its x, y, width and height parameters freely,
3235 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3236 */
Adam Cohend4844c32011-02-18 19:25:06 -08003237 public boolean isLockedToGrid = true;
3238
Adam Cohen482ed822012-03-02 14:15:13 -08003239 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07003240 * Indicates that this item should use the full extents of its parent.
3241 */
3242 public boolean isFullscreen = false;
3243
3244 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003245 * Indicates whether this item can be reordered. Always true except in the case of the
3246 * the AllApps button.
3247 */
3248 public boolean canReorder = true;
3249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003250 // X coordinate of the view in the layout.
3251 @ViewDebug.ExportedProperty
3252 int x;
3253 // Y coordinate of the view in the layout.
3254 @ViewDebug.ExportedProperty
3255 int y;
3256
Romain Guy84f296c2009-11-04 15:00:44 -08003257 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003258
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003259 public LayoutParams(Context c, AttributeSet attrs) {
3260 super(c, attrs);
3261 cellHSpan = 1;
3262 cellVSpan = 1;
3263 }
3264
3265 public LayoutParams(ViewGroup.LayoutParams source) {
3266 super(source);
3267 cellHSpan = 1;
3268 cellVSpan = 1;
3269 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003270
3271 public LayoutParams(LayoutParams source) {
3272 super(source);
3273 this.cellX = source.cellX;
3274 this.cellY = source.cellY;
3275 this.cellHSpan = source.cellHSpan;
3276 this.cellVSpan = source.cellVSpan;
3277 }
3278
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003279 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003280 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003281 this.cellX = cellX;
3282 this.cellY = cellY;
3283 this.cellHSpan = cellHSpan;
3284 this.cellVSpan = cellVSpan;
3285 }
3286
Adam Cohen2374abf2013-04-16 14:56:57 -07003287 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3288 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003289 if (isLockedToGrid) {
3290 final int myCellHSpan = cellHSpan;
3291 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003292 int myCellX = useTmpCoords ? tmpCellX : cellX;
3293 int myCellY = useTmpCoords ? tmpCellY : cellY;
3294
3295 if (invertHorizontally) {
3296 myCellX = colCount - myCellX - cellHSpan;
3297 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003298
Adam Cohend4844c32011-02-18 19:25:06 -08003299 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3300 leftMargin - rightMargin;
3301 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3302 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003303 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3304 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003305 }
3306 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003307
Winson Chungaafa03c2010-06-11 17:34:16 -07003308 public String toString() {
3309 return "(" + this.cellX + ", " + this.cellY + ")";
3310 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003311
3312 public void setWidth(int width) {
3313 this.width = width;
3314 }
3315
3316 public int getWidth() {
3317 return width;
3318 }
3319
3320 public void setHeight(int height) {
3321 this.height = height;
3322 }
3323
3324 public int getHeight() {
3325 return height;
3326 }
3327
3328 public void setX(int x) {
3329 this.x = x;
3330 }
3331
3332 public int getX() {
3333 return x;
3334 }
3335
3336 public void setY(int y) {
3337 this.y = y;
3338 }
3339
3340 public int getY() {
3341 return y;
3342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003343 }
3344
Michael Jurka0280c3b2010-09-17 15:00:07 -07003345 // This class stores info for two purposes:
3346 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3347 // its spanX, spanY, and the screen it is on
3348 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3349 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3350 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003351 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003352 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003353 int cellX = -1;
3354 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003355 int spanX;
3356 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003357 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003358 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003359
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003360 @Override
3361 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003362 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3363 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003364 }
3365 }
Michael Jurkad771c962011-08-09 15:00:48 -07003366
3367 public boolean lastDownOnOccupiedCell() {
3368 return mLastDownOnOccupiedCell;
3369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003370}