blob: e3312d557e287af3866ee4eb3a47a096f3219585 [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;
Adam Cohendedbd962013-07-11 14:21:49 -0700118 boolean mUseActiveGlowBackground = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700119
Winson Chung150fbab2010-09-29 17:14:26 -0700120 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700121 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800122 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700123 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700124 private InterruptibleInOutAnimator[] mDragOutlineAnims =
125 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700126
127 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700128 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700129 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700130
Patrick Dubroy96864c32011-03-10 17:17:23 -0800131 private BubbleTextView mPressedOrFocusedIcon;
132
Adam Cohen482ed822012-03-02 14:15:13 -0800133 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
134 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700135 private HashMap<View, ReorderHintAnimation>
136 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
137
138 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700139
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700140 // When a drag operation is in progress, holds the nearest cell to the touch point
141 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Joe Onorato4be866d2010-10-10 11:26:02 -0700143 private boolean mDragging = false;
144
Patrick Dubroyce34a972010-10-19 10:34:32 -0700145 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700146 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700147
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800148 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700149 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800150
Adam Cohen482ed822012-03-02 14:15:13 -0800151 public static final int MODE_DRAG_OVER = 0;
152 public static final int MODE_ON_DROP = 1;
153 public static final int MODE_ON_DROP_EXTERNAL = 2;
154 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700155 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800156 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
157
Adam Cohena897f392012-04-27 18:12:05 -0700158 static final int LANDSCAPE = 0;
159 static final int PORTRAIT = 1;
160
Adam Cohen7bdfc972012-05-22 16:50:35 -0700161 private static final float REORDER_HINT_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700162 private static final int REORDER_ANIMATION_DURATION = 150;
163 private float mReorderHintAnimationMagnitude;
164
Adam Cohen482ed822012-03-02 14:15:13 -0800165 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
166 private Rect mOccupiedRect = new Rect();
167 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700168 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700169 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700170 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800171
Romain Guy8a0bff52012-05-06 13:14:33 -0700172 private final static PorterDuffXfermode sAddBlendMode =
173 new PorterDuffXfermode(PorterDuff.Mode.ADD);
Michael Jurkaca993832012-06-29 15:17:04 -0700174 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 public CellLayout(Context context) {
177 this(context, null);
178 }
179
180 public CellLayout(Context context, AttributeSet attrs) {
181 this(context, attrs, 0);
182 }
183
184 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
185 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700186 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700187
188 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
189 // the user where a dragged item will land when dropped.
190 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800191 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700192 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700193
Winson Chung892c74d2013-08-22 16:15:50 -0700194 LauncherAppState app = LauncherAppState.getInstance();
195 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
197
Winson Chung11a1a532013-09-13 11:14:45 -0700198 mCellWidth = mCellHeight = -1;
199 mFixedCellHeight = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700200 mWidthGap = mOriginalWidthGap = 0;
201 mHeightGap = mOriginalHeightGap = 0;
202 mMaxGap = Integer.MAX_VALUE;
Winson Chung892c74d2013-08-22 16:15:50 -0700203 mCountX = (int) grid.numColumns;
204 mCountY = (int) grid.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700205 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800206 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700207 mPreviousReorderDirection[0] = INVALID_DIRECTION;
208 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
210 a.recycle();
211
212 setAlwaysDrawnWithCacheEnabled(false);
213
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700214 final Resources res = getResources();
Winson Chung5f8afe62013-08-12 16:19:28 -0700215 mHotseatScale = (float) grid.hotseatIconSize / grid.iconSize;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700216
Adam Cohen410f3cd2013-09-22 12:09:32 -0700217 mNormalBackground = res.getDrawable(R.drawable.screenpanel);
218 mActiveGlowBackground = res.getDrawable(R.drawable.screenpanel_hover);
Michael Jurka33945b22010-12-21 18:19:38 -0800219
Adam Cohenb5ba0972011-09-07 18:02:31 -0700220 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
221 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
222 mForegroundPadding =
223 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800224
Adam Cohen19f37922012-03-21 11:59:11 -0700225 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700226 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700227
Winson Chungb26f3d62011-06-02 10:49:29 -0700228 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700229 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700230
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700231 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700232 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700233 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700234 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800235 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700236 }
237
238 // When dragging things around the home screens, we show a green outline of
239 // where the item will land. The outlines gradually fade out, leaving a trail
240 // behind the drag path.
241 // Set up all the animations that are used to implement this fading.
242 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700243 final float fromAlphaValue = 0;
244 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700245
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700246 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700247
248 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700249 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100250 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700251 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700252 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700253 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700254 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700255 final Bitmap outline = (Bitmap)anim.getTag();
256
257 // If an animation is started and then stopped very quickly, we can still
258 // get spurious updates we've cleared the tag. Guard against this.
259 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700260 @SuppressWarnings("all") // suppress dead code warning
261 final boolean debug = false;
262 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700263 Object val = animation.getAnimatedValue();
264 Log.d(TAG, "anim " + thisIndex + " update: " + val +
265 ", isStopped " + anim.isStopped());
266 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700267 // Try to prevent it from continuing to run
268 animation.cancel();
269 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700270 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800271 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700272 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700273 }
274 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700275 // The animation holds a reference to the drag outline bitmap as long is it's
276 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700277 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700278 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700279 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700280 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700281 anim.setTag(null);
282 }
283 }
284 });
285 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700286 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700287
Michael Jurka18014792010-10-14 09:01:34 -0700288 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700289 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800290
Michael Jurkaa52570f2012-03-20 03:18:20 -0700291 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700292 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700293 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700294
Michael Jurkaa52570f2012-03-20 03:18:20 -0700295 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700296 }
297
Chris Craik01f2d7f2013-10-01 14:41:56 -0700298 public void enableHardwareLayer(boolean hasLayer) {
299 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700300 }
301
302 public void buildHardwareLayer() {
303 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700304 }
305
Adam Cohen307fe232012-08-16 17:55:58 -0700306 public float getChildrenScale() {
307 return mIsHotseat ? mHotseatScale : 1.0f;
308 }
309
Winson Chung5f8afe62013-08-12 16:19:28 -0700310 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700311 mFixedCellWidth = mCellWidth = width;
312 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700313 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
314 mCountX, mCountY);
315 }
316
Adam Cohen2801caf2011-05-13 20:57:39 -0700317 public void setGridSize(int x, int y) {
318 mCountX = x;
319 mCountY = y;
320 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800321 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700322 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700323 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700324 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700325 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700326 }
327
Adam Cohen2374abf2013-04-16 14:56:57 -0700328 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
329 public void setInvertIfRtl(boolean invert) {
330 mShortcutsAndWidgets.setInvertIfRtl(invert);
331 }
332
Patrick Dubroy96864c32011-03-10 17:17:23 -0800333 private void invalidateBubbleTextView(BubbleTextView icon) {
334 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700335 invalidate(icon.getLeft() + getPaddingLeft() - padding,
336 icon.getTop() + getPaddingTop() - padding,
337 icon.getRight() + getPaddingLeft() + padding,
338 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800339 }
340
Adam Cohenb5ba0972011-09-07 18:02:31 -0700341 void setOverScrollAmount(float r, boolean left) {
342 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
343 mOverScrollForegroundDrawable = mOverScrollLeft;
344 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
345 mOverScrollForegroundDrawable = mOverScrollRight;
346 }
347
Adam Cohen02dcfcc2013-10-01 12:37:33 -0700348 r *= FOREGROUND_ALPHA_DAMPER;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700349 mForegroundAlpha = (int) Math.round((r * 255));
350 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
351 invalidate();
352 }
353
Patrick Dubroy96864c32011-03-10 17:17:23 -0800354 void setPressedOrFocusedIcon(BubbleTextView icon) {
355 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
356 // requires an expanded clip rect (due to the glow's blur radius)
357 BubbleTextView oldIcon = mPressedOrFocusedIcon;
358 mPressedOrFocusedIcon = icon;
359 if (oldIcon != null) {
360 invalidateBubbleTextView(oldIcon);
361 }
362 if (mPressedOrFocusedIcon != null) {
363 invalidateBubbleTextView(mPressedOrFocusedIcon);
364 }
365 }
366
Michael Jurka33945b22010-12-21 18:19:38 -0800367 void setIsDragOverlapping(boolean isDragOverlapping) {
368 if (mIsDragOverlapping != isDragOverlapping) {
369 mIsDragOverlapping = isDragOverlapping;
Adam Cohendedbd962013-07-11 14:21:49 -0700370 setUseActiveGlowBackground(mIsDragOverlapping);
Michael Jurka33945b22010-12-21 18:19:38 -0800371 invalidate();
372 }
373 }
374
Adam Cohendedbd962013-07-11 14:21:49 -0700375 void setUseActiveGlowBackground(boolean use) {
376 mUseActiveGlowBackground = use;
377 }
378
Michael Jurka33945b22010-12-21 18:19:38 -0800379 boolean getIsDragOverlapping() {
380 return mIsDragOverlapping;
381 }
382
Adam Cohenebea84d2011-11-09 17:20:41 -0800383 protected void setOverscrollTransformsDirty(boolean dirty) {
384 mScrollingTransformsDirty = dirty;
385 }
386
387 protected void resetOverscrollTransforms() {
388 if (mScrollingTransformsDirty) {
389 setOverscrollTransformsDirty(false);
390 setTranslationX(0);
391 setRotationY(0);
392 // It doesn't matter if we pass true or false here, the important thing is that we
393 // pass 0, which results in the overscroll drawable not being drawn any more.
394 setOverScrollAmount(0, false);
395 setPivotX(getMeasuredWidth() / 2);
396 setPivotY(getMeasuredHeight() / 2);
397 }
398 }
399
Adam Cohen307fe232012-08-16 17:55:58 -0700400 public void scaleRect(Rect r, float scale) {
401 if (scale != 1.0f) {
402 r.left = (int) (r.left * scale + 0.5f);
403 r.top = (int) (r.top * scale + 0.5f);
404 r.right = (int) (r.right * scale + 0.5f);
405 r.bottom = (int) (r.bottom * scale + 0.5f);
406 }
407 }
408
409 Rect temp = new Rect();
410 void scaleRectAboutCenter(Rect in, Rect out, float scale) {
411 int cx = in.centerX();
412 int cy = in.centerY();
413 out.set(in);
414 out.offset(-cx, -cy);
415 scaleRect(out, scale);
416 out.offset(cx, cy);
417 }
418
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700419 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700420 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700421 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
422 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
423 // When we're small, we are either drawn normally or in the "accepts drops" state (during
424 // a drag). However, we also drag the mini hover background *over* one of those two
425 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700426 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700427 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800428
Adam Cohendedbd962013-07-11 14:21:49 -0700429 if (mUseActiveGlowBackground) {
Michael Jurka33945b22010-12-21 18:19:38 -0800430 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700431 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700432 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700433 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700434 }
Michael Jurka33945b22010-12-21 18:19:38 -0800435
436 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
437 bg.setBounds(mBackgroundRect);
438 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700439 }
Romain Guya6abce82009-11-10 02:54:41 -0800440
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700441 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700442 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700443 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700444 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800445 final Rect r = mDragOutlines[i];
Adam Cohen307fe232012-08-16 17:55:58 -0700446 scaleRectAboutCenter(r, temp, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700447 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700448 paint.setAlpha((int)(alpha + .5f));
Adam Cohen307fe232012-08-16 17:55:58 -0700449 canvas.drawBitmap(b, null, temp, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700450 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700451 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800452
453 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
454 // requires an expanded clip rect (due to the glow's blur radius)
455 if (mPressedOrFocusedIcon != null) {
456 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
457 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
458 if (b != null) {
459 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700460 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
461 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800462 null);
463 }
464 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700465
Adam Cohen482ed822012-03-02 14:15:13 -0800466 if (DEBUG_VISUALIZE_OCCUPIED) {
467 int[] pt = new int[2];
468 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700469 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800470 for (int i = 0; i < mCountX; i++) {
471 for (int j = 0; j < mCountY; j++) {
472 if (mOccupied[i][j]) {
473 cellToPoint(i, j, pt);
474 canvas.save();
475 canvas.translate(pt[0], pt[1]);
476 cd.draw(canvas);
477 canvas.restore();
478 }
479 }
480 }
481 }
482
Andrew Flynn850d2e72012-04-26 16:51:20 -0700483 int previewOffset = FolderRingAnimator.sPreviewSize;
484
Adam Cohen69ce2e52011-07-03 19:25:21 -0700485 // The folder outer / inner ring image(s)
Winson Chung5f8afe62013-08-12 16:19:28 -0700486 LauncherAppState app = LauncherAppState.getInstance();
487 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700488 for (int i = 0; i < mFolderOuterRings.size(); i++) {
489 FolderRingAnimator fra = mFolderOuterRings.get(i);
490
Adam Cohen5108bc02013-09-20 17:04:51 -0700491 Drawable d;
492 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700493 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700494 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700495
Winson Chung89f97052013-09-20 11:32:26 -0700496 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700497 int centerX = mTempLocation[0] + mCellWidth / 2;
498 int centerY = mTempLocation[1] + previewOffset / 2 +
499 child.getPaddingTop() + grid.folderBackgroundOffset;
500
Adam Cohen5108bc02013-09-20 17:04:51 -0700501 // Draw outer ring, if it exists
502 if (FolderIcon.HAS_OUTER_RING) {
503 d = FolderRingAnimator.sSharedOuterRingDrawable;
504 width = (int) (fra.getOuterRingSize() * getChildrenScale());
505 height = width;
506 canvas.save();
507 canvas.translate(centerX - width / 2, centerY - height / 2);
508 d.setBounds(0, 0, width, height);
509 d.draw(canvas);
510 canvas.restore();
511 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700512
Winson Chung89f97052013-09-20 11:32:26 -0700513 // Draw inner ring
514 d = FolderRingAnimator.sSharedInnerRingDrawable;
515 width = (int) (fra.getInnerRingSize() * getChildrenScale());
516 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700517 canvas.save();
518 canvas.translate(centerX - width / 2, centerY - width / 2);
519 d.setBounds(0, 0, width, height);
520 d.draw(canvas);
521 canvas.restore();
522 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700523 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700524
525 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
526 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
527 int width = d.getIntrinsicWidth();
528 int height = d.getIntrinsicHeight();
529
530 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700531 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700532 if (child != null) {
533 int centerX = mTempLocation[0] + mCellWidth / 2;
534 int centerY = mTempLocation[1] + previewOffset / 2 +
535 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700536
Winson Chung89f97052013-09-20 11:32:26 -0700537 canvas.save();
538 canvas.translate(centerX - width / 2, centerY - width / 2);
539 d.setBounds(0, 0, width, height);
540 d.draw(canvas);
541 canvas.restore();
542 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700543 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700544 }
545
Adam Cohenb5ba0972011-09-07 18:02:31 -0700546 @Override
547 protected void dispatchDraw(Canvas canvas) {
548 super.dispatchDraw(canvas);
549 if (mForegroundAlpha > 0) {
550 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700551 mOverScrollForegroundDrawable.draw(canvas);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700552 }
553 }
554
Adam Cohen69ce2e52011-07-03 19:25:21 -0700555 public void showFolderAccept(FolderRingAnimator fra) {
556 mFolderOuterRings.add(fra);
557 }
558
559 public void hideFolderAccept(FolderRingAnimator fra) {
560 if (mFolderOuterRings.contains(fra)) {
561 mFolderOuterRings.remove(fra);
562 }
563 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700564 }
565
Adam Cohenc51934b2011-07-26 21:07:43 -0700566 public void setFolderLeaveBehindCell(int x, int y) {
567 mFolderLeaveBehindCell[0] = x;
568 mFolderLeaveBehindCell[1] = y;
569 invalidate();
570 }
571
572 public void clearFolderLeaveBehind() {
573 mFolderLeaveBehindCell[0] = -1;
574 mFolderLeaveBehindCell[1] = -1;
575 invalidate();
576 }
577
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700578 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700579 public boolean shouldDelayChildPressedState() {
580 return false;
581 }
582
Adam Cohen1462de32012-07-24 22:34:36 -0700583 public void restoreInstanceState(SparseArray<Parcelable> states) {
584 dispatchRestoreInstanceState(states);
585 }
586
Michael Jurkae6235dd2011-10-04 15:02:05 -0700587 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700588 public void cancelLongPress() {
589 super.cancelLongPress();
590
591 // Cancel long press for all children
592 final int count = getChildCount();
593 for (int i = 0; i < count; i++) {
594 final View child = getChildAt(i);
595 child.cancelLongPress();
596 }
597 }
598
Michael Jurkadee05892010-07-27 10:01:56 -0700599 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
600 mInterceptTouchListener = listener;
601 }
602
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700604 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 }
606
607 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700608 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 }
610
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800611 public void setIsHotseat(boolean isHotseat) {
612 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700613 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800614 }
615
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800616 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700617 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700618 final LayoutParams lp = params;
619
Andrew Flynnde38e422012-05-08 11:22:15 -0700620 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800621 if (child instanceof BubbleTextView) {
622 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700623 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800624 }
625
Adam Cohen307fe232012-08-16 17:55:58 -0700626 child.setScaleX(getChildrenScale());
627 child.setScaleY(getChildrenScale());
628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 // Generate an id for each view, this assumes we have at most 256x256 cells
630 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700631 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700632 // If the horizontal or vertical span is set to -1, it is taken to
633 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700634 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
635 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636
Winson Chungaafa03c2010-06-11 17:34:16 -0700637 child.setId(childId);
638
Michael Jurkaa52570f2012-03-20 03:18:20 -0700639 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700640
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700641 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700642
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 return true;
644 }
645 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700647
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700649 public void removeAllViews() {
650 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700651 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700652 }
653
654 @Override
655 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700656 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700657 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700658 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700659 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700660 }
661
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700662 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700663 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700664 }
665
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 @Override
667 public void removeView(View view) {
668 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700669 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700670 }
671
672 @Override
673 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700674 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
675 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 }
677
678 @Override
679 public void removeViewInLayout(View view) {
680 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700681 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700682 }
683
684 @Override
685 public void removeViews(int start, int count) {
686 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700687 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700688 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700689 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 }
691
692 @Override
693 public void removeViewsInLayout(int start, int count) {
694 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700695 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700696 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700697 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800698 }
699
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 @Override
701 protected void onAttachedToWindow() {
702 super.onAttachedToWindow();
Adam Cohendcd297f2013-06-18 13:13:40 -0700703 if (getParent() instanceof Workspace) {
704 Workspace workspace = (Workspace) getParent();
705 mCellInfo.screenId = workspace.getIdForScreen(this);
706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 }
708
Michael Jurkaaf442092010-06-10 17:01:57 -0700709 public void setTagToCellInfoForPoint(int touchX, int touchY) {
710 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800711 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700712 final int x = touchX + getScrollX();
713 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700714 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700715
716 boolean found = false;
717 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700718 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800719 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700720
Adam Cohen1b607ed2011-03-03 17:26:50 -0800721 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
722 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700723 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700724
Winson Chungeecf02d2012-03-02 17:14:58 -0800725 float scale = child.getScaleX();
726 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
727 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700728 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
729 // offset that by this CellLayout's padding to test an (x,y) point that is relative
730 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800732 frame.inset((int) (frame.width() * (1f - scale) / 2),
733 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700734
Michael Jurkaaf442092010-06-10 17:01:57 -0700735 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700736 cellInfo.cell = child;
737 cellInfo.cellX = lp.cellX;
738 cellInfo.cellY = lp.cellY;
739 cellInfo.spanX = lp.cellHSpan;
740 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700741 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700742 break;
743 }
744 }
745 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700746
Michael Jurkad771c962011-08-09 15:00:48 -0700747 mLastDownOnOccupiedCell = found;
748
Michael Jurkaaf442092010-06-10 17:01:57 -0700749 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700750 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700751 pointToCellExact(x, y, cellXY);
752
Michael Jurkaaf442092010-06-10 17:01:57 -0700753 cellInfo.cell = null;
754 cellInfo.cellX = cellXY[0];
755 cellInfo.cellY = cellXY[1];
756 cellInfo.spanX = 1;
757 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700758 }
759 setTag(cellInfo);
760 }
761
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 @Override
763 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700764 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
765 // even in the case where we return early. Not clearing here was causing bugs whereby on
766 // long-press we'd end up picking up an item from a previous drag operation.
767 final int action = ev.getAction();
768
769 if (action == MotionEvent.ACTION_DOWN) {
770 clearTagCellInfo();
771 }
772
Michael Jurkadee05892010-07-27 10:01:56 -0700773 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
774 return true;
775 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776
777 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700778 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800780
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 return false;
782 }
783
Adam Cohenc1997fd2011-08-15 18:26:39 -0700784 private void clearTagCellInfo() {
785 final CellInfo cellInfo = mCellInfo;
786 cellInfo.cell = null;
787 cellInfo.cellX = -1;
788 cellInfo.cellY = -1;
789 cellInfo.spanX = 0;
790 cellInfo.spanY = 0;
791 setTag(cellInfo);
792 }
793
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700795 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 }
797
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700798 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700799 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 * @param x X coordinate of the point
801 * @param y Y coordinate of the point
802 * @param result Array of 2 ints to hold the x and y coordinate of the cell
803 */
804 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700805 final int hStartPadding = getPaddingLeft();
806 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807
808 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
809 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
810
Adam Cohend22015c2010-07-26 22:02:18 -0700811 final int xAxis = mCountX;
812 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813
814 if (result[0] < 0) result[0] = 0;
815 if (result[0] >= xAxis) result[0] = xAxis - 1;
816 if (result[1] < 0) result[1] = 0;
817 if (result[1] >= yAxis) result[1] = yAxis - 1;
818 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700819
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 /**
821 * Given a point, return the cell that most closely encloses that point
822 * @param x X coordinate of the point
823 * @param y Y coordinate of the point
824 * @param result Array of 2 ints to hold the x and y coordinate of the cell
825 */
826 void pointToCellRounded(int x, int y, int[] result) {
827 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
828 }
829
830 /**
831 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700832 *
833 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700835 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 * @param result Array of 2 ints to hold the x and y coordinate of the point
837 */
838 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700839 final int hStartPadding = getPaddingLeft();
840 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841
842 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
843 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
844 }
845
Adam Cohene3e27a82011-04-15 12:07:39 -0700846 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800847 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700848 *
849 * @param cellX X coordinate of the cell
850 * @param cellY Y coordinate of the cell
851 *
852 * @param result Array of 2 ints to hold the x and y coordinate of the point
853 */
854 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700855 regionToCenterPoint(cellX, cellY, 1, 1, result);
856 }
857
858 /**
859 * Given a cell coordinate and span return the point that represents the center of the regio
860 *
861 * @param cellX X coordinate of the cell
862 * @param cellY Y coordinate of the cell
863 *
864 * @param result Array of 2 ints to hold the x and y coordinate of the point
865 */
866 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700867 final int hStartPadding = getPaddingLeft();
868 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700869 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
870 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
871 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
872 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700873 }
874
Adam Cohen19f37922012-03-21 11:59:11 -0700875 /**
876 * Given a cell coordinate and span fills out a corresponding pixel rect
877 *
878 * @param cellX X coordinate of the cell
879 * @param cellY Y coordinate of the cell
880 * @param result Rect in which to write the result
881 */
882 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
883 final int hStartPadding = getPaddingLeft();
884 final int vStartPadding = getPaddingTop();
885 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
886 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
887 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
888 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
889 }
890
Adam Cohen482ed822012-03-02 14:15:13 -0800891 public float getDistanceFromCell(float x, float y, int[] cell) {
892 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
893 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
894 Math.pow(y - mTmpPoint[1], 2));
895 return distance;
896 }
897
Romain Guy84f296c2009-11-04 15:00:44 -0800898 int getCellWidth() {
899 return mCellWidth;
900 }
901
902 int getCellHeight() {
903 return mCellHeight;
904 }
905
Adam Cohend4844c32011-02-18 19:25:06 -0800906 int getWidthGap() {
907 return mWidthGap;
908 }
909
910 int getHeightGap() {
911 return mHeightGap;
912 }
913
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700914 Rect getContentRect(Rect r) {
915 if (r == null) {
916 r = new Rect();
917 }
918 int left = getPaddingLeft();
919 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700920 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
921 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700922 r.set(left, top, right, bottom);
923 return r;
924 }
925
Winson Chungfe411c82013-09-26 16:07:17 -0700926 /** Return a rect that has the cellWidth/cellHeight (left, top), and
927 * widthGap/heightGap (right, bottom) */
Winson Chung66700732013-08-20 16:56:15 -0700928 static void getMetrics(Rect metrics, int paddedMeasureWidth,
929 int paddedMeasureHeight, int countX, int countY) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700930 LauncherAppState app = LauncherAppState.getInstance();
931 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -0700932 metrics.set(grid.calculateCellWidth(paddedMeasureWidth, countX),
933 grid.calculateCellHeight(paddedMeasureHeight, countY), 0, 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700934 }
935
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700936 public void setFixedSize(int width, int height) {
937 mFixedWidth = width;
938 mFixedHeight = height;
939 }
940
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 @Override
942 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700943 LauncherAppState app = LauncherAppState.getInstance();
944 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
945
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700948 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
949 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700950 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
951 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700952 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700953 int cw = grid.calculateCellWidth(childWidthSize, mCountX);
954 int ch = grid.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700955 if (cw != mCellWidth || ch != mCellHeight) {
956 mCellWidth = cw;
957 mCellHeight = ch;
958 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
959 mHeightGap, mCountX, mCountY);
960 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700961 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700962
Winson Chung2d75f122013-09-23 16:53:31 -0700963 int newWidth = childWidthSize;
964 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700965 if (mFixedWidth > 0 && mFixedHeight > 0) {
966 newWidth = mFixedWidth;
967 newHeight = mFixedHeight;
968 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
970 }
971
Adam Cohend22015c2010-07-26 22:02:18 -0700972 int numWidthGaps = mCountX - 1;
973 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974
Adam Cohen234c4cd2011-07-17 21:03:04 -0700975 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700976 int hSpace = childWidthSize;
977 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700978 int hFreeSpace = hSpace - (mCountX * mCellWidth);
979 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700980 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
981 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700982 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
983 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700984 } else {
985 mWidthGap = mOriginalWidthGap;
986 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700987 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800988 int count = getChildCount();
Winson Chung5f8afe62013-08-12 16:19:28 -0700989 int maxWidth = 0;
990 int maxHeight = 0;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800991 for (int i = 0; i < count; i++) {
992 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -0700993 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth,
994 MeasureSpec.EXACTLY);
995 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
996 MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800997 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700998 maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
999 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
Michael Jurka8c920dd2011-01-20 14:16:56 -08001000 }
Winson Chung2d75f122013-09-23 16:53:31 -07001001 if (mFixedWidth > 0 && mFixedHeight > 0) {
1002 setMeasuredDimension(maxWidth, maxHeight);
1003 } else {
1004 setMeasuredDimension(widthSize, heightSize);
1005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
1007
1008 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001009 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -07001010 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
1011 (mCountX * mCellWidth);
1012 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
1013 int top = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001016 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -07001017 child.layout(left, top,
1018 left + r - l,
1019 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 }
1021 }
1022
1023 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001024 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1025 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -07001026
1027 // Expand the background drawing bounds by the padding baked into the background drawable
1028 Rect padding = new Rect();
1029 mNormalBackground.getPadding(padding);
1030 mBackgroundRect.set(-padding.left, -padding.top, w + padding.right, h + padding.bottom);
1031
Adam Cohenb5ba0972011-09-07 18:02:31 -07001032 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001033 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001034 }
1035
1036 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001038 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 }
1040
1041 @Override
1042 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001043 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 }
1045
Michael Jurka5f1c5092010-09-03 14:15:02 -07001046 public float getBackgroundAlpha() {
1047 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001048 }
1049
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001050 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001051 if (mBackgroundAlphaMultiplier != multiplier) {
1052 mBackgroundAlphaMultiplier = multiplier;
1053 invalidate();
1054 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001055 }
1056
Adam Cohenddb82192010-11-10 16:32:54 -08001057 public float getBackgroundAlphaMultiplier() {
1058 return mBackgroundAlphaMultiplier;
1059 }
1060
Michael Jurka5f1c5092010-09-03 14:15:02 -07001061 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001062 if (mBackgroundAlpha != alpha) {
1063 mBackgroundAlpha = alpha;
1064 invalidate();
1065 }
Michael Jurkadee05892010-07-27 10:01:56 -07001066 }
1067
Michael Jurkaa52570f2012-03-20 03:18:20 -07001068 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001069 final int childCount = getChildCount();
1070 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001071 getChildAt(i).setAlpha(alpha);
1072 }
1073 }
1074
Michael Jurkaa52570f2012-03-20 03:18:20 -07001075 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1076 if (getChildCount() > 0) {
1077 return (ShortcutAndWidgetContainer) getChildAt(0);
1078 }
1079 return null;
1080 }
1081
Patrick Dubroy440c3602010-07-13 17:50:32 -07001082 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001083 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001084 }
1085
Adam Cohen76fc0852011-06-17 13:26:23 -07001086 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001087 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001088 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001089 boolean[][] occupied = mOccupied;
1090 if (!permanent) {
1091 occupied = mTmpOccupied;
1092 }
1093
Adam Cohen19f37922012-03-21 11:59:11 -07001094 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001095 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1096 final ItemInfo info = (ItemInfo) child.getTag();
1097
1098 // We cancel any existing animations
1099 if (mReorderAnimators.containsKey(lp)) {
1100 mReorderAnimators.get(lp).cancel();
1101 mReorderAnimators.remove(lp);
1102 }
1103
Adam Cohen482ed822012-03-02 14:15:13 -08001104 final int oldX = lp.x;
1105 final int oldY = lp.y;
1106 if (adjustOccupied) {
1107 occupied[lp.cellX][lp.cellY] = false;
1108 occupied[cellX][cellY] = true;
1109 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001110 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001111 if (permanent) {
1112 lp.cellX = info.cellX = cellX;
1113 lp.cellY = info.cellY = cellY;
1114 } else {
1115 lp.tmpCellX = cellX;
1116 lp.tmpCellY = cellY;
1117 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001118 clc.setupLp(lp);
1119 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001120 final int newX = lp.x;
1121 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001122
Adam Cohen76fc0852011-06-17 13:26:23 -07001123 lp.x = oldX;
1124 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001125
Adam Cohen482ed822012-03-02 14:15:13 -08001126 // Exit early if we're not actually moving the view
1127 if (oldX == newX && oldY == newY) {
1128 lp.isLockedToGrid = true;
1129 return true;
1130 }
1131
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001132 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001133 va.setDuration(duration);
1134 mReorderAnimators.put(lp, va);
1135
1136 va.addUpdateListener(new AnimatorUpdateListener() {
1137 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001138 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001139 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001140 lp.x = (int) ((1 - r) * oldX + r * newX);
1141 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001142 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001143 }
1144 });
Adam Cohen482ed822012-03-02 14:15:13 -08001145 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001146 boolean cancelled = false;
1147 public void onAnimationEnd(Animator animation) {
1148 // If the animation was cancelled, it means that another animation
1149 // has interrupted this one, and we don't want to lock the item into
1150 // place just yet.
1151 if (!cancelled) {
1152 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001153 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001154 }
1155 if (mReorderAnimators.containsKey(lp)) {
1156 mReorderAnimators.remove(lp);
1157 }
1158 }
1159 public void onAnimationCancel(Animator animation) {
1160 cancelled = true;
1161 }
1162 });
Adam Cohen482ed822012-03-02 14:15:13 -08001163 va.setStartDelay(delay);
1164 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001165 return true;
1166 }
1167 return false;
1168 }
1169
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001170 /**
1171 * Estimate where the top left cell of the dragged item will land if it is dropped.
1172 *
1173 * @param originX The X value of the top left corner of the item
1174 * @param originY The Y value of the top left corner of the item
1175 * @param spanX The number of horizontal cells that the item spans
1176 * @param spanY The number of vertical cells that the item spans
1177 * @param result The estimated drop cell X and Y.
1178 */
1179 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001180 final int countX = mCountX;
1181 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001182
Michael Jurkaa63c4522010-08-19 13:52:27 -07001183 // pointToCellRounded takes the top left of a cell but will pad that with
1184 // cellWidth/2 and cellHeight/2 when finding the matching cell
1185 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001186
1187 // If the item isn't fully on this screen, snap to the edges
1188 int rightOverhang = result[0] + spanX - countX;
1189 if (rightOverhang > 0) {
1190 result[0] -= rightOverhang; // Snap to right
1191 }
1192 result[0] = Math.max(0, result[0]); // Snap to left
1193 int bottomOverhang = result[1] + spanY - countY;
1194 if (bottomOverhang > 0) {
1195 result[1] -= bottomOverhang; // Snap to bottom
1196 }
1197 result[1] = Math.max(0, result[1]); // Snap to top
1198 }
1199
Adam Cohen482ed822012-03-02 14:15:13 -08001200 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1201 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001202 final int oldDragCellX = mDragCell[0];
1203 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001204
Adam Cohen2801caf2011-05-13 20:57:39 -07001205 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001206 return;
1207 }
1208
Adam Cohen482ed822012-03-02 14:15:13 -08001209 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1210 mDragCell[0] = cellX;
1211 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001212 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001213 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001214 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001215
Joe Onorato4be866d2010-10-10 11:26:02 -07001216 int left = topLeft[0];
1217 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001218
Winson Chungb8c69f32011-10-19 21:36:08 -07001219 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001220 // When drawing the drag outline, it did not account for margin offsets
1221 // added by the view's parent.
1222 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1223 left += lp.leftMargin;
1224 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001225
Adam Cohen99e8b402011-03-25 19:23:43 -07001226 // Offsets due to the size difference between the View and the dragOutline.
1227 // There is a size difference to account for the outer blur, which may lie
1228 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001229 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001230 // We center about the x axis
1231 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1232 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001233 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001234 if (dragOffset != null && dragRegion != null) {
1235 // Center the drag region *horizontally* in the cell and apply a drag
1236 // outline offset
1237 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1238 - dragRegion.width()) / 2;
Winson Chung69737c32013-10-08 17:00:19 -07001239 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1240 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1241 top += dragOffset.y + cellPaddingY;
Winson Chungb8c69f32011-10-19 21:36:08 -07001242 } else {
1243 // Center the drag outline in the cell
1244 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1245 - dragOutline.getWidth()) / 2;
1246 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1247 - dragOutline.getHeight()) / 2;
1248 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001249 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001250 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001251 mDragOutlineAnims[oldIndex].animateOut();
1252 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001253 Rect r = mDragOutlines[mDragOutlineCurrent];
1254 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1255 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001256 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001257 }
Winson Chung150fbab2010-09-29 17:14:26 -07001258
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001259 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1260 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001261 }
1262 }
1263
Adam Cohene0310962011-04-18 16:15:31 -07001264 public void clearDragOutlines() {
1265 final int oldIndex = mDragOutlineCurrent;
1266 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001267 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001268 }
1269
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001271 * Find a vacant area that will fit the given bounds nearest the requested
1272 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001273 *
Romain Guy51afc022009-05-04 18:03:43 -07001274 * @param pixelX The X location at which you want to search for a vacant area.
1275 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001276 * @param spanX Horizontal span of the object.
1277 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001278 * @param result Array in which to place the result, or null (in which case a new array will
1279 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001280 * @return The X, Y cell of a vacant area that can contain this object,
1281 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001283 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1284 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001285 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001286 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001287
Michael Jurka6a1435d2010-09-27 17:35:12 -07001288 /**
1289 * Find a vacant area that will fit the given bounds nearest the requested
1290 * cell location. Uses Euclidean distance to score multiple vacant areas.
1291 *
1292 * @param pixelX The X location at which you want to search for a vacant area.
1293 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001294 * @param minSpanX The minimum horizontal span required
1295 * @param minSpanY The minimum vertical span required
1296 * @param spanX Horizontal span of the object.
1297 * @param spanY Vertical span of the object.
1298 * @param result Array in which to place the result, or null (in which case a new array will
1299 * be allocated)
1300 * @return The X, Y cell of a vacant area that can contain this object,
1301 * nearest the requested location.
1302 */
1303 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1304 int spanY, int[] result, int[] resultSpan) {
1305 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1306 result, resultSpan);
1307 }
1308
1309 /**
1310 * Find a vacant area that will fit the given bounds nearest the requested
1311 * cell location. Uses Euclidean distance to score multiple vacant areas.
1312 *
1313 * @param pixelX The X location at which you want to search for a vacant area.
1314 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001315 * @param spanX Horizontal span of the object.
1316 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001317 * @param ignoreOccupied If true, the result can be an occupied cell
1318 * @param result Array in which to place the result, or null (in which case a new array will
1319 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001320 * @return The X, Y cell of a vacant area that can contain this object,
1321 * nearest the requested location.
1322 */
Adam Cohendf035382011-04-11 17:22:04 -07001323 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1324 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001325 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001326 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001327 }
1328
1329 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1330 private void lazyInitTempRectStack() {
1331 if (mTempRectStack.isEmpty()) {
1332 for (int i = 0; i < mCountX * mCountY; i++) {
1333 mTempRectStack.push(new Rect());
1334 }
1335 }
1336 }
Adam Cohen482ed822012-03-02 14:15:13 -08001337
Adam Cohend41fbf52012-02-16 23:53:59 -08001338 private void recycleTempRects(Stack<Rect> used) {
1339 while (!used.isEmpty()) {
1340 mTempRectStack.push(used.pop());
1341 }
1342 }
1343
1344 /**
1345 * Find a vacant area that will fit the given bounds nearest the requested
1346 * cell location. Uses Euclidean distance to score multiple vacant areas.
1347 *
1348 * @param pixelX The X location at which you want to search for a vacant area.
1349 * @param pixelY The Y location at which you want to search for a vacant area.
1350 * @param minSpanX The minimum horizontal span required
1351 * @param minSpanY The minimum vertical span required
1352 * @param spanX Horizontal span of the object.
1353 * @param spanY Vertical span of the object.
1354 * @param ignoreOccupied If true, the result can be an occupied cell
1355 * @param result Array in which to place the result, or null (in which case a new array will
1356 * be allocated)
1357 * @return The X, Y cell of a vacant area that can contain this object,
1358 * nearest the requested location.
1359 */
1360 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001361 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1362 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001363 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001364 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001365 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001366
Adam Cohene3e27a82011-04-15 12:07:39 -07001367 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1368 // to the center of the item, but we are searching based on the top-left cell, so
1369 // we translate the point over to correspond to the top-left.
1370 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1371 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1372
Jeff Sharkey70864282009-04-07 21:08:40 -07001373 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001374 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001375 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001376 final Rect bestRect = new Rect(-1, -1, -1, -1);
1377 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001378
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001379 final int countX = mCountX;
1380 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001381
Adam Cohend41fbf52012-02-16 23:53:59 -08001382 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1383 spanX < minSpanX || spanY < minSpanY) {
1384 return bestXY;
1385 }
1386
1387 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001388 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001389 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1390 int ySize = -1;
1391 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001392 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001393 // First, let's see if this thing fits anywhere
1394 for (int i = 0; i < minSpanX; i++) {
1395 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001396 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001397 continue inner;
1398 }
Michael Jurkac28de512010-08-13 11:27:44 -07001399 }
1400 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001401 xSize = minSpanX;
1402 ySize = minSpanY;
1403
1404 // We know that the item will fit at _some_ acceptable size, now let's see
1405 // how big we can make it. We'll alternate between incrementing x and y spans
1406 // until we hit a limit.
1407 boolean incX = true;
1408 boolean hitMaxX = xSize >= spanX;
1409 boolean hitMaxY = ySize >= spanY;
1410 while (!(hitMaxX && hitMaxY)) {
1411 if (incX && !hitMaxX) {
1412 for (int j = 0; j < ySize; j++) {
1413 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1414 // We can't move out horizontally
1415 hitMaxX = true;
1416 }
1417 }
1418 if (!hitMaxX) {
1419 xSize++;
1420 }
1421 } else if (!hitMaxY) {
1422 for (int i = 0; i < xSize; i++) {
1423 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1424 // We can't move out vertically
1425 hitMaxY = true;
1426 }
1427 }
1428 if (!hitMaxY) {
1429 ySize++;
1430 }
1431 }
1432 hitMaxX |= xSize >= spanX;
1433 hitMaxY |= ySize >= spanY;
1434 incX = !incX;
1435 }
1436 incX = true;
1437 hitMaxX = xSize >= spanX;
1438 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001439 }
Winson Chung0be025d2011-05-23 17:45:09 -07001440 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001441 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442
Adam Cohend41fbf52012-02-16 23:53:59 -08001443 // We verify that the current rect is not a sub-rect of any of our previous
1444 // candidates. In this case, the current rect is disqualified in favour of the
1445 // containing rect.
1446 Rect currentRect = mTempRectStack.pop();
1447 currentRect.set(x, y, x + xSize, y + ySize);
1448 boolean contained = false;
1449 for (Rect r : validRegions) {
1450 if (r.contains(currentRect)) {
1451 contained = true;
1452 break;
1453 }
1454 }
1455 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001456 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1457 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001458
Adam Cohend41fbf52012-02-16 23:53:59 -08001459 if ((distance <= bestDistance && !contained) ||
1460 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001461 bestDistance = distance;
1462 bestXY[0] = x;
1463 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001464 if (resultSpan != null) {
1465 resultSpan[0] = xSize;
1466 resultSpan[1] = ySize;
1467 }
1468 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 }
1471 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001472 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001473 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474
Adam Cohenc0dcf592011-06-01 15:30:43 -07001475 // Return -1, -1 if no suitable location found
1476 if (bestDistance == Double.MAX_VALUE) {
1477 bestXY[0] = -1;
1478 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001479 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001480 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001481 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001483
Adam Cohen482ed822012-03-02 14:15:13 -08001484 /**
1485 * Find a vacant area that will fit the given bounds nearest the requested
1486 * cell location, and will also weigh in a suggested direction vector of the
1487 * desired location. This method computers distance based on unit grid distances,
1488 * not pixel distances.
1489 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001490 * @param cellX The X cell nearest to which you want to search for a vacant area.
1491 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001492 * @param spanX Horizontal span of the object.
1493 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001494 * @param direction The favored direction in which the views should move from x, y
1495 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1496 * matches exactly. Otherwise we find the best matching direction.
1497 * @param occoupied The array which represents which cells in the CellLayout are occupied
1498 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001499 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001500 * @param result Array in which to place the result, or null (in which case a new array will
1501 * be allocated)
1502 * @return The X, Y cell of a vacant area that can contain this object,
1503 * nearest the requested location.
1504 */
1505 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001506 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001507 // Keep track of best-scoring drop area
1508 final int[] bestXY = result != null ? result : new int[2];
1509 float bestDistance = Float.MAX_VALUE;
1510 int bestDirectionScore = Integer.MIN_VALUE;
1511
1512 final int countX = mCountX;
1513 final int countY = mCountY;
1514
1515 for (int y = 0; y < countY - (spanY - 1); y++) {
1516 inner:
1517 for (int x = 0; x < countX - (spanX - 1); x++) {
1518 // First, let's see if this thing fits anywhere
1519 for (int i = 0; i < spanX; i++) {
1520 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001521 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001522 continue inner;
1523 }
1524 }
1525 }
1526
1527 float distance = (float)
1528 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1529 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001530 computeDirectionVector(x - cellX, y - cellY, curDirection);
1531 // The direction score is just the dot product of the two candidate direction
1532 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001533 int curDirectionScore = direction[0] * curDirection[0] +
1534 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001535 boolean exactDirectionOnly = false;
1536 boolean directionMatches = direction[0] == curDirection[0] &&
1537 direction[0] == curDirection[0];
1538 if ((directionMatches || !exactDirectionOnly) &&
1539 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001540 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1541 bestDistance = distance;
1542 bestDirectionScore = curDirectionScore;
1543 bestXY[0] = x;
1544 bestXY[1] = y;
1545 }
1546 }
1547 }
1548
1549 // Return -1, -1 if no suitable location found
1550 if (bestDistance == Float.MAX_VALUE) {
1551 bestXY[0] = -1;
1552 bestXY[1] = -1;
1553 }
1554 return bestXY;
1555 }
1556
1557 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001558 int[] direction, ItemConfiguration currentState) {
1559 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001560 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001561 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001562 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1563
Adam Cohen8baab352012-03-20 17:39:21 -07001564 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001565
1566 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001567 c.x = mTempLocation[0];
1568 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001569 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001570 }
Adam Cohen8baab352012-03-20 17:39:21 -07001571 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001572 return success;
1573 }
1574
Adam Cohenf3900c22012-11-16 18:28:11 -08001575 /**
1576 * This helper class defines a cluster of views. It helps with defining complex edges
1577 * of the cluster and determining how those edges interact with other views. The edges
1578 * essentially define a fine-grained boundary around the cluster of views -- like a more
1579 * precise version of a bounding box.
1580 */
1581 private class ViewCluster {
1582 final static int LEFT = 0;
1583 final static int TOP = 1;
1584 final static int RIGHT = 2;
1585 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001586
Adam Cohenf3900c22012-11-16 18:28:11 -08001587 ArrayList<View> views;
1588 ItemConfiguration config;
1589 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001590
Adam Cohenf3900c22012-11-16 18:28:11 -08001591 int[] leftEdge = new int[mCountY];
1592 int[] rightEdge = new int[mCountY];
1593 int[] topEdge = new int[mCountX];
1594 int[] bottomEdge = new int[mCountX];
1595 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1596
1597 @SuppressWarnings("unchecked")
1598 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1599 this.views = (ArrayList<View>) views.clone();
1600 this.config = config;
1601 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001602 }
1603
Adam Cohenf3900c22012-11-16 18:28:11 -08001604 void resetEdges() {
1605 for (int i = 0; i < mCountX; i++) {
1606 topEdge[i] = -1;
1607 bottomEdge[i] = -1;
1608 }
1609 for (int i = 0; i < mCountY; i++) {
1610 leftEdge[i] = -1;
1611 rightEdge[i] = -1;
1612 }
1613 leftEdgeDirty = true;
1614 rightEdgeDirty = true;
1615 bottomEdgeDirty = true;
1616 topEdgeDirty = true;
1617 boundingRectDirty = true;
1618 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001619
Adam Cohenf3900c22012-11-16 18:28:11 -08001620 void computeEdge(int which, int[] edge) {
1621 int count = views.size();
1622 for (int i = 0; i < count; i++) {
1623 CellAndSpan cs = config.map.get(views.get(i));
1624 switch (which) {
1625 case LEFT:
1626 int left = cs.x;
1627 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1628 if (left < edge[j] || edge[j] < 0) {
1629 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001630 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001631 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001632 break;
1633 case RIGHT:
1634 int right = cs.x + cs.spanX;
1635 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1636 if (right > edge[j]) {
1637 edge[j] = right;
1638 }
1639 }
1640 break;
1641 case TOP:
1642 int top = cs.y;
1643 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1644 if (top < edge[j] || edge[j] < 0) {
1645 edge[j] = top;
1646 }
1647 }
1648 break;
1649 case BOTTOM:
1650 int bottom = cs.y + cs.spanY;
1651 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1652 if (bottom > edge[j]) {
1653 edge[j] = bottom;
1654 }
1655 }
1656 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001657 }
1658 }
1659 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001660
1661 boolean isViewTouchingEdge(View v, int whichEdge) {
1662 CellAndSpan cs = config.map.get(v);
1663
1664 int[] edge = getEdge(whichEdge);
1665
1666 switch (whichEdge) {
1667 case LEFT:
1668 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1669 if (edge[i] == cs.x + cs.spanX) {
1670 return true;
1671 }
1672 }
1673 break;
1674 case RIGHT:
1675 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1676 if (edge[i] == cs.x) {
1677 return true;
1678 }
1679 }
1680 break;
1681 case TOP:
1682 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1683 if (edge[i] == cs.y + cs.spanY) {
1684 return true;
1685 }
1686 }
1687 break;
1688 case BOTTOM:
1689 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1690 if (edge[i] == cs.y) {
1691 return true;
1692 }
1693 }
1694 break;
1695 }
1696 return false;
1697 }
1698
1699 void shift(int whichEdge, int delta) {
1700 for (View v: views) {
1701 CellAndSpan c = config.map.get(v);
1702 switch (whichEdge) {
1703 case LEFT:
1704 c.x -= delta;
1705 break;
1706 case RIGHT:
1707 c.x += delta;
1708 break;
1709 case TOP:
1710 c.y -= delta;
1711 break;
1712 case BOTTOM:
1713 default:
1714 c.y += delta;
1715 break;
1716 }
1717 }
1718 resetEdges();
1719 }
1720
1721 public void addView(View v) {
1722 views.add(v);
1723 resetEdges();
1724 }
1725
1726 public Rect getBoundingRect() {
1727 if (boundingRectDirty) {
1728 boolean first = true;
1729 for (View v: views) {
1730 CellAndSpan c = config.map.get(v);
1731 if (first) {
1732 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1733 first = false;
1734 } else {
1735 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1736 }
1737 }
1738 }
1739 return boundingRect;
1740 }
1741
1742 public int[] getEdge(int which) {
1743 switch (which) {
1744 case LEFT:
1745 return getLeftEdge();
1746 case RIGHT:
1747 return getRightEdge();
1748 case TOP:
1749 return getTopEdge();
1750 case BOTTOM:
1751 default:
1752 return getBottomEdge();
1753 }
1754 }
1755
1756 public int[] getLeftEdge() {
1757 if (leftEdgeDirty) {
1758 computeEdge(LEFT, leftEdge);
1759 }
1760 return leftEdge;
1761 }
1762
1763 public int[] getRightEdge() {
1764 if (rightEdgeDirty) {
1765 computeEdge(RIGHT, rightEdge);
1766 }
1767 return rightEdge;
1768 }
1769
1770 public int[] getTopEdge() {
1771 if (topEdgeDirty) {
1772 computeEdge(TOP, topEdge);
1773 }
1774 return topEdge;
1775 }
1776
1777 public int[] getBottomEdge() {
1778 if (bottomEdgeDirty) {
1779 computeEdge(BOTTOM, bottomEdge);
1780 }
1781 return bottomEdge;
1782 }
1783
1784 PositionComparator comparator = new PositionComparator();
1785 class PositionComparator implements Comparator<View> {
1786 int whichEdge = 0;
1787 public int compare(View left, View right) {
1788 CellAndSpan l = config.map.get(left);
1789 CellAndSpan r = config.map.get(right);
1790 switch (whichEdge) {
1791 case LEFT:
1792 return (r.x + r.spanX) - (l.x + l.spanX);
1793 case RIGHT:
1794 return l.x - r.x;
1795 case TOP:
1796 return (r.y + r.spanY) - (l.y + l.spanY);
1797 case BOTTOM:
1798 default:
1799 return l.y - r.y;
1800 }
1801 }
1802 }
1803
1804 public void sortConfigurationForEdgePush(int edge) {
1805 comparator.whichEdge = edge;
1806 Collections.sort(config.sortedViews, comparator);
1807 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001808 }
1809
Adam Cohenf3900c22012-11-16 18:28:11 -08001810 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1811 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001812
Adam Cohenf3900c22012-11-16 18:28:11 -08001813 ViewCluster cluster = new ViewCluster(views, currentState);
1814 Rect clusterRect = cluster.getBoundingRect();
1815 int whichEdge;
1816 int pushDistance;
1817 boolean fail = false;
1818
1819 // Determine the edge of the cluster that will be leading the push and how far
1820 // the cluster must be shifted.
1821 if (direction[0] < 0) {
1822 whichEdge = ViewCluster.LEFT;
1823 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001824 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001825 whichEdge = ViewCluster.RIGHT;
1826 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1827 } else if (direction[1] < 0) {
1828 whichEdge = ViewCluster.TOP;
1829 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1830 } else {
1831 whichEdge = ViewCluster.BOTTOM;
1832 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001833 }
1834
Adam Cohenf3900c22012-11-16 18:28:11 -08001835 // Break early for invalid push distance.
1836 if (pushDistance <= 0) {
1837 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001838 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001839
1840 // Mark the occupied state as false for the group of views we want to move.
1841 for (View v: views) {
1842 CellAndSpan c = currentState.map.get(v);
1843 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1844 }
1845
1846 // We save the current configuration -- if we fail to find a solution we will revert
1847 // to the initial state. The process of finding a solution modifies the configuration
1848 // in place, hence the need for revert in the failure case.
1849 currentState.save();
1850
1851 // The pushing algorithm is simplified by considering the views in the order in which
1852 // they would be pushed by the cluster. For example, if the cluster is leading with its
1853 // left edge, we consider sort the views by their right edge, from right to left.
1854 cluster.sortConfigurationForEdgePush(whichEdge);
1855
1856 while (pushDistance > 0 && !fail) {
1857 for (View v: currentState.sortedViews) {
1858 // For each view that isn't in the cluster, we see if the leading edge of the
1859 // cluster is contacting the edge of that view. If so, we add that view to the
1860 // cluster.
1861 if (!cluster.views.contains(v) && v != dragView) {
1862 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1863 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1864 if (!lp.canReorder) {
1865 // The push solution includes the all apps button, this is not viable.
1866 fail = true;
1867 break;
1868 }
1869 cluster.addView(v);
1870 CellAndSpan c = currentState.map.get(v);
1871
1872 // Adding view to cluster, mark it as not occupied.
1873 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1874 }
1875 }
1876 }
1877 pushDistance--;
1878
1879 // The cluster has been completed, now we move the whole thing over in the appropriate
1880 // direction.
1881 cluster.shift(whichEdge, 1);
1882 }
1883
1884 boolean foundSolution = false;
1885 clusterRect = cluster.getBoundingRect();
1886
1887 // Due to the nature of the algorithm, the only check required to verify a valid solution
1888 // is to ensure that completed shifted cluster lies completely within the cell layout.
1889 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1890 clusterRect.bottom <= mCountY) {
1891 foundSolution = true;
1892 } else {
1893 currentState.restore();
1894 }
1895
1896 // In either case, we set the occupied array as marked for the location of the views
1897 for (View v: cluster.views) {
1898 CellAndSpan c = currentState.map.get(v);
1899 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1900 }
1901
1902 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001903 }
1904
Adam Cohen482ed822012-03-02 14:15:13 -08001905 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001906 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001907 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001908
Adam Cohen8baab352012-03-20 17:39:21 -07001909 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001910 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001911 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001912 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001913 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001914 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001915 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001916 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001917 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001918 }
1919 }
Adam Cohen8baab352012-03-20 17:39:21 -07001920
Adam Cohen8baab352012-03-20 17:39:21 -07001921 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001922 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001923 CellAndSpan c = currentState.map.get(v);
1924 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1925 }
1926
Adam Cohen47a876d2012-03-19 13:21:41 -07001927 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1928 int top = boundingRect.top;
1929 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001930 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001931 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001932 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001933 CellAndSpan c = currentState.map.get(v);
1934 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001935 }
1936
Adam Cohen482ed822012-03-02 14:15:13 -08001937 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1938
Adam Cohenf3900c22012-11-16 18:28:11 -08001939 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1940 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001941
Adam Cohen8baab352012-03-20 17:39:21 -07001942 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001943 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001944 int deltaX = mTempLocation[0] - boundingRect.left;
1945 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001946 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001947 CellAndSpan c = currentState.map.get(v);
1948 c.x += deltaX;
1949 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001950 }
1951 success = true;
1952 }
Adam Cohen8baab352012-03-20 17:39:21 -07001953
1954 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001955 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001956 CellAndSpan c = currentState.map.get(v);
1957 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001958 }
1959 return success;
1960 }
1961
1962 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1963 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1964 }
1965
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001966 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1967 // to push items in each of the cardinal directions, in an order based on the direction vector
1968 // passed.
1969 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1970 int[] direction, View ignoreView, ItemConfiguration solution) {
1971 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001972 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001973 // separately in each of the components.
1974 int temp = direction[1];
1975 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001976
1977 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001978 ignoreView, solution)) {
1979 return true;
1980 }
1981 direction[1] = temp;
1982 temp = direction[0];
1983 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001984
1985 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001986 ignoreView, solution)) {
1987 return true;
1988 }
1989 // Revert the direction
1990 direction[0] = temp;
1991
1992 // Now we try pushing in each component of the opposite direction
1993 direction[0] *= -1;
1994 direction[1] *= -1;
1995 temp = direction[1];
1996 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001997 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001998 ignoreView, solution)) {
1999 return true;
2000 }
2001
2002 direction[1] = temp;
2003 temp = direction[0];
2004 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002005 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002006 ignoreView, solution)) {
2007 return true;
2008 }
2009 // revert the direction
2010 direction[0] = temp;
2011 direction[0] *= -1;
2012 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002013
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002014 } else {
2015 // If the direction vector has a single non-zero component, we push first in the
2016 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002017 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002018 ignoreView, solution)) {
2019 return true;
2020 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002021 // Then we try the opposite direction
2022 direction[0] *= -1;
2023 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002024 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002025 ignoreView, solution)) {
2026 return true;
2027 }
2028 // Switch the direction back
2029 direction[0] *= -1;
2030 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002031
2032 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002033 // to find a solution by pushing along the perpendicular axis.
2034
2035 // Swap the components
2036 int temp = direction[1];
2037 direction[1] = direction[0];
2038 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002039 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002040 ignoreView, solution)) {
2041 return true;
2042 }
2043
2044 // Then we try the opposite direction
2045 direction[0] *= -1;
2046 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002047 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002048 ignoreView, solution)) {
2049 return true;
2050 }
2051 // Switch the direction back
2052 direction[0] *= -1;
2053 direction[1] *= -1;
2054
2055 // Swap the components back
2056 temp = direction[1];
2057 direction[1] = direction[0];
2058 direction[0] = temp;
2059 }
2060 return false;
2061 }
2062
Adam Cohen482ed822012-03-02 14:15:13 -08002063 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002064 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002065 // Return early if get invalid cell positions
2066 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002067
Adam Cohen8baab352012-03-20 17:39:21 -07002068 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002069 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002070
Adam Cohen8baab352012-03-20 17:39:21 -07002071 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002072 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002073 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002074 if (c != null) {
2075 c.x = cellX;
2076 c.y = cellY;
2077 }
Adam Cohen482ed822012-03-02 14:15:13 -08002078 }
Adam Cohen482ed822012-03-02 14:15:13 -08002079 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2080 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002081 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002082 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002083 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002084 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002085 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002086 if (Rect.intersects(r0, r1)) {
2087 if (!lp.canReorder) {
2088 return false;
2089 }
2090 mIntersectingViews.add(child);
2091 }
2092 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002093
Winson Chung5f8afe62013-08-12 16:19:28 -07002094 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002095 // we try to find a solution such that no displaced item travels through another item
2096 // without also displacing that item.
2097 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002098 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002099 return true;
2100 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002101
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002102 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002103 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002104 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002105 return true;
2106 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002107
Adam Cohen482ed822012-03-02 14:15:13 -08002108 // Ok, they couldn't move as a block, let's move them individually
2109 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002110 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002111 return false;
2112 }
2113 }
2114 return true;
2115 }
2116
2117 /*
2118 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2119 * the provided point and the provided cell
2120 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002121 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002122 double angle = Math.atan(((float) deltaY) / deltaX);
2123
2124 result[0] = 0;
2125 result[1] = 0;
2126 if (Math.abs(Math.cos(angle)) > 0.5f) {
2127 result[0] = (int) Math.signum(deltaX);
2128 }
2129 if (Math.abs(Math.sin(angle)) > 0.5f) {
2130 result[1] = (int) Math.signum(deltaY);
2131 }
2132 }
2133
Adam Cohen8baab352012-03-20 17:39:21 -07002134 private void copyOccupiedArray(boolean[][] occupied) {
2135 for (int i = 0; i < mCountX; i++) {
2136 for (int j = 0; j < mCountY; j++) {
2137 occupied[i][j] = mOccupied[i][j];
2138 }
2139 }
2140 }
2141
Adam Cohen482ed822012-03-02 14:15:13 -08002142 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
2143 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002144 // Copy the current state into the solution. This solution will be manipulated as necessary.
2145 copyCurrentStateToSolution(solution, false);
2146 // Copy the current occupied array into the temporary occupied array. This array will be
2147 // manipulated as necessary to find a solution.
2148 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002149
2150 // We find the nearest cell into which we would place the dragged item, assuming there's
2151 // nothing in its way.
2152 int result[] = new int[2];
2153 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2154
2155 boolean success = false;
2156 // First we try the exact nearest position of the item being dragged,
2157 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002158 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2159 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002160
2161 if (!success) {
2162 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2163 // x, then 1 in y etc.
2164 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
2165 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
2166 dragView, false, solution);
2167 } else if (spanY > minSpanY) {
2168 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
2169 dragView, true, solution);
2170 }
2171 solution.isSolution = false;
2172 } else {
2173 solution.isSolution = true;
2174 solution.dragViewX = result[0];
2175 solution.dragViewY = result[1];
2176 solution.dragViewSpanX = spanX;
2177 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002178 }
2179 return solution;
2180 }
2181
2182 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002183 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002184 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002185 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002186 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002187 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002188 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002189 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002190 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002191 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002192 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002193 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002194 }
2195 }
2196
2197 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2198 for (int i = 0; i < mCountX; i++) {
2199 for (int j = 0; j < mCountY; j++) {
2200 mTmpOccupied[i][j] = false;
2201 }
2202 }
2203
Michael Jurkaa52570f2012-03-20 03:18:20 -07002204 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002205 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002206 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002207 if (child == dragView) continue;
2208 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002209 CellAndSpan c = solution.map.get(child);
2210 if (c != null) {
2211 lp.tmpCellX = c.x;
2212 lp.tmpCellY = c.y;
2213 lp.cellHSpan = c.spanX;
2214 lp.cellVSpan = c.spanY;
2215 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002216 }
2217 }
2218 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2219 solution.dragViewSpanY, mTmpOccupied, true);
2220 }
2221
2222 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2223 commitDragView) {
2224
2225 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2226 for (int i = 0; i < mCountX; i++) {
2227 for (int j = 0; j < mCountY; j++) {
2228 occupied[i][j] = false;
2229 }
2230 }
2231
Michael Jurkaa52570f2012-03-20 03:18:20 -07002232 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002233 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002234 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002235 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002236 CellAndSpan c = solution.map.get(child);
2237 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002238 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2239 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002240 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002241 }
2242 }
2243 if (commitDragView) {
2244 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2245 solution.dragViewSpanY, occupied, true);
2246 }
2247 }
2248
Adam Cohen19f37922012-03-21 11:59:11 -07002249 // This method starts or changes the reorder hint animations
2250 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2251 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002252 for (int i = 0; i < childCount; i++) {
2253 View child = mShortcutsAndWidgets.getChildAt(i);
2254 if (child == dragView) continue;
2255 CellAndSpan c = solution.map.get(child);
2256 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2257 if (c != null) {
2258 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2259 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002260 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002261 }
2262 }
2263 }
2264
2265 // Class which represents the reorder hint animations. These animations show that an item is
2266 // in a temporary state, and hint at where the item will return to.
2267 class ReorderHintAnimation {
2268 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002269 float finalDeltaX;
2270 float finalDeltaY;
2271 float initDeltaX;
2272 float initDeltaY;
2273 float finalScale;
2274 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002275 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002276 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002277
2278 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2279 int spanX, int spanY) {
2280 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2281 final int x0 = mTmpPoint[0];
2282 final int y0 = mTmpPoint[1];
2283 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2284 final int x1 = mTmpPoint[0];
2285 final int y1 = mTmpPoint[1];
2286 final int dX = x1 - x0;
2287 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002288 finalDeltaX = 0;
2289 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002290 if (dX == dY && dX == 0) {
2291 } else {
2292 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002293 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002294 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002295 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002296 } else {
2297 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002298 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002299 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002300 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002301 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002302 }
2303 }
Adam Cohend024f982012-05-23 18:26:45 -07002304 initDeltaX = child.getTranslationX();
2305 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002306 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002307 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002308 this.child = child;
2309 }
2310
Adam Cohend024f982012-05-23 18:26:45 -07002311 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002312 if (mShakeAnimators.containsKey(child)) {
2313 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002314 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002315 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002316 if (finalDeltaX == 0 && finalDeltaY == 0) {
2317 completeAnimationImmediately();
2318 return;
2319 }
Adam Cohen19f37922012-03-21 11:59:11 -07002320 }
Adam Cohend024f982012-05-23 18:26:45 -07002321 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002322 return;
2323 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002324 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002325 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002326 va.setRepeatMode(ValueAnimator.REVERSE);
2327 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002328 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002329 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002330 va.addUpdateListener(new AnimatorUpdateListener() {
2331 @Override
2332 public void onAnimationUpdate(ValueAnimator animation) {
2333 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002334 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2335 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002336 child.setTranslationX(x);
2337 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002338 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002339 child.setScaleX(s);
2340 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002341 }
2342 });
2343 va.addListener(new AnimatorListenerAdapter() {
2344 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002345 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002346 initDeltaX = 0;
2347 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002348 initScale = getChildrenScale();
Adam Cohen19f37922012-03-21 11:59:11 -07002349 }
2350 });
Adam Cohen19f37922012-03-21 11:59:11 -07002351 mShakeAnimators.put(child, this);
2352 va.start();
2353 }
2354
Adam Cohend024f982012-05-23 18:26:45 -07002355 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002356 if (a != null) {
2357 a.cancel();
2358 }
Adam Cohen19f37922012-03-21 11:59:11 -07002359 }
Adam Cohene7587d22012-05-24 18:50:02 -07002360
Brandon Keely50e6e562012-05-08 16:28:49 -07002361 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002362 if (a != null) {
2363 a.cancel();
2364 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002365
Michael Jurka2ecf9952012-06-18 12:52:28 -07002366 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002367 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002368 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002369 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2370 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002371 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2372 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002373 );
2374 s.setDuration(REORDER_ANIMATION_DURATION);
2375 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2376 s.start();
2377 }
Adam Cohen19f37922012-03-21 11:59:11 -07002378 }
2379
2380 private void completeAndClearReorderHintAnimations() {
2381 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002382 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002383 }
2384 mShakeAnimators.clear();
2385 }
2386
Adam Cohen482ed822012-03-02 14:15:13 -08002387 private void commitTempPlacement() {
2388 for (int i = 0; i < mCountX; i++) {
2389 for (int j = 0; j < mCountY; j++) {
2390 mOccupied[i][j] = mTmpOccupied[i][j];
2391 }
2392 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002393 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002394 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002395 View child = mShortcutsAndWidgets.getChildAt(i);
2396 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2397 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002398 // We do a null check here because the item info can be null in the case of the
2399 // AllApps button in the hotseat.
2400 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002401 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2402 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2403 info.requiresDbUpdate = true;
2404 }
Adam Cohen2acce882012-03-28 19:03:19 -07002405 info.cellX = lp.cellX = lp.tmpCellX;
2406 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002407 info.spanX = lp.cellHSpan;
2408 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002409 }
Adam Cohen482ed822012-03-02 14:15:13 -08002410 }
Adam Cohen2acce882012-03-28 19:03:19 -07002411 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002412 }
2413
2414 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002415 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002416 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002417 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002418 lp.useTmpCoords = useTempCoords;
2419 }
2420 }
2421
Adam Cohen482ed822012-03-02 14:15:13 -08002422 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2423 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2424 int[] result = new int[2];
2425 int[] resultSpan = new int[2];
2426 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2427 resultSpan);
2428 if (result[0] >= 0 && result[1] >= 0) {
2429 copyCurrentStateToSolution(solution, false);
2430 solution.dragViewX = result[0];
2431 solution.dragViewY = result[1];
2432 solution.dragViewSpanX = resultSpan[0];
2433 solution.dragViewSpanY = resultSpan[1];
2434 solution.isSolution = true;
2435 } else {
2436 solution.isSolution = false;
2437 }
2438 return solution;
2439 }
2440
2441 public void prepareChildForDrag(View child) {
2442 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002443 }
2444
Adam Cohen19f37922012-03-21 11:59:11 -07002445 /* This seems like it should be obvious and straight-forward, but when the direction vector
2446 needs to match with the notion of the dragView pushing other views, we have to employ
2447 a slightly more subtle notion of the direction vector. The question is what two points is
2448 the vector between? The center of the dragView and its desired destination? Not quite, as
2449 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2450 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2451 or right, which helps make pushing feel right.
2452 */
2453 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2454 int spanY, View dragView, int[] resultDirection) {
2455 int[] targetDestination = new int[2];
2456
2457 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2458 Rect dragRect = new Rect();
2459 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2460 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2461
2462 Rect dropRegionRect = new Rect();
2463 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2464 dragView, dropRegionRect, mIntersectingViews);
2465
2466 int dropRegionSpanX = dropRegionRect.width();
2467 int dropRegionSpanY = dropRegionRect.height();
2468
2469 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2470 dropRegionRect.height(), dropRegionRect);
2471
2472 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2473 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2474
2475 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2476 deltaX = 0;
2477 }
2478 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2479 deltaY = 0;
2480 }
2481
2482 if (deltaX == 0 && deltaY == 0) {
2483 // No idea what to do, give a random direction.
2484 resultDirection[0] = 1;
2485 resultDirection[1] = 0;
2486 } else {
2487 computeDirectionVector(deltaX, deltaY, resultDirection);
2488 }
2489 }
2490
2491 // For a given cell and span, fetch the set of views intersecting the region.
2492 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2493 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2494 if (boundingRect != null) {
2495 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2496 }
2497 intersectingViews.clear();
2498 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2499 Rect r1 = new Rect();
2500 final int count = mShortcutsAndWidgets.getChildCount();
2501 for (int i = 0; i < count; i++) {
2502 View child = mShortcutsAndWidgets.getChildAt(i);
2503 if (child == dragView) continue;
2504 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2505 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2506 if (Rect.intersects(r0, r1)) {
2507 mIntersectingViews.add(child);
2508 if (boundingRect != null) {
2509 boundingRect.union(r1);
2510 }
2511 }
2512 }
2513 }
2514
2515 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2516 View dragView, int[] result) {
2517 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2518 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2519 mIntersectingViews);
2520 return !mIntersectingViews.isEmpty();
2521 }
2522
2523 void revertTempState() {
2524 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2525 final int count = mShortcutsAndWidgets.getChildCount();
2526 for (int i = 0; i < count; i++) {
2527 View child = mShortcutsAndWidgets.getChildAt(i);
2528 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2529 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2530 lp.tmpCellX = lp.cellX;
2531 lp.tmpCellY = lp.cellY;
2532 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2533 0, false, false);
2534 }
2535 }
2536 completeAndClearReorderHintAnimations();
2537 setItemPlacementDirty(false);
2538 }
2539
Adam Cohenbebf0422012-04-11 18:06:28 -07002540 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2541 View dragView, int[] direction, boolean commit) {
2542 int[] pixelXY = new int[2];
2543 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2544
2545 // First we determine if things have moved enough to cause a different layout
2546 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2547 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2548
2549 setUseTempCoords(true);
2550 if (swapSolution != null && swapSolution.isSolution) {
2551 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2552 // committing anything or animating anything as we just want to determine if a solution
2553 // exists
2554 copySolutionToTempState(swapSolution, dragView);
2555 setItemPlacementDirty(true);
2556 animateItemsToSolution(swapSolution, dragView, commit);
2557
2558 if (commit) {
2559 commitTempPlacement();
2560 completeAndClearReorderHintAnimations();
2561 setItemPlacementDirty(false);
2562 } else {
2563 beginOrAdjustHintAnimations(swapSolution, dragView,
2564 REORDER_ANIMATION_DURATION);
2565 }
2566 mShortcutsAndWidgets.requestLayout();
2567 }
2568 return swapSolution.isSolution;
2569 }
2570
Adam Cohen482ed822012-03-02 14:15:13 -08002571 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2572 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002573 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002574 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002575
2576 if (resultSpan == null) {
2577 resultSpan = new int[2];
2578 }
2579
Adam Cohen19f37922012-03-21 11:59:11 -07002580 // When we are checking drop validity or actually dropping, we don't recompute the
2581 // direction vector, since we want the solution to match the preview, and it's possible
2582 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002583 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2584 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002585 mDirectionVector[0] = mPreviousReorderDirection[0];
2586 mDirectionVector[1] = mPreviousReorderDirection[1];
2587 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002588 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2589 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2590 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002591 }
2592 } else {
2593 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2594 mPreviousReorderDirection[0] = mDirectionVector[0];
2595 mPreviousReorderDirection[1] = mDirectionVector[1];
2596 }
2597
Adam Cohen482ed822012-03-02 14:15:13 -08002598 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2599 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2600
2601 // We attempt the approach which doesn't shuffle views at all
2602 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2603 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2604
2605 ItemConfiguration finalSolution = null;
2606 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2607 finalSolution = swapSolution;
2608 } else if (noShuffleSolution.isSolution) {
2609 finalSolution = noShuffleSolution;
2610 }
2611
2612 boolean foundSolution = true;
2613 if (!DESTRUCTIVE_REORDER) {
2614 setUseTempCoords(true);
2615 }
2616
2617 if (finalSolution != null) {
2618 result[0] = finalSolution.dragViewX;
2619 result[1] = finalSolution.dragViewY;
2620 resultSpan[0] = finalSolution.dragViewSpanX;
2621 resultSpan[1] = finalSolution.dragViewSpanY;
2622
2623 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2624 // committing anything or animating anything as we just want to determine if a solution
2625 // exists
2626 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2627 if (!DESTRUCTIVE_REORDER) {
2628 copySolutionToTempState(finalSolution, dragView);
2629 }
2630 setItemPlacementDirty(true);
2631 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2632
Adam Cohen19f37922012-03-21 11:59:11 -07002633 if (!DESTRUCTIVE_REORDER &&
2634 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002635 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002636 completeAndClearReorderHintAnimations();
2637 setItemPlacementDirty(false);
2638 } else {
2639 beginOrAdjustHintAnimations(finalSolution, dragView,
2640 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002641 }
2642 }
2643 } else {
2644 foundSolution = false;
2645 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2646 }
2647
2648 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2649 setUseTempCoords(false);
2650 }
Adam Cohen482ed822012-03-02 14:15:13 -08002651
Michael Jurkaa52570f2012-03-20 03:18:20 -07002652 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002653 return result;
2654 }
2655
Adam Cohen19f37922012-03-21 11:59:11 -07002656 void setItemPlacementDirty(boolean dirty) {
2657 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002658 }
Adam Cohen19f37922012-03-21 11:59:11 -07002659 boolean isItemPlacementDirty() {
2660 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002661 }
2662
2663 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002664 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002665 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2666 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohen482ed822012-03-02 14:15:13 -08002667 boolean isSolution = false;
2668 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2669
Adam Cohenf3900c22012-11-16 18:28:11 -08002670 void save() {
2671 // Copy current state into savedMap
2672 for (View v: map.keySet()) {
2673 map.get(v).copy(savedMap.get(v));
2674 }
2675 }
2676
2677 void restore() {
2678 // Restore current state from savedMap
2679 for (View v: savedMap.keySet()) {
2680 savedMap.get(v).copy(map.get(v));
2681 }
2682 }
2683
2684 void add(View v, CellAndSpan cs) {
2685 map.put(v, cs);
2686 savedMap.put(v, new CellAndSpan());
2687 sortedViews.add(v);
2688 }
2689
Adam Cohen482ed822012-03-02 14:15:13 -08002690 int area() {
2691 return dragViewSpanX * dragViewSpanY;
2692 }
Adam Cohen8baab352012-03-20 17:39:21 -07002693 }
2694
2695 private class CellAndSpan {
2696 int x, y;
2697 int spanX, spanY;
2698
Adam Cohenf3900c22012-11-16 18:28:11 -08002699 public CellAndSpan() {
2700 }
2701
2702 public void copy(CellAndSpan copy) {
2703 copy.x = x;
2704 copy.y = y;
2705 copy.spanX = spanX;
2706 copy.spanY = spanY;
2707 }
2708
Adam Cohen8baab352012-03-20 17:39:21 -07002709 public CellAndSpan(int x, int y, int spanX, int spanY) {
2710 this.x = x;
2711 this.y = y;
2712 this.spanX = spanX;
2713 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002714 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002715
2716 public String toString() {
2717 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2718 }
2719
Adam Cohen482ed822012-03-02 14:15:13 -08002720 }
2721
Adam Cohendf035382011-04-11 17:22:04 -07002722 /**
2723 * Find a vacant area that will fit the given bounds nearest the requested
2724 * cell location. Uses Euclidean distance to score multiple vacant areas.
2725 *
2726 * @param pixelX The X location at which you want to search for a vacant area.
2727 * @param pixelY The Y location at which you want to search for a vacant area.
2728 * @param spanX Horizontal span of the object.
2729 * @param spanY Vertical span of the object.
2730 * @param ignoreView Considers space occupied by this view as unoccupied
2731 * @param result Previously returned value to possibly recycle.
2732 * @return The X, Y cell of a vacant area that can contain this object,
2733 * nearest the requested location.
2734 */
2735 int[] findNearestVacantArea(
2736 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2737 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2738 }
2739
2740 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002741 * Find a vacant area that will fit the given bounds nearest the requested
2742 * cell location. Uses Euclidean distance to score multiple vacant areas.
2743 *
2744 * @param pixelX The X location at which you want to search for a vacant area.
2745 * @param pixelY The Y location at which you want to search for a vacant area.
2746 * @param minSpanX The minimum horizontal span required
2747 * @param minSpanY The minimum vertical span required
2748 * @param spanX Horizontal span of the object.
2749 * @param spanY Vertical span of the object.
2750 * @param ignoreView Considers space occupied by this view as unoccupied
2751 * @param result Previously returned value to possibly recycle.
2752 * @return The X, Y cell of a vacant area that can contain this object,
2753 * nearest the requested location.
2754 */
2755 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2756 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002757 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2758 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002759 }
2760
2761 /**
Adam Cohendf035382011-04-11 17:22:04 -07002762 * Find a starting cell position that will fit the given bounds nearest the requested
2763 * cell location. Uses Euclidean distance to score multiple vacant areas.
2764 *
2765 * @param pixelX The X location at which you want to search for a vacant area.
2766 * @param pixelY The Y location at which you want to search for a vacant area.
2767 * @param spanX Horizontal span of the object.
2768 * @param spanY Vertical span of the object.
2769 * @param ignoreView Considers space occupied by this view as unoccupied
2770 * @param result Previously returned value to possibly recycle.
2771 * @return The X, Y cell of a vacant area that can contain this object,
2772 * nearest the requested location.
2773 */
2774 int[] findNearestArea(
2775 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2776 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2777 }
2778
Michael Jurka0280c3b2010-09-17 15:00:07 -07002779 boolean existsEmptyCell() {
2780 return findCellForSpan(null, 1, 1);
2781 }
2782
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002783 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002784 * Finds the upper-left coordinate of the first rectangle in the grid that can
2785 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2786 * then this method will only return coordinates for rectangles that contain the cell
2787 * (intersectX, intersectY)
2788 *
2789 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2790 * can be found.
2791 * @param spanX The horizontal span of the cell we want to find.
2792 * @param spanY The vertical span of the cell we want to find.
2793 *
2794 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002795 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002796 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002797 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002798 }
2799
2800 /**
2801 * Like above, but ignores any cells occupied by the item "ignoreView"
2802 *
2803 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2804 * can be found.
2805 * @param spanX The horizontal span of the cell we want to find.
2806 * @param spanY The vertical span of the cell we want to find.
2807 * @param ignoreView The home screen item we should treat as not occupying any space
2808 * @return
2809 */
2810 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002811 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2812 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002813 }
2814
2815 /**
2816 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2817 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2818 *
2819 * @param spanX The horizontal span of the cell we want to find.
2820 * @param spanY The vertical span of the cell we want to find.
2821 * @param ignoreView The home screen item we should treat as not occupying any space
2822 * @param intersectX The X coordinate of the cell that we should try to overlap
2823 * @param intersectX The Y coordinate of the cell that we should try to overlap
2824 *
2825 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2826 */
2827 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2828 int intersectX, int intersectY) {
2829 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002830 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002831 }
2832
2833 /**
2834 * The superset of the above two methods
2835 */
2836 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002837 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002838 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002839 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002840
Michael Jurka28750fb2010-09-24 17:43:49 -07002841 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002842 while (true) {
2843 int startX = 0;
2844 if (intersectX >= 0) {
2845 startX = Math.max(startX, intersectX - (spanX - 1));
2846 }
2847 int endX = mCountX - (spanX - 1);
2848 if (intersectX >= 0) {
2849 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2850 }
2851 int startY = 0;
2852 if (intersectY >= 0) {
2853 startY = Math.max(startY, intersectY - (spanY - 1));
2854 }
2855 int endY = mCountY - (spanY - 1);
2856 if (intersectY >= 0) {
2857 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2858 }
2859
Winson Chungbbc60d82010-11-11 16:34:41 -08002860 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002861 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002862 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002863 for (int i = 0; i < spanX; i++) {
2864 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002865 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002866 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002867 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002868 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002869 continue inner;
2870 }
2871 }
2872 }
2873 if (cellXY != null) {
2874 cellXY[0] = x;
2875 cellXY[1] = y;
2876 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002877 foundCell = true;
2878 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002879 }
2880 }
2881 if (intersectX == -1 && intersectY == -1) {
2882 break;
2883 } else {
2884 // if we failed to find anything, try again but without any requirements of
2885 // intersecting
2886 intersectX = -1;
2887 intersectY = -1;
2888 continue;
2889 }
2890 }
2891
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002892 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002893 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002894 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002895 }
2896
2897 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002898 * A drag event has begun over this layout.
2899 * It may have begun over this layout (in which case onDragChild is called first),
2900 * or it may have begun on another layout.
2901 */
2902 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002903 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002904 mDragging = true;
2905 }
2906
2907 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002908 * Called when drag has left this CellLayout or has been completed (successfully or not)
2909 */
2910 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002911 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002912 // This can actually be called when we aren't in a drag, e.g. when adding a new
2913 // item to this layout via the customize drawer.
2914 // Guard against that case.
2915 if (mDragging) {
2916 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002917 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002918
2919 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002920 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002921 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2922 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002923 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002924 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002925 }
2926
2927 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002928 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002929 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002930 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 *
2932 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002933 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002934 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002935 if (child != null) {
2936 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002937 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002938 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002940 }
2941
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002942 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002943 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002944 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002945 * @param cellX X coordinate of upper left corner expressed as a cell position
2946 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002947 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002949 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002951 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002952 final int cellWidth = mCellWidth;
2953 final int cellHeight = mCellHeight;
2954 final int widthGap = mWidthGap;
2955 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002956
Winson Chung4b825dcd2011-06-19 12:41:22 -07002957 final int hStartPadding = getPaddingLeft();
2958 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002959
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002960 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2961 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2962
2963 int x = hStartPadding + cellX * (cellWidth + widthGap);
2964 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002965
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002966 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002970 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002972 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973 * @param width Width in pixels
2974 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002975 * @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 -08002976 */
Winson Chung66700732013-08-20 16:56:15 -07002977 public static int[] rectToCell(int width, int height, int[] result) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002978 LauncherAppState app = LauncherAppState.getInstance();
2979 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -07002980 Rect padding = grid.getWorkspacePadding(grid.isLandscape ?
2981 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
Winson Chung5f8afe62013-08-12 16:19:28 -07002982
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002983 // Always assume we're working with the smallest span to make sure we
2984 // reserve enough space in both orientations.
Winson Chung66700732013-08-20 16:56:15 -07002985 int parentWidth = grid.calculateCellWidth(grid.widthPx
2986 - padding.left - padding.right, (int) grid.numColumns);
2987 int parentHeight = grid.calculateCellHeight(grid.heightPx
2988 - padding.top - padding.bottom, (int) grid.numRows);
2989 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002990
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002991 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002992 int spanX = (int) Math.ceil(width / (float) smallerSize);
2993 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002994
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002995 if (result == null) {
2996 return new int[] { spanX, spanY };
2997 }
2998 result[0] = spanX;
2999 result[1] = spanY;
3000 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003001 }
3002
Michael Jurkaf12c75c2011-01-25 22:41:40 -08003003 public int[] cellSpansToSize(int hSpans, int vSpans) {
3004 int[] size = new int[2];
3005 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
3006 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
3007 return size;
3008 }
3009
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003010 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08003011 * Calculate the grid spans needed to fit given item
3012 */
3013 public void calculateSpans(ItemInfo info) {
3014 final int minWidth;
3015 final int minHeight;
3016
3017 if (info instanceof LauncherAppWidgetInfo) {
3018 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3019 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3020 } else if (info instanceof PendingAddWidgetInfo) {
3021 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3022 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3023 } else {
3024 // It's not a widget, so it must be 1x1
3025 info.spanX = info.spanY = 1;
3026 return;
3027 }
3028 int[] spans = rectToCell(minWidth, minHeight, null);
3029 info.spanX = spans[0];
3030 info.spanY = spans[1];
3031 }
3032
3033 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034 * Find the first vacant cell, if there is one.
3035 *
3036 * @param vacant Holds the x and y coordinate of the vacant cell
3037 * @param spanX Horizontal cell span.
3038 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003039 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040 * @return True if a vacant cell was found
3041 */
3042 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003043
Michael Jurka0280c3b2010-09-17 15:00:07 -07003044 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003045 }
3046
3047 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3048 int xCount, int yCount, boolean[][] occupied) {
3049
Adam Cohen2801caf2011-05-13 20:57:39 -07003050 for (int y = 0; y < yCount; y++) {
3051 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003052 boolean available = !occupied[x][y];
3053out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3054 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3055 available = available && !occupied[i][j];
3056 if (!available) break out;
3057 }
3058 }
3059
3060 if (available) {
3061 vacant[0] = x;
3062 vacant[1] = y;
3063 return true;
3064 }
3065 }
3066 }
3067
3068 return false;
3069 }
3070
Michael Jurka0280c3b2010-09-17 15:00:07 -07003071 private void clearOccupiedCells() {
3072 for (int x = 0; x < mCountX; x++) {
3073 for (int y = 0; y < mCountY; y++) {
3074 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003075 }
3076 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003078
Adam Cohend41fbf52012-02-16 23:53:59 -08003079 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003080 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003081 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003083
Adam Cohend4844c32011-02-18 19:25:06 -08003084 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003085 markCellsAsOccupiedForView(view, mOccupied);
3086 }
3087 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003088 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003089 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003090 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003091 }
3092
Adam Cohend4844c32011-02-18 19:25:06 -08003093 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003094 markCellsAsUnoccupiedForView(view, mOccupied);
3095 }
3096 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003097 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003098 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003099 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003100 }
3101
Adam Cohen482ed822012-03-02 14:15:13 -08003102 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3103 boolean value) {
3104 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003105 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3106 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003107 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108 }
3109 }
3110 }
3111
Adam Cohen2801caf2011-05-13 20:57:39 -07003112 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003113 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003114 (Math.max((mCountX - 1), 0) * mWidthGap);
3115 }
3116
3117 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003118 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003119 (Math.max((mCountY - 1), 0) * mHeightGap);
3120 }
3121
Michael Jurka66d72172011-04-12 16:29:25 -07003122 public boolean isOccupied(int x, int y) {
3123 if (x < mCountX && y < mCountY) {
3124 return mOccupied[x][y];
3125 } else {
3126 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3127 }
3128 }
3129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003130 @Override
3131 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3132 return new CellLayout.LayoutParams(getContext(), attrs);
3133 }
3134
3135 @Override
3136 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3137 return p instanceof CellLayout.LayoutParams;
3138 }
3139
3140 @Override
3141 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3142 return new CellLayout.LayoutParams(p);
3143 }
3144
Winson Chungaafa03c2010-06-11 17:34:16 -07003145 public static class CellLayoutAnimationController extends LayoutAnimationController {
3146 public CellLayoutAnimationController(Animation animation, float delay) {
3147 super(animation, delay);
3148 }
3149
3150 @Override
3151 protected long getDelayForView(View view) {
3152 return (int) (Math.random() * 150);
3153 }
3154 }
3155
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3157 /**
3158 * Horizontal location of the item in the grid.
3159 */
3160 @ViewDebug.ExportedProperty
3161 public int cellX;
3162
3163 /**
3164 * Vertical location of the item in the grid.
3165 */
3166 @ViewDebug.ExportedProperty
3167 public int cellY;
3168
3169 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003170 * Temporary horizontal location of the item in the grid during reorder
3171 */
3172 public int tmpCellX;
3173
3174 /**
3175 * Temporary vertical location of the item in the grid during reorder
3176 */
3177 public int tmpCellY;
3178
3179 /**
3180 * Indicates that the temporary coordinates should be used to layout the items
3181 */
3182 public boolean useTmpCoords;
3183
3184 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003185 * Number of cells spanned horizontally by the item.
3186 */
3187 @ViewDebug.ExportedProperty
3188 public int cellHSpan;
3189
3190 /**
3191 * Number of cells spanned vertically by the item.
3192 */
3193 @ViewDebug.ExportedProperty
3194 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003195
Adam Cohen1b607ed2011-03-03 17:26:50 -08003196 /**
3197 * Indicates whether the item will set its x, y, width and height parameters freely,
3198 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3199 */
Adam Cohend4844c32011-02-18 19:25:06 -08003200 public boolean isLockedToGrid = true;
3201
Adam Cohen482ed822012-03-02 14:15:13 -08003202 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07003203 * Indicates that this item should use the full extents of its parent.
3204 */
3205 public boolean isFullscreen = false;
3206
3207 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003208 * Indicates whether this item can be reordered. Always true except in the case of the
3209 * the AllApps button.
3210 */
3211 public boolean canReorder = true;
3212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213 // X coordinate of the view in the layout.
3214 @ViewDebug.ExportedProperty
3215 int x;
3216 // Y coordinate of the view in the layout.
3217 @ViewDebug.ExportedProperty
3218 int y;
3219
Romain Guy84f296c2009-11-04 15:00:44 -08003220 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003222 public LayoutParams(Context c, AttributeSet attrs) {
3223 super(c, attrs);
3224 cellHSpan = 1;
3225 cellVSpan = 1;
3226 }
3227
3228 public LayoutParams(ViewGroup.LayoutParams source) {
3229 super(source);
3230 cellHSpan = 1;
3231 cellVSpan = 1;
3232 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003233
3234 public LayoutParams(LayoutParams source) {
3235 super(source);
3236 this.cellX = source.cellX;
3237 this.cellY = source.cellY;
3238 this.cellHSpan = source.cellHSpan;
3239 this.cellVSpan = source.cellVSpan;
3240 }
3241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003242 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003243 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 this.cellX = cellX;
3245 this.cellY = cellY;
3246 this.cellHSpan = cellHSpan;
3247 this.cellVSpan = cellVSpan;
3248 }
3249
Adam Cohen2374abf2013-04-16 14:56:57 -07003250 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3251 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003252 if (isLockedToGrid) {
3253 final int myCellHSpan = cellHSpan;
3254 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003255 int myCellX = useTmpCoords ? tmpCellX : cellX;
3256 int myCellY = useTmpCoords ? tmpCellY : cellY;
3257
3258 if (invertHorizontally) {
3259 myCellX = colCount - myCellX - cellHSpan;
3260 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003261
Adam Cohend4844c32011-02-18 19:25:06 -08003262 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3263 leftMargin - rightMargin;
3264 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3265 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003266 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3267 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003268 }
3269 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003270
Winson Chungaafa03c2010-06-11 17:34:16 -07003271 public String toString() {
3272 return "(" + this.cellX + ", " + this.cellY + ")";
3273 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003274
3275 public void setWidth(int width) {
3276 this.width = width;
3277 }
3278
3279 public int getWidth() {
3280 return width;
3281 }
3282
3283 public void setHeight(int height) {
3284 this.height = height;
3285 }
3286
3287 public int getHeight() {
3288 return height;
3289 }
3290
3291 public void setX(int x) {
3292 this.x = x;
3293 }
3294
3295 public int getX() {
3296 return x;
3297 }
3298
3299 public void setY(int y) {
3300 this.y = y;
3301 }
3302
3303 public int getY() {
3304 return y;
3305 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003306 }
3307
Michael Jurka0280c3b2010-09-17 15:00:07 -07003308 // This class stores info for two purposes:
3309 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3310 // its spanX, spanY, and the screen it is on
3311 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3312 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3313 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003314 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003315 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003316 int cellX = -1;
3317 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003318 int spanX;
3319 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003320 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003321 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003322
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003323 @Override
3324 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003325 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3326 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003327 }
3328 }
Michael Jurkad771c962011-08-09 15:00:48 -07003329
3330 public boolean lastDownOnOccupiedCell() {
3331 return mLastDownOnOccupiedCell;
3332 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003333}