blob: 5c6a694c0d803db9a7b918ebe674186ef0365313 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
80 private final Rect mRect = new Rect();
81 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070082
Patrick Dubroyde7658b2010-09-27 11:15:43 -070083 // These are temporary variables to prevent having to allocate a new object just to
84 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070085 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070086 private final int[] mTmpPoint = new int[2];
Adam Cohen69ce2e52011-07-03 19:25:21 -070087 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070088
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080090 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070091 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Michael Jurkadee05892010-07-27 10:01:56 -070093 private OnTouchListener mInterceptTouchListener;
94
Adam Cohen69ce2e52011-07-03 19:25:21 -070095 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070096 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070097
Adam Cohen02dcfcc2013-10-01 12:37:33 -070098 private float FOREGROUND_ALPHA_DAMPER = 0.65f;
Adam Cohenb5ba0972011-09-07 18:02:31 -070099 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700100 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700101 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -0700102
Michael Jurka33945b22010-12-21 18:19:38 -0800103 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -0800104 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700105 private Drawable mOverScrollForegroundDrawable;
106 private Drawable mOverScrollLeft;
107 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700108 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700109 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700110 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700111
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700112 // These values allow a fixed measurement to be set on the CellLayout.
113 private int mFixedWidth = -1;
114 private int mFixedHeight = -1;
115
Michael Jurka33945b22010-12-21 18:19:38 -0800116 // If we're actively dragging something over this screen, mIsDragOverlapping is true
117 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700118 private final Point mDragCenter = new Point();
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 Cohen19f37922012-03-21 11:59:11 -0700136 private HashMap<View, ReorderHintAnimation>
137 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
138
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 Cohen482ed822012-03-02 14:15:13 -0800152 public static final int MODE_DRAG_OVER = 0;
153 public static final int MODE_ON_DROP = 1;
154 public static final int MODE_ON_DROP_EXTERNAL = 2;
155 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700156 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800157 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
158
Adam Cohena897f392012-04-27 18:12:05 -0700159 static final int LANDSCAPE = 0;
160 static final int PORTRAIT = 1;
161
Adam Cohen7bdfc972012-05-22 16:50:35 -0700162 private static final float REORDER_HINT_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700163 private static final int REORDER_ANIMATION_DURATION = 150;
164 private float mReorderHintAnimationMagnitude;
165
Adam Cohen482ed822012-03-02 14:15:13 -0800166 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
167 private Rect mOccupiedRect = new Rect();
168 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700169 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700170 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700171 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800172
Romain Guy8a0bff52012-05-06 13:14:33 -0700173 private final static PorterDuffXfermode sAddBlendMode =
174 new PorterDuffXfermode(PorterDuff.Mode.ADD);
Michael Jurkaca993832012-06-29 15:17:04 -0700175 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 public CellLayout(Context context) {
178 this(context, null);
179 }
180
181 public CellLayout(Context context, AttributeSet attrs) {
182 this(context, attrs, 0);
183 }
184
185 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
186 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700187 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700188
189 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
190 // the user where a dragged item will land when dropped.
191 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800192 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700193 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700194
Winson Chung892c74d2013-08-22 16:15:50 -0700195 LauncherAppState app = LauncherAppState.getInstance();
196 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
198
Winson Chung11a1a532013-09-13 11:14:45 -0700199 mCellWidth = mCellHeight = -1;
200 mFixedCellHeight = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700201 mWidthGap = mOriginalWidthGap = 0;
202 mHeightGap = mOriginalHeightGap = 0;
203 mMaxGap = Integer.MAX_VALUE;
Winson Chung892c74d2013-08-22 16:15:50 -0700204 mCountX = (int) grid.numColumns;
205 mCountY = (int) grid.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700206 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800207 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700208 mPreviousReorderDirection[0] = INVALID_DIRECTION;
209 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
211 a.recycle();
212
213 setAlwaysDrawnWithCacheEnabled(false);
214
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700215 final Resources res = getResources();
Winson Chung5f8afe62013-08-12 16:19:28 -0700216 mHotseatScale = (float) grid.hotseatIconSize / grid.iconSize;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700217
Adam Cohen410f3cd2013-09-22 12:09:32 -0700218 mNormalBackground = res.getDrawable(R.drawable.screenpanel);
219 mActiveGlowBackground = res.getDrawable(R.drawable.screenpanel_hover);
Michael Jurka33945b22010-12-21 18:19:38 -0800220
Adam Cohenb5ba0972011-09-07 18:02:31 -0700221 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
222 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
223 mForegroundPadding =
224 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800225
Adam Cohen19f37922012-03-21 11:59:11 -0700226 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700227 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700228
Winson Chungb26f3d62011-06-02 10:49:29 -0700229 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700230 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700231
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700232 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700233 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700234 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700235 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800236 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700237 }
238
239 // When dragging things around the home screens, we show a green outline of
240 // where the item will land. The outlines gradually fade out, leaving a trail
241 // behind the drag path.
242 // Set up all the animations that are used to implement this fading.
243 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700244 final float fromAlphaValue = 0;
245 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700246
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700247 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700248
249 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700250 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100251 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700252 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700253 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700254 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700255 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700256 final Bitmap outline = (Bitmap)anim.getTag();
257
258 // If an animation is started and then stopped very quickly, we can still
259 // get spurious updates we've cleared the tag. Guard against this.
260 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700261 @SuppressWarnings("all") // suppress dead code warning
262 final boolean debug = false;
263 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700264 Object val = animation.getAnimatedValue();
265 Log.d(TAG, "anim " + thisIndex + " update: " + val +
266 ", isStopped " + anim.isStopped());
267 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 // Try to prevent it from continuing to run
269 animation.cancel();
270 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700271 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800272 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700274 }
275 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700276 // The animation holds a reference to the drag outline bitmap as long is it's
277 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700278 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700279 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700280 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700281 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700282 anim.setTag(null);
283 }
284 }
285 });
286 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700287 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700288
Michael Jurka18014792010-10-14 09:01:34 -0700289 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700290 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800291
Michael Jurkaa52570f2012-03-20 03:18:20 -0700292 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700293 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700294 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700295
Michael Jurkaa52570f2012-03-20 03:18:20 -0700296 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700297 }
298
Chris Craik01f2d7f2013-10-01 14:41:56 -0700299 public void enableHardwareLayer(boolean hasLayer) {
300 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700301 }
302
303 public void buildHardwareLayer() {
304 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700305 }
306
Adam Cohen307fe232012-08-16 17:55:58 -0700307 public float getChildrenScale() {
308 return mIsHotseat ? mHotseatScale : 1.0f;
309 }
310
Winson Chung5f8afe62013-08-12 16:19:28 -0700311 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700312 mFixedCellWidth = mCellWidth = width;
313 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700314 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
315 mCountX, mCountY);
316 }
317
Adam Cohen2801caf2011-05-13 20:57:39 -0700318 public void setGridSize(int x, int y) {
319 mCountX = x;
320 mCountY = y;
321 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800322 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700323 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700324 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700325 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700326 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700327 }
328
Adam Cohen2374abf2013-04-16 14:56:57 -0700329 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
330 public void setInvertIfRtl(boolean invert) {
331 mShortcutsAndWidgets.setInvertIfRtl(invert);
332 }
333
Patrick Dubroy96864c32011-03-10 17:17:23 -0800334 private void invalidateBubbleTextView(BubbleTextView icon) {
335 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700336 invalidate(icon.getLeft() + getPaddingLeft() - padding,
337 icon.getTop() + getPaddingTop() - padding,
338 icon.getRight() + getPaddingLeft() + padding,
339 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800340 }
341
Adam Cohenb5ba0972011-09-07 18:02:31 -0700342 void setOverScrollAmount(float r, boolean left) {
343 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
344 mOverScrollForegroundDrawable = mOverScrollLeft;
345 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
346 mOverScrollForegroundDrawable = mOverScrollRight;
347 }
348
Adam Cohen02dcfcc2013-10-01 12:37:33 -0700349 r *= FOREGROUND_ALPHA_DAMPER;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700350 mForegroundAlpha = (int) Math.round((r * 255));
351 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
352 invalidate();
353 }
354
Patrick Dubroy96864c32011-03-10 17:17:23 -0800355 void setPressedOrFocusedIcon(BubbleTextView icon) {
356 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
357 // requires an expanded clip rect (due to the glow's blur radius)
358 BubbleTextView oldIcon = mPressedOrFocusedIcon;
359 mPressedOrFocusedIcon = icon;
360 if (oldIcon != null) {
361 invalidateBubbleTextView(oldIcon);
362 }
363 if (mPressedOrFocusedIcon != null) {
364 invalidateBubbleTextView(mPressedOrFocusedIcon);
365 }
366 }
367
Michael Jurka33945b22010-12-21 18:19:38 -0800368 void setIsDragOverlapping(boolean isDragOverlapping) {
369 if (mIsDragOverlapping != isDragOverlapping) {
370 mIsDragOverlapping = isDragOverlapping;
Adam Cohendedbd962013-07-11 14:21:49 -0700371 setUseActiveGlowBackground(mIsDragOverlapping);
Michael Jurka33945b22010-12-21 18:19:38 -0800372 invalidate();
373 }
374 }
375
Adam Cohendedbd962013-07-11 14:21:49 -0700376 void setUseActiveGlowBackground(boolean use) {
377 mUseActiveGlowBackground = use;
378 }
379
Michael Jurka33945b22010-12-21 18:19:38 -0800380 boolean getIsDragOverlapping() {
381 return mIsDragOverlapping;
382 }
383
Adam Cohenebea84d2011-11-09 17:20:41 -0800384 protected void setOverscrollTransformsDirty(boolean dirty) {
385 mScrollingTransformsDirty = dirty;
386 }
387
388 protected void resetOverscrollTransforms() {
389 if (mScrollingTransformsDirty) {
390 setOverscrollTransformsDirty(false);
391 setTranslationX(0);
392 setRotationY(0);
393 // It doesn't matter if we pass true or false here, the important thing is that we
394 // pass 0, which results in the overscroll drawable not being drawn any more.
395 setOverScrollAmount(0, false);
396 setPivotX(getMeasuredWidth() / 2);
397 setPivotY(getMeasuredHeight() / 2);
398 }
399 }
400
Adam Cohen307fe232012-08-16 17:55:58 -0700401 public void scaleRect(Rect r, float scale) {
402 if (scale != 1.0f) {
403 r.left = (int) (r.left * scale + 0.5f);
404 r.top = (int) (r.top * scale + 0.5f);
405 r.right = (int) (r.right * scale + 0.5f);
406 r.bottom = (int) (r.bottom * scale + 0.5f);
407 }
408 }
409
410 Rect temp = new Rect();
411 void scaleRectAboutCenter(Rect in, Rect out, float scale) {
412 int cx = in.centerX();
413 int cy = in.centerY();
414 out.set(in);
415 out.offset(-cx, -cy);
416 scaleRect(out, scale);
417 out.offset(cx, cy);
418 }
419
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700420 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700421 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700422 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
423 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
424 // When we're small, we are either drawn normally or in the "accepts drops" state (during
425 // a drag). However, we also drag the mini hover background *over* one of those two
426 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700427 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700428 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800429
Adam Cohendedbd962013-07-11 14:21:49 -0700430 if (mUseActiveGlowBackground) {
Michael Jurka33945b22010-12-21 18:19:38 -0800431 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700432 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700433 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700434 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700435 }
Michael Jurka33945b22010-12-21 18:19:38 -0800436
437 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
438 bg.setBounds(mBackgroundRect);
439 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700440 }
Romain Guya6abce82009-11-10 02:54:41 -0800441
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700442 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700443 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700444 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700445 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800446 final Rect r = mDragOutlines[i];
Adam Cohen307fe232012-08-16 17:55:58 -0700447 scaleRectAboutCenter(r, temp, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700448 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700449 paint.setAlpha((int)(alpha + .5f));
Adam Cohen307fe232012-08-16 17:55:58 -0700450 canvas.drawBitmap(b, null, temp, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700451 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700452 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800453
454 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
455 // requires an expanded clip rect (due to the glow's blur radius)
456 if (mPressedOrFocusedIcon != null) {
457 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
458 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
459 if (b != null) {
460 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700461 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
462 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800463 null);
464 }
465 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700466
Adam Cohen482ed822012-03-02 14:15:13 -0800467 if (DEBUG_VISUALIZE_OCCUPIED) {
468 int[] pt = new int[2];
469 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700470 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800471 for (int i = 0; i < mCountX; i++) {
472 for (int j = 0; j < mCountY; j++) {
473 if (mOccupied[i][j]) {
474 cellToPoint(i, j, pt);
475 canvas.save();
476 canvas.translate(pt[0], pt[1]);
477 cd.draw(canvas);
478 canvas.restore();
479 }
480 }
481 }
482 }
483
Andrew Flynn850d2e72012-04-26 16:51:20 -0700484 int previewOffset = FolderRingAnimator.sPreviewSize;
485
Adam Cohen69ce2e52011-07-03 19:25:21 -0700486 // The folder outer / inner ring image(s)
Winson Chung5f8afe62013-08-12 16:19:28 -0700487 LauncherAppState app = LauncherAppState.getInstance();
488 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700489 for (int i = 0; i < mFolderOuterRings.size(); i++) {
490 FolderRingAnimator fra = mFolderOuterRings.get(i);
491
Adam Cohen5108bc02013-09-20 17:04:51 -0700492 Drawable d;
493 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700494 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700495 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen5108bc02013-09-20 17:04:51 -0700496 int centerX = mTempLocation[0] + mCellWidth / 2;
497 int centerY = mTempLocation[1] + previewOffset / 2 +
498 child.getPaddingTop() + grid.folderBackgroundOffset;
Winson Chung89f97052013-09-20 11:32:26 -0700499 if (child != null) {
Adam Cohen5108bc02013-09-20 17:04:51 -0700500 // Draw outer ring, if it exists
501 if (FolderIcon.HAS_OUTER_RING) {
502 d = FolderRingAnimator.sSharedOuterRingDrawable;
503 width = (int) (fra.getOuterRingSize() * getChildrenScale());
504 height = width;
505 canvas.save();
506 canvas.translate(centerX - width / 2, centerY - height / 2);
507 d.setBounds(0, 0, width, height);
508 d.draw(canvas);
509 canvas.restore();
510 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700511
Winson Chung89f97052013-09-20 11:32:26 -0700512 // Draw inner ring
513 d = FolderRingAnimator.sSharedInnerRingDrawable;
514 width = (int) (fra.getInnerRingSize() * getChildrenScale());
515 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700516 canvas.save();
517 canvas.translate(centerX - width / 2, centerY - width / 2);
518 d.setBounds(0, 0, width, height);
519 d.draw(canvas);
520 canvas.restore();
521 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700522 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700523
524 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
525 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
526 int width = d.getIntrinsicWidth();
527 int height = d.getIntrinsicHeight();
528
529 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700530 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700531 if (child != null) {
532 int centerX = mTempLocation[0] + mCellWidth / 2;
533 int centerY = mTempLocation[1] + previewOffset / 2 +
534 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700535
Winson Chung89f97052013-09-20 11:32:26 -0700536 canvas.save();
537 canvas.translate(centerX - width / 2, centerY - width / 2);
538 d.setBounds(0, 0, width, height);
539 d.draw(canvas);
540 canvas.restore();
541 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700542 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700543 }
544
Adam Cohenb5ba0972011-09-07 18:02:31 -0700545 @Override
546 protected void dispatchDraw(Canvas canvas) {
547 super.dispatchDraw(canvas);
548 if (mForegroundAlpha > 0) {
549 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
550 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700551 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700552 mOverScrollForegroundDrawable.draw(canvas);
553 p.setXfermode(null);
554 }
555 }
556
Adam Cohen69ce2e52011-07-03 19:25:21 -0700557 public void showFolderAccept(FolderRingAnimator fra) {
558 mFolderOuterRings.add(fra);
559 }
560
561 public void hideFolderAccept(FolderRingAnimator fra) {
562 if (mFolderOuterRings.contains(fra)) {
563 mFolderOuterRings.remove(fra);
564 }
565 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700566 }
567
Adam Cohenc51934b2011-07-26 21:07:43 -0700568 public void setFolderLeaveBehindCell(int x, int y) {
569 mFolderLeaveBehindCell[0] = x;
570 mFolderLeaveBehindCell[1] = y;
571 invalidate();
572 }
573
574 public void clearFolderLeaveBehind() {
575 mFolderLeaveBehindCell[0] = -1;
576 mFolderLeaveBehindCell[1] = -1;
577 invalidate();
578 }
579
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700580 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700581 public boolean shouldDelayChildPressedState() {
582 return false;
583 }
584
Adam Cohen1462de32012-07-24 22:34:36 -0700585 public void restoreInstanceState(SparseArray<Parcelable> states) {
586 dispatchRestoreInstanceState(states);
587 }
588
Michael Jurkae6235dd2011-10-04 15:02:05 -0700589 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700590 public void cancelLongPress() {
591 super.cancelLongPress();
592
593 // Cancel long press for all children
594 final int count = getChildCount();
595 for (int i = 0; i < count; i++) {
596 final View child = getChildAt(i);
597 child.cancelLongPress();
598 }
599 }
600
Michael Jurkadee05892010-07-27 10:01:56 -0700601 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
602 mInterceptTouchListener = listener;
603 }
604
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700606 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 }
608
609 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700610 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
612
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800613 public void setIsHotseat(boolean isHotseat) {
614 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700615 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800616 }
617
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800618 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700619 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700620 final LayoutParams lp = params;
621
Andrew Flynnde38e422012-05-08 11:22:15 -0700622 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800623 if (child instanceof BubbleTextView) {
624 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700625 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800626 }
627
Adam Cohen307fe232012-08-16 17:55:58 -0700628 child.setScaleX(getChildrenScale());
629 child.setScaleY(getChildrenScale());
630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 // Generate an id for each view, this assumes we have at most 256x256 cells
632 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700633 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700634 // If the horizontal or vertical span is set to -1, it is taken to
635 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700636 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
637 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638
Winson Chungaafa03c2010-06-11 17:34:16 -0700639 child.setId(childId);
640
Michael Jurkaa52570f2012-03-20 03:18:20 -0700641 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700642
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700643 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644
Winson Chungaafa03c2010-06-11 17:34:16 -0700645 return true;
646 }
647 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700651 public void removeAllViews() {
652 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700653 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654 }
655
656 @Override
657 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700658 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700659 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700660 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700661 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 }
663
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700664 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700665 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700666 }
667
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 @Override
669 public void removeView(View view) {
670 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700671 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700672 }
673
674 @Override
675 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700676 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
677 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 }
679
680 @Override
681 public void removeViewInLayout(View view) {
682 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
685
686 @Override
687 public void removeViews(int start, int count) {
688 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700689 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700691 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700692 }
693
694 @Override
695 public void removeViewsInLayout(int start, int count) {
696 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700697 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700698 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700699 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800700 }
701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 @Override
703 protected void onAttachedToWindow() {
704 super.onAttachedToWindow();
Adam Cohendcd297f2013-06-18 13:13:40 -0700705 if (getParent() instanceof Workspace) {
706 Workspace workspace = (Workspace) getParent();
707 mCellInfo.screenId = workspace.getIdForScreen(this);
708 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 }
710
Michael Jurkaaf442092010-06-10 17:01:57 -0700711 public void setTagToCellInfoForPoint(int touchX, int touchY) {
712 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800713 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 final int x = touchX + getScrollX();
715 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700716 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700717
718 boolean found = false;
719 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700720 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800721 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700722
Adam Cohen1b607ed2011-03-03 17:26:50 -0800723 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
724 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700725 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700726
Winson Chungeecf02d2012-03-02 17:14:58 -0800727 float scale = child.getScaleX();
728 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
729 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700730 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
731 // offset that by this CellLayout's padding to test an (x,y) point that is relative
732 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700733 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800734 frame.inset((int) (frame.width() * (1f - scale) / 2),
735 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700736
Michael Jurkaaf442092010-06-10 17:01:57 -0700737 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700738 cellInfo.cell = child;
739 cellInfo.cellX = lp.cellX;
740 cellInfo.cellY = lp.cellY;
741 cellInfo.spanX = lp.cellHSpan;
742 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700743 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700744 break;
745 }
746 }
747 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700748
Michael Jurkad771c962011-08-09 15:00:48 -0700749 mLastDownOnOccupiedCell = found;
750
Michael Jurkaaf442092010-06-10 17:01:57 -0700751 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700752 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700753 pointToCellExact(x, y, cellXY);
754
Michael Jurkaaf442092010-06-10 17:01:57 -0700755 cellInfo.cell = null;
756 cellInfo.cellX = cellXY[0];
757 cellInfo.cellY = cellXY[1];
758 cellInfo.spanX = 1;
759 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700760 }
761 setTag(cellInfo);
762 }
763
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 @Override
765 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700766 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
767 // even in the case where we return early. Not clearing here was causing bugs whereby on
768 // long-press we'd end up picking up an item from a previous drag operation.
769 final int action = ev.getAction();
770
771 if (action == MotionEvent.ACTION_DOWN) {
772 clearTagCellInfo();
773 }
774
Michael Jurkadee05892010-07-27 10:01:56 -0700775 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
776 return true;
777 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778
779 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700780 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 return false;
784 }
785
Adam Cohenc1997fd2011-08-15 18:26:39 -0700786 private void clearTagCellInfo() {
787 final CellInfo cellInfo = mCellInfo;
788 cellInfo.cell = null;
789 cellInfo.cellX = -1;
790 cellInfo.cellY = -1;
791 cellInfo.spanX = 0;
792 cellInfo.spanY = 0;
793 setTag(cellInfo);
794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700797 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 }
799
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700800 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700801 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 * @param x X coordinate of the point
803 * @param y Y coordinate of the point
804 * @param result Array of 2 ints to hold the x and y coordinate of the cell
805 */
806 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700807 final int hStartPadding = getPaddingLeft();
808 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809
810 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
811 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
812
Adam Cohend22015c2010-07-26 22:02:18 -0700813 final int xAxis = mCountX;
814 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815
816 if (result[0] < 0) result[0] = 0;
817 if (result[0] >= xAxis) result[0] = xAxis - 1;
818 if (result[1] < 0) result[1] = 0;
819 if (result[1] >= yAxis) result[1] = yAxis - 1;
820 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 /**
823 * Given a point, return the cell that most closely encloses that point
824 * @param x X coordinate of the point
825 * @param y Y coordinate of the point
826 * @param result Array of 2 ints to hold the x and y coordinate of the cell
827 */
828 void pointToCellRounded(int x, int y, int[] result) {
829 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
830 }
831
832 /**
833 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700834 *
835 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700837 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 * @param result Array of 2 ints to hold the x and y coordinate of the point
839 */
840 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700841 final int hStartPadding = getPaddingLeft();
842 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843
844 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
845 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
846 }
847
Adam Cohene3e27a82011-04-15 12:07:39 -0700848 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800849 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700850 *
851 * @param cellX X coordinate of the cell
852 * @param cellY Y coordinate of the cell
853 *
854 * @param result Array of 2 ints to hold the x and y coordinate of the point
855 */
856 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700857 regionToCenterPoint(cellX, cellY, 1, 1, result);
858 }
859
860 /**
861 * Given a cell coordinate and span return the point that represents the center of the regio
862 *
863 * @param cellX X coordinate of the cell
864 * @param cellY Y coordinate of the cell
865 *
866 * @param result Array of 2 ints to hold the x and y coordinate of the point
867 */
868 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700869 final int hStartPadding = getPaddingLeft();
870 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700871 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
872 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
873 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
874 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700875 }
876
Adam Cohen19f37922012-03-21 11:59:11 -0700877 /**
878 * Given a cell coordinate and span fills out a corresponding pixel rect
879 *
880 * @param cellX X coordinate of the cell
881 * @param cellY Y coordinate of the cell
882 * @param result Rect in which to write the result
883 */
884 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
885 final int hStartPadding = getPaddingLeft();
886 final int vStartPadding = getPaddingTop();
887 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
888 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
889 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
890 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
891 }
892
Adam Cohen482ed822012-03-02 14:15:13 -0800893 public float getDistanceFromCell(float x, float y, int[] cell) {
894 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
895 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
896 Math.pow(y - mTmpPoint[1], 2));
897 return distance;
898 }
899
Romain Guy84f296c2009-11-04 15:00:44 -0800900 int getCellWidth() {
901 return mCellWidth;
902 }
903
904 int getCellHeight() {
905 return mCellHeight;
906 }
907
Adam Cohend4844c32011-02-18 19:25:06 -0800908 int getWidthGap() {
909 return mWidthGap;
910 }
911
912 int getHeightGap() {
913 return mHeightGap;
914 }
915
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700916 Rect getContentRect(Rect r) {
917 if (r == null) {
918 r = new Rect();
919 }
920 int left = getPaddingLeft();
921 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700922 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
923 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700924 r.set(left, top, right, bottom);
925 return r;
926 }
927
Winson Chungfe411c82013-09-26 16:07:17 -0700928 /** Return a rect that has the cellWidth/cellHeight (left, top), and
929 * widthGap/heightGap (right, bottom) */
Winson Chung66700732013-08-20 16:56:15 -0700930 static void getMetrics(Rect metrics, int paddedMeasureWidth,
931 int paddedMeasureHeight, int countX, int countY) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700932 LauncherAppState app = LauncherAppState.getInstance();
933 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -0700934 metrics.set(grid.calculateCellWidth(paddedMeasureWidth, countX),
935 grid.calculateCellHeight(paddedMeasureHeight, countY), 0, 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700936 }
937
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700938 public void setFixedSize(int width, int height) {
939 mFixedWidth = width;
940 mFixedHeight = height;
941 }
942
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 @Override
944 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700945 LauncherAppState app = LauncherAppState.getInstance();
946 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
947
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700950 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
951 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700952 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
953 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700954 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700955 int cw = grid.calculateCellWidth(childWidthSize, mCountX);
956 int ch = grid.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700957 if (cw != mCellWidth || ch != mCellHeight) {
958 mCellWidth = cw;
959 mCellHeight = ch;
960 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
961 mHeightGap, mCountX, mCountY);
962 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700963 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700964
Winson Chung2d75f122013-09-23 16:53:31 -0700965 int newWidth = childWidthSize;
966 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700967 if (mFixedWidth > 0 && mFixedHeight > 0) {
968 newWidth = mFixedWidth;
969 newHeight = mFixedHeight;
970 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
972 }
973
Adam Cohend22015c2010-07-26 22:02:18 -0700974 int numWidthGaps = mCountX - 1;
975 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976
Adam Cohen234c4cd2011-07-17 21:03:04 -0700977 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700978 int hSpace = childWidthSize;
979 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700980 int hFreeSpace = hSpace - (mCountX * mCellWidth);
981 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700982 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
983 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700984 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
985 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700986 } else {
987 mWidthGap = mOriginalWidthGap;
988 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700989 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800990 int count = getChildCount();
Winson Chung5f8afe62013-08-12 16:19:28 -0700991 int maxWidth = 0;
992 int maxHeight = 0;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800993 for (int i = 0; i < count; i++) {
994 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -0700995 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth,
996 MeasureSpec.EXACTLY);
997 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
998 MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800999 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -07001000 maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
1001 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
Michael Jurka8c920dd2011-01-20 14:16:56 -08001002 }
Winson Chung2d75f122013-09-23 16:53:31 -07001003 if (mFixedWidth > 0 && mFixedHeight > 0) {
1004 setMeasuredDimension(maxWidth, maxHeight);
1005 } else {
1006 setMeasuredDimension(widthSize, heightSize);
1007 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 }
1009
1010 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001011 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -07001012 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
1013 (mCountX * mCellWidth);
1014 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
1015 int top = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001018 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -07001019 child.layout(left, top,
1020 left + r - l,
1021 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
1023 }
1024
1025 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001026 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1027 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -07001028
1029 // Expand the background drawing bounds by the padding baked into the background drawable
1030 Rect padding = new Rect();
1031 mNormalBackground.getPadding(padding);
1032 mBackgroundRect.set(-padding.left, -padding.top, w + padding.right, h + padding.bottom);
1033
Adam Cohenb5ba0972011-09-07 18:02:31 -07001034 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001035 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001036 }
1037
1038 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001040 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 }
1042
1043 @Override
1044 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001045 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 }
1047
Michael Jurka5f1c5092010-09-03 14:15:02 -07001048 public float getBackgroundAlpha() {
1049 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001050 }
1051
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001052 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001053 if (mBackgroundAlphaMultiplier != multiplier) {
1054 mBackgroundAlphaMultiplier = multiplier;
1055 invalidate();
1056 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001057 }
1058
Adam Cohenddb82192010-11-10 16:32:54 -08001059 public float getBackgroundAlphaMultiplier() {
1060 return mBackgroundAlphaMultiplier;
1061 }
1062
Michael Jurka5f1c5092010-09-03 14:15:02 -07001063 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001064 if (mBackgroundAlpha != alpha) {
1065 mBackgroundAlpha = alpha;
1066 invalidate();
1067 }
Michael Jurkadee05892010-07-27 10:01:56 -07001068 }
1069
Michael Jurkaa52570f2012-03-20 03:18:20 -07001070 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001071 final int childCount = getChildCount();
1072 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001073 getChildAt(i).setAlpha(alpha);
1074 }
1075 }
1076
Michael Jurkaa52570f2012-03-20 03:18:20 -07001077 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1078 if (getChildCount() > 0) {
1079 return (ShortcutAndWidgetContainer) getChildAt(0);
1080 }
1081 return null;
1082 }
1083
Patrick Dubroy440c3602010-07-13 17:50:32 -07001084 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001085 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001086 }
1087
Adam Cohen76fc0852011-06-17 13:26:23 -07001088 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001089 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001090 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001091 boolean[][] occupied = mOccupied;
1092 if (!permanent) {
1093 occupied = mTmpOccupied;
1094 }
1095
Adam Cohen19f37922012-03-21 11:59:11 -07001096 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001097 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1098 final ItemInfo info = (ItemInfo) child.getTag();
1099
1100 // We cancel any existing animations
1101 if (mReorderAnimators.containsKey(lp)) {
1102 mReorderAnimators.get(lp).cancel();
1103 mReorderAnimators.remove(lp);
1104 }
1105
Adam Cohen482ed822012-03-02 14:15:13 -08001106 final int oldX = lp.x;
1107 final int oldY = lp.y;
1108 if (adjustOccupied) {
1109 occupied[lp.cellX][lp.cellY] = false;
1110 occupied[cellX][cellY] = true;
1111 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001112 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001113 if (permanent) {
1114 lp.cellX = info.cellX = cellX;
1115 lp.cellY = info.cellY = cellY;
1116 } else {
1117 lp.tmpCellX = cellX;
1118 lp.tmpCellY = cellY;
1119 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001120 clc.setupLp(lp);
1121 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001122 final int newX = lp.x;
1123 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001124
Adam Cohen76fc0852011-06-17 13:26:23 -07001125 lp.x = oldX;
1126 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001127
Adam Cohen482ed822012-03-02 14:15:13 -08001128 // Exit early if we're not actually moving the view
1129 if (oldX == newX && oldY == newY) {
1130 lp.isLockedToGrid = true;
1131 return true;
1132 }
1133
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001134 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001135 va.setDuration(duration);
1136 mReorderAnimators.put(lp, va);
1137
1138 va.addUpdateListener(new AnimatorUpdateListener() {
1139 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001140 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001141 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001142 lp.x = (int) ((1 - r) * oldX + r * newX);
1143 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001144 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001145 }
1146 });
Adam Cohen482ed822012-03-02 14:15:13 -08001147 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001148 boolean cancelled = false;
1149 public void onAnimationEnd(Animator animation) {
1150 // If the animation was cancelled, it means that another animation
1151 // has interrupted this one, and we don't want to lock the item into
1152 // place just yet.
1153 if (!cancelled) {
1154 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001155 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001156 }
1157 if (mReorderAnimators.containsKey(lp)) {
1158 mReorderAnimators.remove(lp);
1159 }
1160 }
1161 public void onAnimationCancel(Animator animation) {
1162 cancelled = true;
1163 }
1164 });
Adam Cohen482ed822012-03-02 14:15:13 -08001165 va.setStartDelay(delay);
1166 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001167 return true;
1168 }
1169 return false;
1170 }
1171
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001172 /**
1173 * Estimate where the top left cell of the dragged item will land if it is dropped.
1174 *
1175 * @param originX The X value of the top left corner of the item
1176 * @param originY The Y value of the top left corner of the item
1177 * @param spanX The number of horizontal cells that the item spans
1178 * @param spanY The number of vertical cells that the item spans
1179 * @param result The estimated drop cell X and Y.
1180 */
1181 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001182 final int countX = mCountX;
1183 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001184
Michael Jurkaa63c4522010-08-19 13:52:27 -07001185 // pointToCellRounded takes the top left of a cell but will pad that with
1186 // cellWidth/2 and cellHeight/2 when finding the matching cell
1187 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001188
1189 // If the item isn't fully on this screen, snap to the edges
1190 int rightOverhang = result[0] + spanX - countX;
1191 if (rightOverhang > 0) {
1192 result[0] -= rightOverhang; // Snap to right
1193 }
1194 result[0] = Math.max(0, result[0]); // Snap to left
1195 int bottomOverhang = result[1] + spanY - countY;
1196 if (bottomOverhang > 0) {
1197 result[1] -= bottomOverhang; // Snap to bottom
1198 }
1199 result[1] = Math.max(0, result[1]); // Snap to top
1200 }
1201
Adam Cohen482ed822012-03-02 14:15:13 -08001202 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1203 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001204 final int oldDragCellX = mDragCell[0];
1205 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001206
Winson Chungb8c69f32011-10-19 21:36:08 -07001207 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001208 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1209 } else {
1210 mDragCenter.set(originX, originY);
1211 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001212
Adam Cohen2801caf2011-05-13 20:57:39 -07001213 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001214 return;
1215 }
1216
Adam Cohen482ed822012-03-02 14:15:13 -08001217 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1218 mDragCell[0] = cellX;
1219 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001220 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001221 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001222 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001223
Joe Onorato4be866d2010-10-10 11:26:02 -07001224 int left = topLeft[0];
1225 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001226
Winson Chung892c74d2013-08-22 16:15:50 -07001227 // Offset icons by their padding
1228 if (v instanceof BubbleTextView) {
1229 top += v.getPaddingTop();
1230 }
1231
Winson Chungb8c69f32011-10-19 21:36:08 -07001232 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001233 // When drawing the drag outline, it did not account for margin offsets
1234 // added by the view's parent.
1235 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1236 left += lp.leftMargin;
1237 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001238
Adam Cohen99e8b402011-03-25 19:23:43 -07001239 // Offsets due to the size difference between the View and the dragOutline.
1240 // There is a size difference to account for the outer blur, which may lie
1241 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001242 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001243 // We center about the x axis
1244 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1245 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001246 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001247 if (dragOffset != null && dragRegion != null) {
1248 // Center the drag region *horizontally* in the cell and apply a drag
1249 // outline offset
1250 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1251 - dragRegion.width()) / 2;
1252 top += dragOffset.y;
1253 } else {
1254 // Center the drag outline in the cell
1255 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1256 - dragOutline.getWidth()) / 2;
1257 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1258 - dragOutline.getHeight()) / 2;
1259 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001260 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001261 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001262 mDragOutlineAnims[oldIndex].animateOut();
1263 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001264 Rect r = mDragOutlines[mDragOutlineCurrent];
1265 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1266 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001267 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001268 }
Winson Chung150fbab2010-09-29 17:14:26 -07001269
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001270 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1271 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001272 }
1273 }
1274
Adam Cohene0310962011-04-18 16:15:31 -07001275 public void clearDragOutlines() {
1276 final int oldIndex = mDragOutlineCurrent;
1277 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001278 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001279 }
1280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001282 * Find a vacant area that will fit the given bounds nearest the requested
1283 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001284 *
Romain Guy51afc022009-05-04 18:03:43 -07001285 * @param pixelX The X location at which you want to search for a vacant area.
1286 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001287 * @param spanX Horizontal span of the object.
1288 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001289 * @param result Array in which to place the result, or null (in which case a new array will
1290 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001291 * @return The X, Y cell of a vacant area that can contain this object,
1292 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001294 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1295 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001296 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001297 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001298
Michael Jurka6a1435d2010-09-27 17:35:12 -07001299 /**
1300 * Find a vacant area that will fit the given bounds nearest the requested
1301 * cell location. Uses Euclidean distance to score multiple vacant areas.
1302 *
1303 * @param pixelX The X location at which you want to search for a vacant area.
1304 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001305 * @param minSpanX The minimum horizontal span required
1306 * @param minSpanY The minimum vertical span required
1307 * @param spanX Horizontal span of the object.
1308 * @param spanY Vertical span of the object.
1309 * @param result Array in which to place the result, or null (in which case a new array will
1310 * be allocated)
1311 * @return The X, Y cell of a vacant area that can contain this object,
1312 * nearest the requested location.
1313 */
1314 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1315 int spanY, int[] result, int[] resultSpan) {
1316 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1317 result, resultSpan);
1318 }
1319
1320 /**
1321 * Find a vacant area that will fit the given bounds nearest the requested
1322 * cell location. Uses Euclidean distance to score multiple vacant areas.
1323 *
1324 * @param pixelX The X location at which you want to search for a vacant area.
1325 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001326 * @param spanX Horizontal span of the object.
1327 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001328 * @param ignoreOccupied If true, the result can be an occupied cell
1329 * @param result Array in which to place the result, or null (in which case a new array will
1330 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001331 * @return The X, Y cell of a vacant area that can contain this object,
1332 * nearest the requested location.
1333 */
Adam Cohendf035382011-04-11 17:22:04 -07001334 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1335 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001336 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001337 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001338 }
1339
1340 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1341 private void lazyInitTempRectStack() {
1342 if (mTempRectStack.isEmpty()) {
1343 for (int i = 0; i < mCountX * mCountY; i++) {
1344 mTempRectStack.push(new Rect());
1345 }
1346 }
1347 }
Adam Cohen482ed822012-03-02 14:15:13 -08001348
Adam Cohend41fbf52012-02-16 23:53:59 -08001349 private void recycleTempRects(Stack<Rect> used) {
1350 while (!used.isEmpty()) {
1351 mTempRectStack.push(used.pop());
1352 }
1353 }
1354
1355 /**
1356 * Find a vacant area that will fit the given bounds nearest the requested
1357 * cell location. Uses Euclidean distance to score multiple vacant areas.
1358 *
1359 * @param pixelX The X location at which you want to search for a vacant area.
1360 * @param pixelY The Y location at which you want to search for a vacant area.
1361 * @param minSpanX The minimum horizontal span required
1362 * @param minSpanY The minimum vertical span required
1363 * @param spanX Horizontal span of the object.
1364 * @param spanY Vertical span of the object.
1365 * @param ignoreOccupied If true, the result can be an occupied cell
1366 * @param result Array in which to place the result, or null (in which case a new array will
1367 * be allocated)
1368 * @return The X, Y cell of a vacant area that can contain this object,
1369 * nearest the requested location.
1370 */
1371 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001372 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1373 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001374 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001375 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001376 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001377
Adam Cohene3e27a82011-04-15 12:07:39 -07001378 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1379 // to the center of the item, but we are searching based on the top-left cell, so
1380 // we translate the point over to correspond to the top-left.
1381 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1382 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1383
Jeff Sharkey70864282009-04-07 21:08:40 -07001384 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001385 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001386 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001387 final Rect bestRect = new Rect(-1, -1, -1, -1);
1388 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001389
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001390 final int countX = mCountX;
1391 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001392
Adam Cohend41fbf52012-02-16 23:53:59 -08001393 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1394 spanX < minSpanX || spanY < minSpanY) {
1395 return bestXY;
1396 }
1397
1398 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001399 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001400 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1401 int ySize = -1;
1402 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001403 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001404 // First, let's see if this thing fits anywhere
1405 for (int i = 0; i < minSpanX; i++) {
1406 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001407 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001408 continue inner;
1409 }
Michael Jurkac28de512010-08-13 11:27:44 -07001410 }
1411 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001412 xSize = minSpanX;
1413 ySize = minSpanY;
1414
1415 // We know that the item will fit at _some_ acceptable size, now let's see
1416 // how big we can make it. We'll alternate between incrementing x and y spans
1417 // until we hit a limit.
1418 boolean incX = true;
1419 boolean hitMaxX = xSize >= spanX;
1420 boolean hitMaxY = ySize >= spanY;
1421 while (!(hitMaxX && hitMaxY)) {
1422 if (incX && !hitMaxX) {
1423 for (int j = 0; j < ySize; j++) {
1424 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1425 // We can't move out horizontally
1426 hitMaxX = true;
1427 }
1428 }
1429 if (!hitMaxX) {
1430 xSize++;
1431 }
1432 } else if (!hitMaxY) {
1433 for (int i = 0; i < xSize; i++) {
1434 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1435 // We can't move out vertically
1436 hitMaxY = true;
1437 }
1438 }
1439 if (!hitMaxY) {
1440 ySize++;
1441 }
1442 }
1443 hitMaxX |= xSize >= spanX;
1444 hitMaxY |= ySize >= spanY;
1445 incX = !incX;
1446 }
1447 incX = true;
1448 hitMaxX = xSize >= spanX;
1449 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001450 }
Winson Chung0be025d2011-05-23 17:45:09 -07001451 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001452 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453
Adam Cohend41fbf52012-02-16 23:53:59 -08001454 // We verify that the current rect is not a sub-rect of any of our previous
1455 // candidates. In this case, the current rect is disqualified in favour of the
1456 // containing rect.
1457 Rect currentRect = mTempRectStack.pop();
1458 currentRect.set(x, y, x + xSize, y + ySize);
1459 boolean contained = false;
1460 for (Rect r : validRegions) {
1461 if (r.contains(currentRect)) {
1462 contained = true;
1463 break;
1464 }
1465 }
1466 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001467 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1468 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001469
Adam Cohend41fbf52012-02-16 23:53:59 -08001470 if ((distance <= bestDistance && !contained) ||
1471 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001472 bestDistance = distance;
1473 bestXY[0] = x;
1474 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001475 if (resultSpan != null) {
1476 resultSpan[0] = xSize;
1477 resultSpan[1] = ySize;
1478 }
1479 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001480 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 }
1482 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001483 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001484 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485
Adam Cohenc0dcf592011-06-01 15:30:43 -07001486 // Return -1, -1 if no suitable location found
1487 if (bestDistance == Double.MAX_VALUE) {
1488 bestXY[0] = -1;
1489 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001490 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001491 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001492 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001494
Adam Cohen482ed822012-03-02 14:15:13 -08001495 /**
1496 * Find a vacant area that will fit the given bounds nearest the requested
1497 * cell location, and will also weigh in a suggested direction vector of the
1498 * desired location. This method computers distance based on unit grid distances,
1499 * not pixel distances.
1500 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001501 * @param cellX The X cell nearest to which you want to search for a vacant area.
1502 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001503 * @param spanX Horizontal span of the object.
1504 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001505 * @param direction The favored direction in which the views should move from x, y
1506 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1507 * matches exactly. Otherwise we find the best matching direction.
1508 * @param occoupied The array which represents which cells in the CellLayout are occupied
1509 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001510 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001511 * @param result Array in which to place the result, or null (in which case a new array will
1512 * be allocated)
1513 * @return The X, Y cell of a vacant area that can contain this object,
1514 * nearest the requested location.
1515 */
1516 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001517 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001518 // Keep track of best-scoring drop area
1519 final int[] bestXY = result != null ? result : new int[2];
1520 float bestDistance = Float.MAX_VALUE;
1521 int bestDirectionScore = Integer.MIN_VALUE;
1522
1523 final int countX = mCountX;
1524 final int countY = mCountY;
1525
1526 for (int y = 0; y < countY - (spanY - 1); y++) {
1527 inner:
1528 for (int x = 0; x < countX - (spanX - 1); x++) {
1529 // First, let's see if this thing fits anywhere
1530 for (int i = 0; i < spanX; i++) {
1531 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001532 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001533 continue inner;
1534 }
1535 }
1536 }
1537
1538 float distance = (float)
1539 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1540 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001541 computeDirectionVector(x - cellX, y - cellY, curDirection);
1542 // The direction score is just the dot product of the two candidate direction
1543 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001544 int curDirectionScore = direction[0] * curDirection[0] +
1545 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001546 boolean exactDirectionOnly = false;
1547 boolean directionMatches = direction[0] == curDirection[0] &&
1548 direction[0] == curDirection[0];
1549 if ((directionMatches || !exactDirectionOnly) &&
1550 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001551 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1552 bestDistance = distance;
1553 bestDirectionScore = curDirectionScore;
1554 bestXY[0] = x;
1555 bestXY[1] = y;
1556 }
1557 }
1558 }
1559
1560 // Return -1, -1 if no suitable location found
1561 if (bestDistance == Float.MAX_VALUE) {
1562 bestXY[0] = -1;
1563 bestXY[1] = -1;
1564 }
1565 return bestXY;
1566 }
1567
1568 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001569 int[] direction, ItemConfiguration currentState) {
1570 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001571 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001572 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001573 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1574
Adam Cohen8baab352012-03-20 17:39:21 -07001575 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001576
1577 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001578 c.x = mTempLocation[0];
1579 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001580 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001581 }
Adam Cohen8baab352012-03-20 17:39:21 -07001582 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001583 return success;
1584 }
1585
Adam Cohenf3900c22012-11-16 18:28:11 -08001586 /**
1587 * This helper class defines a cluster of views. It helps with defining complex edges
1588 * of the cluster and determining how those edges interact with other views. The edges
1589 * essentially define a fine-grained boundary around the cluster of views -- like a more
1590 * precise version of a bounding box.
1591 */
1592 private class ViewCluster {
1593 final static int LEFT = 0;
1594 final static int TOP = 1;
1595 final static int RIGHT = 2;
1596 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001597
Adam Cohenf3900c22012-11-16 18:28:11 -08001598 ArrayList<View> views;
1599 ItemConfiguration config;
1600 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001601
Adam Cohenf3900c22012-11-16 18:28:11 -08001602 int[] leftEdge = new int[mCountY];
1603 int[] rightEdge = new int[mCountY];
1604 int[] topEdge = new int[mCountX];
1605 int[] bottomEdge = new int[mCountX];
1606 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1607
1608 @SuppressWarnings("unchecked")
1609 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1610 this.views = (ArrayList<View>) views.clone();
1611 this.config = config;
1612 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001613 }
1614
Adam Cohenf3900c22012-11-16 18:28:11 -08001615 void resetEdges() {
1616 for (int i = 0; i < mCountX; i++) {
1617 topEdge[i] = -1;
1618 bottomEdge[i] = -1;
1619 }
1620 for (int i = 0; i < mCountY; i++) {
1621 leftEdge[i] = -1;
1622 rightEdge[i] = -1;
1623 }
1624 leftEdgeDirty = true;
1625 rightEdgeDirty = true;
1626 bottomEdgeDirty = true;
1627 topEdgeDirty = true;
1628 boundingRectDirty = true;
1629 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001630
Adam Cohenf3900c22012-11-16 18:28:11 -08001631 void computeEdge(int which, int[] edge) {
1632 int count = views.size();
1633 for (int i = 0; i < count; i++) {
1634 CellAndSpan cs = config.map.get(views.get(i));
1635 switch (which) {
1636 case LEFT:
1637 int left = cs.x;
1638 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1639 if (left < edge[j] || edge[j] < 0) {
1640 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001641 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001642 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001643 break;
1644 case RIGHT:
1645 int right = cs.x + cs.spanX;
1646 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1647 if (right > edge[j]) {
1648 edge[j] = right;
1649 }
1650 }
1651 break;
1652 case TOP:
1653 int top = cs.y;
1654 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1655 if (top < edge[j] || edge[j] < 0) {
1656 edge[j] = top;
1657 }
1658 }
1659 break;
1660 case BOTTOM:
1661 int bottom = cs.y + cs.spanY;
1662 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1663 if (bottom > edge[j]) {
1664 edge[j] = bottom;
1665 }
1666 }
1667 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001668 }
1669 }
1670 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001671
1672 boolean isViewTouchingEdge(View v, int whichEdge) {
1673 CellAndSpan cs = config.map.get(v);
1674
1675 int[] edge = getEdge(whichEdge);
1676
1677 switch (whichEdge) {
1678 case LEFT:
1679 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1680 if (edge[i] == cs.x + cs.spanX) {
1681 return true;
1682 }
1683 }
1684 break;
1685 case RIGHT:
1686 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1687 if (edge[i] == cs.x) {
1688 return true;
1689 }
1690 }
1691 break;
1692 case TOP:
1693 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1694 if (edge[i] == cs.y + cs.spanY) {
1695 return true;
1696 }
1697 }
1698 break;
1699 case BOTTOM:
1700 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1701 if (edge[i] == cs.y) {
1702 return true;
1703 }
1704 }
1705 break;
1706 }
1707 return false;
1708 }
1709
1710 void shift(int whichEdge, int delta) {
1711 for (View v: views) {
1712 CellAndSpan c = config.map.get(v);
1713 switch (whichEdge) {
1714 case LEFT:
1715 c.x -= delta;
1716 break;
1717 case RIGHT:
1718 c.x += delta;
1719 break;
1720 case TOP:
1721 c.y -= delta;
1722 break;
1723 case BOTTOM:
1724 default:
1725 c.y += delta;
1726 break;
1727 }
1728 }
1729 resetEdges();
1730 }
1731
1732 public void addView(View v) {
1733 views.add(v);
1734 resetEdges();
1735 }
1736
1737 public Rect getBoundingRect() {
1738 if (boundingRectDirty) {
1739 boolean first = true;
1740 for (View v: views) {
1741 CellAndSpan c = config.map.get(v);
1742 if (first) {
1743 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1744 first = false;
1745 } else {
1746 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1747 }
1748 }
1749 }
1750 return boundingRect;
1751 }
1752
1753 public int[] getEdge(int which) {
1754 switch (which) {
1755 case LEFT:
1756 return getLeftEdge();
1757 case RIGHT:
1758 return getRightEdge();
1759 case TOP:
1760 return getTopEdge();
1761 case BOTTOM:
1762 default:
1763 return getBottomEdge();
1764 }
1765 }
1766
1767 public int[] getLeftEdge() {
1768 if (leftEdgeDirty) {
1769 computeEdge(LEFT, leftEdge);
1770 }
1771 return leftEdge;
1772 }
1773
1774 public int[] getRightEdge() {
1775 if (rightEdgeDirty) {
1776 computeEdge(RIGHT, rightEdge);
1777 }
1778 return rightEdge;
1779 }
1780
1781 public int[] getTopEdge() {
1782 if (topEdgeDirty) {
1783 computeEdge(TOP, topEdge);
1784 }
1785 return topEdge;
1786 }
1787
1788 public int[] getBottomEdge() {
1789 if (bottomEdgeDirty) {
1790 computeEdge(BOTTOM, bottomEdge);
1791 }
1792 return bottomEdge;
1793 }
1794
1795 PositionComparator comparator = new PositionComparator();
1796 class PositionComparator implements Comparator<View> {
1797 int whichEdge = 0;
1798 public int compare(View left, View right) {
1799 CellAndSpan l = config.map.get(left);
1800 CellAndSpan r = config.map.get(right);
1801 switch (whichEdge) {
1802 case LEFT:
1803 return (r.x + r.spanX) - (l.x + l.spanX);
1804 case RIGHT:
1805 return l.x - r.x;
1806 case TOP:
1807 return (r.y + r.spanY) - (l.y + l.spanY);
1808 case BOTTOM:
1809 default:
1810 return l.y - r.y;
1811 }
1812 }
1813 }
1814
1815 public void sortConfigurationForEdgePush(int edge) {
1816 comparator.whichEdge = edge;
1817 Collections.sort(config.sortedViews, comparator);
1818 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001819 }
1820
Adam Cohenf3900c22012-11-16 18:28:11 -08001821 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1822 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001823
Adam Cohenf3900c22012-11-16 18:28:11 -08001824 ViewCluster cluster = new ViewCluster(views, currentState);
1825 Rect clusterRect = cluster.getBoundingRect();
1826 int whichEdge;
1827 int pushDistance;
1828 boolean fail = false;
1829
1830 // Determine the edge of the cluster that will be leading the push and how far
1831 // the cluster must be shifted.
1832 if (direction[0] < 0) {
1833 whichEdge = ViewCluster.LEFT;
1834 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001835 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001836 whichEdge = ViewCluster.RIGHT;
1837 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1838 } else if (direction[1] < 0) {
1839 whichEdge = ViewCluster.TOP;
1840 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1841 } else {
1842 whichEdge = ViewCluster.BOTTOM;
1843 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001844 }
1845
Adam Cohenf3900c22012-11-16 18:28:11 -08001846 // Break early for invalid push distance.
1847 if (pushDistance <= 0) {
1848 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001849 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001850
1851 // Mark the occupied state as false for the group of views we want to move.
1852 for (View v: views) {
1853 CellAndSpan c = currentState.map.get(v);
1854 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1855 }
1856
1857 // We save the current configuration -- if we fail to find a solution we will revert
1858 // to the initial state. The process of finding a solution modifies the configuration
1859 // in place, hence the need for revert in the failure case.
1860 currentState.save();
1861
1862 // The pushing algorithm is simplified by considering the views in the order in which
1863 // they would be pushed by the cluster. For example, if the cluster is leading with its
1864 // left edge, we consider sort the views by their right edge, from right to left.
1865 cluster.sortConfigurationForEdgePush(whichEdge);
1866
1867 while (pushDistance > 0 && !fail) {
1868 for (View v: currentState.sortedViews) {
1869 // For each view that isn't in the cluster, we see if the leading edge of the
1870 // cluster is contacting the edge of that view. If so, we add that view to the
1871 // cluster.
1872 if (!cluster.views.contains(v) && v != dragView) {
1873 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1874 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1875 if (!lp.canReorder) {
1876 // The push solution includes the all apps button, this is not viable.
1877 fail = true;
1878 break;
1879 }
1880 cluster.addView(v);
1881 CellAndSpan c = currentState.map.get(v);
1882
1883 // Adding view to cluster, mark it as not occupied.
1884 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1885 }
1886 }
1887 }
1888 pushDistance--;
1889
1890 // The cluster has been completed, now we move the whole thing over in the appropriate
1891 // direction.
1892 cluster.shift(whichEdge, 1);
1893 }
1894
1895 boolean foundSolution = false;
1896 clusterRect = cluster.getBoundingRect();
1897
1898 // Due to the nature of the algorithm, the only check required to verify a valid solution
1899 // is to ensure that completed shifted cluster lies completely within the cell layout.
1900 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1901 clusterRect.bottom <= mCountY) {
1902 foundSolution = true;
1903 } else {
1904 currentState.restore();
1905 }
1906
1907 // In either case, we set the occupied array as marked for the location of the views
1908 for (View v: cluster.views) {
1909 CellAndSpan c = currentState.map.get(v);
1910 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1911 }
1912
1913 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001914 }
1915
Adam Cohen482ed822012-03-02 14:15:13 -08001916 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001917 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001918 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001919
Adam Cohen8baab352012-03-20 17:39:21 -07001920 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001921 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001922 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001923 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001924 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001925 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001926 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001927 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001928 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001929 }
1930 }
Adam Cohen8baab352012-03-20 17:39:21 -07001931
Adam Cohen8baab352012-03-20 17:39:21 -07001932 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001933 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001934 CellAndSpan c = currentState.map.get(v);
1935 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1936 }
1937
Adam Cohen47a876d2012-03-19 13:21:41 -07001938 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1939 int top = boundingRect.top;
1940 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001941 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001942 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001943 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001944 CellAndSpan c = currentState.map.get(v);
1945 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001946 }
1947
Adam Cohen482ed822012-03-02 14:15:13 -08001948 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1949
Adam Cohenf3900c22012-11-16 18:28:11 -08001950 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1951 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001952
Adam Cohen8baab352012-03-20 17:39:21 -07001953 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001954 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001955 int deltaX = mTempLocation[0] - boundingRect.left;
1956 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001957 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001958 CellAndSpan c = currentState.map.get(v);
1959 c.x += deltaX;
1960 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001961 }
1962 success = true;
1963 }
Adam Cohen8baab352012-03-20 17:39:21 -07001964
1965 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001966 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001967 CellAndSpan c = currentState.map.get(v);
1968 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001969 }
1970 return success;
1971 }
1972
1973 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1974 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1975 }
1976
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001977 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1978 // to push items in each of the cardinal directions, in an order based on the direction vector
1979 // passed.
1980 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1981 int[] direction, View ignoreView, ItemConfiguration solution) {
1982 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001983 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001984 // separately in each of the components.
1985 int temp = direction[1];
1986 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001987
1988 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001989 ignoreView, solution)) {
1990 return true;
1991 }
1992 direction[1] = temp;
1993 temp = direction[0];
1994 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001995
1996 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001997 ignoreView, solution)) {
1998 return true;
1999 }
2000 // Revert the direction
2001 direction[0] = temp;
2002
2003 // Now we try pushing in each component of the opposite direction
2004 direction[0] *= -1;
2005 direction[1] *= -1;
2006 temp = direction[1];
2007 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002008 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002009 ignoreView, solution)) {
2010 return true;
2011 }
2012
2013 direction[1] = temp;
2014 temp = direction[0];
2015 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002016 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002017 ignoreView, solution)) {
2018 return true;
2019 }
2020 // revert the direction
2021 direction[0] = temp;
2022 direction[0] *= -1;
2023 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002024
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002025 } else {
2026 // If the direction vector has a single non-zero component, we push first in the
2027 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002028 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002029 ignoreView, solution)) {
2030 return true;
2031 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002032 // Then we try the opposite direction
2033 direction[0] *= -1;
2034 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002035 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002036 ignoreView, solution)) {
2037 return true;
2038 }
2039 // Switch the direction back
2040 direction[0] *= -1;
2041 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002042
2043 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002044 // to find a solution by pushing along the perpendicular axis.
2045
2046 // Swap the components
2047 int temp = direction[1];
2048 direction[1] = direction[0];
2049 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002050 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002051 ignoreView, solution)) {
2052 return true;
2053 }
2054
2055 // Then we try the opposite direction
2056 direction[0] *= -1;
2057 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002058 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002059 ignoreView, solution)) {
2060 return true;
2061 }
2062 // Switch the direction back
2063 direction[0] *= -1;
2064 direction[1] *= -1;
2065
2066 // Swap the components back
2067 temp = direction[1];
2068 direction[1] = direction[0];
2069 direction[0] = temp;
2070 }
2071 return false;
2072 }
2073
Adam Cohen482ed822012-03-02 14:15:13 -08002074 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002075 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002076 // Return early if get invalid cell positions
2077 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002078
Adam Cohen8baab352012-03-20 17:39:21 -07002079 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002080 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002081
Adam Cohen8baab352012-03-20 17:39:21 -07002082 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002083 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002084 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002085 if (c != null) {
2086 c.x = cellX;
2087 c.y = cellY;
2088 }
Adam Cohen482ed822012-03-02 14:15:13 -08002089 }
Adam Cohen482ed822012-03-02 14:15:13 -08002090 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2091 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002092 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002093 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002094 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002095 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002096 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002097 if (Rect.intersects(r0, r1)) {
2098 if (!lp.canReorder) {
2099 return false;
2100 }
2101 mIntersectingViews.add(child);
2102 }
2103 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002104
Winson Chung5f8afe62013-08-12 16:19:28 -07002105 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002106 // we try to find a solution such that no displaced item travels through another item
2107 // without also displacing that item.
2108 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002109 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002110 return true;
2111 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002112
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002113 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002114 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002115 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002116 return true;
2117 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002118
Adam Cohen482ed822012-03-02 14:15:13 -08002119 // Ok, they couldn't move as a block, let's move them individually
2120 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002121 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002122 return false;
2123 }
2124 }
2125 return true;
2126 }
2127
2128 /*
2129 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2130 * the provided point and the provided cell
2131 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002132 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002133 double angle = Math.atan(((float) deltaY) / deltaX);
2134
2135 result[0] = 0;
2136 result[1] = 0;
2137 if (Math.abs(Math.cos(angle)) > 0.5f) {
2138 result[0] = (int) Math.signum(deltaX);
2139 }
2140 if (Math.abs(Math.sin(angle)) > 0.5f) {
2141 result[1] = (int) Math.signum(deltaY);
2142 }
2143 }
2144
Adam Cohen8baab352012-03-20 17:39:21 -07002145 private void copyOccupiedArray(boolean[][] occupied) {
2146 for (int i = 0; i < mCountX; i++) {
2147 for (int j = 0; j < mCountY; j++) {
2148 occupied[i][j] = mOccupied[i][j];
2149 }
2150 }
2151 }
2152
Adam Cohen482ed822012-03-02 14:15:13 -08002153 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
2154 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002155 // Copy the current state into the solution. This solution will be manipulated as necessary.
2156 copyCurrentStateToSolution(solution, false);
2157 // Copy the current occupied array into the temporary occupied array. This array will be
2158 // manipulated as necessary to find a solution.
2159 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002160
2161 // We find the nearest cell into which we would place the dragged item, assuming there's
2162 // nothing in its way.
2163 int result[] = new int[2];
2164 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2165
2166 boolean success = false;
2167 // First we try the exact nearest position of the item being dragged,
2168 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002169 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2170 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002171
2172 if (!success) {
2173 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2174 // x, then 1 in y etc.
2175 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
2176 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
2177 dragView, false, solution);
2178 } else if (spanY > minSpanY) {
2179 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
2180 dragView, true, solution);
2181 }
2182 solution.isSolution = false;
2183 } else {
2184 solution.isSolution = true;
2185 solution.dragViewX = result[0];
2186 solution.dragViewY = result[1];
2187 solution.dragViewSpanX = spanX;
2188 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002189 }
2190 return solution;
2191 }
2192
2193 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002194 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002195 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002196 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002197 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002198 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002199 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002200 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002201 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002202 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002203 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002204 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002205 }
2206 }
2207
2208 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2209 for (int i = 0; i < mCountX; i++) {
2210 for (int j = 0; j < mCountY; j++) {
2211 mTmpOccupied[i][j] = false;
2212 }
2213 }
2214
Michael Jurkaa52570f2012-03-20 03:18:20 -07002215 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002216 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002217 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002218 if (child == dragView) continue;
2219 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002220 CellAndSpan c = solution.map.get(child);
2221 if (c != null) {
2222 lp.tmpCellX = c.x;
2223 lp.tmpCellY = c.y;
2224 lp.cellHSpan = c.spanX;
2225 lp.cellVSpan = c.spanY;
2226 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002227 }
2228 }
2229 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2230 solution.dragViewSpanY, mTmpOccupied, true);
2231 }
2232
2233 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2234 commitDragView) {
2235
2236 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2237 for (int i = 0; i < mCountX; i++) {
2238 for (int j = 0; j < mCountY; j++) {
2239 occupied[i][j] = false;
2240 }
2241 }
2242
Michael Jurkaa52570f2012-03-20 03:18:20 -07002243 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002244 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002245 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002246 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002247 CellAndSpan c = solution.map.get(child);
2248 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002249 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2250 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002251 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002252 }
2253 }
2254 if (commitDragView) {
2255 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2256 solution.dragViewSpanY, occupied, true);
2257 }
2258 }
2259
Adam Cohen19f37922012-03-21 11:59:11 -07002260 // This method starts or changes the reorder hint animations
2261 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2262 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002263 for (int i = 0; i < childCount; i++) {
2264 View child = mShortcutsAndWidgets.getChildAt(i);
2265 if (child == dragView) continue;
2266 CellAndSpan c = solution.map.get(child);
2267 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2268 if (c != null) {
2269 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2270 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002271 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002272 }
2273 }
2274 }
2275
2276 // Class which represents the reorder hint animations. These animations show that an item is
2277 // in a temporary state, and hint at where the item will return to.
2278 class ReorderHintAnimation {
2279 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002280 float finalDeltaX;
2281 float finalDeltaY;
2282 float initDeltaX;
2283 float initDeltaY;
2284 float finalScale;
2285 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002286 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002287 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002288
2289 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2290 int spanX, int spanY) {
2291 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2292 final int x0 = mTmpPoint[0];
2293 final int y0 = mTmpPoint[1];
2294 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2295 final int x1 = mTmpPoint[0];
2296 final int y1 = mTmpPoint[1];
2297 final int dX = x1 - x0;
2298 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002299 finalDeltaX = 0;
2300 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002301 if (dX == dY && dX == 0) {
2302 } else {
2303 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002304 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002305 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002306 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002307 } else {
2308 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002309 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002310 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002311 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002312 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002313 }
2314 }
Adam Cohend024f982012-05-23 18:26:45 -07002315 initDeltaX = child.getTranslationX();
2316 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002317 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002318 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002319 this.child = child;
2320 }
2321
Adam Cohend024f982012-05-23 18:26:45 -07002322 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002323 if (mShakeAnimators.containsKey(child)) {
2324 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002325 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002326 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002327 if (finalDeltaX == 0 && finalDeltaY == 0) {
2328 completeAnimationImmediately();
2329 return;
2330 }
Adam Cohen19f37922012-03-21 11:59:11 -07002331 }
Adam Cohend024f982012-05-23 18:26:45 -07002332 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002333 return;
2334 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002335 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002336 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002337 va.setRepeatMode(ValueAnimator.REVERSE);
2338 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002339 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002340 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002341 va.addUpdateListener(new AnimatorUpdateListener() {
2342 @Override
2343 public void onAnimationUpdate(ValueAnimator animation) {
2344 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002345 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2346 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002347 child.setTranslationX(x);
2348 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002349 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002350 child.setScaleX(s);
2351 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002352 }
2353 });
2354 va.addListener(new AnimatorListenerAdapter() {
2355 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002356 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002357 initDeltaX = 0;
2358 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002359 initScale = getChildrenScale();
Adam Cohen19f37922012-03-21 11:59:11 -07002360 }
2361 });
Adam Cohen19f37922012-03-21 11:59:11 -07002362 mShakeAnimators.put(child, this);
2363 va.start();
2364 }
2365
Adam Cohend024f982012-05-23 18:26:45 -07002366 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002367 if (a != null) {
2368 a.cancel();
2369 }
Adam Cohen19f37922012-03-21 11:59:11 -07002370 }
Adam Cohene7587d22012-05-24 18:50:02 -07002371
Brandon Keely50e6e562012-05-08 16:28:49 -07002372 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002373 if (a != null) {
2374 a.cancel();
2375 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002376
Michael Jurka2ecf9952012-06-18 12:52:28 -07002377 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002378 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002379 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002380 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2381 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002382 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2383 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002384 );
2385 s.setDuration(REORDER_ANIMATION_DURATION);
2386 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2387 s.start();
2388 }
Adam Cohen19f37922012-03-21 11:59:11 -07002389 }
2390
2391 private void completeAndClearReorderHintAnimations() {
2392 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002393 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002394 }
2395 mShakeAnimators.clear();
2396 }
2397
Adam Cohen482ed822012-03-02 14:15:13 -08002398 private void commitTempPlacement() {
2399 for (int i = 0; i < mCountX; i++) {
2400 for (int j = 0; j < mCountY; j++) {
2401 mOccupied[i][j] = mTmpOccupied[i][j];
2402 }
2403 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002404 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002405 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002406 View child = mShortcutsAndWidgets.getChildAt(i);
2407 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2408 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002409 // We do a null check here because the item info can be null in the case of the
2410 // AllApps button in the hotseat.
2411 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002412 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2413 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2414 info.requiresDbUpdate = true;
2415 }
Adam Cohen2acce882012-03-28 19:03:19 -07002416 info.cellX = lp.cellX = lp.tmpCellX;
2417 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002418 info.spanX = lp.cellHSpan;
2419 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002420 }
Adam Cohen482ed822012-03-02 14:15:13 -08002421 }
Adam Cohen2acce882012-03-28 19:03:19 -07002422 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002423 }
2424
2425 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002426 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002427 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002428 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002429 lp.useTmpCoords = useTempCoords;
2430 }
2431 }
2432
Adam Cohen482ed822012-03-02 14:15:13 -08002433 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2434 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2435 int[] result = new int[2];
2436 int[] resultSpan = new int[2];
2437 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2438 resultSpan);
2439 if (result[0] >= 0 && result[1] >= 0) {
2440 copyCurrentStateToSolution(solution, false);
2441 solution.dragViewX = result[0];
2442 solution.dragViewY = result[1];
2443 solution.dragViewSpanX = resultSpan[0];
2444 solution.dragViewSpanY = resultSpan[1];
2445 solution.isSolution = true;
2446 } else {
2447 solution.isSolution = false;
2448 }
2449 return solution;
2450 }
2451
2452 public void prepareChildForDrag(View child) {
2453 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002454 }
2455
Adam Cohen19f37922012-03-21 11:59:11 -07002456 /* This seems like it should be obvious and straight-forward, but when the direction vector
2457 needs to match with the notion of the dragView pushing other views, we have to employ
2458 a slightly more subtle notion of the direction vector. The question is what two points is
2459 the vector between? The center of the dragView and its desired destination? Not quite, as
2460 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2461 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2462 or right, which helps make pushing feel right.
2463 */
2464 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2465 int spanY, View dragView, int[] resultDirection) {
2466 int[] targetDestination = new int[2];
2467
2468 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2469 Rect dragRect = new Rect();
2470 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2471 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2472
2473 Rect dropRegionRect = new Rect();
2474 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2475 dragView, dropRegionRect, mIntersectingViews);
2476
2477 int dropRegionSpanX = dropRegionRect.width();
2478 int dropRegionSpanY = dropRegionRect.height();
2479
2480 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2481 dropRegionRect.height(), dropRegionRect);
2482
2483 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2484 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2485
2486 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2487 deltaX = 0;
2488 }
2489 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2490 deltaY = 0;
2491 }
2492
2493 if (deltaX == 0 && deltaY == 0) {
2494 // No idea what to do, give a random direction.
2495 resultDirection[0] = 1;
2496 resultDirection[1] = 0;
2497 } else {
2498 computeDirectionVector(deltaX, deltaY, resultDirection);
2499 }
2500 }
2501
2502 // For a given cell and span, fetch the set of views intersecting the region.
2503 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2504 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2505 if (boundingRect != null) {
2506 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2507 }
2508 intersectingViews.clear();
2509 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2510 Rect r1 = new Rect();
2511 final int count = mShortcutsAndWidgets.getChildCount();
2512 for (int i = 0; i < count; i++) {
2513 View child = mShortcutsAndWidgets.getChildAt(i);
2514 if (child == dragView) continue;
2515 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2516 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2517 if (Rect.intersects(r0, r1)) {
2518 mIntersectingViews.add(child);
2519 if (boundingRect != null) {
2520 boundingRect.union(r1);
2521 }
2522 }
2523 }
2524 }
2525
2526 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2527 View dragView, int[] result) {
2528 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2529 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2530 mIntersectingViews);
2531 return !mIntersectingViews.isEmpty();
2532 }
2533
2534 void revertTempState() {
2535 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2536 final int count = mShortcutsAndWidgets.getChildCount();
2537 for (int i = 0; i < count; i++) {
2538 View child = mShortcutsAndWidgets.getChildAt(i);
2539 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2540 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2541 lp.tmpCellX = lp.cellX;
2542 lp.tmpCellY = lp.cellY;
2543 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2544 0, false, false);
2545 }
2546 }
2547 completeAndClearReorderHintAnimations();
2548 setItemPlacementDirty(false);
2549 }
2550
Adam Cohenbebf0422012-04-11 18:06:28 -07002551 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2552 View dragView, int[] direction, boolean commit) {
2553 int[] pixelXY = new int[2];
2554 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2555
2556 // First we determine if things have moved enough to cause a different layout
2557 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2558 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2559
2560 setUseTempCoords(true);
2561 if (swapSolution != null && swapSolution.isSolution) {
2562 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2563 // committing anything or animating anything as we just want to determine if a solution
2564 // exists
2565 copySolutionToTempState(swapSolution, dragView);
2566 setItemPlacementDirty(true);
2567 animateItemsToSolution(swapSolution, dragView, commit);
2568
2569 if (commit) {
2570 commitTempPlacement();
2571 completeAndClearReorderHintAnimations();
2572 setItemPlacementDirty(false);
2573 } else {
2574 beginOrAdjustHintAnimations(swapSolution, dragView,
2575 REORDER_ANIMATION_DURATION);
2576 }
2577 mShortcutsAndWidgets.requestLayout();
2578 }
2579 return swapSolution.isSolution;
2580 }
2581
Adam Cohen482ed822012-03-02 14:15:13 -08002582 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2583 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002584 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002585 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002586
2587 if (resultSpan == null) {
2588 resultSpan = new int[2];
2589 }
2590
Adam Cohen19f37922012-03-21 11:59:11 -07002591 // When we are checking drop validity or actually dropping, we don't recompute the
2592 // direction vector, since we want the solution to match the preview, and it's possible
2593 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002594 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2595 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002596 mDirectionVector[0] = mPreviousReorderDirection[0];
2597 mDirectionVector[1] = mPreviousReorderDirection[1];
2598 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002599 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2600 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2601 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002602 }
2603 } else {
2604 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2605 mPreviousReorderDirection[0] = mDirectionVector[0];
2606 mPreviousReorderDirection[1] = mDirectionVector[1];
2607 }
2608
Adam Cohen482ed822012-03-02 14:15:13 -08002609 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2610 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2611
2612 // We attempt the approach which doesn't shuffle views at all
2613 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2614 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2615
2616 ItemConfiguration finalSolution = null;
2617 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2618 finalSolution = swapSolution;
2619 } else if (noShuffleSolution.isSolution) {
2620 finalSolution = noShuffleSolution;
2621 }
2622
2623 boolean foundSolution = true;
2624 if (!DESTRUCTIVE_REORDER) {
2625 setUseTempCoords(true);
2626 }
2627
2628 if (finalSolution != null) {
2629 result[0] = finalSolution.dragViewX;
2630 result[1] = finalSolution.dragViewY;
2631 resultSpan[0] = finalSolution.dragViewSpanX;
2632 resultSpan[1] = finalSolution.dragViewSpanY;
2633
2634 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2635 // committing anything or animating anything as we just want to determine if a solution
2636 // exists
2637 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2638 if (!DESTRUCTIVE_REORDER) {
2639 copySolutionToTempState(finalSolution, dragView);
2640 }
2641 setItemPlacementDirty(true);
2642 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2643
Adam Cohen19f37922012-03-21 11:59:11 -07002644 if (!DESTRUCTIVE_REORDER &&
2645 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002646 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002647 completeAndClearReorderHintAnimations();
2648 setItemPlacementDirty(false);
2649 } else {
2650 beginOrAdjustHintAnimations(finalSolution, dragView,
2651 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002652 }
2653 }
2654 } else {
2655 foundSolution = false;
2656 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2657 }
2658
2659 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2660 setUseTempCoords(false);
2661 }
Adam Cohen482ed822012-03-02 14:15:13 -08002662
Michael Jurkaa52570f2012-03-20 03:18:20 -07002663 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002664 return result;
2665 }
2666
Adam Cohen19f37922012-03-21 11:59:11 -07002667 void setItemPlacementDirty(boolean dirty) {
2668 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002669 }
Adam Cohen19f37922012-03-21 11:59:11 -07002670 boolean isItemPlacementDirty() {
2671 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002672 }
2673
2674 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002675 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002676 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2677 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohen482ed822012-03-02 14:15:13 -08002678 boolean isSolution = false;
2679 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2680
Adam Cohenf3900c22012-11-16 18:28:11 -08002681 void save() {
2682 // Copy current state into savedMap
2683 for (View v: map.keySet()) {
2684 map.get(v).copy(savedMap.get(v));
2685 }
2686 }
2687
2688 void restore() {
2689 // Restore current state from savedMap
2690 for (View v: savedMap.keySet()) {
2691 savedMap.get(v).copy(map.get(v));
2692 }
2693 }
2694
2695 void add(View v, CellAndSpan cs) {
2696 map.put(v, cs);
2697 savedMap.put(v, new CellAndSpan());
2698 sortedViews.add(v);
2699 }
2700
Adam Cohen482ed822012-03-02 14:15:13 -08002701 int area() {
2702 return dragViewSpanX * dragViewSpanY;
2703 }
Adam Cohen8baab352012-03-20 17:39:21 -07002704 }
2705
2706 private class CellAndSpan {
2707 int x, y;
2708 int spanX, spanY;
2709
Adam Cohenf3900c22012-11-16 18:28:11 -08002710 public CellAndSpan() {
2711 }
2712
2713 public void copy(CellAndSpan copy) {
2714 copy.x = x;
2715 copy.y = y;
2716 copy.spanX = spanX;
2717 copy.spanY = spanY;
2718 }
2719
Adam Cohen8baab352012-03-20 17:39:21 -07002720 public CellAndSpan(int x, int y, int spanX, int spanY) {
2721 this.x = x;
2722 this.y = y;
2723 this.spanX = spanX;
2724 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002725 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002726
2727 public String toString() {
2728 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2729 }
2730
Adam Cohen482ed822012-03-02 14:15:13 -08002731 }
2732
Adam Cohendf035382011-04-11 17:22:04 -07002733 /**
2734 * Find a vacant area that will fit the given bounds nearest the requested
2735 * cell location. Uses Euclidean distance to score multiple vacant areas.
2736 *
2737 * @param pixelX The X location at which you want to search for a vacant area.
2738 * @param pixelY The Y location at which you want to search for a vacant area.
2739 * @param spanX Horizontal span of the object.
2740 * @param spanY Vertical span of the object.
2741 * @param ignoreView Considers space occupied by this view as unoccupied
2742 * @param result Previously returned value to possibly recycle.
2743 * @return The X, Y cell of a vacant area that can contain this object,
2744 * nearest the requested location.
2745 */
2746 int[] findNearestVacantArea(
2747 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2748 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2749 }
2750
2751 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002752 * Find a vacant area that will fit the given bounds nearest the requested
2753 * cell location. Uses Euclidean distance to score multiple vacant areas.
2754 *
2755 * @param pixelX The X location at which you want to search for a vacant area.
2756 * @param pixelY The Y location at which you want to search for a vacant area.
2757 * @param minSpanX The minimum horizontal span required
2758 * @param minSpanY The minimum vertical span required
2759 * @param spanX Horizontal span of the object.
2760 * @param spanY Vertical span of the object.
2761 * @param ignoreView Considers space occupied by this view as unoccupied
2762 * @param result Previously returned value to possibly recycle.
2763 * @return The X, Y cell of a vacant area that can contain this object,
2764 * nearest the requested location.
2765 */
2766 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2767 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002768 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2769 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002770 }
2771
2772 /**
Adam Cohendf035382011-04-11 17:22:04 -07002773 * Find a starting cell position that will fit the given bounds nearest the requested
2774 * cell location. Uses Euclidean distance to score multiple vacant areas.
2775 *
2776 * @param pixelX The X location at which you want to search for a vacant area.
2777 * @param pixelY The Y location at which you want to search for a vacant area.
2778 * @param spanX Horizontal span of the object.
2779 * @param spanY Vertical span of the object.
2780 * @param ignoreView Considers space occupied by this view as unoccupied
2781 * @param result Previously returned value to possibly recycle.
2782 * @return The X, Y cell of a vacant area that can contain this object,
2783 * nearest the requested location.
2784 */
2785 int[] findNearestArea(
2786 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2787 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2788 }
2789
Michael Jurka0280c3b2010-09-17 15:00:07 -07002790 boolean existsEmptyCell() {
2791 return findCellForSpan(null, 1, 1);
2792 }
2793
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002794 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002795 * Finds the upper-left coordinate of the first rectangle in the grid that can
2796 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2797 * then this method will only return coordinates for rectangles that contain the cell
2798 * (intersectX, intersectY)
2799 *
2800 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2801 * can be found.
2802 * @param spanX The horizontal span of the cell we want to find.
2803 * @param spanY The vertical span of the cell we want to find.
2804 *
2805 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002806 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002807 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002808 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002809 }
2810
2811 /**
2812 * Like above, but ignores any cells occupied by the item "ignoreView"
2813 *
2814 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2815 * can be found.
2816 * @param spanX The horizontal span of the cell we want to find.
2817 * @param spanY The vertical span of the cell we want to find.
2818 * @param ignoreView The home screen item we should treat as not occupying any space
2819 * @return
2820 */
2821 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002822 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2823 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002824 }
2825
2826 /**
2827 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2828 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2829 *
2830 * @param spanX The horizontal span of the cell we want to find.
2831 * @param spanY The vertical span of the cell we want to find.
2832 * @param ignoreView The home screen item we should treat as not occupying any space
2833 * @param intersectX The X coordinate of the cell that we should try to overlap
2834 * @param intersectX The Y coordinate of the cell that we should try to overlap
2835 *
2836 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2837 */
2838 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2839 int intersectX, int intersectY) {
2840 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002841 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002842 }
2843
2844 /**
2845 * The superset of the above two methods
2846 */
2847 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002848 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002849 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002850 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002851
Michael Jurka28750fb2010-09-24 17:43:49 -07002852 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002853 while (true) {
2854 int startX = 0;
2855 if (intersectX >= 0) {
2856 startX = Math.max(startX, intersectX - (spanX - 1));
2857 }
2858 int endX = mCountX - (spanX - 1);
2859 if (intersectX >= 0) {
2860 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2861 }
2862 int startY = 0;
2863 if (intersectY >= 0) {
2864 startY = Math.max(startY, intersectY - (spanY - 1));
2865 }
2866 int endY = mCountY - (spanY - 1);
2867 if (intersectY >= 0) {
2868 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2869 }
2870
Winson Chungbbc60d82010-11-11 16:34:41 -08002871 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002872 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002873 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002874 for (int i = 0; i < spanX; i++) {
2875 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002876 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002877 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002878 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002879 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002880 continue inner;
2881 }
2882 }
2883 }
2884 if (cellXY != null) {
2885 cellXY[0] = x;
2886 cellXY[1] = y;
2887 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002888 foundCell = true;
2889 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002890 }
2891 }
2892 if (intersectX == -1 && intersectY == -1) {
2893 break;
2894 } else {
2895 // if we failed to find anything, try again but without any requirements of
2896 // intersecting
2897 intersectX = -1;
2898 intersectY = -1;
2899 continue;
2900 }
2901 }
2902
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002903 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002904 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002905 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002906 }
2907
2908 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002909 * A drag event has begun over this layout.
2910 * It may have begun over this layout (in which case onDragChild is called first),
2911 * or it may have begun on another layout.
2912 */
2913 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002914 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002915 mDragging = true;
2916 }
2917
2918 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002919 * Called when drag has left this CellLayout or has been completed (successfully or not)
2920 */
2921 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002922 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002923 // This can actually be called when we aren't in a drag, e.g. when adding a new
2924 // item to this layout via the customize drawer.
2925 // Guard against that case.
2926 if (mDragging) {
2927 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002928 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002929
2930 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002931 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002932 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2933 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002934 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002935 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002936 }
2937
2938 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002939 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002940 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002941 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002942 *
2943 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002945 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002946 if (child != null) {
2947 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002948 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002949 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002951 }
2952
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002953 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002954 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002955 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 * @param cellX X coordinate of upper left corner expressed as a cell position
2957 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002958 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002960 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002961 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002962 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 final int cellWidth = mCellWidth;
2964 final int cellHeight = mCellHeight;
2965 final int widthGap = mWidthGap;
2966 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002967
Winson Chung4b825dcd2011-06-19 12:41:22 -07002968 final int hStartPadding = getPaddingLeft();
2969 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2972 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2973
2974 int x = hStartPadding + cellX * (cellWidth + widthGap);
2975 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002976
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002977 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002979
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002980 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002981 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002982 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002983 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002984 * @param width Width in pixels
2985 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002986 * @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 -08002987 */
Winson Chung66700732013-08-20 16:56:15 -07002988 public static int[] rectToCell(int width, int height, int[] result) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002989 LauncherAppState app = LauncherAppState.getInstance();
2990 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -07002991 Rect padding = grid.getWorkspacePadding(grid.isLandscape ?
2992 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
Winson Chung5f8afe62013-08-12 16:19:28 -07002993
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002994 // Always assume we're working with the smallest span to make sure we
2995 // reserve enough space in both orientations.
Winson Chung66700732013-08-20 16:56:15 -07002996 int parentWidth = grid.calculateCellWidth(grid.widthPx
2997 - padding.left - padding.right, (int) grid.numColumns);
2998 int parentHeight = grid.calculateCellHeight(grid.heightPx
2999 - padding.top - padding.bottom, (int) grid.numRows);
3000 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04003001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003002 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07003003 int spanX = (int) Math.ceil(width / (float) smallerSize);
3004 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04003005
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07003006 if (result == null) {
3007 return new int[] { spanX, spanY };
3008 }
3009 result[0] = spanX;
3010 result[1] = spanY;
3011 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003012 }
3013
Michael Jurkaf12c75c2011-01-25 22:41:40 -08003014 public int[] cellSpansToSize(int hSpans, int vSpans) {
3015 int[] size = new int[2];
3016 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
3017 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
3018 return size;
3019 }
3020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003021 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08003022 * Calculate the grid spans needed to fit given item
3023 */
3024 public void calculateSpans(ItemInfo info) {
3025 final int minWidth;
3026 final int minHeight;
3027
3028 if (info instanceof LauncherAppWidgetInfo) {
3029 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3030 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3031 } else if (info instanceof PendingAddWidgetInfo) {
3032 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3033 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3034 } else {
3035 // It's not a widget, so it must be 1x1
3036 info.spanX = info.spanY = 1;
3037 return;
3038 }
3039 int[] spans = rectToCell(minWidth, minHeight, null);
3040 info.spanX = spans[0];
3041 info.spanY = spans[1];
3042 }
3043
3044 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003045 * Find the first vacant cell, if there is one.
3046 *
3047 * @param vacant Holds the x and y coordinate of the vacant cell
3048 * @param spanX Horizontal cell span.
3049 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003050 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003051 * @return True if a vacant cell was found
3052 */
3053 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003054
Michael Jurka0280c3b2010-09-17 15:00:07 -07003055 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003056 }
3057
3058 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3059 int xCount, int yCount, boolean[][] occupied) {
3060
Adam Cohen2801caf2011-05-13 20:57:39 -07003061 for (int y = 0; y < yCount; y++) {
3062 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003063 boolean available = !occupied[x][y];
3064out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3065 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3066 available = available && !occupied[i][j];
3067 if (!available) break out;
3068 }
3069 }
3070
3071 if (available) {
3072 vacant[0] = x;
3073 vacant[1] = y;
3074 return true;
3075 }
3076 }
3077 }
3078
3079 return false;
3080 }
3081
Michael Jurka0280c3b2010-09-17 15:00:07 -07003082 private void clearOccupiedCells() {
3083 for (int x = 0; x < mCountX; x++) {
3084 for (int y = 0; y < mCountY; y++) {
3085 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003086 }
3087 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003089
Adam Cohend41fbf52012-02-16 23:53:59 -08003090 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003091 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003092 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003093 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003094
Adam Cohend4844c32011-02-18 19:25:06 -08003095 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003096 markCellsAsOccupiedForView(view, mOccupied);
3097 }
3098 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003099 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003100 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003101 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003102 }
3103
Adam Cohend4844c32011-02-18 19:25:06 -08003104 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003105 markCellsAsUnoccupiedForView(view, mOccupied);
3106 }
3107 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003108 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003109 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003110 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003111 }
3112
Adam Cohen482ed822012-03-02 14:15:13 -08003113 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3114 boolean value) {
3115 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003116 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3117 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003118 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003119 }
3120 }
3121 }
3122
Adam Cohen2801caf2011-05-13 20:57:39 -07003123 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003124 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003125 (Math.max((mCountX - 1), 0) * mWidthGap);
3126 }
3127
3128 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003129 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003130 (Math.max((mCountY - 1), 0) * mHeightGap);
3131 }
3132
Michael Jurka66d72172011-04-12 16:29:25 -07003133 public boolean isOccupied(int x, int y) {
3134 if (x < mCountX && y < mCountY) {
3135 return mOccupied[x][y];
3136 } else {
3137 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3138 }
3139 }
3140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003141 @Override
3142 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3143 return new CellLayout.LayoutParams(getContext(), attrs);
3144 }
3145
3146 @Override
3147 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3148 return p instanceof CellLayout.LayoutParams;
3149 }
3150
3151 @Override
3152 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3153 return new CellLayout.LayoutParams(p);
3154 }
3155
Winson Chungaafa03c2010-06-11 17:34:16 -07003156 public static class CellLayoutAnimationController extends LayoutAnimationController {
3157 public CellLayoutAnimationController(Animation animation, float delay) {
3158 super(animation, delay);
3159 }
3160
3161 @Override
3162 protected long getDelayForView(View view) {
3163 return (int) (Math.random() * 150);
3164 }
3165 }
3166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3168 /**
3169 * Horizontal location of the item in the grid.
3170 */
3171 @ViewDebug.ExportedProperty
3172 public int cellX;
3173
3174 /**
3175 * Vertical location of the item in the grid.
3176 */
3177 @ViewDebug.ExportedProperty
3178 public int cellY;
3179
3180 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003181 * Temporary horizontal location of the item in the grid during reorder
3182 */
3183 public int tmpCellX;
3184
3185 /**
3186 * Temporary vertical location of the item in the grid during reorder
3187 */
3188 public int tmpCellY;
3189
3190 /**
3191 * Indicates that the temporary coordinates should be used to layout the items
3192 */
3193 public boolean useTmpCoords;
3194
3195 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003196 * Number of cells spanned horizontally by the item.
3197 */
3198 @ViewDebug.ExportedProperty
3199 public int cellHSpan;
3200
3201 /**
3202 * Number of cells spanned vertically by the item.
3203 */
3204 @ViewDebug.ExportedProperty
3205 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003206
Adam Cohen1b607ed2011-03-03 17:26:50 -08003207 /**
3208 * Indicates whether the item will set its x, y, width and height parameters freely,
3209 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3210 */
Adam Cohend4844c32011-02-18 19:25:06 -08003211 public boolean isLockedToGrid = true;
3212
Adam Cohen482ed822012-03-02 14:15:13 -08003213 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07003214 * Indicates that this item should use the full extents of its parent.
3215 */
3216 public boolean isFullscreen = false;
3217
3218 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003219 * Indicates whether this item can be reordered. Always true except in the case of the
3220 * the AllApps button.
3221 */
3222 public boolean canReorder = true;
3223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003224 // X coordinate of the view in the layout.
3225 @ViewDebug.ExportedProperty
3226 int x;
3227 // Y coordinate of the view in the layout.
3228 @ViewDebug.ExportedProperty
3229 int y;
3230
Romain Guy84f296c2009-11-04 15:00:44 -08003231 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003233 public LayoutParams(Context c, AttributeSet attrs) {
3234 super(c, attrs);
3235 cellHSpan = 1;
3236 cellVSpan = 1;
3237 }
3238
3239 public LayoutParams(ViewGroup.LayoutParams source) {
3240 super(source);
3241 cellHSpan = 1;
3242 cellVSpan = 1;
3243 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003244
3245 public LayoutParams(LayoutParams source) {
3246 super(source);
3247 this.cellX = source.cellX;
3248 this.cellY = source.cellY;
3249 this.cellHSpan = source.cellHSpan;
3250 this.cellVSpan = source.cellVSpan;
3251 }
3252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003253 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003254 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003255 this.cellX = cellX;
3256 this.cellY = cellY;
3257 this.cellHSpan = cellHSpan;
3258 this.cellVSpan = cellVSpan;
3259 }
3260
Adam Cohen2374abf2013-04-16 14:56:57 -07003261 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3262 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003263 if (isLockedToGrid) {
3264 final int myCellHSpan = cellHSpan;
3265 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003266 int myCellX = useTmpCoords ? tmpCellX : cellX;
3267 int myCellY = useTmpCoords ? tmpCellY : cellY;
3268
3269 if (invertHorizontally) {
3270 myCellX = colCount - myCellX - cellHSpan;
3271 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003272
Adam Cohend4844c32011-02-18 19:25:06 -08003273 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3274 leftMargin - rightMargin;
3275 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3276 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003277 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3278 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003279 }
3280 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003281
Winson Chungaafa03c2010-06-11 17:34:16 -07003282 public String toString() {
3283 return "(" + this.cellX + ", " + this.cellY + ")";
3284 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003285
3286 public void setWidth(int width) {
3287 this.width = width;
3288 }
3289
3290 public int getWidth() {
3291 return width;
3292 }
3293
3294 public void setHeight(int height) {
3295 this.height = height;
3296 }
3297
3298 public int getHeight() {
3299 return height;
3300 }
3301
3302 public void setX(int x) {
3303 this.x = x;
3304 }
3305
3306 public int getX() {
3307 return x;
3308 }
3309
3310 public void setY(int y) {
3311 this.y = y;
3312 }
3313
3314 public int getY() {
3315 return y;
3316 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003317 }
3318
Michael Jurka0280c3b2010-09-17 15:00:07 -07003319 // This class stores info for two purposes:
3320 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3321 // its spanX, spanY, and the screen it is on
3322 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3323 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3324 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003325 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003326 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003327 int cellX = -1;
3328 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003329 int spanX;
3330 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003331 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003332 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003333
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003334 @Override
3335 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003336 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3337 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003338 }
3339 }
Michael Jurkad771c962011-08-09 15:00:48 -07003340
3341 public boolean lastDownOnOccupiedCell() {
3342 return mLastDownOnOccupiedCell;
3343 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003344}