blob: 901f42bdfbfe002194098990ba76d8d5e76691c7 [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 Cohenb5ba0972011-09-07 18:02:31 -070098 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070099 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700100 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -0700101
Michael Jurka33945b22010-12-21 18:19:38 -0800102 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -0800103 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700104 private Drawable mOverScrollForegroundDrawable;
105 private Drawable mOverScrollLeft;
106 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700107 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700108 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700109 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700110
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700111 // These values allow a fixed measurement to be set on the CellLayout.
112 private int mFixedWidth = -1;
113 private int mFixedHeight = -1;
114
Michael Jurka33945b22010-12-21 18:19:38 -0800115 // If we're actively dragging something over this screen, mIsDragOverlapping is true
116 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700117 private final Point mDragCenter = new Point();
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
Winson Chung967289b2011-06-30 18:09:30 -0700217 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700218 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
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
Adam Cohen2801caf2011-05-13 20:57:39 -0700298 public void enableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700299 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_HARDWARE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700300 }
301
302 public void disableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700303 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700304 }
305
306 public void buildHardwareLayer() {
307 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700308 }
309
Adam Cohen307fe232012-08-16 17:55:58 -0700310 public float getChildrenScale() {
311 return mIsHotseat ? mHotseatScale : 1.0f;
312 }
313
Winson Chung5f8afe62013-08-12 16:19:28 -0700314 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700315 mFixedCellWidth = mCellWidth = width;
316 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700317 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
318 mCountX, mCountY);
319 }
320
Adam Cohen2801caf2011-05-13 20:57:39 -0700321 public void setGridSize(int x, int y) {
322 mCountX = x;
323 mCountY = y;
324 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800325 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700326 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700327 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700328 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700329 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700330 }
331
Adam Cohen2374abf2013-04-16 14:56:57 -0700332 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
333 public void setInvertIfRtl(boolean invert) {
334 mShortcutsAndWidgets.setInvertIfRtl(invert);
335 }
336
Patrick Dubroy96864c32011-03-10 17:17:23 -0800337 private void invalidateBubbleTextView(BubbleTextView icon) {
338 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700339 invalidate(icon.getLeft() + getPaddingLeft() - padding,
340 icon.getTop() + getPaddingTop() - padding,
341 icon.getRight() + getPaddingLeft() + padding,
342 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800343 }
344
Adam Cohenb5ba0972011-09-07 18:02:31 -0700345 void setOverScrollAmount(float r, boolean left) {
346 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
347 mOverScrollForegroundDrawable = mOverScrollLeft;
348 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
349 mOverScrollForegroundDrawable = mOverScrollRight;
350 }
351
352 mForegroundAlpha = (int) Math.round((r * 255));
353 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
354 invalidate();
355 }
356
Patrick Dubroy96864c32011-03-10 17:17:23 -0800357 void setPressedOrFocusedIcon(BubbleTextView icon) {
358 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
359 // requires an expanded clip rect (due to the glow's blur radius)
360 BubbleTextView oldIcon = mPressedOrFocusedIcon;
361 mPressedOrFocusedIcon = icon;
362 if (oldIcon != null) {
363 invalidateBubbleTextView(oldIcon);
364 }
365 if (mPressedOrFocusedIcon != null) {
366 invalidateBubbleTextView(mPressedOrFocusedIcon);
367 }
368 }
369
Michael Jurka33945b22010-12-21 18:19:38 -0800370 void setIsDragOverlapping(boolean isDragOverlapping) {
371 if (mIsDragOverlapping != isDragOverlapping) {
372 mIsDragOverlapping = isDragOverlapping;
Adam Cohendedbd962013-07-11 14:21:49 -0700373 setUseActiveGlowBackground(mIsDragOverlapping);
Michael Jurka33945b22010-12-21 18:19:38 -0800374 invalidate();
375 }
376 }
377
Adam Cohendedbd962013-07-11 14:21:49 -0700378 void setUseActiveGlowBackground(boolean use) {
379 mUseActiveGlowBackground = use;
380 }
381
Michael Jurka33945b22010-12-21 18:19:38 -0800382 boolean getIsDragOverlapping() {
383 return mIsDragOverlapping;
384 }
385
Adam Cohenebea84d2011-11-09 17:20:41 -0800386 protected void setOverscrollTransformsDirty(boolean dirty) {
387 mScrollingTransformsDirty = dirty;
388 }
389
390 protected void resetOverscrollTransforms() {
391 if (mScrollingTransformsDirty) {
392 setOverscrollTransformsDirty(false);
393 setTranslationX(0);
394 setRotationY(0);
395 // It doesn't matter if we pass true or false here, the important thing is that we
396 // pass 0, which results in the overscroll drawable not being drawn any more.
397 setOverScrollAmount(0, false);
398 setPivotX(getMeasuredWidth() / 2);
399 setPivotY(getMeasuredHeight() / 2);
400 }
401 }
402
Adam Cohen307fe232012-08-16 17:55:58 -0700403 public void scaleRect(Rect r, float scale) {
404 if (scale != 1.0f) {
405 r.left = (int) (r.left * scale + 0.5f);
406 r.top = (int) (r.top * scale + 0.5f);
407 r.right = (int) (r.right * scale + 0.5f);
408 r.bottom = (int) (r.bottom * scale + 0.5f);
409 }
410 }
411
412 Rect temp = new Rect();
413 void scaleRectAboutCenter(Rect in, Rect out, float scale) {
414 int cx = in.centerX();
415 int cy = in.centerY();
416 out.set(in);
417 out.offset(-cx, -cy);
418 scaleRect(out, scale);
419 out.offset(cx, cy);
420 }
421
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700422 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700423 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700424 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
425 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
426 // When we're small, we are either drawn normally or in the "accepts drops" state (during
427 // a drag). However, we also drag the mini hover background *over* one of those two
428 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700429 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700430 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800431
Adam Cohendedbd962013-07-11 14:21:49 -0700432 if (mUseActiveGlowBackground) {
Michael Jurka33945b22010-12-21 18:19:38 -0800433 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700434 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700435 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700436 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700437 }
Michael Jurka33945b22010-12-21 18:19:38 -0800438
439 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
440 bg.setBounds(mBackgroundRect);
441 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700442 }
Romain Guya6abce82009-11-10 02:54:41 -0800443
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700444 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700445 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700446 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700447 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800448 final Rect r = mDragOutlines[i];
Adam Cohen307fe232012-08-16 17:55:58 -0700449 scaleRectAboutCenter(r, temp, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700450 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700451 paint.setAlpha((int)(alpha + .5f));
Adam Cohen307fe232012-08-16 17:55:58 -0700452 canvas.drawBitmap(b, null, temp, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700453 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700454 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800455
456 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
457 // requires an expanded clip rect (due to the glow's blur radius)
458 if (mPressedOrFocusedIcon != null) {
459 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
460 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
461 if (b != null) {
462 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700463 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
464 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800465 null);
466 }
467 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700468
Adam Cohen482ed822012-03-02 14:15:13 -0800469 if (DEBUG_VISUALIZE_OCCUPIED) {
470 int[] pt = new int[2];
471 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700472 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800473 for (int i = 0; i < mCountX; i++) {
474 for (int j = 0; j < mCountY; j++) {
475 if (mOccupied[i][j]) {
476 cellToPoint(i, j, pt);
477 canvas.save();
478 canvas.translate(pt[0], pt[1]);
479 cd.draw(canvas);
480 canvas.restore();
481 }
482 }
483 }
484 }
485
Andrew Flynn850d2e72012-04-26 16:51:20 -0700486 int previewOffset = FolderRingAnimator.sPreviewSize;
487
Adam Cohen69ce2e52011-07-03 19:25:21 -0700488 // The folder outer / inner ring image(s)
Winson Chung5f8afe62013-08-12 16:19:28 -0700489 LauncherAppState app = LauncherAppState.getInstance();
490 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700491 for (int i = 0; i < mFolderOuterRings.size(); i++) {
492 FolderRingAnimator fra = mFolderOuterRings.get(i);
493
494 // Draw outer ring
495 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
Winson Chung5f8afe62013-08-12 16:19:28 -0700496 int width = (int) (fra.getOuterRingSize() * getChildrenScale());
Adam Cohen69ce2e52011-07-03 19:25:21 -0700497 int height = width;
498 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700499 View child = getChildAt(fra.mCellX, fra.mCellY);
Winson Chung89f97052013-09-20 11:32:26 -0700500 if (child != null) {
501 int centerX = mTempLocation[0] + mCellWidth / 2;
502 int centerY = mTempLocation[1] + previewOffset / 2 +
503 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700504
Winson Chung89f97052013-09-20 11:32:26 -0700505 canvas.save();
506 canvas.translate(centerX - width / 2, centerY - height / 2);
507 d.setBounds(0, 0, width, height);
508 d.draw(canvas);
509 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700510
Winson Chung89f97052013-09-20 11:32:26 -0700511 // Draw inner ring
512 d = FolderRingAnimator.sSharedInnerRingDrawable;
513 width = (int) (fra.getInnerRingSize() * getChildrenScale());
514 height = width;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700515
Winson Chung89f97052013-09-20 11:32:26 -0700516 centerX = mTempLocation[0] + mCellWidth / 2;
517 centerY = mTempLocation[1] + previewOffset / 2 +
518 child.getPaddingTop() + grid.folderBackgroundOffset;
519 canvas.save();
520 canvas.translate(centerX - width / 2, centerY - width / 2);
521 d.setBounds(0, 0, width, height);
522 d.draw(canvas);
523 canvas.restore();
524 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700525 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700526
527 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
528 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
529 int width = d.getIntrinsicWidth();
530 int height = d.getIntrinsicHeight();
531
532 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700533 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700534 if (child != null) {
535 int centerX = mTempLocation[0] + mCellWidth / 2;
536 int centerY = mTempLocation[1] + previewOffset / 2 +
537 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700538
Winson Chung89f97052013-09-20 11:32:26 -0700539 canvas.save();
540 canvas.translate(centerX - width / 2, centerY - width / 2);
541 d.setBounds(0, 0, width, height);
542 d.draw(canvas);
543 canvas.restore();
544 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700545 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700546 }
547
Adam Cohenb5ba0972011-09-07 18:02:31 -0700548 @Override
549 protected void dispatchDraw(Canvas canvas) {
550 super.dispatchDraw(canvas);
551 if (mForegroundAlpha > 0) {
552 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
553 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700554 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700555 mOverScrollForegroundDrawable.draw(canvas);
556 p.setXfermode(null);
557 }
558 }
559
Adam Cohen69ce2e52011-07-03 19:25:21 -0700560 public void showFolderAccept(FolderRingAnimator fra) {
561 mFolderOuterRings.add(fra);
562 }
563
564 public void hideFolderAccept(FolderRingAnimator fra) {
565 if (mFolderOuterRings.contains(fra)) {
566 mFolderOuterRings.remove(fra);
567 }
568 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700569 }
570
Adam Cohenc51934b2011-07-26 21:07:43 -0700571 public void setFolderLeaveBehindCell(int x, int y) {
572 mFolderLeaveBehindCell[0] = x;
573 mFolderLeaveBehindCell[1] = y;
574 invalidate();
575 }
576
577 public void clearFolderLeaveBehind() {
578 mFolderLeaveBehindCell[0] = -1;
579 mFolderLeaveBehindCell[1] = -1;
580 invalidate();
581 }
582
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700583 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700584 public boolean shouldDelayChildPressedState() {
585 return false;
586 }
587
Adam Cohen1462de32012-07-24 22:34:36 -0700588 public void restoreInstanceState(SparseArray<Parcelable> states) {
589 dispatchRestoreInstanceState(states);
590 }
591
Michael Jurkae6235dd2011-10-04 15:02:05 -0700592 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700593 public void cancelLongPress() {
594 super.cancelLongPress();
595
596 // Cancel long press for all children
597 final int count = getChildCount();
598 for (int i = 0; i < count; i++) {
599 final View child = getChildAt(i);
600 child.cancelLongPress();
601 }
602 }
603
Michael Jurkadee05892010-07-27 10:01:56 -0700604 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
605 mInterceptTouchListener = listener;
606 }
607
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700609 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 }
611
612 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700613 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 }
615
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800616 public void setIsHotseat(boolean isHotseat) {
617 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700618 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800619 }
620
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800621 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700622 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700623 final LayoutParams lp = params;
624
Andrew Flynnde38e422012-05-08 11:22:15 -0700625 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800626 if (child instanceof BubbleTextView) {
627 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700628 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800629 }
630
Adam Cohen307fe232012-08-16 17:55:58 -0700631 child.setScaleX(getChildrenScale());
632 child.setScaleY(getChildrenScale());
633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 // Generate an id for each view, this assumes we have at most 256x256 cells
635 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700636 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700637 // If the horizontal or vertical span is set to -1, it is taken to
638 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700639 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
640 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641
Winson Chungaafa03c2010-06-11 17:34:16 -0700642 child.setId(childId);
643
Michael Jurkaa52570f2012-03-20 03:18:20 -0700644 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700645
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700646 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700647
Winson Chungaafa03c2010-06-11 17:34:16 -0700648 return true;
649 }
650 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654 public void removeAllViews() {
655 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700656 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700657 }
658
659 @Override
660 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700661 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700662 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700663 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700664 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700665 }
666
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700667 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700668 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700669 }
670
Michael Jurka0280c3b2010-09-17 15:00:07 -0700671 @Override
672 public void removeView(View view) {
673 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700674 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700675 }
676
677 @Override
678 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700679 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
680 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700681 }
682
683 @Override
684 public void removeViewInLayout(View view) {
685 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700686 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700687 }
688
689 @Override
690 public void removeViews(int start, int count) {
691 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700692 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700693 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700694 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700695 }
696
697 @Override
698 public void removeViewsInLayout(int start, int count) {
699 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700700 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700701 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700702 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800703 }
704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 @Override
706 protected void onAttachedToWindow() {
707 super.onAttachedToWindow();
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 if (getParent() instanceof Workspace) {
709 Workspace workspace = (Workspace) getParent();
710 mCellInfo.screenId = workspace.getIdForScreen(this);
711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
713
Michael Jurkaaf442092010-06-10 17:01:57 -0700714 public void setTagToCellInfoForPoint(int touchX, int touchY) {
715 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800716 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 final int x = touchX + getScrollX();
718 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700719 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700720
721 boolean found = false;
722 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700723 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800724 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700725
Adam Cohen1b607ed2011-03-03 17:26:50 -0800726 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
727 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700728 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700729
Winson Chungeecf02d2012-03-02 17:14:58 -0800730 float scale = child.getScaleX();
731 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
732 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700733 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
734 // offset that by this CellLayout's padding to test an (x,y) point that is relative
735 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700736 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800737 frame.inset((int) (frame.width() * (1f - scale) / 2),
738 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700739
Michael Jurkaaf442092010-06-10 17:01:57 -0700740 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700741 cellInfo.cell = child;
742 cellInfo.cellX = lp.cellX;
743 cellInfo.cellY = lp.cellY;
744 cellInfo.spanX = lp.cellHSpan;
745 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700746 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700747 break;
748 }
749 }
750 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700751
Michael Jurkad771c962011-08-09 15:00:48 -0700752 mLastDownOnOccupiedCell = found;
753
Michael Jurkaaf442092010-06-10 17:01:57 -0700754 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700755 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700756 pointToCellExact(x, y, cellXY);
757
Michael Jurkaaf442092010-06-10 17:01:57 -0700758 cellInfo.cell = null;
759 cellInfo.cellX = cellXY[0];
760 cellInfo.cellY = cellXY[1];
761 cellInfo.spanX = 1;
762 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700763 }
764 setTag(cellInfo);
765 }
766
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 @Override
768 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700769 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
770 // even in the case where we return early. Not clearing here was causing bugs whereby on
771 // long-press we'd end up picking up an item from a previous drag operation.
772 final int action = ev.getAction();
773
774 if (action == MotionEvent.ACTION_DOWN) {
775 clearTagCellInfo();
776 }
777
Michael Jurkadee05892010-07-27 10:01:56 -0700778 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
779 return true;
780 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781
782 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700783 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800785
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 return false;
787 }
788
Adam Cohenc1997fd2011-08-15 18:26:39 -0700789 private void clearTagCellInfo() {
790 final CellInfo cellInfo = mCellInfo;
791 cellInfo.cell = null;
792 cellInfo.cellX = -1;
793 cellInfo.cellY = -1;
794 cellInfo.spanX = 0;
795 cellInfo.spanY = 0;
796 setTag(cellInfo);
797 }
798
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700800 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 }
802
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700803 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700804 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 * @param x X coordinate of the point
806 * @param y Y coordinate of the point
807 * @param result Array of 2 ints to hold the x and y coordinate of the cell
808 */
809 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700810 final int hStartPadding = getPaddingLeft();
811 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812
813 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
814 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
815
Adam Cohend22015c2010-07-26 22:02:18 -0700816 final int xAxis = mCountX;
817 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818
819 if (result[0] < 0) result[0] = 0;
820 if (result[0] >= xAxis) result[0] = xAxis - 1;
821 if (result[1] < 0) result[1] = 0;
822 if (result[1] >= yAxis) result[1] = yAxis - 1;
823 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700824
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 /**
826 * Given a point, return the cell that most closely encloses that point
827 * @param x X coordinate of the point
828 * @param y Y coordinate of the point
829 * @param result Array of 2 ints to hold the x and y coordinate of the cell
830 */
831 void pointToCellRounded(int x, int y, int[] result) {
832 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
833 }
834
835 /**
836 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700837 *
838 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700840 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 * @param result Array of 2 ints to hold the x and y coordinate of the point
842 */
843 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700844 final int hStartPadding = getPaddingLeft();
845 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846
847 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
848 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
849 }
850
Adam Cohene3e27a82011-04-15 12:07:39 -0700851 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800852 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700853 *
854 * @param cellX X coordinate of the cell
855 * @param cellY Y coordinate of the cell
856 *
857 * @param result Array of 2 ints to hold the x and y coordinate of the point
858 */
859 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700860 regionToCenterPoint(cellX, cellY, 1, 1, result);
861 }
862
863 /**
864 * Given a cell coordinate and span return the point that represents the center of the regio
865 *
866 * @param cellX X coordinate of the cell
867 * @param cellY Y coordinate of the cell
868 *
869 * @param result Array of 2 ints to hold the x and y coordinate of the point
870 */
871 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700872 final int hStartPadding = getPaddingLeft();
873 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700874 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
875 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
876 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
877 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700878 }
879
Adam Cohen19f37922012-03-21 11:59:11 -0700880 /**
881 * Given a cell coordinate and span fills out a corresponding pixel rect
882 *
883 * @param cellX X coordinate of the cell
884 * @param cellY Y coordinate of the cell
885 * @param result Rect in which to write the result
886 */
887 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
888 final int hStartPadding = getPaddingLeft();
889 final int vStartPadding = getPaddingTop();
890 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
891 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
892 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
893 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
894 }
895
Adam Cohen482ed822012-03-02 14:15:13 -0800896 public float getDistanceFromCell(float x, float y, int[] cell) {
897 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
898 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
899 Math.pow(y - mTmpPoint[1], 2));
900 return distance;
901 }
902
Romain Guy84f296c2009-11-04 15:00:44 -0800903 int getCellWidth() {
904 return mCellWidth;
905 }
906
907 int getCellHeight() {
908 return mCellHeight;
909 }
910
Adam Cohend4844c32011-02-18 19:25:06 -0800911 int getWidthGap() {
912 return mWidthGap;
913 }
914
915 int getHeightGap() {
916 return mHeightGap;
917 }
918
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700919 Rect getContentRect(Rect r) {
920 if (r == null) {
921 r = new Rect();
922 }
923 int left = getPaddingLeft();
924 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700925 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
926 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700927 r.set(left, top, right, bottom);
928 return r;
929 }
930
Winson Chung66700732013-08-20 16:56:15 -0700931 static void getMetrics(Rect metrics, int paddedMeasureWidth,
932 int paddedMeasureHeight, int countX, int countY) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700933 LauncherAppState app = LauncherAppState.getInstance();
934 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -0700935 metrics.set(grid.calculateCellWidth(paddedMeasureWidth, countX),
936 grid.calculateCellHeight(paddedMeasureHeight, countY), 0, 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700937 }
938
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700939 public void setFixedSize(int width, int height) {
940 mFixedWidth = width;
941 mFixedHeight = height;
942 }
943
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 @Override
945 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700946 LauncherAppState app = LauncherAppState.getInstance();
947 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
948
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700951 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
952 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung11a1a532013-09-13 11:14:45 -0700953 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
954 int cw = grid.calculateCellWidth(widthSize, mCountX);
955 int ch = grid.calculateCellHeight(heightSize, mCountY);
956 if (cw != mCellWidth || ch != mCellHeight) {
957 mCellWidth = cw;
958 mCellHeight = ch;
959 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
960 mHeightGap, mCountX, mCountY);
961 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700962 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700963
Winson Chung5f8afe62013-08-12 16:19:28 -0700964 int newWidth = widthSize;
965 int newHeight = heightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700966 if (mFixedWidth > 0 && mFixedHeight > 0) {
967 newWidth = mFixedWidth;
968 newHeight = mFixedHeight;
969 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
971 }
972
Adam Cohend22015c2010-07-26 22:02:18 -0700973 int numWidthGaps = mCountX - 1;
974 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975
Adam Cohen234c4cd2011-07-17 21:03:04 -0700976 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700977 int hSpace = widthSize - getPaddingLeft() - getPaddingRight();
978 int vSpace = heightSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700979 int hFreeSpace = hSpace - (mCountX * mCellWidth);
980 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700981 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
982 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700983 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
984 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700985 } else {
986 mWidthGap = mOriginalWidthGap;
987 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700988 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800989 int count = getChildCount();
Winson Chung5f8afe62013-08-12 16:19:28 -0700990 int maxWidth = 0;
991 int maxHeight = 0;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800992 for (int i = 0; i < count; i++) {
993 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700994 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
995 getPaddingRight(), MeasureSpec.EXACTLY);
996 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
997 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800998 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700999 maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
1000 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
Michael Jurka8c920dd2011-01-20 14:16:56 -08001001 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001002 setMeasuredDimension(maxWidth, maxHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 }
1004
1005 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001006 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001009 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001010 child.layout(getPaddingLeft(), getPaddingTop(),
1011 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 }
1013 }
1014
1015 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001016 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1017 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001018 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001019 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001020 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001021 }
1022
1023 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001025 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 }
1027
1028 @Override
1029 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001030 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 }
1032
Michael Jurka5f1c5092010-09-03 14:15:02 -07001033 public float getBackgroundAlpha() {
1034 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001035 }
1036
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001037 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001038 if (mBackgroundAlphaMultiplier != multiplier) {
1039 mBackgroundAlphaMultiplier = multiplier;
1040 invalidate();
1041 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001042 }
1043
Adam Cohenddb82192010-11-10 16:32:54 -08001044 public float getBackgroundAlphaMultiplier() {
1045 return mBackgroundAlphaMultiplier;
1046 }
1047
Michael Jurka5f1c5092010-09-03 14:15:02 -07001048 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001049 if (mBackgroundAlpha != alpha) {
1050 mBackgroundAlpha = alpha;
1051 invalidate();
1052 }
Michael Jurkadee05892010-07-27 10:01:56 -07001053 }
1054
Michael Jurkaa52570f2012-03-20 03:18:20 -07001055 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001056 final int childCount = getChildCount();
1057 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001058 getChildAt(i).setAlpha(alpha);
1059 }
1060 }
1061
Michael Jurkaa52570f2012-03-20 03:18:20 -07001062 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1063 if (getChildCount() > 0) {
1064 return (ShortcutAndWidgetContainer) getChildAt(0);
1065 }
1066 return null;
1067 }
1068
Patrick Dubroy440c3602010-07-13 17:50:32 -07001069 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001070 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001071 }
1072
Adam Cohen76fc0852011-06-17 13:26:23 -07001073 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001074 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001075 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001076 boolean[][] occupied = mOccupied;
1077 if (!permanent) {
1078 occupied = mTmpOccupied;
1079 }
1080
Adam Cohen19f37922012-03-21 11:59:11 -07001081 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001082 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1083 final ItemInfo info = (ItemInfo) child.getTag();
1084
1085 // We cancel any existing animations
1086 if (mReorderAnimators.containsKey(lp)) {
1087 mReorderAnimators.get(lp).cancel();
1088 mReorderAnimators.remove(lp);
1089 }
1090
Adam Cohen482ed822012-03-02 14:15:13 -08001091 final int oldX = lp.x;
1092 final int oldY = lp.y;
1093 if (adjustOccupied) {
1094 occupied[lp.cellX][lp.cellY] = false;
1095 occupied[cellX][cellY] = true;
1096 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001097 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001098 if (permanent) {
1099 lp.cellX = info.cellX = cellX;
1100 lp.cellY = info.cellY = cellY;
1101 } else {
1102 lp.tmpCellX = cellX;
1103 lp.tmpCellY = cellY;
1104 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001105 clc.setupLp(lp);
1106 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001107 final int newX = lp.x;
1108 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001109
Adam Cohen76fc0852011-06-17 13:26:23 -07001110 lp.x = oldX;
1111 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001112
Adam Cohen482ed822012-03-02 14:15:13 -08001113 // Exit early if we're not actually moving the view
1114 if (oldX == newX && oldY == newY) {
1115 lp.isLockedToGrid = true;
1116 return true;
1117 }
1118
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001119 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001120 va.setDuration(duration);
1121 mReorderAnimators.put(lp, va);
1122
1123 va.addUpdateListener(new AnimatorUpdateListener() {
1124 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001125 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001126 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001127 lp.x = (int) ((1 - r) * oldX + r * newX);
1128 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001129 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001130 }
1131 });
Adam Cohen482ed822012-03-02 14:15:13 -08001132 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001133 boolean cancelled = false;
1134 public void onAnimationEnd(Animator animation) {
1135 // If the animation was cancelled, it means that another animation
1136 // has interrupted this one, and we don't want to lock the item into
1137 // place just yet.
1138 if (!cancelled) {
1139 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001140 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001141 }
1142 if (mReorderAnimators.containsKey(lp)) {
1143 mReorderAnimators.remove(lp);
1144 }
1145 }
1146 public void onAnimationCancel(Animator animation) {
1147 cancelled = true;
1148 }
1149 });
Adam Cohen482ed822012-03-02 14:15:13 -08001150 va.setStartDelay(delay);
1151 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001152 return true;
1153 }
1154 return false;
1155 }
1156
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001157 /**
1158 * Estimate where the top left cell of the dragged item will land if it is dropped.
1159 *
1160 * @param originX The X value of the top left corner of the item
1161 * @param originY The Y value of the top left corner of the item
1162 * @param spanX The number of horizontal cells that the item spans
1163 * @param spanY The number of vertical cells that the item spans
1164 * @param result The estimated drop cell X and Y.
1165 */
1166 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001167 final int countX = mCountX;
1168 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001169
Michael Jurkaa63c4522010-08-19 13:52:27 -07001170 // pointToCellRounded takes the top left of a cell but will pad that with
1171 // cellWidth/2 and cellHeight/2 when finding the matching cell
1172 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001173
1174 // If the item isn't fully on this screen, snap to the edges
1175 int rightOverhang = result[0] + spanX - countX;
1176 if (rightOverhang > 0) {
1177 result[0] -= rightOverhang; // Snap to right
1178 }
1179 result[0] = Math.max(0, result[0]); // Snap to left
1180 int bottomOverhang = result[1] + spanY - countY;
1181 if (bottomOverhang > 0) {
1182 result[1] -= bottomOverhang; // Snap to bottom
1183 }
1184 result[1] = Math.max(0, result[1]); // Snap to top
1185 }
1186
Adam Cohen482ed822012-03-02 14:15:13 -08001187 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1188 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001189 final int oldDragCellX = mDragCell[0];
1190 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001191
Winson Chungb8c69f32011-10-19 21:36:08 -07001192 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001193 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1194 } else {
1195 mDragCenter.set(originX, originY);
1196 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001197
Adam Cohen2801caf2011-05-13 20:57:39 -07001198 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001199 return;
1200 }
1201
Adam Cohen482ed822012-03-02 14:15:13 -08001202 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1203 mDragCell[0] = cellX;
1204 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001205 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001206 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001207 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001208
Joe Onorato4be866d2010-10-10 11:26:02 -07001209 int left = topLeft[0];
1210 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001211
Winson Chung892c74d2013-08-22 16:15:50 -07001212 // Offset icons by their padding
1213 if (v instanceof BubbleTextView) {
1214 top += v.getPaddingTop();
1215 }
1216
Winson Chungb8c69f32011-10-19 21:36:08 -07001217 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001218 // When drawing the drag outline, it did not account for margin offsets
1219 // added by the view's parent.
1220 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1221 left += lp.leftMargin;
1222 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001223
Adam Cohen99e8b402011-03-25 19:23:43 -07001224 // Offsets due to the size difference between the View and the dragOutline.
1225 // There is a size difference to account for the outer blur, which may lie
1226 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001227 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001228 // We center about the x axis
1229 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1230 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001231 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001232 if (dragOffset != null && dragRegion != null) {
1233 // Center the drag region *horizontally* in the cell and apply a drag
1234 // outline offset
1235 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1236 - dragRegion.width()) / 2;
1237 top += dragOffset.y;
1238 } else {
1239 // Center the drag outline in the cell
1240 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1241 - dragOutline.getWidth()) / 2;
1242 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1243 - dragOutline.getHeight()) / 2;
1244 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001245 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001246 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001247 mDragOutlineAnims[oldIndex].animateOut();
1248 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001249 Rect r = mDragOutlines[mDragOutlineCurrent];
1250 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1251 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001252 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001253 }
Winson Chung150fbab2010-09-29 17:14:26 -07001254
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001255 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1256 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001257 }
1258 }
1259
Adam Cohene0310962011-04-18 16:15:31 -07001260 public void clearDragOutlines() {
1261 final int oldIndex = mDragOutlineCurrent;
1262 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001263 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001264 }
1265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001267 * Find a vacant area that will fit the given bounds nearest the requested
1268 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001269 *
Romain Guy51afc022009-05-04 18:03:43 -07001270 * @param pixelX The X location at which you want to search for a vacant area.
1271 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001272 * @param spanX Horizontal span of the object.
1273 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001274 * @param result Array in which to place the result, or null (in which case a new array will
1275 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001276 * @return The X, Y cell of a vacant area that can contain this object,
1277 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001279 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1280 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001281 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001282 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001283
Michael Jurka6a1435d2010-09-27 17:35:12 -07001284 /**
1285 * Find a vacant area that will fit the given bounds nearest the requested
1286 * cell location. Uses Euclidean distance to score multiple vacant areas.
1287 *
1288 * @param pixelX The X location at which you want to search for a vacant area.
1289 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001290 * @param minSpanX The minimum horizontal span required
1291 * @param minSpanY The minimum vertical span required
1292 * @param spanX Horizontal span of the object.
1293 * @param spanY Vertical span of the object.
1294 * @param result Array in which to place the result, or null (in which case a new array will
1295 * be allocated)
1296 * @return The X, Y cell of a vacant area that can contain this object,
1297 * nearest the requested location.
1298 */
1299 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1300 int spanY, int[] result, int[] resultSpan) {
1301 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1302 result, resultSpan);
1303 }
1304
1305 /**
1306 * Find a vacant area that will fit the given bounds nearest the requested
1307 * cell location. Uses Euclidean distance to score multiple vacant areas.
1308 *
1309 * @param pixelX The X location at which you want to search for a vacant area.
1310 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001311 * @param spanX Horizontal span of the object.
1312 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001313 * @param ignoreOccupied If true, the result can be an occupied cell
1314 * @param result Array in which to place the result, or null (in which case a new array will
1315 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001316 * @return The X, Y cell of a vacant area that can contain this object,
1317 * nearest the requested location.
1318 */
Adam Cohendf035382011-04-11 17:22:04 -07001319 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1320 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001321 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001322 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001323 }
1324
1325 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1326 private void lazyInitTempRectStack() {
1327 if (mTempRectStack.isEmpty()) {
1328 for (int i = 0; i < mCountX * mCountY; i++) {
1329 mTempRectStack.push(new Rect());
1330 }
1331 }
1332 }
Adam Cohen482ed822012-03-02 14:15:13 -08001333
Adam Cohend41fbf52012-02-16 23:53:59 -08001334 private void recycleTempRects(Stack<Rect> used) {
1335 while (!used.isEmpty()) {
1336 mTempRectStack.push(used.pop());
1337 }
1338 }
1339
1340 /**
1341 * Find a vacant area that will fit the given bounds nearest the requested
1342 * cell location. Uses Euclidean distance to score multiple vacant areas.
1343 *
1344 * @param pixelX The X location at which you want to search for a vacant area.
1345 * @param pixelY The Y location at which you want to search for a vacant area.
1346 * @param minSpanX The minimum horizontal span required
1347 * @param minSpanY The minimum vertical span required
1348 * @param spanX Horizontal span of the object.
1349 * @param spanY Vertical span of the object.
1350 * @param ignoreOccupied If true, the result can be an occupied cell
1351 * @param result Array in which to place the result, or null (in which case a new array will
1352 * be allocated)
1353 * @return The X, Y cell of a vacant area that can contain this object,
1354 * nearest the requested location.
1355 */
1356 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001357 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1358 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001359 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001360 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001361 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001362
Adam Cohene3e27a82011-04-15 12:07:39 -07001363 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1364 // to the center of the item, but we are searching based on the top-left cell, so
1365 // we translate the point over to correspond to the top-left.
1366 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1367 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1368
Jeff Sharkey70864282009-04-07 21:08:40 -07001369 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001370 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001371 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001372 final Rect bestRect = new Rect(-1, -1, -1, -1);
1373 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001374
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001375 final int countX = mCountX;
1376 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001377
Adam Cohend41fbf52012-02-16 23:53:59 -08001378 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1379 spanX < minSpanX || spanY < minSpanY) {
1380 return bestXY;
1381 }
1382
1383 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001384 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001385 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1386 int ySize = -1;
1387 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001388 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001389 // First, let's see if this thing fits anywhere
1390 for (int i = 0; i < minSpanX; i++) {
1391 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001392 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001393 continue inner;
1394 }
Michael Jurkac28de512010-08-13 11:27:44 -07001395 }
1396 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001397 xSize = minSpanX;
1398 ySize = minSpanY;
1399
1400 // We know that the item will fit at _some_ acceptable size, now let's see
1401 // how big we can make it. We'll alternate between incrementing x and y spans
1402 // until we hit a limit.
1403 boolean incX = true;
1404 boolean hitMaxX = xSize >= spanX;
1405 boolean hitMaxY = ySize >= spanY;
1406 while (!(hitMaxX && hitMaxY)) {
1407 if (incX && !hitMaxX) {
1408 for (int j = 0; j < ySize; j++) {
1409 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1410 // We can't move out horizontally
1411 hitMaxX = true;
1412 }
1413 }
1414 if (!hitMaxX) {
1415 xSize++;
1416 }
1417 } else if (!hitMaxY) {
1418 for (int i = 0; i < xSize; i++) {
1419 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1420 // We can't move out vertically
1421 hitMaxY = true;
1422 }
1423 }
1424 if (!hitMaxY) {
1425 ySize++;
1426 }
1427 }
1428 hitMaxX |= xSize >= spanX;
1429 hitMaxY |= ySize >= spanY;
1430 incX = !incX;
1431 }
1432 incX = true;
1433 hitMaxX = xSize >= spanX;
1434 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001435 }
Winson Chung0be025d2011-05-23 17:45:09 -07001436 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001437 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438
Adam Cohend41fbf52012-02-16 23:53:59 -08001439 // We verify that the current rect is not a sub-rect of any of our previous
1440 // candidates. In this case, the current rect is disqualified in favour of the
1441 // containing rect.
1442 Rect currentRect = mTempRectStack.pop();
1443 currentRect.set(x, y, x + xSize, y + ySize);
1444 boolean contained = false;
1445 for (Rect r : validRegions) {
1446 if (r.contains(currentRect)) {
1447 contained = true;
1448 break;
1449 }
1450 }
1451 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001452 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1453 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001454
Adam Cohend41fbf52012-02-16 23:53:59 -08001455 if ((distance <= bestDistance && !contained) ||
1456 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001457 bestDistance = distance;
1458 bestXY[0] = x;
1459 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001460 if (resultSpan != null) {
1461 resultSpan[0] = xSize;
1462 resultSpan[1] = ySize;
1463 }
1464 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 }
1467 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001468 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001469 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470
Adam Cohenc0dcf592011-06-01 15:30:43 -07001471 // Return -1, -1 if no suitable location found
1472 if (bestDistance == Double.MAX_VALUE) {
1473 bestXY[0] = -1;
1474 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001475 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001476 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001477 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001479
Adam Cohen482ed822012-03-02 14:15:13 -08001480 /**
1481 * Find a vacant area that will fit the given bounds nearest the requested
1482 * cell location, and will also weigh in a suggested direction vector of the
1483 * desired location. This method computers distance based on unit grid distances,
1484 * not pixel distances.
1485 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001486 * @param cellX The X cell nearest to which you want to search for a vacant area.
1487 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001488 * @param spanX Horizontal span of the object.
1489 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001490 * @param direction The favored direction in which the views should move from x, y
1491 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1492 * matches exactly. Otherwise we find the best matching direction.
1493 * @param occoupied The array which represents which cells in the CellLayout are occupied
1494 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001495 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001496 * @param result Array in which to place the result, or null (in which case a new array will
1497 * be allocated)
1498 * @return The X, Y cell of a vacant area that can contain this object,
1499 * nearest the requested location.
1500 */
1501 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001502 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001503 // Keep track of best-scoring drop area
1504 final int[] bestXY = result != null ? result : new int[2];
1505 float bestDistance = Float.MAX_VALUE;
1506 int bestDirectionScore = Integer.MIN_VALUE;
1507
1508 final int countX = mCountX;
1509 final int countY = mCountY;
1510
1511 for (int y = 0; y < countY - (spanY - 1); y++) {
1512 inner:
1513 for (int x = 0; x < countX - (spanX - 1); x++) {
1514 // First, let's see if this thing fits anywhere
1515 for (int i = 0; i < spanX; i++) {
1516 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001517 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001518 continue inner;
1519 }
1520 }
1521 }
1522
1523 float distance = (float)
1524 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1525 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001526 computeDirectionVector(x - cellX, y - cellY, curDirection);
1527 // The direction score is just the dot product of the two candidate direction
1528 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001529 int curDirectionScore = direction[0] * curDirection[0] +
1530 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001531 boolean exactDirectionOnly = false;
1532 boolean directionMatches = direction[0] == curDirection[0] &&
1533 direction[0] == curDirection[0];
1534 if ((directionMatches || !exactDirectionOnly) &&
1535 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001536 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1537 bestDistance = distance;
1538 bestDirectionScore = curDirectionScore;
1539 bestXY[0] = x;
1540 bestXY[1] = y;
1541 }
1542 }
1543 }
1544
1545 // Return -1, -1 if no suitable location found
1546 if (bestDistance == Float.MAX_VALUE) {
1547 bestXY[0] = -1;
1548 bestXY[1] = -1;
1549 }
1550 return bestXY;
1551 }
1552
1553 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001554 int[] direction, ItemConfiguration currentState) {
1555 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001556 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001557 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001558 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1559
Adam Cohen8baab352012-03-20 17:39:21 -07001560 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001561
1562 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001563 c.x = mTempLocation[0];
1564 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001565 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001566 }
Adam Cohen8baab352012-03-20 17:39:21 -07001567 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001568 return success;
1569 }
1570
Adam Cohenf3900c22012-11-16 18:28:11 -08001571 /**
1572 * This helper class defines a cluster of views. It helps with defining complex edges
1573 * of the cluster and determining how those edges interact with other views. The edges
1574 * essentially define a fine-grained boundary around the cluster of views -- like a more
1575 * precise version of a bounding box.
1576 */
1577 private class ViewCluster {
1578 final static int LEFT = 0;
1579 final static int TOP = 1;
1580 final static int RIGHT = 2;
1581 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001582
Adam Cohenf3900c22012-11-16 18:28:11 -08001583 ArrayList<View> views;
1584 ItemConfiguration config;
1585 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001586
Adam Cohenf3900c22012-11-16 18:28:11 -08001587 int[] leftEdge = new int[mCountY];
1588 int[] rightEdge = new int[mCountY];
1589 int[] topEdge = new int[mCountX];
1590 int[] bottomEdge = new int[mCountX];
1591 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1592
1593 @SuppressWarnings("unchecked")
1594 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1595 this.views = (ArrayList<View>) views.clone();
1596 this.config = config;
1597 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001598 }
1599
Adam Cohenf3900c22012-11-16 18:28:11 -08001600 void resetEdges() {
1601 for (int i = 0; i < mCountX; i++) {
1602 topEdge[i] = -1;
1603 bottomEdge[i] = -1;
1604 }
1605 for (int i = 0; i < mCountY; i++) {
1606 leftEdge[i] = -1;
1607 rightEdge[i] = -1;
1608 }
1609 leftEdgeDirty = true;
1610 rightEdgeDirty = true;
1611 bottomEdgeDirty = true;
1612 topEdgeDirty = true;
1613 boundingRectDirty = true;
1614 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001615
Adam Cohenf3900c22012-11-16 18:28:11 -08001616 void computeEdge(int which, int[] edge) {
1617 int count = views.size();
1618 for (int i = 0; i < count; i++) {
1619 CellAndSpan cs = config.map.get(views.get(i));
1620 switch (which) {
1621 case LEFT:
1622 int left = cs.x;
1623 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1624 if (left < edge[j] || edge[j] < 0) {
1625 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001626 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001627 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001628 break;
1629 case RIGHT:
1630 int right = cs.x + cs.spanX;
1631 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1632 if (right > edge[j]) {
1633 edge[j] = right;
1634 }
1635 }
1636 break;
1637 case TOP:
1638 int top = cs.y;
1639 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1640 if (top < edge[j] || edge[j] < 0) {
1641 edge[j] = top;
1642 }
1643 }
1644 break;
1645 case BOTTOM:
1646 int bottom = cs.y + cs.spanY;
1647 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1648 if (bottom > edge[j]) {
1649 edge[j] = bottom;
1650 }
1651 }
1652 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001653 }
1654 }
1655 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001656
1657 boolean isViewTouchingEdge(View v, int whichEdge) {
1658 CellAndSpan cs = config.map.get(v);
1659
1660 int[] edge = getEdge(whichEdge);
1661
1662 switch (whichEdge) {
1663 case LEFT:
1664 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1665 if (edge[i] == cs.x + cs.spanX) {
1666 return true;
1667 }
1668 }
1669 break;
1670 case RIGHT:
1671 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1672 if (edge[i] == cs.x) {
1673 return true;
1674 }
1675 }
1676 break;
1677 case TOP:
1678 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1679 if (edge[i] == cs.y + cs.spanY) {
1680 return true;
1681 }
1682 }
1683 break;
1684 case BOTTOM:
1685 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1686 if (edge[i] == cs.y) {
1687 return true;
1688 }
1689 }
1690 break;
1691 }
1692 return false;
1693 }
1694
1695 void shift(int whichEdge, int delta) {
1696 for (View v: views) {
1697 CellAndSpan c = config.map.get(v);
1698 switch (whichEdge) {
1699 case LEFT:
1700 c.x -= delta;
1701 break;
1702 case RIGHT:
1703 c.x += delta;
1704 break;
1705 case TOP:
1706 c.y -= delta;
1707 break;
1708 case BOTTOM:
1709 default:
1710 c.y += delta;
1711 break;
1712 }
1713 }
1714 resetEdges();
1715 }
1716
1717 public void addView(View v) {
1718 views.add(v);
1719 resetEdges();
1720 }
1721
1722 public Rect getBoundingRect() {
1723 if (boundingRectDirty) {
1724 boolean first = true;
1725 for (View v: views) {
1726 CellAndSpan c = config.map.get(v);
1727 if (first) {
1728 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1729 first = false;
1730 } else {
1731 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1732 }
1733 }
1734 }
1735 return boundingRect;
1736 }
1737
1738 public int[] getEdge(int which) {
1739 switch (which) {
1740 case LEFT:
1741 return getLeftEdge();
1742 case RIGHT:
1743 return getRightEdge();
1744 case TOP:
1745 return getTopEdge();
1746 case BOTTOM:
1747 default:
1748 return getBottomEdge();
1749 }
1750 }
1751
1752 public int[] getLeftEdge() {
1753 if (leftEdgeDirty) {
1754 computeEdge(LEFT, leftEdge);
1755 }
1756 return leftEdge;
1757 }
1758
1759 public int[] getRightEdge() {
1760 if (rightEdgeDirty) {
1761 computeEdge(RIGHT, rightEdge);
1762 }
1763 return rightEdge;
1764 }
1765
1766 public int[] getTopEdge() {
1767 if (topEdgeDirty) {
1768 computeEdge(TOP, topEdge);
1769 }
1770 return topEdge;
1771 }
1772
1773 public int[] getBottomEdge() {
1774 if (bottomEdgeDirty) {
1775 computeEdge(BOTTOM, bottomEdge);
1776 }
1777 return bottomEdge;
1778 }
1779
1780 PositionComparator comparator = new PositionComparator();
1781 class PositionComparator implements Comparator<View> {
1782 int whichEdge = 0;
1783 public int compare(View left, View right) {
1784 CellAndSpan l = config.map.get(left);
1785 CellAndSpan r = config.map.get(right);
1786 switch (whichEdge) {
1787 case LEFT:
1788 return (r.x + r.spanX) - (l.x + l.spanX);
1789 case RIGHT:
1790 return l.x - r.x;
1791 case TOP:
1792 return (r.y + r.spanY) - (l.y + l.spanY);
1793 case BOTTOM:
1794 default:
1795 return l.y - r.y;
1796 }
1797 }
1798 }
1799
1800 public void sortConfigurationForEdgePush(int edge) {
1801 comparator.whichEdge = edge;
1802 Collections.sort(config.sortedViews, comparator);
1803 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001804 }
1805
Adam Cohenf3900c22012-11-16 18:28:11 -08001806 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1807 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001808
Adam Cohenf3900c22012-11-16 18:28:11 -08001809 ViewCluster cluster = new ViewCluster(views, currentState);
1810 Rect clusterRect = cluster.getBoundingRect();
1811 int whichEdge;
1812 int pushDistance;
1813 boolean fail = false;
1814
1815 // Determine the edge of the cluster that will be leading the push and how far
1816 // the cluster must be shifted.
1817 if (direction[0] < 0) {
1818 whichEdge = ViewCluster.LEFT;
1819 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001820 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001821 whichEdge = ViewCluster.RIGHT;
1822 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1823 } else if (direction[1] < 0) {
1824 whichEdge = ViewCluster.TOP;
1825 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1826 } else {
1827 whichEdge = ViewCluster.BOTTOM;
1828 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001829 }
1830
Adam Cohenf3900c22012-11-16 18:28:11 -08001831 // Break early for invalid push distance.
1832 if (pushDistance <= 0) {
1833 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001834 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001835
1836 // Mark the occupied state as false for the group of views we want to move.
1837 for (View v: views) {
1838 CellAndSpan c = currentState.map.get(v);
1839 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1840 }
1841
1842 // We save the current configuration -- if we fail to find a solution we will revert
1843 // to the initial state. The process of finding a solution modifies the configuration
1844 // in place, hence the need for revert in the failure case.
1845 currentState.save();
1846
1847 // The pushing algorithm is simplified by considering the views in the order in which
1848 // they would be pushed by the cluster. For example, if the cluster is leading with its
1849 // left edge, we consider sort the views by their right edge, from right to left.
1850 cluster.sortConfigurationForEdgePush(whichEdge);
1851
1852 while (pushDistance > 0 && !fail) {
1853 for (View v: currentState.sortedViews) {
1854 // For each view that isn't in the cluster, we see if the leading edge of the
1855 // cluster is contacting the edge of that view. If so, we add that view to the
1856 // cluster.
1857 if (!cluster.views.contains(v) && v != dragView) {
1858 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1859 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1860 if (!lp.canReorder) {
1861 // The push solution includes the all apps button, this is not viable.
1862 fail = true;
1863 break;
1864 }
1865 cluster.addView(v);
1866 CellAndSpan c = currentState.map.get(v);
1867
1868 // Adding view to cluster, mark it as not occupied.
1869 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1870 }
1871 }
1872 }
1873 pushDistance--;
1874
1875 // The cluster has been completed, now we move the whole thing over in the appropriate
1876 // direction.
1877 cluster.shift(whichEdge, 1);
1878 }
1879
1880 boolean foundSolution = false;
1881 clusterRect = cluster.getBoundingRect();
1882
1883 // Due to the nature of the algorithm, the only check required to verify a valid solution
1884 // is to ensure that completed shifted cluster lies completely within the cell layout.
1885 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1886 clusterRect.bottom <= mCountY) {
1887 foundSolution = true;
1888 } else {
1889 currentState.restore();
1890 }
1891
1892 // In either case, we set the occupied array as marked for the location of the views
1893 for (View v: cluster.views) {
1894 CellAndSpan c = currentState.map.get(v);
1895 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1896 }
1897
1898 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001899 }
1900
Adam Cohen482ed822012-03-02 14:15:13 -08001901 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001902 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001903 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001904
Adam Cohen8baab352012-03-20 17:39:21 -07001905 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001906 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001907 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001908 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001909 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001910 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001911 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001912 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001913 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001914 }
1915 }
Adam Cohen8baab352012-03-20 17:39:21 -07001916
Adam Cohen8baab352012-03-20 17:39:21 -07001917 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001918 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001919 CellAndSpan c = currentState.map.get(v);
1920 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1921 }
1922
Adam Cohen47a876d2012-03-19 13:21:41 -07001923 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1924 int top = boundingRect.top;
1925 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001926 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001927 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001928 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001929 CellAndSpan c = currentState.map.get(v);
1930 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001931 }
1932
Adam Cohen482ed822012-03-02 14:15:13 -08001933 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1934
Adam Cohenf3900c22012-11-16 18:28:11 -08001935 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1936 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001937
Adam Cohen8baab352012-03-20 17:39:21 -07001938 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001939 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001940 int deltaX = mTempLocation[0] - boundingRect.left;
1941 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001942 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001943 CellAndSpan c = currentState.map.get(v);
1944 c.x += deltaX;
1945 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001946 }
1947 success = true;
1948 }
Adam Cohen8baab352012-03-20 17:39:21 -07001949
1950 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001951 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001952 CellAndSpan c = currentState.map.get(v);
1953 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001954 }
1955 return success;
1956 }
1957
1958 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1959 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1960 }
1961
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001962 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1963 // to push items in each of the cardinal directions, in an order based on the direction vector
1964 // passed.
1965 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1966 int[] direction, View ignoreView, ItemConfiguration solution) {
1967 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001968 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001969 // separately in each of the components.
1970 int temp = direction[1];
1971 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001972
1973 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001974 ignoreView, solution)) {
1975 return true;
1976 }
1977 direction[1] = temp;
1978 temp = direction[0];
1979 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001980
1981 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001982 ignoreView, solution)) {
1983 return true;
1984 }
1985 // Revert the direction
1986 direction[0] = temp;
1987
1988 // Now we try pushing in each component of the opposite direction
1989 direction[0] *= -1;
1990 direction[1] *= -1;
1991 temp = direction[1];
1992 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001993 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001994 ignoreView, solution)) {
1995 return true;
1996 }
1997
1998 direction[1] = temp;
1999 temp = direction[0];
2000 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002001 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002002 ignoreView, solution)) {
2003 return true;
2004 }
2005 // revert the direction
2006 direction[0] = temp;
2007 direction[0] *= -1;
2008 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002009
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002010 } else {
2011 // If the direction vector has a single non-zero component, we push first in the
2012 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002013 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002014 ignoreView, solution)) {
2015 return true;
2016 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002017 // Then we try the opposite direction
2018 direction[0] *= -1;
2019 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002020 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002021 ignoreView, solution)) {
2022 return true;
2023 }
2024 // Switch the direction back
2025 direction[0] *= -1;
2026 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002027
2028 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002029 // to find a solution by pushing along the perpendicular axis.
2030
2031 // Swap the components
2032 int temp = direction[1];
2033 direction[1] = direction[0];
2034 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002035 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002036 ignoreView, solution)) {
2037 return true;
2038 }
2039
2040 // Then we try the opposite direction
2041 direction[0] *= -1;
2042 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002043 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002044 ignoreView, solution)) {
2045 return true;
2046 }
2047 // Switch the direction back
2048 direction[0] *= -1;
2049 direction[1] *= -1;
2050
2051 // Swap the components back
2052 temp = direction[1];
2053 direction[1] = direction[0];
2054 direction[0] = temp;
2055 }
2056 return false;
2057 }
2058
Adam Cohen482ed822012-03-02 14:15:13 -08002059 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002060 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002061 // Return early if get invalid cell positions
2062 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002063
Adam Cohen8baab352012-03-20 17:39:21 -07002064 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002065 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002066
Adam Cohen8baab352012-03-20 17:39:21 -07002067 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002068 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002069 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002070 if (c != null) {
2071 c.x = cellX;
2072 c.y = cellY;
2073 }
Adam Cohen482ed822012-03-02 14:15:13 -08002074 }
Adam Cohen482ed822012-03-02 14:15:13 -08002075 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2076 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002077 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002078 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002079 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002080 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002081 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002082 if (Rect.intersects(r0, r1)) {
2083 if (!lp.canReorder) {
2084 return false;
2085 }
2086 mIntersectingViews.add(child);
2087 }
2088 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002089
Winson Chung5f8afe62013-08-12 16:19:28 -07002090 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002091 // we try to find a solution such that no displaced item travels through another item
2092 // without also displacing that item.
2093 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002094 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002095 return true;
2096 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002097
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002098 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002099 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002100 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002101 return true;
2102 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002103
Adam Cohen482ed822012-03-02 14:15:13 -08002104 // Ok, they couldn't move as a block, let's move them individually
2105 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002106 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002107 return false;
2108 }
2109 }
2110 return true;
2111 }
2112
2113 /*
2114 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2115 * the provided point and the provided cell
2116 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002117 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002118 double angle = Math.atan(((float) deltaY) / deltaX);
2119
2120 result[0] = 0;
2121 result[1] = 0;
2122 if (Math.abs(Math.cos(angle)) > 0.5f) {
2123 result[0] = (int) Math.signum(deltaX);
2124 }
2125 if (Math.abs(Math.sin(angle)) > 0.5f) {
2126 result[1] = (int) Math.signum(deltaY);
2127 }
2128 }
2129
Adam Cohen8baab352012-03-20 17:39:21 -07002130 private void copyOccupiedArray(boolean[][] occupied) {
2131 for (int i = 0; i < mCountX; i++) {
2132 for (int j = 0; j < mCountY; j++) {
2133 occupied[i][j] = mOccupied[i][j];
2134 }
2135 }
2136 }
2137
Adam Cohen482ed822012-03-02 14:15:13 -08002138 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
2139 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002140 // Copy the current state into the solution. This solution will be manipulated as necessary.
2141 copyCurrentStateToSolution(solution, false);
2142 // Copy the current occupied array into the temporary occupied array. This array will be
2143 // manipulated as necessary to find a solution.
2144 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002145
2146 // We find the nearest cell into which we would place the dragged item, assuming there's
2147 // nothing in its way.
2148 int result[] = new int[2];
2149 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2150
2151 boolean success = false;
2152 // First we try the exact nearest position of the item being dragged,
2153 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002154 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2155 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002156
2157 if (!success) {
2158 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2159 // x, then 1 in y etc.
2160 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
2161 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
2162 dragView, false, solution);
2163 } else if (spanY > minSpanY) {
2164 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
2165 dragView, true, solution);
2166 }
2167 solution.isSolution = false;
2168 } else {
2169 solution.isSolution = true;
2170 solution.dragViewX = result[0];
2171 solution.dragViewY = result[1];
2172 solution.dragViewSpanX = spanX;
2173 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002174 }
2175 return solution;
2176 }
2177
2178 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002179 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002180 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002181 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002182 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002183 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002184 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002185 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002186 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002187 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002188 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002189 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002190 }
2191 }
2192
2193 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2194 for (int i = 0; i < mCountX; i++) {
2195 for (int j = 0; j < mCountY; j++) {
2196 mTmpOccupied[i][j] = false;
2197 }
2198 }
2199
Michael Jurkaa52570f2012-03-20 03:18:20 -07002200 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002201 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002202 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002203 if (child == dragView) continue;
2204 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002205 CellAndSpan c = solution.map.get(child);
2206 if (c != null) {
2207 lp.tmpCellX = c.x;
2208 lp.tmpCellY = c.y;
2209 lp.cellHSpan = c.spanX;
2210 lp.cellVSpan = c.spanY;
2211 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002212 }
2213 }
2214 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2215 solution.dragViewSpanY, mTmpOccupied, true);
2216 }
2217
2218 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2219 commitDragView) {
2220
2221 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2222 for (int i = 0; i < mCountX; i++) {
2223 for (int j = 0; j < mCountY; j++) {
2224 occupied[i][j] = false;
2225 }
2226 }
2227
Michael Jurkaa52570f2012-03-20 03:18:20 -07002228 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002229 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002230 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002231 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002232 CellAndSpan c = solution.map.get(child);
2233 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002234 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2235 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002236 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002237 }
2238 }
2239 if (commitDragView) {
2240 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2241 solution.dragViewSpanY, occupied, true);
2242 }
2243 }
2244
Adam Cohen19f37922012-03-21 11:59:11 -07002245 // This method starts or changes the reorder hint animations
2246 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2247 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002248 for (int i = 0; i < childCount; i++) {
2249 View child = mShortcutsAndWidgets.getChildAt(i);
2250 if (child == dragView) continue;
2251 CellAndSpan c = solution.map.get(child);
2252 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2253 if (c != null) {
2254 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2255 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002256 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002257 }
2258 }
2259 }
2260
2261 // Class which represents the reorder hint animations. These animations show that an item is
2262 // in a temporary state, and hint at where the item will return to.
2263 class ReorderHintAnimation {
2264 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002265 float finalDeltaX;
2266 float finalDeltaY;
2267 float initDeltaX;
2268 float initDeltaY;
2269 float finalScale;
2270 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002271 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002272 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002273
2274 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2275 int spanX, int spanY) {
2276 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2277 final int x0 = mTmpPoint[0];
2278 final int y0 = mTmpPoint[1];
2279 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2280 final int x1 = mTmpPoint[0];
2281 final int y1 = mTmpPoint[1];
2282 final int dX = x1 - x0;
2283 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002284 finalDeltaX = 0;
2285 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002286 if (dX == dY && dX == 0) {
2287 } else {
2288 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002289 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002290 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002291 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002292 } else {
2293 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002294 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002295 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002296 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002297 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002298 }
2299 }
Adam Cohend024f982012-05-23 18:26:45 -07002300 initDeltaX = child.getTranslationX();
2301 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002302 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002303 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002304 this.child = child;
2305 }
2306
Adam Cohend024f982012-05-23 18:26:45 -07002307 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002308 if (mShakeAnimators.containsKey(child)) {
2309 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002310 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002311 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002312 if (finalDeltaX == 0 && finalDeltaY == 0) {
2313 completeAnimationImmediately();
2314 return;
2315 }
Adam Cohen19f37922012-03-21 11:59:11 -07002316 }
Adam Cohend024f982012-05-23 18:26:45 -07002317 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002318 return;
2319 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002320 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002321 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002322 va.setRepeatMode(ValueAnimator.REVERSE);
2323 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002324 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002325 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002326 va.addUpdateListener(new AnimatorUpdateListener() {
2327 @Override
2328 public void onAnimationUpdate(ValueAnimator animation) {
2329 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002330 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2331 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002332 child.setTranslationX(x);
2333 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002334 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002335 child.setScaleX(s);
2336 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002337 }
2338 });
2339 va.addListener(new AnimatorListenerAdapter() {
2340 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002341 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002342 initDeltaX = 0;
2343 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002344 initScale = getChildrenScale();
Adam Cohen19f37922012-03-21 11:59:11 -07002345 }
2346 });
Adam Cohen19f37922012-03-21 11:59:11 -07002347 mShakeAnimators.put(child, this);
2348 va.start();
2349 }
2350
Adam Cohend024f982012-05-23 18:26:45 -07002351 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002352 if (a != null) {
2353 a.cancel();
2354 }
Adam Cohen19f37922012-03-21 11:59:11 -07002355 }
Adam Cohene7587d22012-05-24 18:50:02 -07002356
Brandon Keely50e6e562012-05-08 16:28:49 -07002357 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002358 if (a != null) {
2359 a.cancel();
2360 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002361
Michael Jurka2ecf9952012-06-18 12:52:28 -07002362 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002363 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002364 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002365 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2366 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002367 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2368 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002369 );
2370 s.setDuration(REORDER_ANIMATION_DURATION);
2371 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2372 s.start();
2373 }
Adam Cohen19f37922012-03-21 11:59:11 -07002374 }
2375
2376 private void completeAndClearReorderHintAnimations() {
2377 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002378 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002379 }
2380 mShakeAnimators.clear();
2381 }
2382
Adam Cohen482ed822012-03-02 14:15:13 -08002383 private void commitTempPlacement() {
2384 for (int i = 0; i < mCountX; i++) {
2385 for (int j = 0; j < mCountY; j++) {
2386 mOccupied[i][j] = mTmpOccupied[i][j];
2387 }
2388 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002389 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002390 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002391 View child = mShortcutsAndWidgets.getChildAt(i);
2392 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2393 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002394 // We do a null check here because the item info can be null in the case of the
2395 // AllApps button in the hotseat.
2396 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002397 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2398 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2399 info.requiresDbUpdate = true;
2400 }
Adam Cohen2acce882012-03-28 19:03:19 -07002401 info.cellX = lp.cellX = lp.tmpCellX;
2402 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002403 info.spanX = lp.cellHSpan;
2404 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002405 }
Adam Cohen482ed822012-03-02 14:15:13 -08002406 }
Adam Cohen2acce882012-03-28 19:03:19 -07002407 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002408 }
2409
2410 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002411 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002412 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002413 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002414 lp.useTmpCoords = useTempCoords;
2415 }
2416 }
2417
Adam Cohen482ed822012-03-02 14:15:13 -08002418 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2419 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2420 int[] result = new int[2];
2421 int[] resultSpan = new int[2];
2422 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2423 resultSpan);
2424 if (result[0] >= 0 && result[1] >= 0) {
2425 copyCurrentStateToSolution(solution, false);
2426 solution.dragViewX = result[0];
2427 solution.dragViewY = result[1];
2428 solution.dragViewSpanX = resultSpan[0];
2429 solution.dragViewSpanY = resultSpan[1];
2430 solution.isSolution = true;
2431 } else {
2432 solution.isSolution = false;
2433 }
2434 return solution;
2435 }
2436
2437 public void prepareChildForDrag(View child) {
2438 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002439 }
2440
Adam Cohen19f37922012-03-21 11:59:11 -07002441 /* This seems like it should be obvious and straight-forward, but when the direction vector
2442 needs to match with the notion of the dragView pushing other views, we have to employ
2443 a slightly more subtle notion of the direction vector. The question is what two points is
2444 the vector between? The center of the dragView and its desired destination? Not quite, as
2445 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2446 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2447 or right, which helps make pushing feel right.
2448 */
2449 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2450 int spanY, View dragView, int[] resultDirection) {
2451 int[] targetDestination = new int[2];
2452
2453 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2454 Rect dragRect = new Rect();
2455 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2456 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2457
2458 Rect dropRegionRect = new Rect();
2459 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2460 dragView, dropRegionRect, mIntersectingViews);
2461
2462 int dropRegionSpanX = dropRegionRect.width();
2463 int dropRegionSpanY = dropRegionRect.height();
2464
2465 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2466 dropRegionRect.height(), dropRegionRect);
2467
2468 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2469 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2470
2471 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2472 deltaX = 0;
2473 }
2474 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2475 deltaY = 0;
2476 }
2477
2478 if (deltaX == 0 && deltaY == 0) {
2479 // No idea what to do, give a random direction.
2480 resultDirection[0] = 1;
2481 resultDirection[1] = 0;
2482 } else {
2483 computeDirectionVector(deltaX, deltaY, resultDirection);
2484 }
2485 }
2486
2487 // For a given cell and span, fetch the set of views intersecting the region.
2488 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2489 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2490 if (boundingRect != null) {
2491 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2492 }
2493 intersectingViews.clear();
2494 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2495 Rect r1 = new Rect();
2496 final int count = mShortcutsAndWidgets.getChildCount();
2497 for (int i = 0; i < count; i++) {
2498 View child = mShortcutsAndWidgets.getChildAt(i);
2499 if (child == dragView) continue;
2500 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2501 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2502 if (Rect.intersects(r0, r1)) {
2503 mIntersectingViews.add(child);
2504 if (boundingRect != null) {
2505 boundingRect.union(r1);
2506 }
2507 }
2508 }
2509 }
2510
2511 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2512 View dragView, int[] result) {
2513 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2514 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2515 mIntersectingViews);
2516 return !mIntersectingViews.isEmpty();
2517 }
2518
2519 void revertTempState() {
2520 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2521 final int count = mShortcutsAndWidgets.getChildCount();
2522 for (int i = 0; i < count; i++) {
2523 View child = mShortcutsAndWidgets.getChildAt(i);
2524 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2525 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2526 lp.tmpCellX = lp.cellX;
2527 lp.tmpCellY = lp.cellY;
2528 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2529 0, false, false);
2530 }
2531 }
2532 completeAndClearReorderHintAnimations();
2533 setItemPlacementDirty(false);
2534 }
2535
Adam Cohenbebf0422012-04-11 18:06:28 -07002536 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2537 View dragView, int[] direction, boolean commit) {
2538 int[] pixelXY = new int[2];
2539 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2540
2541 // First we determine if things have moved enough to cause a different layout
2542 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2543 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2544
2545 setUseTempCoords(true);
2546 if (swapSolution != null && swapSolution.isSolution) {
2547 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2548 // committing anything or animating anything as we just want to determine if a solution
2549 // exists
2550 copySolutionToTempState(swapSolution, dragView);
2551 setItemPlacementDirty(true);
2552 animateItemsToSolution(swapSolution, dragView, commit);
2553
2554 if (commit) {
2555 commitTempPlacement();
2556 completeAndClearReorderHintAnimations();
2557 setItemPlacementDirty(false);
2558 } else {
2559 beginOrAdjustHintAnimations(swapSolution, dragView,
2560 REORDER_ANIMATION_DURATION);
2561 }
2562 mShortcutsAndWidgets.requestLayout();
2563 }
2564 return swapSolution.isSolution;
2565 }
2566
Adam Cohen482ed822012-03-02 14:15:13 -08002567 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2568 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002569 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002570 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002571
2572 if (resultSpan == null) {
2573 resultSpan = new int[2];
2574 }
2575
Adam Cohen19f37922012-03-21 11:59:11 -07002576 // When we are checking drop validity or actually dropping, we don't recompute the
2577 // direction vector, since we want the solution to match the preview, and it's possible
2578 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002579 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2580 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002581 mDirectionVector[0] = mPreviousReorderDirection[0];
2582 mDirectionVector[1] = mPreviousReorderDirection[1];
2583 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002584 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2585 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2586 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002587 }
2588 } else {
2589 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2590 mPreviousReorderDirection[0] = mDirectionVector[0];
2591 mPreviousReorderDirection[1] = mDirectionVector[1];
2592 }
2593
Adam Cohen482ed822012-03-02 14:15:13 -08002594 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2595 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2596
2597 // We attempt the approach which doesn't shuffle views at all
2598 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2599 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2600
2601 ItemConfiguration finalSolution = null;
2602 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2603 finalSolution = swapSolution;
2604 } else if (noShuffleSolution.isSolution) {
2605 finalSolution = noShuffleSolution;
2606 }
2607
2608 boolean foundSolution = true;
2609 if (!DESTRUCTIVE_REORDER) {
2610 setUseTempCoords(true);
2611 }
2612
2613 if (finalSolution != null) {
2614 result[0] = finalSolution.dragViewX;
2615 result[1] = finalSolution.dragViewY;
2616 resultSpan[0] = finalSolution.dragViewSpanX;
2617 resultSpan[1] = finalSolution.dragViewSpanY;
2618
2619 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2620 // committing anything or animating anything as we just want to determine if a solution
2621 // exists
2622 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2623 if (!DESTRUCTIVE_REORDER) {
2624 copySolutionToTempState(finalSolution, dragView);
2625 }
2626 setItemPlacementDirty(true);
2627 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2628
Adam Cohen19f37922012-03-21 11:59:11 -07002629 if (!DESTRUCTIVE_REORDER &&
2630 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002631 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002632 completeAndClearReorderHintAnimations();
2633 setItemPlacementDirty(false);
2634 } else {
2635 beginOrAdjustHintAnimations(finalSolution, dragView,
2636 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002637 }
2638 }
2639 } else {
2640 foundSolution = false;
2641 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2642 }
2643
2644 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2645 setUseTempCoords(false);
2646 }
Adam Cohen482ed822012-03-02 14:15:13 -08002647
Michael Jurkaa52570f2012-03-20 03:18:20 -07002648 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002649 return result;
2650 }
2651
Adam Cohen19f37922012-03-21 11:59:11 -07002652 void setItemPlacementDirty(boolean dirty) {
2653 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002654 }
Adam Cohen19f37922012-03-21 11:59:11 -07002655 boolean isItemPlacementDirty() {
2656 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002657 }
2658
2659 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002660 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002661 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2662 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohen482ed822012-03-02 14:15:13 -08002663 boolean isSolution = false;
2664 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2665
Adam Cohenf3900c22012-11-16 18:28:11 -08002666 void save() {
2667 // Copy current state into savedMap
2668 for (View v: map.keySet()) {
2669 map.get(v).copy(savedMap.get(v));
2670 }
2671 }
2672
2673 void restore() {
2674 // Restore current state from savedMap
2675 for (View v: savedMap.keySet()) {
2676 savedMap.get(v).copy(map.get(v));
2677 }
2678 }
2679
2680 void add(View v, CellAndSpan cs) {
2681 map.put(v, cs);
2682 savedMap.put(v, new CellAndSpan());
2683 sortedViews.add(v);
2684 }
2685
Adam Cohen482ed822012-03-02 14:15:13 -08002686 int area() {
2687 return dragViewSpanX * dragViewSpanY;
2688 }
Adam Cohen8baab352012-03-20 17:39:21 -07002689 }
2690
2691 private class CellAndSpan {
2692 int x, y;
2693 int spanX, spanY;
2694
Adam Cohenf3900c22012-11-16 18:28:11 -08002695 public CellAndSpan() {
2696 }
2697
2698 public void copy(CellAndSpan copy) {
2699 copy.x = x;
2700 copy.y = y;
2701 copy.spanX = spanX;
2702 copy.spanY = spanY;
2703 }
2704
Adam Cohen8baab352012-03-20 17:39:21 -07002705 public CellAndSpan(int x, int y, int spanX, int spanY) {
2706 this.x = x;
2707 this.y = y;
2708 this.spanX = spanX;
2709 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002710 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002711
2712 public String toString() {
2713 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2714 }
2715
Adam Cohen482ed822012-03-02 14:15:13 -08002716 }
2717
Adam Cohendf035382011-04-11 17:22:04 -07002718 /**
2719 * Find a vacant area that will fit the given bounds nearest the requested
2720 * cell location. Uses Euclidean distance to score multiple vacant areas.
2721 *
2722 * @param pixelX The X location at which you want to search for a vacant area.
2723 * @param pixelY The Y location at which you want to search for a vacant area.
2724 * @param spanX Horizontal span of the object.
2725 * @param spanY Vertical span of the object.
2726 * @param ignoreView Considers space occupied by this view as unoccupied
2727 * @param result Previously returned value to possibly recycle.
2728 * @return The X, Y cell of a vacant area that can contain this object,
2729 * nearest the requested location.
2730 */
2731 int[] findNearestVacantArea(
2732 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2733 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2734 }
2735
2736 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002737 * Find a vacant area that will fit the given bounds nearest the requested
2738 * cell location. Uses Euclidean distance to score multiple vacant areas.
2739 *
2740 * @param pixelX The X location at which you want to search for a vacant area.
2741 * @param pixelY The Y location at which you want to search for a vacant area.
2742 * @param minSpanX The minimum horizontal span required
2743 * @param minSpanY The minimum vertical span required
2744 * @param spanX Horizontal span of the object.
2745 * @param spanY Vertical span of the object.
2746 * @param ignoreView Considers space occupied by this view as unoccupied
2747 * @param result Previously returned value to possibly recycle.
2748 * @return The X, Y cell of a vacant area that can contain this object,
2749 * nearest the requested location.
2750 */
2751 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2752 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002753 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2754 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002755 }
2756
2757 /**
Adam Cohendf035382011-04-11 17:22:04 -07002758 * Find a starting cell position that will fit the given bounds nearest the requested
2759 * cell location. Uses Euclidean distance to score multiple vacant areas.
2760 *
2761 * @param pixelX The X location at which you want to search for a vacant area.
2762 * @param pixelY The Y location at which you want to search for a vacant area.
2763 * @param spanX Horizontal span of the object.
2764 * @param spanY Vertical span of the object.
2765 * @param ignoreView Considers space occupied by this view as unoccupied
2766 * @param result Previously returned value to possibly recycle.
2767 * @return The X, Y cell of a vacant area that can contain this object,
2768 * nearest the requested location.
2769 */
2770 int[] findNearestArea(
2771 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2772 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2773 }
2774
Michael Jurka0280c3b2010-09-17 15:00:07 -07002775 boolean existsEmptyCell() {
2776 return findCellForSpan(null, 1, 1);
2777 }
2778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002780 * Finds the upper-left coordinate of the first rectangle in the grid that can
2781 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2782 * then this method will only return coordinates for rectangles that contain the cell
2783 * (intersectX, intersectY)
2784 *
2785 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2786 * can be found.
2787 * @param spanX The horizontal span of the cell we want to find.
2788 * @param spanY The vertical span of the cell we want to find.
2789 *
2790 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002791 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002792 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002793 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002794 }
2795
2796 /**
2797 * Like above, but ignores any cells occupied by the item "ignoreView"
2798 *
2799 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2800 * can be found.
2801 * @param spanX The horizontal span of the cell we want to find.
2802 * @param spanY The vertical span of the cell we want to find.
2803 * @param ignoreView The home screen item we should treat as not occupying any space
2804 * @return
2805 */
2806 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002807 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2808 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002809 }
2810
2811 /**
2812 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2813 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2814 *
2815 * @param spanX The horizontal span of the cell we want to find.
2816 * @param spanY The vertical span of the cell we want to find.
2817 * @param ignoreView The home screen item we should treat as not occupying any space
2818 * @param intersectX The X coordinate of the cell that we should try to overlap
2819 * @param intersectX The Y coordinate of the cell that we should try to overlap
2820 *
2821 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2822 */
2823 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2824 int intersectX, int intersectY) {
2825 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002826 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002827 }
2828
2829 /**
2830 * The superset of the above two methods
2831 */
2832 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002833 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002834 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002835 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002836
Michael Jurka28750fb2010-09-24 17:43:49 -07002837 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002838 while (true) {
2839 int startX = 0;
2840 if (intersectX >= 0) {
2841 startX = Math.max(startX, intersectX - (spanX - 1));
2842 }
2843 int endX = mCountX - (spanX - 1);
2844 if (intersectX >= 0) {
2845 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2846 }
2847 int startY = 0;
2848 if (intersectY >= 0) {
2849 startY = Math.max(startY, intersectY - (spanY - 1));
2850 }
2851 int endY = mCountY - (spanY - 1);
2852 if (intersectY >= 0) {
2853 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2854 }
2855
Winson Chungbbc60d82010-11-11 16:34:41 -08002856 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002857 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002858 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002859 for (int i = 0; i < spanX; i++) {
2860 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002861 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002862 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002863 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002864 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002865 continue inner;
2866 }
2867 }
2868 }
2869 if (cellXY != null) {
2870 cellXY[0] = x;
2871 cellXY[1] = y;
2872 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002873 foundCell = true;
2874 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002875 }
2876 }
2877 if (intersectX == -1 && intersectY == -1) {
2878 break;
2879 } else {
2880 // if we failed to find anything, try again but without any requirements of
2881 // intersecting
2882 intersectX = -1;
2883 intersectY = -1;
2884 continue;
2885 }
2886 }
2887
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002888 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002889 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002890 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002891 }
2892
2893 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002894 * A drag event has begun over this layout.
2895 * It may have begun over this layout (in which case onDragChild is called first),
2896 * or it may have begun on another layout.
2897 */
2898 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002899 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002900 mDragging = true;
2901 }
2902
2903 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002904 * Called when drag has left this CellLayout or has been completed (successfully or not)
2905 */
2906 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002907 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002908 // This can actually be called when we aren't in a drag, e.g. when adding a new
2909 // item to this layout via the customize drawer.
2910 // Guard against that case.
2911 if (mDragging) {
2912 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002913 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002914
2915 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002916 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002917 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2918 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002919 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002920 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002921 }
2922
2923 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002924 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002925 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002926 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002927 *
2928 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002930 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002931 if (child != null) {
2932 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002933 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002934 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002936 }
2937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002940 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002941 * @param cellX X coordinate of upper left corner expressed as a cell position
2942 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002943 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002945 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002946 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002947 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948 final int cellWidth = mCellWidth;
2949 final int cellHeight = mCellHeight;
2950 final int widthGap = mWidthGap;
2951 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002952
Winson Chung4b825dcd2011-06-19 12:41:22 -07002953 final int hStartPadding = getPaddingLeft();
2954 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002955
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2957 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2958
2959 int x = hStartPadding + cellX * (cellWidth + widthGap);
2960 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002961
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002962 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002964
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002966 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002968 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 * @param width Width in pixels
2970 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002971 * @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 -08002972 */
Winson Chung66700732013-08-20 16:56:15 -07002973 public static int[] rectToCell(int width, int height, int[] result) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002974 LauncherAppState app = LauncherAppState.getInstance();
2975 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -07002976 Rect padding = grid.getWorkspacePadding(grid.isLandscape ?
2977 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
Winson Chung5f8afe62013-08-12 16:19:28 -07002978
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 // Always assume we're working with the smallest span to make sure we
2980 // reserve enough space in both orientations.
Winson Chung66700732013-08-20 16:56:15 -07002981 int parentWidth = grid.calculateCellWidth(grid.widthPx
2982 - padding.left - padding.right, (int) grid.numColumns);
2983 int parentHeight = grid.calculateCellHeight(grid.heightPx
2984 - padding.top - padding.bottom, (int) grid.numRows);
2985 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002987 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002988 int spanX = (int) Math.ceil(width / (float) smallerSize);
2989 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002990
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002991 if (result == null) {
2992 return new int[] { spanX, spanY };
2993 }
2994 result[0] = spanX;
2995 result[1] = spanY;
2996 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002997 }
2998
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002999 public int[] cellSpansToSize(int hSpans, int vSpans) {
3000 int[] size = new int[2];
3001 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
3002 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
3003 return size;
3004 }
3005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08003007 * Calculate the grid spans needed to fit given item
3008 */
3009 public void calculateSpans(ItemInfo info) {
3010 final int minWidth;
3011 final int minHeight;
3012
3013 if (info instanceof LauncherAppWidgetInfo) {
3014 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3015 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3016 } else if (info instanceof PendingAddWidgetInfo) {
3017 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3018 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3019 } else {
3020 // It's not a widget, so it must be 1x1
3021 info.spanX = info.spanY = 1;
3022 return;
3023 }
3024 int[] spans = rectToCell(minWidth, minHeight, null);
3025 info.spanX = spans[0];
3026 info.spanY = spans[1];
3027 }
3028
3029 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030 * Find the first vacant cell, if there is one.
3031 *
3032 * @param vacant Holds the x and y coordinate of the vacant cell
3033 * @param spanX Horizontal cell span.
3034 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003035 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003036 * @return True if a vacant cell was found
3037 */
3038 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003039
Michael Jurka0280c3b2010-09-17 15:00:07 -07003040 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041 }
3042
3043 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3044 int xCount, int yCount, boolean[][] occupied) {
3045
Adam Cohen2801caf2011-05-13 20:57:39 -07003046 for (int y = 0; y < yCount; y++) {
3047 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003048 boolean available = !occupied[x][y];
3049out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3050 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3051 available = available && !occupied[i][j];
3052 if (!available) break out;
3053 }
3054 }
3055
3056 if (available) {
3057 vacant[0] = x;
3058 vacant[1] = y;
3059 return true;
3060 }
3061 }
3062 }
3063
3064 return false;
3065 }
3066
Michael Jurka0280c3b2010-09-17 15:00:07 -07003067 private void clearOccupiedCells() {
3068 for (int x = 0; x < mCountX; x++) {
3069 for (int y = 0; y < mCountY; y++) {
3070 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003071 }
3072 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003074
Adam Cohend41fbf52012-02-16 23:53:59 -08003075 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003076 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003077 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003078 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003079
Adam Cohend4844c32011-02-18 19:25:06 -08003080 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003081 markCellsAsOccupiedForView(view, mOccupied);
3082 }
3083 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003084 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003085 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003086 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003087 }
3088
Adam Cohend4844c32011-02-18 19:25:06 -08003089 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003090 markCellsAsUnoccupiedForView(view, mOccupied);
3091 }
3092 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003093 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003094 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003095 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003096 }
3097
Adam Cohen482ed822012-03-02 14:15:13 -08003098 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3099 boolean value) {
3100 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003101 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3102 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003103 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003104 }
3105 }
3106 }
3107
Adam Cohen2801caf2011-05-13 20:57:39 -07003108 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003109 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003110 (Math.max((mCountX - 1), 0) * mWidthGap);
3111 }
3112
3113 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003114 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003115 (Math.max((mCountY - 1), 0) * mHeightGap);
3116 }
3117
Michael Jurka66d72172011-04-12 16:29:25 -07003118 public boolean isOccupied(int x, int y) {
3119 if (x < mCountX && y < mCountY) {
3120 return mOccupied[x][y];
3121 } else {
3122 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3123 }
3124 }
3125
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003126 @Override
3127 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3128 return new CellLayout.LayoutParams(getContext(), attrs);
3129 }
3130
3131 @Override
3132 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3133 return p instanceof CellLayout.LayoutParams;
3134 }
3135
3136 @Override
3137 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3138 return new CellLayout.LayoutParams(p);
3139 }
3140
Winson Chungaafa03c2010-06-11 17:34:16 -07003141 public static class CellLayoutAnimationController extends LayoutAnimationController {
3142 public CellLayoutAnimationController(Animation animation, float delay) {
3143 super(animation, delay);
3144 }
3145
3146 @Override
3147 protected long getDelayForView(View view) {
3148 return (int) (Math.random() * 150);
3149 }
3150 }
3151
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003152 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3153 /**
3154 * Horizontal location of the item in the grid.
3155 */
3156 @ViewDebug.ExportedProperty
3157 public int cellX;
3158
3159 /**
3160 * Vertical location of the item in the grid.
3161 */
3162 @ViewDebug.ExportedProperty
3163 public int cellY;
3164
3165 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003166 * Temporary horizontal location of the item in the grid during reorder
3167 */
3168 public int tmpCellX;
3169
3170 /**
3171 * Temporary vertical location of the item in the grid during reorder
3172 */
3173 public int tmpCellY;
3174
3175 /**
3176 * Indicates that the temporary coordinates should be used to layout the items
3177 */
3178 public boolean useTmpCoords;
3179
3180 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181 * Number of cells spanned horizontally by the item.
3182 */
3183 @ViewDebug.ExportedProperty
3184 public int cellHSpan;
3185
3186 /**
3187 * Number of cells spanned vertically by the item.
3188 */
3189 @ViewDebug.ExportedProperty
3190 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003191
Adam Cohen1b607ed2011-03-03 17:26:50 -08003192 /**
3193 * Indicates whether the item will set its x, y, width and height parameters freely,
3194 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3195 */
Adam Cohend4844c32011-02-18 19:25:06 -08003196 public boolean isLockedToGrid = true;
3197
Adam Cohen482ed822012-03-02 14:15:13 -08003198 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07003199 * Indicates that this item should use the full extents of its parent.
3200 */
3201 public boolean isFullscreen = false;
3202
3203 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003204 * Indicates whether this item can be reordered. Always true except in the case of the
3205 * the AllApps button.
3206 */
3207 public boolean canReorder = true;
3208
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003209 // X coordinate of the view in the layout.
3210 @ViewDebug.ExportedProperty
3211 int x;
3212 // Y coordinate of the view in the layout.
3213 @ViewDebug.ExportedProperty
3214 int y;
3215
Romain Guy84f296c2009-11-04 15:00:44 -08003216 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003218 public LayoutParams(Context c, AttributeSet attrs) {
3219 super(c, attrs);
3220 cellHSpan = 1;
3221 cellVSpan = 1;
3222 }
3223
3224 public LayoutParams(ViewGroup.LayoutParams source) {
3225 super(source);
3226 cellHSpan = 1;
3227 cellVSpan = 1;
3228 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003229
3230 public LayoutParams(LayoutParams source) {
3231 super(source);
3232 this.cellX = source.cellX;
3233 this.cellY = source.cellY;
3234 this.cellHSpan = source.cellHSpan;
3235 this.cellVSpan = source.cellVSpan;
3236 }
3237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003239 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003240 this.cellX = cellX;
3241 this.cellY = cellY;
3242 this.cellHSpan = cellHSpan;
3243 this.cellVSpan = cellVSpan;
3244 }
3245
Adam Cohen2374abf2013-04-16 14:56:57 -07003246 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3247 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003248 if (isLockedToGrid) {
3249 final int myCellHSpan = cellHSpan;
3250 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003251 int myCellX = useTmpCoords ? tmpCellX : cellX;
3252 int myCellY = useTmpCoords ? tmpCellY : cellY;
3253
3254 if (invertHorizontally) {
3255 myCellX = colCount - myCellX - cellHSpan;
3256 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003257
Adam Cohend4844c32011-02-18 19:25:06 -08003258 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3259 leftMargin - rightMargin;
3260 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3261 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003262 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3263 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003264 }
3265 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003266
Winson Chungaafa03c2010-06-11 17:34:16 -07003267 public String toString() {
3268 return "(" + this.cellX + ", " + this.cellY + ")";
3269 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003270
3271 public void setWidth(int width) {
3272 this.width = width;
3273 }
3274
3275 public int getWidth() {
3276 return width;
3277 }
3278
3279 public void setHeight(int height) {
3280 this.height = height;
3281 }
3282
3283 public int getHeight() {
3284 return height;
3285 }
3286
3287 public void setX(int x) {
3288 this.x = x;
3289 }
3290
3291 public int getX() {
3292 return x;
3293 }
3294
3295 public void setY(int y) {
3296 this.y = y;
3297 }
3298
3299 public int getY() {
3300 return y;
3301 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003302 }
3303
Michael Jurka0280c3b2010-09-17 15:00:07 -07003304 // This class stores info for two purposes:
3305 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3306 // its spanX, spanY, and the screen it is on
3307 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3308 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3309 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003310 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003311 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003312 int cellX = -1;
3313 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003314 int spanX;
3315 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003316 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003317 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003319 @Override
3320 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003321 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3322 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003323 }
3324 }
Michael Jurkad771c962011-08-09 15:00:48 -07003325
3326 public boolean lastDownOnOccupiedCell() {
3327 return mLastDownOnOccupiedCell;
3328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003329}