blob: 1d10553f7b6379b475b6f1f373c45a7087cceb90 [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);
Michael Jurka18014792010-10-14 09:01:34 -07001028 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001029 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001030 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001031 }
1032
1033 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001035 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037
1038 @Override
1039 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001040 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 }
1042
Michael Jurka5f1c5092010-09-03 14:15:02 -07001043 public float getBackgroundAlpha() {
1044 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001045 }
1046
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001047 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001048 if (mBackgroundAlphaMultiplier != multiplier) {
1049 mBackgroundAlphaMultiplier = multiplier;
1050 invalidate();
1051 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001052 }
1053
Adam Cohenddb82192010-11-10 16:32:54 -08001054 public float getBackgroundAlphaMultiplier() {
1055 return mBackgroundAlphaMultiplier;
1056 }
1057
Michael Jurka5f1c5092010-09-03 14:15:02 -07001058 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001059 if (mBackgroundAlpha != alpha) {
1060 mBackgroundAlpha = alpha;
1061 invalidate();
1062 }
Michael Jurkadee05892010-07-27 10:01:56 -07001063 }
1064
Michael Jurkaa52570f2012-03-20 03:18:20 -07001065 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001066 final int childCount = getChildCount();
1067 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001068 getChildAt(i).setAlpha(alpha);
1069 }
1070 }
1071
Michael Jurkaa52570f2012-03-20 03:18:20 -07001072 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1073 if (getChildCount() > 0) {
1074 return (ShortcutAndWidgetContainer) getChildAt(0);
1075 }
1076 return null;
1077 }
1078
Patrick Dubroy440c3602010-07-13 17:50:32 -07001079 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001080 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001081 }
1082
Adam Cohen76fc0852011-06-17 13:26:23 -07001083 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001084 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001085 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001086 boolean[][] occupied = mOccupied;
1087 if (!permanent) {
1088 occupied = mTmpOccupied;
1089 }
1090
Adam Cohen19f37922012-03-21 11:59:11 -07001091 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001092 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1093 final ItemInfo info = (ItemInfo) child.getTag();
1094
1095 // We cancel any existing animations
1096 if (mReorderAnimators.containsKey(lp)) {
1097 mReorderAnimators.get(lp).cancel();
1098 mReorderAnimators.remove(lp);
1099 }
1100
Adam Cohen482ed822012-03-02 14:15:13 -08001101 final int oldX = lp.x;
1102 final int oldY = lp.y;
1103 if (adjustOccupied) {
1104 occupied[lp.cellX][lp.cellY] = false;
1105 occupied[cellX][cellY] = true;
1106 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001107 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001108 if (permanent) {
1109 lp.cellX = info.cellX = cellX;
1110 lp.cellY = info.cellY = cellY;
1111 } else {
1112 lp.tmpCellX = cellX;
1113 lp.tmpCellY = cellY;
1114 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001115 clc.setupLp(lp);
1116 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001117 final int newX = lp.x;
1118 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001119
Adam Cohen76fc0852011-06-17 13:26:23 -07001120 lp.x = oldX;
1121 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001122
Adam Cohen482ed822012-03-02 14:15:13 -08001123 // Exit early if we're not actually moving the view
1124 if (oldX == newX && oldY == newY) {
1125 lp.isLockedToGrid = true;
1126 return true;
1127 }
1128
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001129 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001130 va.setDuration(duration);
1131 mReorderAnimators.put(lp, va);
1132
1133 va.addUpdateListener(new AnimatorUpdateListener() {
1134 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001135 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001136 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001137 lp.x = (int) ((1 - r) * oldX + r * newX);
1138 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001139 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001140 }
1141 });
Adam Cohen482ed822012-03-02 14:15:13 -08001142 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001143 boolean cancelled = false;
1144 public void onAnimationEnd(Animator animation) {
1145 // If the animation was cancelled, it means that another animation
1146 // has interrupted this one, and we don't want to lock the item into
1147 // place just yet.
1148 if (!cancelled) {
1149 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001150 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001151 }
1152 if (mReorderAnimators.containsKey(lp)) {
1153 mReorderAnimators.remove(lp);
1154 }
1155 }
1156 public void onAnimationCancel(Animator animation) {
1157 cancelled = true;
1158 }
1159 });
Adam Cohen482ed822012-03-02 14:15:13 -08001160 va.setStartDelay(delay);
1161 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001162 return true;
1163 }
1164 return false;
1165 }
1166
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001167 /**
1168 * Estimate where the top left cell of the dragged item will land if it is dropped.
1169 *
1170 * @param originX The X value of the top left corner of the item
1171 * @param originY The Y value of the top left corner of the item
1172 * @param spanX The number of horizontal cells that the item spans
1173 * @param spanY The number of vertical cells that the item spans
1174 * @param result The estimated drop cell X and Y.
1175 */
1176 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001177 final int countX = mCountX;
1178 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001179
Michael Jurkaa63c4522010-08-19 13:52:27 -07001180 // pointToCellRounded takes the top left of a cell but will pad that with
1181 // cellWidth/2 and cellHeight/2 when finding the matching cell
1182 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001183
1184 // If the item isn't fully on this screen, snap to the edges
1185 int rightOverhang = result[0] + spanX - countX;
1186 if (rightOverhang > 0) {
1187 result[0] -= rightOverhang; // Snap to right
1188 }
1189 result[0] = Math.max(0, result[0]); // Snap to left
1190 int bottomOverhang = result[1] + spanY - countY;
1191 if (bottomOverhang > 0) {
1192 result[1] -= bottomOverhang; // Snap to bottom
1193 }
1194 result[1] = Math.max(0, result[1]); // Snap to top
1195 }
1196
Adam Cohen482ed822012-03-02 14:15:13 -08001197 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1198 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001199 final int oldDragCellX = mDragCell[0];
1200 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001201
Winson Chungb8c69f32011-10-19 21:36:08 -07001202 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001203 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1204 } else {
1205 mDragCenter.set(originX, originY);
1206 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001207
Adam Cohen2801caf2011-05-13 20:57:39 -07001208 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001209 return;
1210 }
1211
Adam Cohen482ed822012-03-02 14:15:13 -08001212 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1213 mDragCell[0] = cellX;
1214 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001215 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001216 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001217 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001218
Joe Onorato4be866d2010-10-10 11:26:02 -07001219 int left = topLeft[0];
1220 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001221
Winson Chung892c74d2013-08-22 16:15:50 -07001222 // Offset icons by their padding
1223 if (v instanceof BubbleTextView) {
1224 top += v.getPaddingTop();
1225 }
1226
Winson Chungb8c69f32011-10-19 21:36:08 -07001227 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001228 // When drawing the drag outline, it did not account for margin offsets
1229 // added by the view's parent.
1230 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1231 left += lp.leftMargin;
1232 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001233
Adam Cohen99e8b402011-03-25 19:23:43 -07001234 // Offsets due to the size difference between the View and the dragOutline.
1235 // There is a size difference to account for the outer blur, which may lie
1236 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001237 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001238 // We center about the x axis
1239 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1240 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001241 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001242 if (dragOffset != null && dragRegion != null) {
1243 // Center the drag region *horizontally* in the cell and apply a drag
1244 // outline offset
1245 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1246 - dragRegion.width()) / 2;
1247 top += dragOffset.y;
1248 } else {
1249 // Center the drag outline in the cell
1250 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1251 - dragOutline.getWidth()) / 2;
1252 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1253 - dragOutline.getHeight()) / 2;
1254 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001255 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001256 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001257 mDragOutlineAnims[oldIndex].animateOut();
1258 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001259 Rect r = mDragOutlines[mDragOutlineCurrent];
1260 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1261 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001262 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001263 }
Winson Chung150fbab2010-09-29 17:14:26 -07001264
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001265 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1266 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001267 }
1268 }
1269
Adam Cohene0310962011-04-18 16:15:31 -07001270 public void clearDragOutlines() {
1271 final int oldIndex = mDragOutlineCurrent;
1272 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001273 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001274 }
1275
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001277 * Find a vacant area that will fit the given bounds nearest the requested
1278 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001279 *
Romain Guy51afc022009-05-04 18:03:43 -07001280 * @param pixelX The X location at which you want to search for a vacant area.
1281 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001282 * @param spanX Horizontal span of the object.
1283 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001284 * @param result Array in which to place the result, or null (in which case a new array will
1285 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001286 * @return The X, Y cell of a vacant area that can contain this object,
1287 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001289 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1290 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001291 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001292 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001293
Michael Jurka6a1435d2010-09-27 17:35:12 -07001294 /**
1295 * Find a vacant area that will fit the given bounds nearest the requested
1296 * cell location. Uses Euclidean distance to score multiple vacant areas.
1297 *
1298 * @param pixelX The X location at which you want to search for a vacant area.
1299 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001300 * @param minSpanX The minimum horizontal span required
1301 * @param minSpanY The minimum vertical span required
1302 * @param spanX Horizontal span of the object.
1303 * @param spanY Vertical span of the object.
1304 * @param result Array in which to place the result, or null (in which case a new array will
1305 * be allocated)
1306 * @return The X, Y cell of a vacant area that can contain this object,
1307 * nearest the requested location.
1308 */
1309 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1310 int spanY, int[] result, int[] resultSpan) {
1311 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1312 result, resultSpan);
1313 }
1314
1315 /**
1316 * Find a vacant area that will fit the given bounds nearest the requested
1317 * cell location. Uses Euclidean distance to score multiple vacant areas.
1318 *
1319 * @param pixelX The X location at which you want to search for a vacant area.
1320 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001321 * @param spanX Horizontal span of the object.
1322 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001323 * @param ignoreOccupied If true, the result can be an occupied cell
1324 * @param result Array in which to place the result, or null (in which case a new array will
1325 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001326 * @return The X, Y cell of a vacant area that can contain this object,
1327 * nearest the requested location.
1328 */
Adam Cohendf035382011-04-11 17:22:04 -07001329 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1330 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001331 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001332 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001333 }
1334
1335 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1336 private void lazyInitTempRectStack() {
1337 if (mTempRectStack.isEmpty()) {
1338 for (int i = 0; i < mCountX * mCountY; i++) {
1339 mTempRectStack.push(new Rect());
1340 }
1341 }
1342 }
Adam Cohen482ed822012-03-02 14:15:13 -08001343
Adam Cohend41fbf52012-02-16 23:53:59 -08001344 private void recycleTempRects(Stack<Rect> used) {
1345 while (!used.isEmpty()) {
1346 mTempRectStack.push(used.pop());
1347 }
1348 }
1349
1350 /**
1351 * Find a vacant area that will fit the given bounds nearest the requested
1352 * cell location. Uses Euclidean distance to score multiple vacant areas.
1353 *
1354 * @param pixelX The X location at which you want to search for a vacant area.
1355 * @param pixelY The Y location at which you want to search for a vacant area.
1356 * @param minSpanX The minimum horizontal span required
1357 * @param minSpanY The minimum vertical span required
1358 * @param spanX Horizontal span of the object.
1359 * @param spanY Vertical span of the object.
1360 * @param ignoreOccupied If true, the result can be an occupied cell
1361 * @param result Array in which to place the result, or null (in which case a new array will
1362 * be allocated)
1363 * @return The X, Y cell of a vacant area that can contain this object,
1364 * nearest the requested location.
1365 */
1366 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001367 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1368 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001369 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001370 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001371 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001372
Adam Cohene3e27a82011-04-15 12:07:39 -07001373 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1374 // to the center of the item, but we are searching based on the top-left cell, so
1375 // we translate the point over to correspond to the top-left.
1376 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1377 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1378
Jeff Sharkey70864282009-04-07 21:08:40 -07001379 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001380 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001381 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001382 final Rect bestRect = new Rect(-1, -1, -1, -1);
1383 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001384
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001385 final int countX = mCountX;
1386 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001387
Adam Cohend41fbf52012-02-16 23:53:59 -08001388 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1389 spanX < minSpanX || spanY < minSpanY) {
1390 return bestXY;
1391 }
1392
1393 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001394 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001395 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1396 int ySize = -1;
1397 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001398 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001399 // First, let's see if this thing fits anywhere
1400 for (int i = 0; i < minSpanX; i++) {
1401 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001402 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001403 continue inner;
1404 }
Michael Jurkac28de512010-08-13 11:27:44 -07001405 }
1406 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001407 xSize = minSpanX;
1408 ySize = minSpanY;
1409
1410 // We know that the item will fit at _some_ acceptable size, now let's see
1411 // how big we can make it. We'll alternate between incrementing x and y spans
1412 // until we hit a limit.
1413 boolean incX = true;
1414 boolean hitMaxX = xSize >= spanX;
1415 boolean hitMaxY = ySize >= spanY;
1416 while (!(hitMaxX && hitMaxY)) {
1417 if (incX && !hitMaxX) {
1418 for (int j = 0; j < ySize; j++) {
1419 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1420 // We can't move out horizontally
1421 hitMaxX = true;
1422 }
1423 }
1424 if (!hitMaxX) {
1425 xSize++;
1426 }
1427 } else if (!hitMaxY) {
1428 for (int i = 0; i < xSize; i++) {
1429 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1430 // We can't move out vertically
1431 hitMaxY = true;
1432 }
1433 }
1434 if (!hitMaxY) {
1435 ySize++;
1436 }
1437 }
1438 hitMaxX |= xSize >= spanX;
1439 hitMaxY |= ySize >= spanY;
1440 incX = !incX;
1441 }
1442 incX = true;
1443 hitMaxX = xSize >= spanX;
1444 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001445 }
Winson Chung0be025d2011-05-23 17:45:09 -07001446 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001447 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448
Adam Cohend41fbf52012-02-16 23:53:59 -08001449 // We verify that the current rect is not a sub-rect of any of our previous
1450 // candidates. In this case, the current rect is disqualified in favour of the
1451 // containing rect.
1452 Rect currentRect = mTempRectStack.pop();
1453 currentRect.set(x, y, x + xSize, y + ySize);
1454 boolean contained = false;
1455 for (Rect r : validRegions) {
1456 if (r.contains(currentRect)) {
1457 contained = true;
1458 break;
1459 }
1460 }
1461 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001462 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1463 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001464
Adam Cohend41fbf52012-02-16 23:53:59 -08001465 if ((distance <= bestDistance && !contained) ||
1466 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001467 bestDistance = distance;
1468 bestXY[0] = x;
1469 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001470 if (resultSpan != null) {
1471 resultSpan[0] = xSize;
1472 resultSpan[1] = ySize;
1473 }
1474 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001475 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001478 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001479 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480
Adam Cohenc0dcf592011-06-01 15:30:43 -07001481 // Return -1, -1 if no suitable location found
1482 if (bestDistance == Double.MAX_VALUE) {
1483 bestXY[0] = -1;
1484 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001485 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001486 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001487 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001489
Adam Cohen482ed822012-03-02 14:15:13 -08001490 /**
1491 * Find a vacant area that will fit the given bounds nearest the requested
1492 * cell location, and will also weigh in a suggested direction vector of the
1493 * desired location. This method computers distance based on unit grid distances,
1494 * not pixel distances.
1495 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001496 * @param cellX The X cell nearest to which you want to search for a vacant area.
1497 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001498 * @param spanX Horizontal span of the object.
1499 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001500 * @param direction The favored direction in which the views should move from x, y
1501 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1502 * matches exactly. Otherwise we find the best matching direction.
1503 * @param occoupied The array which represents which cells in the CellLayout are occupied
1504 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001505 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001506 * @param result Array in which to place the result, or null (in which case a new array will
1507 * be allocated)
1508 * @return The X, Y cell of a vacant area that can contain this object,
1509 * nearest the requested location.
1510 */
1511 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001512 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001513 // Keep track of best-scoring drop area
1514 final int[] bestXY = result != null ? result : new int[2];
1515 float bestDistance = Float.MAX_VALUE;
1516 int bestDirectionScore = Integer.MIN_VALUE;
1517
1518 final int countX = mCountX;
1519 final int countY = mCountY;
1520
1521 for (int y = 0; y < countY - (spanY - 1); y++) {
1522 inner:
1523 for (int x = 0; x < countX - (spanX - 1); x++) {
1524 // First, let's see if this thing fits anywhere
1525 for (int i = 0; i < spanX; i++) {
1526 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001527 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001528 continue inner;
1529 }
1530 }
1531 }
1532
1533 float distance = (float)
1534 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1535 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001536 computeDirectionVector(x - cellX, y - cellY, curDirection);
1537 // The direction score is just the dot product of the two candidate direction
1538 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001539 int curDirectionScore = direction[0] * curDirection[0] +
1540 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001541 boolean exactDirectionOnly = false;
1542 boolean directionMatches = direction[0] == curDirection[0] &&
1543 direction[0] == curDirection[0];
1544 if ((directionMatches || !exactDirectionOnly) &&
1545 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001546 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1547 bestDistance = distance;
1548 bestDirectionScore = curDirectionScore;
1549 bestXY[0] = x;
1550 bestXY[1] = y;
1551 }
1552 }
1553 }
1554
1555 // Return -1, -1 if no suitable location found
1556 if (bestDistance == Float.MAX_VALUE) {
1557 bestXY[0] = -1;
1558 bestXY[1] = -1;
1559 }
1560 return bestXY;
1561 }
1562
1563 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001564 int[] direction, ItemConfiguration currentState) {
1565 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001566 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001567 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001568 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1569
Adam Cohen8baab352012-03-20 17:39:21 -07001570 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001571
1572 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001573 c.x = mTempLocation[0];
1574 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001575 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001576 }
Adam Cohen8baab352012-03-20 17:39:21 -07001577 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001578 return success;
1579 }
1580
Adam Cohenf3900c22012-11-16 18:28:11 -08001581 /**
1582 * This helper class defines a cluster of views. It helps with defining complex edges
1583 * of the cluster and determining how those edges interact with other views. The edges
1584 * essentially define a fine-grained boundary around the cluster of views -- like a more
1585 * precise version of a bounding box.
1586 */
1587 private class ViewCluster {
1588 final static int LEFT = 0;
1589 final static int TOP = 1;
1590 final static int RIGHT = 2;
1591 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001592
Adam Cohenf3900c22012-11-16 18:28:11 -08001593 ArrayList<View> views;
1594 ItemConfiguration config;
1595 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001596
Adam Cohenf3900c22012-11-16 18:28:11 -08001597 int[] leftEdge = new int[mCountY];
1598 int[] rightEdge = new int[mCountY];
1599 int[] topEdge = new int[mCountX];
1600 int[] bottomEdge = new int[mCountX];
1601 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1602
1603 @SuppressWarnings("unchecked")
1604 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1605 this.views = (ArrayList<View>) views.clone();
1606 this.config = config;
1607 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001608 }
1609
Adam Cohenf3900c22012-11-16 18:28:11 -08001610 void resetEdges() {
1611 for (int i = 0; i < mCountX; i++) {
1612 topEdge[i] = -1;
1613 bottomEdge[i] = -1;
1614 }
1615 for (int i = 0; i < mCountY; i++) {
1616 leftEdge[i] = -1;
1617 rightEdge[i] = -1;
1618 }
1619 leftEdgeDirty = true;
1620 rightEdgeDirty = true;
1621 bottomEdgeDirty = true;
1622 topEdgeDirty = true;
1623 boundingRectDirty = true;
1624 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001625
Adam Cohenf3900c22012-11-16 18:28:11 -08001626 void computeEdge(int which, int[] edge) {
1627 int count = views.size();
1628 for (int i = 0; i < count; i++) {
1629 CellAndSpan cs = config.map.get(views.get(i));
1630 switch (which) {
1631 case LEFT:
1632 int left = cs.x;
1633 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1634 if (left < edge[j] || edge[j] < 0) {
1635 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001636 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001637 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001638 break;
1639 case RIGHT:
1640 int right = cs.x + cs.spanX;
1641 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1642 if (right > edge[j]) {
1643 edge[j] = right;
1644 }
1645 }
1646 break;
1647 case TOP:
1648 int top = cs.y;
1649 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1650 if (top < edge[j] || edge[j] < 0) {
1651 edge[j] = top;
1652 }
1653 }
1654 break;
1655 case BOTTOM:
1656 int bottom = cs.y + cs.spanY;
1657 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1658 if (bottom > edge[j]) {
1659 edge[j] = bottom;
1660 }
1661 }
1662 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001663 }
1664 }
1665 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001666
1667 boolean isViewTouchingEdge(View v, int whichEdge) {
1668 CellAndSpan cs = config.map.get(v);
1669
1670 int[] edge = getEdge(whichEdge);
1671
1672 switch (whichEdge) {
1673 case LEFT:
1674 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1675 if (edge[i] == cs.x + cs.spanX) {
1676 return true;
1677 }
1678 }
1679 break;
1680 case RIGHT:
1681 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1682 if (edge[i] == cs.x) {
1683 return true;
1684 }
1685 }
1686 break;
1687 case TOP:
1688 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1689 if (edge[i] == cs.y + cs.spanY) {
1690 return true;
1691 }
1692 }
1693 break;
1694 case BOTTOM:
1695 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1696 if (edge[i] == cs.y) {
1697 return true;
1698 }
1699 }
1700 break;
1701 }
1702 return false;
1703 }
1704
1705 void shift(int whichEdge, int delta) {
1706 for (View v: views) {
1707 CellAndSpan c = config.map.get(v);
1708 switch (whichEdge) {
1709 case LEFT:
1710 c.x -= delta;
1711 break;
1712 case RIGHT:
1713 c.x += delta;
1714 break;
1715 case TOP:
1716 c.y -= delta;
1717 break;
1718 case BOTTOM:
1719 default:
1720 c.y += delta;
1721 break;
1722 }
1723 }
1724 resetEdges();
1725 }
1726
1727 public void addView(View v) {
1728 views.add(v);
1729 resetEdges();
1730 }
1731
1732 public Rect getBoundingRect() {
1733 if (boundingRectDirty) {
1734 boolean first = true;
1735 for (View v: views) {
1736 CellAndSpan c = config.map.get(v);
1737 if (first) {
1738 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1739 first = false;
1740 } else {
1741 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1742 }
1743 }
1744 }
1745 return boundingRect;
1746 }
1747
1748 public int[] getEdge(int which) {
1749 switch (which) {
1750 case LEFT:
1751 return getLeftEdge();
1752 case RIGHT:
1753 return getRightEdge();
1754 case TOP:
1755 return getTopEdge();
1756 case BOTTOM:
1757 default:
1758 return getBottomEdge();
1759 }
1760 }
1761
1762 public int[] getLeftEdge() {
1763 if (leftEdgeDirty) {
1764 computeEdge(LEFT, leftEdge);
1765 }
1766 return leftEdge;
1767 }
1768
1769 public int[] getRightEdge() {
1770 if (rightEdgeDirty) {
1771 computeEdge(RIGHT, rightEdge);
1772 }
1773 return rightEdge;
1774 }
1775
1776 public int[] getTopEdge() {
1777 if (topEdgeDirty) {
1778 computeEdge(TOP, topEdge);
1779 }
1780 return topEdge;
1781 }
1782
1783 public int[] getBottomEdge() {
1784 if (bottomEdgeDirty) {
1785 computeEdge(BOTTOM, bottomEdge);
1786 }
1787 return bottomEdge;
1788 }
1789
1790 PositionComparator comparator = new PositionComparator();
1791 class PositionComparator implements Comparator<View> {
1792 int whichEdge = 0;
1793 public int compare(View left, View right) {
1794 CellAndSpan l = config.map.get(left);
1795 CellAndSpan r = config.map.get(right);
1796 switch (whichEdge) {
1797 case LEFT:
1798 return (r.x + r.spanX) - (l.x + l.spanX);
1799 case RIGHT:
1800 return l.x - r.x;
1801 case TOP:
1802 return (r.y + r.spanY) - (l.y + l.spanY);
1803 case BOTTOM:
1804 default:
1805 return l.y - r.y;
1806 }
1807 }
1808 }
1809
1810 public void sortConfigurationForEdgePush(int edge) {
1811 comparator.whichEdge = edge;
1812 Collections.sort(config.sortedViews, comparator);
1813 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001814 }
1815
Adam Cohenf3900c22012-11-16 18:28:11 -08001816 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1817 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001818
Adam Cohenf3900c22012-11-16 18:28:11 -08001819 ViewCluster cluster = new ViewCluster(views, currentState);
1820 Rect clusterRect = cluster.getBoundingRect();
1821 int whichEdge;
1822 int pushDistance;
1823 boolean fail = false;
1824
1825 // Determine the edge of the cluster that will be leading the push and how far
1826 // the cluster must be shifted.
1827 if (direction[0] < 0) {
1828 whichEdge = ViewCluster.LEFT;
1829 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001830 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001831 whichEdge = ViewCluster.RIGHT;
1832 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1833 } else if (direction[1] < 0) {
1834 whichEdge = ViewCluster.TOP;
1835 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1836 } else {
1837 whichEdge = ViewCluster.BOTTOM;
1838 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001839 }
1840
Adam Cohenf3900c22012-11-16 18:28:11 -08001841 // Break early for invalid push distance.
1842 if (pushDistance <= 0) {
1843 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001844 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001845
1846 // Mark the occupied state as false for the group of views we want to move.
1847 for (View v: views) {
1848 CellAndSpan c = currentState.map.get(v);
1849 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1850 }
1851
1852 // We save the current configuration -- if we fail to find a solution we will revert
1853 // to the initial state. The process of finding a solution modifies the configuration
1854 // in place, hence the need for revert in the failure case.
1855 currentState.save();
1856
1857 // The pushing algorithm is simplified by considering the views in the order in which
1858 // they would be pushed by the cluster. For example, if the cluster is leading with its
1859 // left edge, we consider sort the views by their right edge, from right to left.
1860 cluster.sortConfigurationForEdgePush(whichEdge);
1861
1862 while (pushDistance > 0 && !fail) {
1863 for (View v: currentState.sortedViews) {
1864 // For each view that isn't in the cluster, we see if the leading edge of the
1865 // cluster is contacting the edge of that view. If so, we add that view to the
1866 // cluster.
1867 if (!cluster.views.contains(v) && v != dragView) {
1868 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1869 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1870 if (!lp.canReorder) {
1871 // The push solution includes the all apps button, this is not viable.
1872 fail = true;
1873 break;
1874 }
1875 cluster.addView(v);
1876 CellAndSpan c = currentState.map.get(v);
1877
1878 // Adding view to cluster, mark it as not occupied.
1879 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1880 }
1881 }
1882 }
1883 pushDistance--;
1884
1885 // The cluster has been completed, now we move the whole thing over in the appropriate
1886 // direction.
1887 cluster.shift(whichEdge, 1);
1888 }
1889
1890 boolean foundSolution = false;
1891 clusterRect = cluster.getBoundingRect();
1892
1893 // Due to the nature of the algorithm, the only check required to verify a valid solution
1894 // is to ensure that completed shifted cluster lies completely within the cell layout.
1895 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1896 clusterRect.bottom <= mCountY) {
1897 foundSolution = true;
1898 } else {
1899 currentState.restore();
1900 }
1901
1902 // In either case, we set the occupied array as marked for the location of the views
1903 for (View v: cluster.views) {
1904 CellAndSpan c = currentState.map.get(v);
1905 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1906 }
1907
1908 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001909 }
1910
Adam Cohen482ed822012-03-02 14:15:13 -08001911 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001912 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001913 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001914
Adam Cohen8baab352012-03-20 17:39:21 -07001915 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001916 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001917 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001918 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001919 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001920 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001921 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001922 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001923 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001924 }
1925 }
Adam Cohen8baab352012-03-20 17:39:21 -07001926
Adam Cohen8baab352012-03-20 17:39:21 -07001927 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001928 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001929 CellAndSpan c = currentState.map.get(v);
1930 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1931 }
1932
Adam Cohen47a876d2012-03-19 13:21:41 -07001933 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1934 int top = boundingRect.top;
1935 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001936 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001937 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001938 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001939 CellAndSpan c = currentState.map.get(v);
1940 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001941 }
1942
Adam Cohen482ed822012-03-02 14:15:13 -08001943 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1944
Adam Cohenf3900c22012-11-16 18:28:11 -08001945 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1946 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001947
Adam Cohen8baab352012-03-20 17:39:21 -07001948 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001949 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001950 int deltaX = mTempLocation[0] - boundingRect.left;
1951 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001952 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001953 CellAndSpan c = currentState.map.get(v);
1954 c.x += deltaX;
1955 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001956 }
1957 success = true;
1958 }
Adam Cohen8baab352012-03-20 17:39:21 -07001959
1960 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001961 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001962 CellAndSpan c = currentState.map.get(v);
1963 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001964 }
1965 return success;
1966 }
1967
1968 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1969 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1970 }
1971
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001972 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1973 // to push items in each of the cardinal directions, in an order based on the direction vector
1974 // passed.
1975 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1976 int[] direction, View ignoreView, ItemConfiguration solution) {
1977 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001978 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001979 // separately in each of the components.
1980 int temp = direction[1];
1981 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001982
1983 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001984 ignoreView, solution)) {
1985 return true;
1986 }
1987 direction[1] = temp;
1988 temp = direction[0];
1989 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001990
1991 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001992 ignoreView, solution)) {
1993 return true;
1994 }
1995 // Revert the direction
1996 direction[0] = temp;
1997
1998 // Now we try pushing in each component of the opposite direction
1999 direction[0] *= -1;
2000 direction[1] *= -1;
2001 temp = direction[1];
2002 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002003 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002004 ignoreView, solution)) {
2005 return true;
2006 }
2007
2008 direction[1] = temp;
2009 temp = direction[0];
2010 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002011 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002012 ignoreView, solution)) {
2013 return true;
2014 }
2015 // revert the direction
2016 direction[0] = temp;
2017 direction[0] *= -1;
2018 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002019
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002020 } else {
2021 // If the direction vector has a single non-zero component, we push first in the
2022 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002023 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002024 ignoreView, solution)) {
2025 return true;
2026 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002027 // Then we try the opposite direction
2028 direction[0] *= -1;
2029 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002030 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002031 ignoreView, solution)) {
2032 return true;
2033 }
2034 // Switch the direction back
2035 direction[0] *= -1;
2036 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002037
2038 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002039 // to find a solution by pushing along the perpendicular axis.
2040
2041 // Swap the components
2042 int temp = direction[1];
2043 direction[1] = direction[0];
2044 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002045 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002046 ignoreView, solution)) {
2047 return true;
2048 }
2049
2050 // Then we try the opposite direction
2051 direction[0] *= -1;
2052 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002053 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002054 ignoreView, solution)) {
2055 return true;
2056 }
2057 // Switch the direction back
2058 direction[0] *= -1;
2059 direction[1] *= -1;
2060
2061 // Swap the components back
2062 temp = direction[1];
2063 direction[1] = direction[0];
2064 direction[0] = temp;
2065 }
2066 return false;
2067 }
2068
Adam Cohen482ed822012-03-02 14:15:13 -08002069 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002070 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002071 // Return early if get invalid cell positions
2072 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002073
Adam Cohen8baab352012-03-20 17:39:21 -07002074 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002075 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002076
Adam Cohen8baab352012-03-20 17:39:21 -07002077 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002078 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002079 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002080 if (c != null) {
2081 c.x = cellX;
2082 c.y = cellY;
2083 }
Adam Cohen482ed822012-03-02 14:15:13 -08002084 }
Adam Cohen482ed822012-03-02 14:15:13 -08002085 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2086 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002087 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002088 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002089 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002090 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002091 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002092 if (Rect.intersects(r0, r1)) {
2093 if (!lp.canReorder) {
2094 return false;
2095 }
2096 mIntersectingViews.add(child);
2097 }
2098 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002099
Winson Chung5f8afe62013-08-12 16:19:28 -07002100 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002101 // we try to find a solution such that no displaced item travels through another item
2102 // without also displacing that item.
2103 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002104 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002105 return true;
2106 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002107
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002108 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002109 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002110 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002111 return true;
2112 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002113
Adam Cohen482ed822012-03-02 14:15:13 -08002114 // Ok, they couldn't move as a block, let's move them individually
2115 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002116 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002117 return false;
2118 }
2119 }
2120 return true;
2121 }
2122
2123 /*
2124 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2125 * the provided point and the provided cell
2126 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002127 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002128 double angle = Math.atan(((float) deltaY) / deltaX);
2129
2130 result[0] = 0;
2131 result[1] = 0;
2132 if (Math.abs(Math.cos(angle)) > 0.5f) {
2133 result[0] = (int) Math.signum(deltaX);
2134 }
2135 if (Math.abs(Math.sin(angle)) > 0.5f) {
2136 result[1] = (int) Math.signum(deltaY);
2137 }
2138 }
2139
Adam Cohen8baab352012-03-20 17:39:21 -07002140 private void copyOccupiedArray(boolean[][] occupied) {
2141 for (int i = 0; i < mCountX; i++) {
2142 for (int j = 0; j < mCountY; j++) {
2143 occupied[i][j] = mOccupied[i][j];
2144 }
2145 }
2146 }
2147
Adam Cohen482ed822012-03-02 14:15:13 -08002148 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
2149 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002150 // Copy the current state into the solution. This solution will be manipulated as necessary.
2151 copyCurrentStateToSolution(solution, false);
2152 // Copy the current occupied array into the temporary occupied array. This array will be
2153 // manipulated as necessary to find a solution.
2154 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002155
2156 // We find the nearest cell into which we would place the dragged item, assuming there's
2157 // nothing in its way.
2158 int result[] = new int[2];
2159 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2160
2161 boolean success = false;
2162 // First we try the exact nearest position of the item being dragged,
2163 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002164 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2165 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002166
2167 if (!success) {
2168 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2169 // x, then 1 in y etc.
2170 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
2171 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
2172 dragView, false, solution);
2173 } else if (spanY > minSpanY) {
2174 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
2175 dragView, true, solution);
2176 }
2177 solution.isSolution = false;
2178 } else {
2179 solution.isSolution = true;
2180 solution.dragViewX = result[0];
2181 solution.dragViewY = result[1];
2182 solution.dragViewSpanX = spanX;
2183 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002184 }
2185 return solution;
2186 }
2187
2188 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002189 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002190 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002191 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002192 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002193 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002194 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002195 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002196 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002197 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002198 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002199 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002200 }
2201 }
2202
2203 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2204 for (int i = 0; i < mCountX; i++) {
2205 for (int j = 0; j < mCountY; j++) {
2206 mTmpOccupied[i][j] = false;
2207 }
2208 }
2209
Michael Jurkaa52570f2012-03-20 03:18:20 -07002210 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002211 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002212 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002213 if (child == dragView) continue;
2214 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002215 CellAndSpan c = solution.map.get(child);
2216 if (c != null) {
2217 lp.tmpCellX = c.x;
2218 lp.tmpCellY = c.y;
2219 lp.cellHSpan = c.spanX;
2220 lp.cellVSpan = c.spanY;
2221 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002222 }
2223 }
2224 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2225 solution.dragViewSpanY, mTmpOccupied, true);
2226 }
2227
2228 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2229 commitDragView) {
2230
2231 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2232 for (int i = 0; i < mCountX; i++) {
2233 for (int j = 0; j < mCountY; j++) {
2234 occupied[i][j] = false;
2235 }
2236 }
2237
Michael Jurkaa52570f2012-03-20 03:18:20 -07002238 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002239 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002240 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002241 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002242 CellAndSpan c = solution.map.get(child);
2243 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002244 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2245 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002246 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002247 }
2248 }
2249 if (commitDragView) {
2250 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2251 solution.dragViewSpanY, occupied, true);
2252 }
2253 }
2254
Adam Cohen19f37922012-03-21 11:59:11 -07002255 // This method starts or changes the reorder hint animations
2256 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2257 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002258 for (int i = 0; i < childCount; i++) {
2259 View child = mShortcutsAndWidgets.getChildAt(i);
2260 if (child == dragView) continue;
2261 CellAndSpan c = solution.map.get(child);
2262 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2263 if (c != null) {
2264 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2265 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002266 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002267 }
2268 }
2269 }
2270
2271 // Class which represents the reorder hint animations. These animations show that an item is
2272 // in a temporary state, and hint at where the item will return to.
2273 class ReorderHintAnimation {
2274 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002275 float finalDeltaX;
2276 float finalDeltaY;
2277 float initDeltaX;
2278 float initDeltaY;
2279 float finalScale;
2280 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002281 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002282 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002283
2284 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2285 int spanX, int spanY) {
2286 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2287 final int x0 = mTmpPoint[0];
2288 final int y0 = mTmpPoint[1];
2289 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2290 final int x1 = mTmpPoint[0];
2291 final int y1 = mTmpPoint[1];
2292 final int dX = x1 - x0;
2293 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002294 finalDeltaX = 0;
2295 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002296 if (dX == dY && dX == 0) {
2297 } else {
2298 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002299 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002300 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002301 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002302 } else {
2303 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002304 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002305 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002306 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002307 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002308 }
2309 }
Adam Cohend024f982012-05-23 18:26:45 -07002310 initDeltaX = child.getTranslationX();
2311 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002312 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002313 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002314 this.child = child;
2315 }
2316
Adam Cohend024f982012-05-23 18:26:45 -07002317 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002318 if (mShakeAnimators.containsKey(child)) {
2319 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002320 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002321 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002322 if (finalDeltaX == 0 && finalDeltaY == 0) {
2323 completeAnimationImmediately();
2324 return;
2325 }
Adam Cohen19f37922012-03-21 11:59:11 -07002326 }
Adam Cohend024f982012-05-23 18:26:45 -07002327 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002328 return;
2329 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002330 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002331 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002332 va.setRepeatMode(ValueAnimator.REVERSE);
2333 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002334 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002335 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002336 va.addUpdateListener(new AnimatorUpdateListener() {
2337 @Override
2338 public void onAnimationUpdate(ValueAnimator animation) {
2339 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002340 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2341 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002342 child.setTranslationX(x);
2343 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002344 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002345 child.setScaleX(s);
2346 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002347 }
2348 });
2349 va.addListener(new AnimatorListenerAdapter() {
2350 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002351 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002352 initDeltaX = 0;
2353 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002354 initScale = getChildrenScale();
Adam Cohen19f37922012-03-21 11:59:11 -07002355 }
2356 });
Adam Cohen19f37922012-03-21 11:59:11 -07002357 mShakeAnimators.put(child, this);
2358 va.start();
2359 }
2360
Adam Cohend024f982012-05-23 18:26:45 -07002361 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002362 if (a != null) {
2363 a.cancel();
2364 }
Adam Cohen19f37922012-03-21 11:59:11 -07002365 }
Adam Cohene7587d22012-05-24 18:50:02 -07002366
Brandon Keely50e6e562012-05-08 16:28:49 -07002367 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002368 if (a != null) {
2369 a.cancel();
2370 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002371
Michael Jurka2ecf9952012-06-18 12:52:28 -07002372 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002373 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002374 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002375 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2376 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002377 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2378 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002379 );
2380 s.setDuration(REORDER_ANIMATION_DURATION);
2381 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2382 s.start();
2383 }
Adam Cohen19f37922012-03-21 11:59:11 -07002384 }
2385
2386 private void completeAndClearReorderHintAnimations() {
2387 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002388 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002389 }
2390 mShakeAnimators.clear();
2391 }
2392
Adam Cohen482ed822012-03-02 14:15:13 -08002393 private void commitTempPlacement() {
2394 for (int i = 0; i < mCountX; i++) {
2395 for (int j = 0; j < mCountY; j++) {
2396 mOccupied[i][j] = mTmpOccupied[i][j];
2397 }
2398 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002399 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002400 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002401 View child = mShortcutsAndWidgets.getChildAt(i);
2402 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2403 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002404 // We do a null check here because the item info can be null in the case of the
2405 // AllApps button in the hotseat.
2406 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002407 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2408 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2409 info.requiresDbUpdate = true;
2410 }
Adam Cohen2acce882012-03-28 19:03:19 -07002411 info.cellX = lp.cellX = lp.tmpCellX;
2412 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002413 info.spanX = lp.cellHSpan;
2414 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002415 }
Adam Cohen482ed822012-03-02 14:15:13 -08002416 }
Adam Cohen2acce882012-03-28 19:03:19 -07002417 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002418 }
2419
2420 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002421 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002422 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002423 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002424 lp.useTmpCoords = useTempCoords;
2425 }
2426 }
2427
Adam Cohen482ed822012-03-02 14:15:13 -08002428 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2429 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2430 int[] result = new int[2];
2431 int[] resultSpan = new int[2];
2432 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2433 resultSpan);
2434 if (result[0] >= 0 && result[1] >= 0) {
2435 copyCurrentStateToSolution(solution, false);
2436 solution.dragViewX = result[0];
2437 solution.dragViewY = result[1];
2438 solution.dragViewSpanX = resultSpan[0];
2439 solution.dragViewSpanY = resultSpan[1];
2440 solution.isSolution = true;
2441 } else {
2442 solution.isSolution = false;
2443 }
2444 return solution;
2445 }
2446
2447 public void prepareChildForDrag(View child) {
2448 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002449 }
2450
Adam Cohen19f37922012-03-21 11:59:11 -07002451 /* This seems like it should be obvious and straight-forward, but when the direction vector
2452 needs to match with the notion of the dragView pushing other views, we have to employ
2453 a slightly more subtle notion of the direction vector. The question is what two points is
2454 the vector between? The center of the dragView and its desired destination? Not quite, as
2455 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2456 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2457 or right, which helps make pushing feel right.
2458 */
2459 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2460 int spanY, View dragView, int[] resultDirection) {
2461 int[] targetDestination = new int[2];
2462
2463 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2464 Rect dragRect = new Rect();
2465 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2466 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2467
2468 Rect dropRegionRect = new Rect();
2469 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2470 dragView, dropRegionRect, mIntersectingViews);
2471
2472 int dropRegionSpanX = dropRegionRect.width();
2473 int dropRegionSpanY = dropRegionRect.height();
2474
2475 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2476 dropRegionRect.height(), dropRegionRect);
2477
2478 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2479 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2480
2481 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2482 deltaX = 0;
2483 }
2484 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2485 deltaY = 0;
2486 }
2487
2488 if (deltaX == 0 && deltaY == 0) {
2489 // No idea what to do, give a random direction.
2490 resultDirection[0] = 1;
2491 resultDirection[1] = 0;
2492 } else {
2493 computeDirectionVector(deltaX, deltaY, resultDirection);
2494 }
2495 }
2496
2497 // For a given cell and span, fetch the set of views intersecting the region.
2498 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2499 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2500 if (boundingRect != null) {
2501 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2502 }
2503 intersectingViews.clear();
2504 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2505 Rect r1 = new Rect();
2506 final int count = mShortcutsAndWidgets.getChildCount();
2507 for (int i = 0; i < count; i++) {
2508 View child = mShortcutsAndWidgets.getChildAt(i);
2509 if (child == dragView) continue;
2510 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2511 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2512 if (Rect.intersects(r0, r1)) {
2513 mIntersectingViews.add(child);
2514 if (boundingRect != null) {
2515 boundingRect.union(r1);
2516 }
2517 }
2518 }
2519 }
2520
2521 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2522 View dragView, int[] result) {
2523 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2524 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2525 mIntersectingViews);
2526 return !mIntersectingViews.isEmpty();
2527 }
2528
2529 void revertTempState() {
2530 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2531 final int count = mShortcutsAndWidgets.getChildCount();
2532 for (int i = 0; i < count; i++) {
2533 View child = mShortcutsAndWidgets.getChildAt(i);
2534 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2535 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2536 lp.tmpCellX = lp.cellX;
2537 lp.tmpCellY = lp.cellY;
2538 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2539 0, false, false);
2540 }
2541 }
2542 completeAndClearReorderHintAnimations();
2543 setItemPlacementDirty(false);
2544 }
2545
Adam Cohenbebf0422012-04-11 18:06:28 -07002546 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2547 View dragView, int[] direction, boolean commit) {
2548 int[] pixelXY = new int[2];
2549 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2550
2551 // First we determine if things have moved enough to cause a different layout
2552 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2553 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2554
2555 setUseTempCoords(true);
2556 if (swapSolution != null && swapSolution.isSolution) {
2557 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2558 // committing anything or animating anything as we just want to determine if a solution
2559 // exists
2560 copySolutionToTempState(swapSolution, dragView);
2561 setItemPlacementDirty(true);
2562 animateItemsToSolution(swapSolution, dragView, commit);
2563
2564 if (commit) {
2565 commitTempPlacement();
2566 completeAndClearReorderHintAnimations();
2567 setItemPlacementDirty(false);
2568 } else {
2569 beginOrAdjustHintAnimations(swapSolution, dragView,
2570 REORDER_ANIMATION_DURATION);
2571 }
2572 mShortcutsAndWidgets.requestLayout();
2573 }
2574 return swapSolution.isSolution;
2575 }
2576
Adam Cohen482ed822012-03-02 14:15:13 -08002577 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2578 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002579 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002580 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002581
2582 if (resultSpan == null) {
2583 resultSpan = new int[2];
2584 }
2585
Adam Cohen19f37922012-03-21 11:59:11 -07002586 // When we are checking drop validity or actually dropping, we don't recompute the
2587 // direction vector, since we want the solution to match the preview, and it's possible
2588 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002589 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2590 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002591 mDirectionVector[0] = mPreviousReorderDirection[0];
2592 mDirectionVector[1] = mPreviousReorderDirection[1];
2593 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002594 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2595 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2596 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002597 }
2598 } else {
2599 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2600 mPreviousReorderDirection[0] = mDirectionVector[0];
2601 mPreviousReorderDirection[1] = mDirectionVector[1];
2602 }
2603
Adam Cohen482ed822012-03-02 14:15:13 -08002604 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2605 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2606
2607 // We attempt the approach which doesn't shuffle views at all
2608 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2609 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2610
2611 ItemConfiguration finalSolution = null;
2612 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2613 finalSolution = swapSolution;
2614 } else if (noShuffleSolution.isSolution) {
2615 finalSolution = noShuffleSolution;
2616 }
2617
2618 boolean foundSolution = true;
2619 if (!DESTRUCTIVE_REORDER) {
2620 setUseTempCoords(true);
2621 }
2622
2623 if (finalSolution != null) {
2624 result[0] = finalSolution.dragViewX;
2625 result[1] = finalSolution.dragViewY;
2626 resultSpan[0] = finalSolution.dragViewSpanX;
2627 resultSpan[1] = finalSolution.dragViewSpanY;
2628
2629 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2630 // committing anything or animating anything as we just want to determine if a solution
2631 // exists
2632 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2633 if (!DESTRUCTIVE_REORDER) {
2634 copySolutionToTempState(finalSolution, dragView);
2635 }
2636 setItemPlacementDirty(true);
2637 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2638
Adam Cohen19f37922012-03-21 11:59:11 -07002639 if (!DESTRUCTIVE_REORDER &&
2640 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002641 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002642 completeAndClearReorderHintAnimations();
2643 setItemPlacementDirty(false);
2644 } else {
2645 beginOrAdjustHintAnimations(finalSolution, dragView,
2646 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002647 }
2648 }
2649 } else {
2650 foundSolution = false;
2651 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2652 }
2653
2654 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2655 setUseTempCoords(false);
2656 }
Adam Cohen482ed822012-03-02 14:15:13 -08002657
Michael Jurkaa52570f2012-03-20 03:18:20 -07002658 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002659 return result;
2660 }
2661
Adam Cohen19f37922012-03-21 11:59:11 -07002662 void setItemPlacementDirty(boolean dirty) {
2663 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002664 }
Adam Cohen19f37922012-03-21 11:59:11 -07002665 boolean isItemPlacementDirty() {
2666 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002667 }
2668
2669 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002670 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002671 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2672 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohen482ed822012-03-02 14:15:13 -08002673 boolean isSolution = false;
2674 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2675
Adam Cohenf3900c22012-11-16 18:28:11 -08002676 void save() {
2677 // Copy current state into savedMap
2678 for (View v: map.keySet()) {
2679 map.get(v).copy(savedMap.get(v));
2680 }
2681 }
2682
2683 void restore() {
2684 // Restore current state from savedMap
2685 for (View v: savedMap.keySet()) {
2686 savedMap.get(v).copy(map.get(v));
2687 }
2688 }
2689
2690 void add(View v, CellAndSpan cs) {
2691 map.put(v, cs);
2692 savedMap.put(v, new CellAndSpan());
2693 sortedViews.add(v);
2694 }
2695
Adam Cohen482ed822012-03-02 14:15:13 -08002696 int area() {
2697 return dragViewSpanX * dragViewSpanY;
2698 }
Adam Cohen8baab352012-03-20 17:39:21 -07002699 }
2700
2701 private class CellAndSpan {
2702 int x, y;
2703 int spanX, spanY;
2704
Adam Cohenf3900c22012-11-16 18:28:11 -08002705 public CellAndSpan() {
2706 }
2707
2708 public void copy(CellAndSpan copy) {
2709 copy.x = x;
2710 copy.y = y;
2711 copy.spanX = spanX;
2712 copy.spanY = spanY;
2713 }
2714
Adam Cohen8baab352012-03-20 17:39:21 -07002715 public CellAndSpan(int x, int y, int spanX, int spanY) {
2716 this.x = x;
2717 this.y = y;
2718 this.spanX = spanX;
2719 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002720 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002721
2722 public String toString() {
2723 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2724 }
2725
Adam Cohen482ed822012-03-02 14:15:13 -08002726 }
2727
Adam Cohendf035382011-04-11 17:22:04 -07002728 /**
2729 * Find a vacant area that will fit the given bounds nearest the requested
2730 * cell location. Uses Euclidean distance to score multiple vacant areas.
2731 *
2732 * @param pixelX The X location at which you want to search for a vacant area.
2733 * @param pixelY The Y location at which you want to search for a vacant area.
2734 * @param spanX Horizontal span of the object.
2735 * @param spanY Vertical span of the object.
2736 * @param ignoreView Considers space occupied by this view as unoccupied
2737 * @param result Previously returned value to possibly recycle.
2738 * @return The X, Y cell of a vacant area that can contain this object,
2739 * nearest the requested location.
2740 */
2741 int[] findNearestVacantArea(
2742 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2743 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2744 }
2745
2746 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002747 * Find a vacant area that will fit the given bounds nearest the requested
2748 * cell location. Uses Euclidean distance to score multiple vacant areas.
2749 *
2750 * @param pixelX The X location at which you want to search for a vacant area.
2751 * @param pixelY The Y location at which you want to search for a vacant area.
2752 * @param minSpanX The minimum horizontal span required
2753 * @param minSpanY The minimum vertical span required
2754 * @param spanX Horizontal span of the object.
2755 * @param spanY Vertical span of the object.
2756 * @param ignoreView Considers space occupied by this view as unoccupied
2757 * @param result Previously returned value to possibly recycle.
2758 * @return The X, Y cell of a vacant area that can contain this object,
2759 * nearest the requested location.
2760 */
2761 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2762 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002763 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2764 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002765 }
2766
2767 /**
Adam Cohendf035382011-04-11 17:22:04 -07002768 * Find a starting cell position that will fit the given bounds nearest the requested
2769 * cell location. Uses Euclidean distance to score multiple vacant areas.
2770 *
2771 * @param pixelX The X location at which you want to search for a vacant area.
2772 * @param pixelY The Y location at which you want to search for a vacant area.
2773 * @param spanX Horizontal span of the object.
2774 * @param spanY Vertical span of the object.
2775 * @param ignoreView Considers space occupied by this view as unoccupied
2776 * @param result Previously returned value to possibly recycle.
2777 * @return The X, Y cell of a vacant area that can contain this object,
2778 * nearest the requested location.
2779 */
2780 int[] findNearestArea(
2781 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2782 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2783 }
2784
Michael Jurka0280c3b2010-09-17 15:00:07 -07002785 boolean existsEmptyCell() {
2786 return findCellForSpan(null, 1, 1);
2787 }
2788
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002790 * Finds the upper-left coordinate of the first rectangle in the grid that can
2791 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2792 * then this method will only return coordinates for rectangles that contain the cell
2793 * (intersectX, intersectY)
2794 *
2795 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2796 * can be found.
2797 * @param spanX The horizontal span of the cell we want to find.
2798 * @param spanY The vertical span of the cell we want to find.
2799 *
2800 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002801 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002802 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002803 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002804 }
2805
2806 /**
2807 * Like above, but ignores any cells occupied by the item "ignoreView"
2808 *
2809 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2810 * can be found.
2811 * @param spanX The horizontal span of the cell we want to find.
2812 * @param spanY The vertical span of the cell we want to find.
2813 * @param ignoreView The home screen item we should treat as not occupying any space
2814 * @return
2815 */
2816 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002817 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2818 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002819 }
2820
2821 /**
2822 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2823 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2824 *
2825 * @param spanX The horizontal span of the cell we want to find.
2826 * @param spanY The vertical span of the cell we want to find.
2827 * @param ignoreView The home screen item we should treat as not occupying any space
2828 * @param intersectX The X coordinate of the cell that we should try to overlap
2829 * @param intersectX The Y coordinate of the cell that we should try to overlap
2830 *
2831 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2832 */
2833 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2834 int intersectX, int intersectY) {
2835 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002836 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002837 }
2838
2839 /**
2840 * The superset of the above two methods
2841 */
2842 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002843 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002844 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002845 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002846
Michael Jurka28750fb2010-09-24 17:43:49 -07002847 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002848 while (true) {
2849 int startX = 0;
2850 if (intersectX >= 0) {
2851 startX = Math.max(startX, intersectX - (spanX - 1));
2852 }
2853 int endX = mCountX - (spanX - 1);
2854 if (intersectX >= 0) {
2855 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2856 }
2857 int startY = 0;
2858 if (intersectY >= 0) {
2859 startY = Math.max(startY, intersectY - (spanY - 1));
2860 }
2861 int endY = mCountY - (spanY - 1);
2862 if (intersectY >= 0) {
2863 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2864 }
2865
Winson Chungbbc60d82010-11-11 16:34:41 -08002866 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002867 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002868 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002869 for (int i = 0; i < spanX; i++) {
2870 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002871 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002872 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002873 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002874 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002875 continue inner;
2876 }
2877 }
2878 }
2879 if (cellXY != null) {
2880 cellXY[0] = x;
2881 cellXY[1] = y;
2882 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002883 foundCell = true;
2884 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002885 }
2886 }
2887 if (intersectX == -1 && intersectY == -1) {
2888 break;
2889 } else {
2890 // if we failed to find anything, try again but without any requirements of
2891 // intersecting
2892 intersectX = -1;
2893 intersectY = -1;
2894 continue;
2895 }
2896 }
2897
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002898 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002899 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002900 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002901 }
2902
2903 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002904 * A drag event has begun over this layout.
2905 * It may have begun over this layout (in which case onDragChild is called first),
2906 * or it may have begun on another layout.
2907 */
2908 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002909 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002910 mDragging = true;
2911 }
2912
2913 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002914 * Called when drag has left this CellLayout or has been completed (successfully or not)
2915 */
2916 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002917 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002918 // This can actually be called when we aren't in a drag, e.g. when adding a new
2919 // item to this layout via the customize drawer.
2920 // Guard against that case.
2921 if (mDragging) {
2922 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002923 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002924
2925 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002926 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002927 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2928 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002929 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002930 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002931 }
2932
2933 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002934 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002935 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002936 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002937 *
2938 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002940 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002941 if (child != null) {
2942 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002943 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002944 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002945 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002946 }
2947
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002950 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002951 * @param cellX X coordinate of upper left corner expressed as a cell position
2952 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002953 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002954 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002955 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002957 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002958 final int cellWidth = mCellWidth;
2959 final int cellHeight = mCellHeight;
2960 final int widthGap = mWidthGap;
2961 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002962
Winson Chung4b825dcd2011-06-19 12:41:22 -07002963 final int hStartPadding = getPaddingLeft();
2964 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2967 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2968
2969 int x = hStartPadding + cellX * (cellWidth + widthGap);
2970 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002971
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002972 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002974
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002976 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002978 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 * @param width Width in pixels
2980 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002981 * @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 -08002982 */
Winson Chung66700732013-08-20 16:56:15 -07002983 public static int[] rectToCell(int width, int height, int[] result) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002984 LauncherAppState app = LauncherAppState.getInstance();
2985 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -07002986 Rect padding = grid.getWorkspacePadding(grid.isLandscape ?
2987 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
Winson Chung5f8afe62013-08-12 16:19:28 -07002988
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989 // Always assume we're working with the smallest span to make sure we
2990 // reserve enough space in both orientations.
Winson Chung66700732013-08-20 16:56:15 -07002991 int parentWidth = grid.calculateCellWidth(grid.widthPx
2992 - padding.left - padding.right, (int) grid.numColumns);
2993 int parentHeight = grid.calculateCellHeight(grid.heightPx
2994 - padding.top - padding.bottom, (int) grid.numRows);
2995 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002996
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002997 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002998 int spanX = (int) Math.ceil(width / (float) smallerSize);
2999 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04003000
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07003001 if (result == null) {
3002 return new int[] { spanX, spanY };
3003 }
3004 result[0] = spanX;
3005 result[1] = spanY;
3006 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003007 }
3008
Michael Jurkaf12c75c2011-01-25 22:41:40 -08003009 public int[] cellSpansToSize(int hSpans, int vSpans) {
3010 int[] size = new int[2];
3011 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
3012 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
3013 return size;
3014 }
3015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003016 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08003017 * Calculate the grid spans needed to fit given item
3018 */
3019 public void calculateSpans(ItemInfo info) {
3020 final int minWidth;
3021 final int minHeight;
3022
3023 if (info instanceof LauncherAppWidgetInfo) {
3024 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3025 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3026 } else if (info instanceof PendingAddWidgetInfo) {
3027 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3028 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3029 } else {
3030 // It's not a widget, so it must be 1x1
3031 info.spanX = info.spanY = 1;
3032 return;
3033 }
3034 int[] spans = rectToCell(minWidth, minHeight, null);
3035 info.spanX = spans[0];
3036 info.spanY = spans[1];
3037 }
3038
3039 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040 * Find the first vacant cell, if there is one.
3041 *
3042 * @param vacant Holds the x and y coordinate of the vacant cell
3043 * @param spanX Horizontal cell span.
3044 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003045 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003046 * @return True if a vacant cell was found
3047 */
3048 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049
Michael Jurka0280c3b2010-09-17 15:00:07 -07003050 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003051 }
3052
3053 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3054 int xCount, int yCount, boolean[][] occupied) {
3055
Adam Cohen2801caf2011-05-13 20:57:39 -07003056 for (int y = 0; y < yCount; y++) {
3057 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003058 boolean available = !occupied[x][y];
3059out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3060 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3061 available = available && !occupied[i][j];
3062 if (!available) break out;
3063 }
3064 }
3065
3066 if (available) {
3067 vacant[0] = x;
3068 vacant[1] = y;
3069 return true;
3070 }
3071 }
3072 }
3073
3074 return false;
3075 }
3076
Michael Jurka0280c3b2010-09-17 15:00:07 -07003077 private void clearOccupiedCells() {
3078 for (int x = 0; x < mCountX; x++) {
3079 for (int y = 0; y < mCountY; y++) {
3080 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003081 }
3082 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003083 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003084
Adam Cohend41fbf52012-02-16 23:53:59 -08003085 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003086 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003087 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003089
Adam Cohend4844c32011-02-18 19:25:06 -08003090 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003091 markCellsAsOccupiedForView(view, mOccupied);
3092 }
3093 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003094 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003095 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003096 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003097 }
3098
Adam Cohend4844c32011-02-18 19:25:06 -08003099 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003100 markCellsAsUnoccupiedForView(view, mOccupied);
3101 }
3102 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003103 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003104 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003105 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003106 }
3107
Adam Cohen482ed822012-03-02 14:15:13 -08003108 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3109 boolean value) {
3110 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003111 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3112 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003113 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114 }
3115 }
3116 }
3117
Adam Cohen2801caf2011-05-13 20:57:39 -07003118 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003119 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003120 (Math.max((mCountX - 1), 0) * mWidthGap);
3121 }
3122
3123 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003124 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003125 (Math.max((mCountY - 1), 0) * mHeightGap);
3126 }
3127
Michael Jurka66d72172011-04-12 16:29:25 -07003128 public boolean isOccupied(int x, int y) {
3129 if (x < mCountX && y < mCountY) {
3130 return mOccupied[x][y];
3131 } else {
3132 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3133 }
3134 }
3135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136 @Override
3137 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3138 return new CellLayout.LayoutParams(getContext(), attrs);
3139 }
3140
3141 @Override
3142 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3143 return p instanceof CellLayout.LayoutParams;
3144 }
3145
3146 @Override
3147 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3148 return new CellLayout.LayoutParams(p);
3149 }
3150
Winson Chungaafa03c2010-06-11 17:34:16 -07003151 public static class CellLayoutAnimationController extends LayoutAnimationController {
3152 public CellLayoutAnimationController(Animation animation, float delay) {
3153 super(animation, delay);
3154 }
3155
3156 @Override
3157 protected long getDelayForView(View view) {
3158 return (int) (Math.random() * 150);
3159 }
3160 }
3161
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003162 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3163 /**
3164 * Horizontal location of the item in the grid.
3165 */
3166 @ViewDebug.ExportedProperty
3167 public int cellX;
3168
3169 /**
3170 * Vertical location of the item in the grid.
3171 */
3172 @ViewDebug.ExportedProperty
3173 public int cellY;
3174
3175 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003176 * Temporary horizontal location of the item in the grid during reorder
3177 */
3178 public int tmpCellX;
3179
3180 /**
3181 * Temporary vertical location of the item in the grid during reorder
3182 */
3183 public int tmpCellY;
3184
3185 /**
3186 * Indicates that the temporary coordinates should be used to layout the items
3187 */
3188 public boolean useTmpCoords;
3189
3190 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003191 * Number of cells spanned horizontally by the item.
3192 */
3193 @ViewDebug.ExportedProperty
3194 public int cellHSpan;
3195
3196 /**
3197 * Number of cells spanned vertically by the item.
3198 */
3199 @ViewDebug.ExportedProperty
3200 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003201
Adam Cohen1b607ed2011-03-03 17:26:50 -08003202 /**
3203 * Indicates whether the item will set its x, y, width and height parameters freely,
3204 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3205 */
Adam Cohend4844c32011-02-18 19:25:06 -08003206 public boolean isLockedToGrid = true;
3207
Adam Cohen482ed822012-03-02 14:15:13 -08003208 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07003209 * Indicates that this item should use the full extents of its parent.
3210 */
3211 public boolean isFullscreen = false;
3212
3213 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003214 * Indicates whether this item can be reordered. Always true except in the case of the
3215 * the AllApps button.
3216 */
3217 public boolean canReorder = true;
3218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003219 // X coordinate of the view in the layout.
3220 @ViewDebug.ExportedProperty
3221 int x;
3222 // Y coordinate of the view in the layout.
3223 @ViewDebug.ExportedProperty
3224 int y;
3225
Romain Guy84f296c2009-11-04 15:00:44 -08003226 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003228 public LayoutParams(Context c, AttributeSet attrs) {
3229 super(c, attrs);
3230 cellHSpan = 1;
3231 cellVSpan = 1;
3232 }
3233
3234 public LayoutParams(ViewGroup.LayoutParams source) {
3235 super(source);
3236 cellHSpan = 1;
3237 cellVSpan = 1;
3238 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003239
3240 public LayoutParams(LayoutParams source) {
3241 super(source);
3242 this.cellX = source.cellX;
3243 this.cellY = source.cellY;
3244 this.cellHSpan = source.cellHSpan;
3245 this.cellVSpan = source.cellVSpan;
3246 }
3247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003248 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003249 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003250 this.cellX = cellX;
3251 this.cellY = cellY;
3252 this.cellHSpan = cellHSpan;
3253 this.cellVSpan = cellVSpan;
3254 }
3255
Adam Cohen2374abf2013-04-16 14:56:57 -07003256 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3257 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003258 if (isLockedToGrid) {
3259 final int myCellHSpan = cellHSpan;
3260 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003261 int myCellX = useTmpCoords ? tmpCellX : cellX;
3262 int myCellY = useTmpCoords ? tmpCellY : cellY;
3263
3264 if (invertHorizontally) {
3265 myCellX = colCount - myCellX - cellHSpan;
3266 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003267
Adam Cohend4844c32011-02-18 19:25:06 -08003268 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3269 leftMargin - rightMargin;
3270 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3271 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003272 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3273 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003274 }
3275 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003276
Winson Chungaafa03c2010-06-11 17:34:16 -07003277 public String toString() {
3278 return "(" + this.cellX + ", " + this.cellY + ")";
3279 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003280
3281 public void setWidth(int width) {
3282 this.width = width;
3283 }
3284
3285 public int getWidth() {
3286 return width;
3287 }
3288
3289 public void setHeight(int height) {
3290 this.height = height;
3291 }
3292
3293 public int getHeight() {
3294 return height;
3295 }
3296
3297 public void setX(int x) {
3298 this.x = x;
3299 }
3300
3301 public int getX() {
3302 return x;
3303 }
3304
3305 public void setY(int y) {
3306 this.y = y;
3307 }
3308
3309 public int getY() {
3310 return y;
3311 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003312 }
3313
Michael Jurka0280c3b2010-09-17 15:00:07 -07003314 // This class stores info for two purposes:
3315 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3316 // its spanX, spanY, and the screen it is on
3317 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3318 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3319 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003320 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003321 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003322 int cellX = -1;
3323 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003324 int spanX;
3325 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003326 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003327 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003328
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003329 @Override
3330 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003331 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3332 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003333 }
3334 }
Michael Jurkad771c962011-08-09 15:00:48 -07003335
3336 public boolean lastDownOnOccupiedCell() {
3337 return mLastDownOnOccupiedCell;
3338 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003339}