blob: c5ad30a51e16a704e1436580bd978a176854a975 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Brandon Keely50e6e562012-05-08 16:28:49 -070021import android.animation.AnimatorSet;
Chet Haase00397b12010-10-07 11:13:10 -070022import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070023import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070028import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080030import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070032import android.graphics.Point;
Adam Cohenb5ba0972011-09-07 18:02:31 -070033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080036import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070037import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070038import android.graphics.drawable.NinePatchDrawable;
Adam Cohen1462de32012-07-24 22:34:36 -070039import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070042import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.R;
52import com.android.launcher3.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070053
Adam Cohen69ce2e52011-07-03 19:25:21 -070054import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080056import java.util.Collections;
57import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070058import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080059import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070060
Michael Jurkabdb5c532011-02-01 15:05:06 -080061public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070062 static final String TAG = "CellLayout";
63
Adam Cohen2acce882012-03-28 19:03:19 -070064 private Launcher mLauncher;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 private int mCellWidth;
66 private int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070067 private int mFixedCellWidth;
68 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070069
Adam Cohend22015c2010-07-26 22:02:18 -070070 private int mCountX;
71 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
Adam Cohen234c4cd2011-07-17 21:03:04 -070073 private int mOriginalWidthGap;
74 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075 private int mWidthGap;
76 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070077 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080078 private boolean mScrollingTransformsDirty = false;
Adam Cohen917e3882013-10-31 15:03:35 -070079 private boolean mDropPending = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
81 private final Rect mRect = new Rect();
82 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070083
Patrick Dubroyde7658b2010-09-27 11:15:43 -070084 // These are temporary variables to prevent having to allocate a new object just to
85 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070086 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070087 private final int[] mTmpPoint = new int[2];
Adam Cohen69ce2e52011-07-03 19:25:21 -070088 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070089
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080091 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070092 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093
Michael Jurkadee05892010-07-27 10:01:56 -070094 private OnTouchListener mInterceptTouchListener;
95
Adam Cohen69ce2e52011-07-03 19:25:21 -070096 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070097 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070098
Adam Cohen02dcfcc2013-10-01 12:37:33 -070099 private float FOREGROUND_ALPHA_DAMPER = 0.65f;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700100 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700101 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700102 private float mBackgroundAlphaMultiplier = 1.0f;
Winson Chung59a488a2013-12-10 12:32:14 -0800103 private boolean mDrawBackground = true;
Adam Cohenf34bab52010-09-30 14:11:56 -0700104
Michael Jurka33945b22010-12-21 18:19:38 -0800105 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -0800106 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700107 private Drawable mOverScrollForegroundDrawable;
108 private Drawable mOverScrollLeft;
109 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700110 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700111 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700112 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700113
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700114 // These values allow a fixed measurement to be set on the CellLayout.
115 private int mFixedWidth = -1;
116 private int mFixedHeight = -1;
117
Michael Jurka33945b22010-12-21 18:19:38 -0800118 // If we're actively dragging something over this screen, mIsDragOverlapping is true
119 private boolean mIsDragOverlapping = false;
Adam Cohendedbd962013-07-11 14:21:49 -0700120 boolean mUseActiveGlowBackground = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700121
Winson Chung150fbab2010-09-29 17:14:26 -0700122 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700123 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800124 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700125 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700126 private InterruptibleInOutAnimator[] mDragOutlineAnims =
127 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700128
129 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700130 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700131 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700132
Patrick Dubroy96864c32011-03-10 17:17:23 -0800133 private BubbleTextView mPressedOrFocusedIcon;
134
Adam Cohen482ed822012-03-02 14:15:13 -0800135 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
136 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700137 private HashMap<View, ReorderHintAnimation>
138 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
139
140 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700141
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700142 // When a drag operation is in progress, holds the nearest cell to the touch point
143 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Joe Onorato4be866d2010-10-10 11:26:02 -0700145 private boolean mDragging = false;
146
Patrick Dubroyce34a972010-10-19 10:34:32 -0700147 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700148 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700149
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800150 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700151 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800152
Adam Cohen482ed822012-03-02 14:15:13 -0800153 public static final int MODE_DRAG_OVER = 0;
154 public static final int MODE_ON_DROP = 1;
155 public static final int MODE_ON_DROP_EXTERNAL = 2;
156 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700157 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800158 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
159
Adam Cohena897f392012-04-27 18:12:05 -0700160 static final int LANDSCAPE = 0;
161 static final int PORTRAIT = 1;
162
Adam Cohen7bdfc972012-05-22 16:50:35 -0700163 private static final float REORDER_HINT_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700164 private static final int REORDER_ANIMATION_DURATION = 150;
165 private float mReorderHintAnimationMagnitude;
166
Adam Cohen482ed822012-03-02 14:15:13 -0800167 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
168 private Rect mOccupiedRect = new Rect();
169 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700170 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700171 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700172 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800173
Winson Chung3a6e7f32013-10-09 15:50:52 -0700174 private Rect mTempRect = new Rect();
175
Romain Guy8a0bff52012-05-06 13:14:33 -0700176 private final static PorterDuffXfermode sAddBlendMode =
177 new PorterDuffXfermode(PorterDuff.Mode.ADD);
Michael Jurkaca993832012-06-29 15:17:04 -0700178 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 public CellLayout(Context context) {
181 this(context, null);
182 }
183
184 public CellLayout(Context context, AttributeSet attrs) {
185 this(context, attrs, 0);
186 }
187
188 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
189 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700190 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700191
192 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
193 // the user where a dragged item will land when dropped.
194 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800195 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700196 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700197
Winson Chung892c74d2013-08-22 16:15:50 -0700198 LauncherAppState app = LauncherAppState.getInstance();
199 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
201
Winson Chung11a1a532013-09-13 11:14:45 -0700202 mCellWidth = mCellHeight = -1;
203 mFixedCellHeight = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700204 mWidthGap = mOriginalWidthGap = 0;
205 mHeightGap = mOriginalHeightGap = 0;
206 mMaxGap = Integer.MAX_VALUE;
Winson Chung892c74d2013-08-22 16:15:50 -0700207 mCountX = (int) grid.numColumns;
208 mCountY = (int) grid.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700209 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800210 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700211 mPreviousReorderDirection[0] = INVALID_DIRECTION;
212 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
214 a.recycle();
215
216 setAlwaysDrawnWithCacheEnabled(false);
217
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700218 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700219 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700220
Adam Cohen410f3cd2013-09-22 12:09:32 -0700221 mNormalBackground = res.getDrawable(R.drawable.screenpanel);
222 mActiveGlowBackground = res.getDrawable(R.drawable.screenpanel_hover);
Michael Jurka33945b22010-12-21 18:19:38 -0800223
Adam Cohenb5ba0972011-09-07 18:02:31 -0700224 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
225 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
226 mForegroundPadding =
227 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800228
Adam Cohen19f37922012-03-21 11:59:11 -0700229 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700230 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700231
Winson Chungb26f3d62011-06-02 10:49:29 -0700232 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700233 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700234
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700235 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700236 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700237 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700238 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800239 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700240 }
241
242 // When dragging things around the home screens, we show a green outline of
243 // where the item will land. The outlines gradually fade out, leaving a trail
244 // behind the drag path.
245 // Set up all the animations that are used to implement this fading.
246 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700247 final float fromAlphaValue = 0;
248 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700249
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700250 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700251
252 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700253 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100254 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700255 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700256 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700257 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700258 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700259 final Bitmap outline = (Bitmap)anim.getTag();
260
261 // If an animation is started and then stopped very quickly, we can still
262 // get spurious updates we've cleared the tag. Guard against this.
263 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700264 @SuppressWarnings("all") // suppress dead code warning
265 final boolean debug = false;
266 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700267 Object val = animation.getAnimatedValue();
268 Log.d(TAG, "anim " + thisIndex + " update: " + val +
269 ", isStopped " + anim.isStopped());
270 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700271 // Try to prevent it from continuing to run
272 animation.cancel();
273 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700274 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800275 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700276 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700277 }
278 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700279 // The animation holds a reference to the drag outline bitmap as long is it's
280 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700281 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700282 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700283 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700284 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700285 anim.setTag(null);
286 }
287 }
288 });
289 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700290 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700291
Michael Jurka18014792010-10-14 09:01:34 -0700292 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700293 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800294
Michael Jurkaa52570f2012-03-20 03:18:20 -0700295 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700296 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700297 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700298
Michael Jurkaa52570f2012-03-20 03:18:20 -0700299 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700300 }
301
Chris Craik01f2d7f2013-10-01 14:41:56 -0700302 public void enableHardwareLayer(boolean hasLayer) {
303 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700304 }
305
306 public void buildHardwareLayer() {
307 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700308 }
309
Adam Cohen307fe232012-08-16 17:55:58 -0700310 public float getChildrenScale() {
311 return mIsHotseat ? mHotseatScale : 1.0f;
312 }
313
Winson Chung5f8afe62013-08-12 16:19:28 -0700314 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700315 mFixedCellWidth = mCellWidth = width;
316 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700317 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
318 mCountX, mCountY);
319 }
320
Adam Cohen2801caf2011-05-13 20:57:39 -0700321 public void setGridSize(int x, int y) {
322 mCountX = x;
323 mCountY = y;
324 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800325 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700326 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700327 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700328 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700329 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700330 }
331
Adam Cohen2374abf2013-04-16 14:56:57 -0700332 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
333 public void setInvertIfRtl(boolean invert) {
334 mShortcutsAndWidgets.setInvertIfRtl(invert);
335 }
336
Adam Cohen917e3882013-10-31 15:03:35 -0700337 public void setDropPending(boolean pending) {
338 mDropPending = pending;
339 }
340
341 public boolean isDropPending() {
342 return mDropPending;
343 }
344
Patrick Dubroy96864c32011-03-10 17:17:23 -0800345 private void invalidateBubbleTextView(BubbleTextView icon) {
346 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700347 invalidate(icon.getLeft() + getPaddingLeft() - padding,
348 icon.getTop() + getPaddingTop() - padding,
349 icon.getRight() + getPaddingLeft() + padding,
350 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800351 }
352
Adam Cohenb5ba0972011-09-07 18:02:31 -0700353 void setOverScrollAmount(float r, boolean left) {
354 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
355 mOverScrollForegroundDrawable = mOverScrollLeft;
356 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
357 mOverScrollForegroundDrawable = mOverScrollRight;
358 }
359
Adam Cohen02dcfcc2013-10-01 12:37:33 -0700360 r *= FOREGROUND_ALPHA_DAMPER;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700361 mForegroundAlpha = (int) Math.round((r * 255));
362 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
363 invalidate();
364 }
365
Patrick Dubroy96864c32011-03-10 17:17:23 -0800366 void setPressedOrFocusedIcon(BubbleTextView icon) {
367 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
368 // requires an expanded clip rect (due to the glow's blur radius)
369 BubbleTextView oldIcon = mPressedOrFocusedIcon;
370 mPressedOrFocusedIcon = icon;
371 if (oldIcon != null) {
372 invalidateBubbleTextView(oldIcon);
373 }
374 if (mPressedOrFocusedIcon != null) {
375 invalidateBubbleTextView(mPressedOrFocusedIcon);
376 }
377 }
378
Michael Jurka33945b22010-12-21 18:19:38 -0800379 void setIsDragOverlapping(boolean isDragOverlapping) {
380 if (mIsDragOverlapping != isDragOverlapping) {
381 mIsDragOverlapping = isDragOverlapping;
Adam Cohendedbd962013-07-11 14:21:49 -0700382 setUseActiveGlowBackground(mIsDragOverlapping);
Michael Jurka33945b22010-12-21 18:19:38 -0800383 invalidate();
384 }
385 }
386
Adam Cohendedbd962013-07-11 14:21:49 -0700387 void setUseActiveGlowBackground(boolean use) {
388 mUseActiveGlowBackground = use;
389 }
390
Winson Chung59a488a2013-12-10 12:32:14 -0800391 void disableBackground() {
392 mDrawBackground = false;
393 }
394
Michael Jurka33945b22010-12-21 18:19:38 -0800395 boolean getIsDragOverlapping() {
396 return mIsDragOverlapping;
397 }
398
Adam Cohenebea84d2011-11-09 17:20:41 -0800399 protected void setOverscrollTransformsDirty(boolean dirty) {
400 mScrollingTransformsDirty = dirty;
401 }
402
403 protected void resetOverscrollTransforms() {
404 if (mScrollingTransformsDirty) {
405 setOverscrollTransformsDirty(false);
406 setTranslationX(0);
407 setRotationY(0);
408 // It doesn't matter if we pass true or false here, the important thing is that we
409 // pass 0, which results in the overscroll drawable not being drawn any more.
410 setOverScrollAmount(0, false);
411 setPivotX(getMeasuredWidth() / 2);
412 setPivotY(getMeasuredHeight() / 2);
413 }
414 }
415
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700416 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700417 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700418 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
419 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
420 // When we're small, we are either drawn normally or in the "accepts drops" state (during
421 // a drag). However, we also drag the mini hover background *over* one of those two
422 // backgrounds
Winson Chung59a488a2013-12-10 12:32:14 -0800423 if (mDrawBackground && mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700424 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800425
Adam Cohendedbd962013-07-11 14:21:49 -0700426 if (mUseActiveGlowBackground) {
Michael Jurka33945b22010-12-21 18:19:38 -0800427 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700428 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700429 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700430 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700431 }
Michael Jurka33945b22010-12-21 18:19:38 -0800432
433 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
434 bg.setBounds(mBackgroundRect);
435 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700436 }
Romain Guya6abce82009-11-10 02:54:41 -0800437
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700438 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700439 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700440 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700441 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800442 final Rect r = mDragOutlines[i];
Winson Chung3a6e7f32013-10-09 15:50:52 -0700443 mTempRect.set(r);
444 Utilities.scaleRectAboutCenter(mTempRect, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700445 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700446 paint.setAlpha((int)(alpha + .5f));
Winson Chung3a6e7f32013-10-09 15:50:52 -0700447 canvas.drawBitmap(b, null, mTempRect, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700448 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700449 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800450
451 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
452 // requires an expanded clip rect (due to the glow's blur radius)
453 if (mPressedOrFocusedIcon != null) {
454 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
455 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
456 if (b != null) {
Winson Chung3a6e7f32013-10-09 15:50:52 -0700457 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
458 (mCountX * mCellWidth);
459 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
460 int top = getPaddingTop();
Patrick Dubroy96864c32011-03-10 17:17:23 -0800461 canvas.drawBitmap(b,
Winson Chung3a6e7f32013-10-09 15:50:52 -0700462 mPressedOrFocusedIcon.getLeft() + left - padding,
463 mPressedOrFocusedIcon.getTop() + top - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800464 null);
465 }
466 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700467
Adam Cohen482ed822012-03-02 14:15:13 -0800468 if (DEBUG_VISUALIZE_OCCUPIED) {
469 int[] pt = new int[2];
470 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700471 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800472 for (int i = 0; i < mCountX; i++) {
473 for (int j = 0; j < mCountY; j++) {
474 if (mOccupied[i][j]) {
475 cellToPoint(i, j, pt);
476 canvas.save();
477 canvas.translate(pt[0], pt[1]);
478 cd.draw(canvas);
479 canvas.restore();
480 }
481 }
482 }
483 }
484
Andrew Flynn850d2e72012-04-26 16:51:20 -0700485 int previewOffset = FolderRingAnimator.sPreviewSize;
486
Adam Cohen69ce2e52011-07-03 19:25:21 -0700487 // The folder outer / inner ring image(s)
Winson Chung5f8afe62013-08-12 16:19:28 -0700488 LauncherAppState app = LauncherAppState.getInstance();
489 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700490 for (int i = 0; i < mFolderOuterRings.size(); i++) {
491 FolderRingAnimator fra = mFolderOuterRings.get(i);
492
Adam Cohen5108bc02013-09-20 17:04:51 -0700493 Drawable d;
494 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700495 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700496 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700497
Winson Chung89f97052013-09-20 11:32:26 -0700498 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700499 int centerX = mTempLocation[0] + mCellWidth / 2;
500 int centerY = mTempLocation[1] + previewOffset / 2 +
501 child.getPaddingTop() + grid.folderBackgroundOffset;
502
Adam Cohen5108bc02013-09-20 17:04:51 -0700503 // Draw outer ring, if it exists
504 if (FolderIcon.HAS_OUTER_RING) {
505 d = FolderRingAnimator.sSharedOuterRingDrawable;
506 width = (int) (fra.getOuterRingSize() * getChildrenScale());
507 height = width;
508 canvas.save();
509 canvas.translate(centerX - width / 2, centerY - height / 2);
510 d.setBounds(0, 0, width, height);
511 d.draw(canvas);
512 canvas.restore();
513 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700514
Winson Chung89f97052013-09-20 11:32:26 -0700515 // Draw inner ring
516 d = FolderRingAnimator.sSharedInnerRingDrawable;
517 width = (int) (fra.getInnerRingSize() * getChildrenScale());
518 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700519 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);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700553 mOverScrollForegroundDrawable.draw(canvas);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700554 }
555 }
556
Adam Cohen69ce2e52011-07-03 19:25:21 -0700557 public void showFolderAccept(FolderRingAnimator fra) {
558 mFolderOuterRings.add(fra);
559 }
560
561 public void hideFolderAccept(FolderRingAnimator fra) {
562 if (mFolderOuterRings.contains(fra)) {
563 mFolderOuterRings.remove(fra);
564 }
565 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700566 }
567
Adam Cohenc51934b2011-07-26 21:07:43 -0700568 public void setFolderLeaveBehindCell(int x, int y) {
569 mFolderLeaveBehindCell[0] = x;
570 mFolderLeaveBehindCell[1] = y;
571 invalidate();
572 }
573
574 public void clearFolderLeaveBehind() {
575 mFolderLeaveBehindCell[0] = -1;
576 mFolderLeaveBehindCell[1] = -1;
577 invalidate();
578 }
579
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700580 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700581 public boolean shouldDelayChildPressedState() {
582 return false;
583 }
584
Adam Cohen1462de32012-07-24 22:34:36 -0700585 public void restoreInstanceState(SparseArray<Parcelable> states) {
586 dispatchRestoreInstanceState(states);
587 }
588
Michael Jurkae6235dd2011-10-04 15:02:05 -0700589 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700590 public void cancelLongPress() {
591 super.cancelLongPress();
592
593 // Cancel long press for all children
594 final int count = getChildCount();
595 for (int i = 0; i < count; i++) {
596 final View child = getChildAt(i);
597 child.cancelLongPress();
598 }
599 }
600
Michael Jurkadee05892010-07-27 10:01:56 -0700601 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
602 mInterceptTouchListener = listener;
603 }
604
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700606 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 }
608
609 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700610 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
612
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800613 public void setIsHotseat(boolean isHotseat) {
614 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700615 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800616 }
617
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800618 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700619 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700620 final LayoutParams lp = params;
621
Andrew Flynnde38e422012-05-08 11:22:15 -0700622 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800623 if (child instanceof BubbleTextView) {
624 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700625 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800626 }
627
Adam Cohen307fe232012-08-16 17:55:58 -0700628 child.setScaleX(getChildrenScale());
629 child.setScaleY(getChildrenScale());
630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 // Generate an id for each view, this assumes we have at most 256x256 cells
632 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700633 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700634 // If the horizontal or vertical span is set to -1, it is taken to
635 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700636 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
637 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638
Winson Chungaafa03c2010-06-11 17:34:16 -0700639 child.setId(childId);
640
Michael Jurkaa52570f2012-03-20 03:18:20 -0700641 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700642
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700643 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644
Winson Chungaafa03c2010-06-11 17:34:16 -0700645 return true;
646 }
647 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700651 public void removeAllViews() {
652 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700653 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654 }
655
656 @Override
657 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700658 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700659 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700660 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700661 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 }
663
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700664 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700665 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700666 }
667
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 @Override
669 public void removeView(View view) {
670 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700671 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700672 }
673
674 @Override
675 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700676 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
677 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 }
679
680 @Override
681 public void removeViewInLayout(View view) {
682 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
685
686 @Override
687 public void removeViews(int start, int count) {
688 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700689 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700691 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700692 }
693
694 @Override
695 public void removeViewsInLayout(int start, int count) {
696 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700697 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700698 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700699 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800700 }
701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 @Override
703 protected void onAttachedToWindow() {
704 super.onAttachedToWindow();
Adam Cohendcd297f2013-06-18 13:13:40 -0700705 if (getParent() instanceof Workspace) {
706 Workspace workspace = (Workspace) getParent();
707 mCellInfo.screenId = workspace.getIdForScreen(this);
708 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 }
710
Michael Jurkaaf442092010-06-10 17:01:57 -0700711 public void setTagToCellInfoForPoint(int touchX, int touchY) {
712 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800713 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 final int x = touchX + getScrollX();
715 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700716 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700717
718 boolean found = false;
719 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700720 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800721 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700722
Adam Cohen1b607ed2011-03-03 17:26:50 -0800723 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
724 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700725 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700726
Winson Chungeecf02d2012-03-02 17:14:58 -0800727 float scale = child.getScaleX();
728 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
729 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700730 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
731 // offset that by this CellLayout's padding to test an (x,y) point that is relative
732 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700733 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800734 frame.inset((int) (frame.width() * (1f - scale) / 2),
735 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700736
Michael Jurkaaf442092010-06-10 17:01:57 -0700737 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700738 cellInfo.cell = child;
739 cellInfo.cellX = lp.cellX;
740 cellInfo.cellY = lp.cellY;
741 cellInfo.spanX = lp.cellHSpan;
742 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700743 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700744 break;
745 }
746 }
747 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700748
Michael Jurkad771c962011-08-09 15:00:48 -0700749 mLastDownOnOccupiedCell = found;
750
Michael Jurkaaf442092010-06-10 17:01:57 -0700751 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700752 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700753 pointToCellExact(x, y, cellXY);
754
Michael Jurkaaf442092010-06-10 17:01:57 -0700755 cellInfo.cell = null;
756 cellInfo.cellX = cellXY[0];
757 cellInfo.cellY = cellXY[1];
758 cellInfo.spanX = 1;
759 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700760 }
761 setTag(cellInfo);
762 }
763
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 @Override
765 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700766 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
767 // even in the case where we return early. Not clearing here was causing bugs whereby on
768 // long-press we'd end up picking up an item from a previous drag operation.
769 final int action = ev.getAction();
770
771 if (action == MotionEvent.ACTION_DOWN) {
772 clearTagCellInfo();
773 }
774
Michael Jurkadee05892010-07-27 10:01:56 -0700775 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
776 return true;
777 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778
779 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700780 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 return false;
784 }
785
Adam Cohenc1997fd2011-08-15 18:26:39 -0700786 private void clearTagCellInfo() {
787 final CellInfo cellInfo = mCellInfo;
788 cellInfo.cell = null;
789 cellInfo.cellX = -1;
790 cellInfo.cellY = -1;
791 cellInfo.spanX = 0;
792 cellInfo.spanY = 0;
793 setTag(cellInfo);
794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700797 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 }
799
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700800 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700801 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 * @param x X coordinate of the point
803 * @param y Y coordinate of the point
804 * @param result Array of 2 ints to hold the x and y coordinate of the cell
805 */
806 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700807 final int hStartPadding = getPaddingLeft();
808 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809
810 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
811 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
812
Adam Cohend22015c2010-07-26 22:02:18 -0700813 final int xAxis = mCountX;
814 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815
816 if (result[0] < 0) result[0] = 0;
817 if (result[0] >= xAxis) result[0] = xAxis - 1;
818 if (result[1] < 0) result[1] = 0;
819 if (result[1] >= yAxis) result[1] = yAxis - 1;
820 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 /**
823 * Given a point, return the cell that most closely encloses that point
824 * @param x X coordinate of the point
825 * @param y Y coordinate of the point
826 * @param result Array of 2 ints to hold the x and y coordinate of the cell
827 */
828 void pointToCellRounded(int x, int y, int[] result) {
829 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
830 }
831
832 /**
833 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700834 *
835 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700837 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 * @param result Array of 2 ints to hold the x and y coordinate of the point
839 */
840 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700841 final int hStartPadding = getPaddingLeft();
842 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843
844 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
845 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
846 }
847
Adam Cohene3e27a82011-04-15 12:07:39 -0700848 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800849 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700850 *
851 * @param cellX X coordinate of the cell
852 * @param cellY Y coordinate of the cell
853 *
854 * @param result Array of 2 ints to hold the x and y coordinate of the point
855 */
856 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700857 regionToCenterPoint(cellX, cellY, 1, 1, result);
858 }
859
860 /**
861 * Given a cell coordinate and span return the point that represents the center of the regio
862 *
863 * @param cellX X coordinate of the cell
864 * @param cellY Y coordinate of the cell
865 *
866 * @param result Array of 2 ints to hold the x and y coordinate of the point
867 */
868 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700869 final int hStartPadding = getPaddingLeft();
870 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700871 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
872 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
873 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
874 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700875 }
876
Adam Cohen19f37922012-03-21 11:59:11 -0700877 /**
878 * Given a cell coordinate and span fills out a corresponding pixel rect
879 *
880 * @param cellX X coordinate of the cell
881 * @param cellY Y coordinate of the cell
882 * @param result Rect in which to write the result
883 */
884 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
885 final int hStartPadding = getPaddingLeft();
886 final int vStartPadding = getPaddingTop();
887 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
888 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
889 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
890 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
891 }
892
Adam Cohen482ed822012-03-02 14:15:13 -0800893 public float getDistanceFromCell(float x, float y, int[] cell) {
894 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
895 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
896 Math.pow(y - mTmpPoint[1], 2));
897 return distance;
898 }
899
Romain Guy84f296c2009-11-04 15:00:44 -0800900 int getCellWidth() {
901 return mCellWidth;
902 }
903
904 int getCellHeight() {
905 return mCellHeight;
906 }
907
Adam Cohend4844c32011-02-18 19:25:06 -0800908 int getWidthGap() {
909 return mWidthGap;
910 }
911
912 int getHeightGap() {
913 return mHeightGap;
914 }
915
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700916 Rect getContentRect(Rect r) {
917 if (r == null) {
918 r = new Rect();
919 }
920 int left = getPaddingLeft();
921 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700922 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
923 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700924 r.set(left, top, right, bottom);
925 return r;
926 }
927
Winson Chungfe411c82013-09-26 16:07:17 -0700928 /** Return a rect that has the cellWidth/cellHeight (left, top), and
929 * widthGap/heightGap (right, bottom) */
Winson Chung66700732013-08-20 16:56:15 -0700930 static void getMetrics(Rect metrics, int paddedMeasureWidth,
931 int paddedMeasureHeight, int countX, int countY) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700932 LauncherAppState app = LauncherAppState.getInstance();
933 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -0700934 metrics.set(grid.calculateCellWidth(paddedMeasureWidth, countX),
935 grid.calculateCellHeight(paddedMeasureHeight, countY), 0, 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700936 }
937
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700938 public void setFixedSize(int width, int height) {
939 mFixedWidth = width;
940 mFixedHeight = height;
941 }
942
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 @Override
944 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700945 LauncherAppState app = LauncherAppState.getInstance();
946 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
947
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700950 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
951 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700952 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
953 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700954 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700955 int cw = grid.calculateCellWidth(childWidthSize, mCountX);
956 int ch = grid.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700957 if (cw != mCellWidth || ch != mCellHeight) {
958 mCellWidth = cw;
959 mCellHeight = ch;
960 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
961 mHeightGap, mCountX, mCountY);
962 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700963 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700964
Winson Chung2d75f122013-09-23 16:53:31 -0700965 int newWidth = childWidthSize;
966 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700967 if (mFixedWidth > 0 && mFixedHeight > 0) {
968 newWidth = mFixedWidth;
969 newHeight = mFixedHeight;
970 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
972 }
973
Adam Cohend22015c2010-07-26 22:02:18 -0700974 int numWidthGaps = mCountX - 1;
975 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976
Adam Cohen234c4cd2011-07-17 21:03:04 -0700977 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700978 int hSpace = childWidthSize;
979 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700980 int hFreeSpace = hSpace - (mCountX * mCellWidth);
981 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700982 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
983 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700984 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
985 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700986 } else {
987 mWidthGap = mOriginalWidthGap;
988 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700989 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800990 int count = getChildCount();
Winson Chung5f8afe62013-08-12 16:19:28 -0700991 int maxWidth = 0;
992 int maxHeight = 0;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800993 for (int i = 0; i < count; i++) {
994 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -0700995 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth,
996 MeasureSpec.EXACTLY);
997 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
998 MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800999 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -07001000 maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
1001 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
Michael Jurka8c920dd2011-01-20 14:16:56 -08001002 }
Winson Chung2d75f122013-09-23 16:53:31 -07001003 if (mFixedWidth > 0 && mFixedHeight > 0) {
1004 setMeasuredDimension(maxWidth, maxHeight);
1005 } else {
1006 setMeasuredDimension(widthSize, heightSize);
1007 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 }
1009
1010 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001011 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -07001012 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
1013 (mCountX * mCellWidth);
1014 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
1015 int top = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001018 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -07001019 child.layout(left, top,
1020 left + r - l,
1021 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
1023 }
1024
1025 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001026 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1027 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -07001028
1029 // Expand the background drawing bounds by the padding baked into the background drawable
1030 Rect padding = new Rect();
1031 mNormalBackground.getPadding(padding);
1032 mBackgroundRect.set(-padding.left, -padding.top, w + padding.right, h + padding.bottom);
1033
Adam Cohenb5ba0972011-09-07 18:02:31 -07001034 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001035 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001036 }
1037
1038 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001040 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 }
1042
1043 @Override
1044 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001045 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 }
1047
Michael Jurka5f1c5092010-09-03 14:15:02 -07001048 public float getBackgroundAlpha() {
1049 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001050 }
1051
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001052 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001053 if (mBackgroundAlphaMultiplier != multiplier) {
1054 mBackgroundAlphaMultiplier = multiplier;
1055 invalidate();
1056 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001057 }
1058
Adam Cohenddb82192010-11-10 16:32:54 -08001059 public float getBackgroundAlphaMultiplier() {
1060 return mBackgroundAlphaMultiplier;
1061 }
1062
Michael Jurka5f1c5092010-09-03 14:15:02 -07001063 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001064 if (mBackgroundAlpha != alpha) {
1065 mBackgroundAlpha = alpha;
1066 invalidate();
1067 }
Michael Jurkadee05892010-07-27 10:01:56 -07001068 }
1069
Michael Jurkaa52570f2012-03-20 03:18:20 -07001070 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001071 final int childCount = getChildCount();
1072 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001073 getChildAt(i).setAlpha(alpha);
1074 }
1075 }
1076
Michael Jurkaa52570f2012-03-20 03:18:20 -07001077 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1078 if (getChildCount() > 0) {
1079 return (ShortcutAndWidgetContainer) getChildAt(0);
1080 }
1081 return null;
1082 }
1083
Patrick Dubroy440c3602010-07-13 17:50:32 -07001084 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001085 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001086 }
1087
Adam Cohen76fc0852011-06-17 13:26:23 -07001088 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001089 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001090 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001091 boolean[][] occupied = mOccupied;
1092 if (!permanent) {
1093 occupied = mTmpOccupied;
1094 }
1095
Adam Cohen19f37922012-03-21 11:59:11 -07001096 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001097 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1098 final ItemInfo info = (ItemInfo) child.getTag();
1099
1100 // We cancel any existing animations
1101 if (mReorderAnimators.containsKey(lp)) {
1102 mReorderAnimators.get(lp).cancel();
1103 mReorderAnimators.remove(lp);
1104 }
1105
Adam Cohen482ed822012-03-02 14:15:13 -08001106 final int oldX = lp.x;
1107 final int oldY = lp.y;
1108 if (adjustOccupied) {
1109 occupied[lp.cellX][lp.cellY] = false;
1110 occupied[cellX][cellY] = true;
1111 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001112 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001113 if (permanent) {
1114 lp.cellX = info.cellX = cellX;
1115 lp.cellY = info.cellY = cellY;
1116 } else {
1117 lp.tmpCellX = cellX;
1118 lp.tmpCellY = cellY;
1119 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001120 clc.setupLp(lp);
1121 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001122 final int newX = lp.x;
1123 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001124
Adam Cohen76fc0852011-06-17 13:26:23 -07001125 lp.x = oldX;
1126 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001127
Adam Cohen482ed822012-03-02 14:15:13 -08001128 // Exit early if we're not actually moving the view
1129 if (oldX == newX && oldY == newY) {
1130 lp.isLockedToGrid = true;
1131 return true;
1132 }
1133
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001134 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001135 va.setDuration(duration);
1136 mReorderAnimators.put(lp, va);
1137
1138 va.addUpdateListener(new AnimatorUpdateListener() {
1139 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001140 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001141 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001142 lp.x = (int) ((1 - r) * oldX + r * newX);
1143 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001144 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001145 }
1146 });
Adam Cohen482ed822012-03-02 14:15:13 -08001147 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001148 boolean cancelled = false;
1149 public void onAnimationEnd(Animator animation) {
1150 // If the animation was cancelled, it means that another animation
1151 // has interrupted this one, and we don't want to lock the item into
1152 // place just yet.
1153 if (!cancelled) {
1154 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001155 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001156 }
1157 if (mReorderAnimators.containsKey(lp)) {
1158 mReorderAnimators.remove(lp);
1159 }
1160 }
1161 public void onAnimationCancel(Animator animation) {
1162 cancelled = true;
1163 }
1164 });
Adam Cohen482ed822012-03-02 14:15:13 -08001165 va.setStartDelay(delay);
1166 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001167 return true;
1168 }
1169 return false;
1170 }
1171
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001172 /**
1173 * Estimate where the top left cell of the dragged item will land if it is dropped.
1174 *
1175 * @param originX The X value of the top left corner of the item
1176 * @param originY The Y value of the top left corner of the item
1177 * @param spanX The number of horizontal cells that the item spans
1178 * @param spanY The number of vertical cells that the item spans
1179 * @param result The estimated drop cell X and Y.
1180 */
1181 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001182 final int countX = mCountX;
1183 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001184
Michael Jurkaa63c4522010-08-19 13:52:27 -07001185 // pointToCellRounded takes the top left of a cell but will pad that with
1186 // cellWidth/2 and cellHeight/2 when finding the matching cell
1187 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001188
1189 // If the item isn't fully on this screen, snap to the edges
1190 int rightOverhang = result[0] + spanX - countX;
1191 if (rightOverhang > 0) {
1192 result[0] -= rightOverhang; // Snap to right
1193 }
1194 result[0] = Math.max(0, result[0]); // Snap to left
1195 int bottomOverhang = result[1] + spanY - countY;
1196 if (bottomOverhang > 0) {
1197 result[1] -= bottomOverhang; // Snap to bottom
1198 }
1199 result[1] = Math.max(0, result[1]); // Snap to top
1200 }
1201
Adam Cohen482ed822012-03-02 14:15:13 -08001202 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1203 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001204 final int oldDragCellX = mDragCell[0];
1205 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001206
Adam Cohen2801caf2011-05-13 20:57:39 -07001207 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001208 return;
1209 }
1210
Adam Cohen482ed822012-03-02 14:15:13 -08001211 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1212 mDragCell[0] = cellX;
1213 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001214 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001215 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001216 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001217
Joe Onorato4be866d2010-10-10 11:26:02 -07001218 int left = topLeft[0];
1219 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001220
Winson Chungb8c69f32011-10-19 21:36:08 -07001221 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001222 // When drawing the drag outline, it did not account for margin offsets
1223 // added by the view's parent.
1224 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1225 left += lp.leftMargin;
1226 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001227
Adam Cohen99e8b402011-03-25 19:23:43 -07001228 // Offsets due to the size difference between the View and the dragOutline.
1229 // There is a size difference to account for the outer blur, which may lie
1230 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001231 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001232 // We center about the x axis
1233 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1234 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001235 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001236 if (dragOffset != null && dragRegion != null) {
1237 // Center the drag region *horizontally* in the cell and apply a drag
1238 // outline offset
1239 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1240 - dragRegion.width()) / 2;
Winson Chung69737c32013-10-08 17:00:19 -07001241 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1242 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1243 top += dragOffset.y + cellPaddingY;
Winson Chungb8c69f32011-10-19 21:36:08 -07001244 } else {
1245 // Center the drag outline in the cell
1246 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1247 - dragOutline.getWidth()) / 2;
1248 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1249 - dragOutline.getHeight()) / 2;
1250 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001251 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001252 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001253 mDragOutlineAnims[oldIndex].animateOut();
1254 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001255 Rect r = mDragOutlines[mDragOutlineCurrent];
1256 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1257 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001258 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001259 }
Winson Chung150fbab2010-09-29 17:14:26 -07001260
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001261 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1262 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001263 }
1264 }
1265
Adam Cohene0310962011-04-18 16:15:31 -07001266 public void clearDragOutlines() {
1267 final int oldIndex = mDragOutlineCurrent;
1268 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001269 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001270 }
1271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001273 * Find a vacant area that will fit the given bounds nearest the requested
1274 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001275 *
Romain Guy51afc022009-05-04 18:03:43 -07001276 * @param pixelX The X location at which you want to search for a vacant area.
1277 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001278 * @param spanX Horizontal span of the object.
1279 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001280 * @param result Array in which to place the result, or null (in which case a new array will
1281 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001282 * @return The X, Y cell of a vacant area that can contain this object,
1283 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001285 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1286 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001287 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001288 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001289
Michael Jurka6a1435d2010-09-27 17:35:12 -07001290 /**
1291 * Find a vacant area that will fit the given bounds nearest the requested
1292 * cell location. Uses Euclidean distance to score multiple vacant areas.
1293 *
1294 * @param pixelX The X location at which you want to search for a vacant area.
1295 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001296 * @param minSpanX The minimum horizontal span required
1297 * @param minSpanY The minimum vertical span required
1298 * @param spanX Horizontal span of the object.
1299 * @param spanY Vertical span of the object.
1300 * @param result Array in which to place the result, or null (in which case a new array will
1301 * be allocated)
1302 * @return The X, Y cell of a vacant area that can contain this object,
1303 * nearest the requested location.
1304 */
1305 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1306 int spanY, int[] result, int[] resultSpan) {
1307 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1308 result, resultSpan);
1309 }
1310
1311 /**
1312 * Find a vacant area that will fit the given bounds nearest the requested
1313 * cell location. Uses Euclidean distance to score multiple vacant areas.
1314 *
1315 * @param pixelX The X location at which you want to search for a vacant area.
1316 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001317 * @param spanX Horizontal span of the object.
1318 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001319 * @param ignoreOccupied If true, the result can be an occupied cell
1320 * @param result Array in which to place the result, or null (in which case a new array will
1321 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001322 * @return The X, Y cell of a vacant area that can contain this object,
1323 * nearest the requested location.
1324 */
Adam Cohendf035382011-04-11 17:22:04 -07001325 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1326 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001327 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001328 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001329 }
1330
1331 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1332 private void lazyInitTempRectStack() {
1333 if (mTempRectStack.isEmpty()) {
1334 for (int i = 0; i < mCountX * mCountY; i++) {
1335 mTempRectStack.push(new Rect());
1336 }
1337 }
1338 }
Adam Cohen482ed822012-03-02 14:15:13 -08001339
Adam Cohend41fbf52012-02-16 23:53:59 -08001340 private void recycleTempRects(Stack<Rect> used) {
1341 while (!used.isEmpty()) {
1342 mTempRectStack.push(used.pop());
1343 }
1344 }
1345
1346 /**
1347 * Find a vacant area that will fit the given bounds nearest the requested
1348 * cell location. Uses Euclidean distance to score multiple vacant areas.
1349 *
1350 * @param pixelX The X location at which you want to search for a vacant area.
1351 * @param pixelY The Y location at which you want to search for a vacant area.
1352 * @param minSpanX The minimum horizontal span required
1353 * @param minSpanY The minimum vertical span required
1354 * @param spanX Horizontal span of the object.
1355 * @param spanY Vertical span of the object.
1356 * @param ignoreOccupied If true, the result can be an occupied cell
1357 * @param result Array in which to place the result, or null (in which case a new array will
1358 * be allocated)
1359 * @return The X, Y cell of a vacant area that can contain this object,
1360 * nearest the requested location.
1361 */
1362 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001363 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1364 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001365 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001366 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001367 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001368
Adam Cohene3e27a82011-04-15 12:07:39 -07001369 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1370 // to the center of the item, but we are searching based on the top-left cell, so
1371 // we translate the point over to correspond to the top-left.
1372 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1373 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1374
Jeff Sharkey70864282009-04-07 21:08:40 -07001375 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001376 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001377 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001378 final Rect bestRect = new Rect(-1, -1, -1, -1);
1379 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001380
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001381 final int countX = mCountX;
1382 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001383
Adam Cohend41fbf52012-02-16 23:53:59 -08001384 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1385 spanX < minSpanX || spanY < minSpanY) {
1386 return bestXY;
1387 }
1388
1389 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001390 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001391 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1392 int ySize = -1;
1393 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001394 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001395 // First, let's see if this thing fits anywhere
1396 for (int i = 0; i < minSpanX; i++) {
1397 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001398 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001399 continue inner;
1400 }
Michael Jurkac28de512010-08-13 11:27:44 -07001401 }
1402 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001403 xSize = minSpanX;
1404 ySize = minSpanY;
1405
1406 // We know that the item will fit at _some_ acceptable size, now let's see
1407 // how big we can make it. We'll alternate between incrementing x and y spans
1408 // until we hit a limit.
1409 boolean incX = true;
1410 boolean hitMaxX = xSize >= spanX;
1411 boolean hitMaxY = ySize >= spanY;
1412 while (!(hitMaxX && hitMaxY)) {
1413 if (incX && !hitMaxX) {
1414 for (int j = 0; j < ySize; j++) {
1415 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1416 // We can't move out horizontally
1417 hitMaxX = true;
1418 }
1419 }
1420 if (!hitMaxX) {
1421 xSize++;
1422 }
1423 } else if (!hitMaxY) {
1424 for (int i = 0; i < xSize; i++) {
1425 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1426 // We can't move out vertically
1427 hitMaxY = true;
1428 }
1429 }
1430 if (!hitMaxY) {
1431 ySize++;
1432 }
1433 }
1434 hitMaxX |= xSize >= spanX;
1435 hitMaxY |= ySize >= spanY;
1436 incX = !incX;
1437 }
1438 incX = true;
1439 hitMaxX = xSize >= spanX;
1440 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001441 }
Winson Chung0be025d2011-05-23 17:45:09 -07001442 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001443 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444
Adam Cohend41fbf52012-02-16 23:53:59 -08001445 // We verify that the current rect is not a sub-rect of any of our previous
1446 // candidates. In this case, the current rect is disqualified in favour of the
1447 // containing rect.
1448 Rect currentRect = mTempRectStack.pop();
1449 currentRect.set(x, y, x + xSize, y + ySize);
1450 boolean contained = false;
1451 for (Rect r : validRegions) {
1452 if (r.contains(currentRect)) {
1453 contained = true;
1454 break;
1455 }
1456 }
1457 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001458 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1459 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001460
Adam Cohend41fbf52012-02-16 23:53:59 -08001461 if ((distance <= bestDistance && !contained) ||
1462 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001463 bestDistance = distance;
1464 bestXY[0] = x;
1465 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001466 if (resultSpan != null) {
1467 resultSpan[0] = xSize;
1468 resultSpan[1] = ySize;
1469 }
1470 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001474 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001475 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476
Adam Cohenc0dcf592011-06-01 15:30:43 -07001477 // Return -1, -1 if no suitable location found
1478 if (bestDistance == Double.MAX_VALUE) {
1479 bestXY[0] = -1;
1480 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001481 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001482 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001483 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001485
Adam Cohen482ed822012-03-02 14:15:13 -08001486 /**
1487 * Find a vacant area that will fit the given bounds nearest the requested
1488 * cell location, and will also weigh in a suggested direction vector of the
1489 * desired location. This method computers distance based on unit grid distances,
1490 * not pixel distances.
1491 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001492 * @param cellX The X cell nearest to which you want to search for a vacant area.
1493 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001494 * @param spanX Horizontal span of the object.
1495 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001496 * @param direction The favored direction in which the views should move from x, y
1497 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1498 * matches exactly. Otherwise we find the best matching direction.
1499 * @param occoupied The array which represents which cells in the CellLayout are occupied
1500 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001501 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001502 * @param result Array in which to place the result, or null (in which case a new array will
1503 * be allocated)
1504 * @return The X, Y cell of a vacant area that can contain this object,
1505 * nearest the requested location.
1506 */
1507 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001508 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001509 // Keep track of best-scoring drop area
1510 final int[] bestXY = result != null ? result : new int[2];
1511 float bestDistance = Float.MAX_VALUE;
1512 int bestDirectionScore = Integer.MIN_VALUE;
1513
1514 final int countX = mCountX;
1515 final int countY = mCountY;
1516
1517 for (int y = 0; y < countY - (spanY - 1); y++) {
1518 inner:
1519 for (int x = 0; x < countX - (spanX - 1); x++) {
1520 // First, let's see if this thing fits anywhere
1521 for (int i = 0; i < spanX; i++) {
1522 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001523 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001524 continue inner;
1525 }
1526 }
1527 }
1528
1529 float distance = (float)
1530 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1531 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001532 computeDirectionVector(x - cellX, y - cellY, curDirection);
1533 // The direction score is just the dot product of the two candidate direction
1534 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001535 int curDirectionScore = direction[0] * curDirection[0] +
1536 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001537 boolean exactDirectionOnly = false;
1538 boolean directionMatches = direction[0] == curDirection[0] &&
1539 direction[0] == curDirection[0];
1540 if ((directionMatches || !exactDirectionOnly) &&
1541 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001542 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1543 bestDistance = distance;
1544 bestDirectionScore = curDirectionScore;
1545 bestXY[0] = x;
1546 bestXY[1] = y;
1547 }
1548 }
1549 }
1550
1551 // Return -1, -1 if no suitable location found
1552 if (bestDistance == Float.MAX_VALUE) {
1553 bestXY[0] = -1;
1554 bestXY[1] = -1;
1555 }
1556 return bestXY;
1557 }
1558
1559 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001560 int[] direction, ItemConfiguration currentState) {
1561 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001562 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001563 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001564 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1565
Adam Cohen8baab352012-03-20 17:39:21 -07001566 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001567
1568 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001569 c.x = mTempLocation[0];
1570 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001571 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001572 }
Adam Cohen8baab352012-03-20 17:39:21 -07001573 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001574 return success;
1575 }
1576
Adam Cohenf3900c22012-11-16 18:28:11 -08001577 /**
1578 * This helper class defines a cluster of views. It helps with defining complex edges
1579 * of the cluster and determining how those edges interact with other views. The edges
1580 * essentially define a fine-grained boundary around the cluster of views -- like a more
1581 * precise version of a bounding box.
1582 */
1583 private class ViewCluster {
1584 final static int LEFT = 0;
1585 final static int TOP = 1;
1586 final static int RIGHT = 2;
1587 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001588
Adam Cohenf3900c22012-11-16 18:28:11 -08001589 ArrayList<View> views;
1590 ItemConfiguration config;
1591 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001592
Adam Cohenf3900c22012-11-16 18:28:11 -08001593 int[] leftEdge = new int[mCountY];
1594 int[] rightEdge = new int[mCountY];
1595 int[] topEdge = new int[mCountX];
1596 int[] bottomEdge = new int[mCountX];
1597 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1598
1599 @SuppressWarnings("unchecked")
1600 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1601 this.views = (ArrayList<View>) views.clone();
1602 this.config = config;
1603 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001604 }
1605
Adam Cohenf3900c22012-11-16 18:28:11 -08001606 void resetEdges() {
1607 for (int i = 0; i < mCountX; i++) {
1608 topEdge[i] = -1;
1609 bottomEdge[i] = -1;
1610 }
1611 for (int i = 0; i < mCountY; i++) {
1612 leftEdge[i] = -1;
1613 rightEdge[i] = -1;
1614 }
1615 leftEdgeDirty = true;
1616 rightEdgeDirty = true;
1617 bottomEdgeDirty = true;
1618 topEdgeDirty = true;
1619 boundingRectDirty = true;
1620 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001621
Adam Cohenf3900c22012-11-16 18:28:11 -08001622 void computeEdge(int which, int[] edge) {
1623 int count = views.size();
1624 for (int i = 0; i < count; i++) {
1625 CellAndSpan cs = config.map.get(views.get(i));
1626 switch (which) {
1627 case LEFT:
1628 int left = cs.x;
1629 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1630 if (left < edge[j] || edge[j] < 0) {
1631 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001632 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001633 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001634 break;
1635 case RIGHT:
1636 int right = cs.x + cs.spanX;
1637 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1638 if (right > edge[j]) {
1639 edge[j] = right;
1640 }
1641 }
1642 break;
1643 case TOP:
1644 int top = cs.y;
1645 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1646 if (top < edge[j] || edge[j] < 0) {
1647 edge[j] = top;
1648 }
1649 }
1650 break;
1651 case BOTTOM:
1652 int bottom = cs.y + cs.spanY;
1653 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1654 if (bottom > edge[j]) {
1655 edge[j] = bottom;
1656 }
1657 }
1658 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001659 }
1660 }
1661 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001662
1663 boolean isViewTouchingEdge(View v, int whichEdge) {
1664 CellAndSpan cs = config.map.get(v);
1665
1666 int[] edge = getEdge(whichEdge);
1667
1668 switch (whichEdge) {
1669 case LEFT:
1670 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1671 if (edge[i] == cs.x + cs.spanX) {
1672 return true;
1673 }
1674 }
1675 break;
1676 case RIGHT:
1677 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1678 if (edge[i] == cs.x) {
1679 return true;
1680 }
1681 }
1682 break;
1683 case TOP:
1684 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1685 if (edge[i] == cs.y + cs.spanY) {
1686 return true;
1687 }
1688 }
1689 break;
1690 case BOTTOM:
1691 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1692 if (edge[i] == cs.y) {
1693 return true;
1694 }
1695 }
1696 break;
1697 }
1698 return false;
1699 }
1700
1701 void shift(int whichEdge, int delta) {
1702 for (View v: views) {
1703 CellAndSpan c = config.map.get(v);
1704 switch (whichEdge) {
1705 case LEFT:
1706 c.x -= delta;
1707 break;
1708 case RIGHT:
1709 c.x += delta;
1710 break;
1711 case TOP:
1712 c.y -= delta;
1713 break;
1714 case BOTTOM:
1715 default:
1716 c.y += delta;
1717 break;
1718 }
1719 }
1720 resetEdges();
1721 }
1722
1723 public void addView(View v) {
1724 views.add(v);
1725 resetEdges();
1726 }
1727
1728 public Rect getBoundingRect() {
1729 if (boundingRectDirty) {
1730 boolean first = true;
1731 for (View v: views) {
1732 CellAndSpan c = config.map.get(v);
1733 if (first) {
1734 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1735 first = false;
1736 } else {
1737 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1738 }
1739 }
1740 }
1741 return boundingRect;
1742 }
1743
1744 public int[] getEdge(int which) {
1745 switch (which) {
1746 case LEFT:
1747 return getLeftEdge();
1748 case RIGHT:
1749 return getRightEdge();
1750 case TOP:
1751 return getTopEdge();
1752 case BOTTOM:
1753 default:
1754 return getBottomEdge();
1755 }
1756 }
1757
1758 public int[] getLeftEdge() {
1759 if (leftEdgeDirty) {
1760 computeEdge(LEFT, leftEdge);
1761 }
1762 return leftEdge;
1763 }
1764
1765 public int[] getRightEdge() {
1766 if (rightEdgeDirty) {
1767 computeEdge(RIGHT, rightEdge);
1768 }
1769 return rightEdge;
1770 }
1771
1772 public int[] getTopEdge() {
1773 if (topEdgeDirty) {
1774 computeEdge(TOP, topEdge);
1775 }
1776 return topEdge;
1777 }
1778
1779 public int[] getBottomEdge() {
1780 if (bottomEdgeDirty) {
1781 computeEdge(BOTTOM, bottomEdge);
1782 }
1783 return bottomEdge;
1784 }
1785
1786 PositionComparator comparator = new PositionComparator();
1787 class PositionComparator implements Comparator<View> {
1788 int whichEdge = 0;
1789 public int compare(View left, View right) {
1790 CellAndSpan l = config.map.get(left);
1791 CellAndSpan r = config.map.get(right);
1792 switch (whichEdge) {
1793 case LEFT:
1794 return (r.x + r.spanX) - (l.x + l.spanX);
1795 case RIGHT:
1796 return l.x - r.x;
1797 case TOP:
1798 return (r.y + r.spanY) - (l.y + l.spanY);
1799 case BOTTOM:
1800 default:
1801 return l.y - r.y;
1802 }
1803 }
1804 }
1805
1806 public void sortConfigurationForEdgePush(int edge) {
1807 comparator.whichEdge = edge;
1808 Collections.sort(config.sortedViews, comparator);
1809 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001810 }
1811
Adam Cohenf3900c22012-11-16 18:28:11 -08001812 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1813 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001814
Adam Cohenf3900c22012-11-16 18:28:11 -08001815 ViewCluster cluster = new ViewCluster(views, currentState);
1816 Rect clusterRect = cluster.getBoundingRect();
1817 int whichEdge;
1818 int pushDistance;
1819 boolean fail = false;
1820
1821 // Determine the edge of the cluster that will be leading the push and how far
1822 // the cluster must be shifted.
1823 if (direction[0] < 0) {
1824 whichEdge = ViewCluster.LEFT;
1825 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001826 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001827 whichEdge = ViewCluster.RIGHT;
1828 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1829 } else if (direction[1] < 0) {
1830 whichEdge = ViewCluster.TOP;
1831 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1832 } else {
1833 whichEdge = ViewCluster.BOTTOM;
1834 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001835 }
1836
Adam Cohenf3900c22012-11-16 18:28:11 -08001837 // Break early for invalid push distance.
1838 if (pushDistance <= 0) {
1839 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001840 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001841
1842 // Mark the occupied state as false for the group of views we want to move.
1843 for (View v: views) {
1844 CellAndSpan c = currentState.map.get(v);
1845 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1846 }
1847
1848 // We save the current configuration -- if we fail to find a solution we will revert
1849 // to the initial state. The process of finding a solution modifies the configuration
1850 // in place, hence the need for revert in the failure case.
1851 currentState.save();
1852
1853 // The pushing algorithm is simplified by considering the views in the order in which
1854 // they would be pushed by the cluster. For example, if the cluster is leading with its
1855 // left edge, we consider sort the views by their right edge, from right to left.
1856 cluster.sortConfigurationForEdgePush(whichEdge);
1857
1858 while (pushDistance > 0 && !fail) {
1859 for (View v: currentState.sortedViews) {
1860 // For each view that isn't in the cluster, we see if the leading edge of the
1861 // cluster is contacting the edge of that view. If so, we add that view to the
1862 // cluster.
1863 if (!cluster.views.contains(v) && v != dragView) {
1864 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1865 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1866 if (!lp.canReorder) {
1867 // The push solution includes the all apps button, this is not viable.
1868 fail = true;
1869 break;
1870 }
1871 cluster.addView(v);
1872 CellAndSpan c = currentState.map.get(v);
1873
1874 // Adding view to cluster, mark it as not occupied.
1875 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1876 }
1877 }
1878 }
1879 pushDistance--;
1880
1881 // The cluster has been completed, now we move the whole thing over in the appropriate
1882 // direction.
1883 cluster.shift(whichEdge, 1);
1884 }
1885
1886 boolean foundSolution = false;
1887 clusterRect = cluster.getBoundingRect();
1888
1889 // Due to the nature of the algorithm, the only check required to verify a valid solution
1890 // is to ensure that completed shifted cluster lies completely within the cell layout.
1891 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1892 clusterRect.bottom <= mCountY) {
1893 foundSolution = true;
1894 } else {
1895 currentState.restore();
1896 }
1897
1898 // In either case, we set the occupied array as marked for the location of the views
1899 for (View v: cluster.views) {
1900 CellAndSpan c = currentState.map.get(v);
1901 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1902 }
1903
1904 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001905 }
1906
Adam Cohen482ed822012-03-02 14:15:13 -08001907 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001908 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001909 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001910
Adam Cohen8baab352012-03-20 17:39:21 -07001911 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001912 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001913 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001914 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001915 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001916 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001917 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001918 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001919 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001920 }
1921 }
Adam Cohen8baab352012-03-20 17:39:21 -07001922
Adam Cohen8baab352012-03-20 17:39:21 -07001923 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001924 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001925 CellAndSpan c = currentState.map.get(v);
1926 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1927 }
1928
Adam Cohen47a876d2012-03-19 13:21:41 -07001929 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1930 int top = boundingRect.top;
1931 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001932 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001933 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001934 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001935 CellAndSpan c = currentState.map.get(v);
1936 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001937 }
1938
Adam Cohen482ed822012-03-02 14:15:13 -08001939 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1940
Adam Cohenf3900c22012-11-16 18:28:11 -08001941 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1942 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001943
Adam Cohen8baab352012-03-20 17:39:21 -07001944 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001945 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001946 int deltaX = mTempLocation[0] - boundingRect.left;
1947 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001948 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001949 CellAndSpan c = currentState.map.get(v);
1950 c.x += deltaX;
1951 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001952 }
1953 success = true;
1954 }
Adam Cohen8baab352012-03-20 17:39:21 -07001955
1956 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001957 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001958 CellAndSpan c = currentState.map.get(v);
1959 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001960 }
1961 return success;
1962 }
1963
1964 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1965 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1966 }
1967
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001968 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1969 // to push items in each of the cardinal directions, in an order based on the direction vector
1970 // passed.
1971 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1972 int[] direction, View ignoreView, ItemConfiguration solution) {
1973 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001974 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001975 // separately in each of the components.
1976 int temp = direction[1];
1977 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001978
1979 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001980 ignoreView, solution)) {
1981 return true;
1982 }
1983 direction[1] = temp;
1984 temp = direction[0];
1985 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001986
1987 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001988 ignoreView, solution)) {
1989 return true;
1990 }
1991 // Revert the direction
1992 direction[0] = temp;
1993
1994 // Now we try pushing in each component of the opposite direction
1995 direction[0] *= -1;
1996 direction[1] *= -1;
1997 temp = direction[1];
1998 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001999 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002000 ignoreView, solution)) {
2001 return true;
2002 }
2003
2004 direction[1] = temp;
2005 temp = direction[0];
2006 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002007 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002008 ignoreView, solution)) {
2009 return true;
2010 }
2011 // revert the direction
2012 direction[0] = temp;
2013 direction[0] *= -1;
2014 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002015
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002016 } else {
2017 // If the direction vector has a single non-zero component, we push first in the
2018 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002019 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002020 ignoreView, solution)) {
2021 return true;
2022 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002023 // Then we try the opposite direction
2024 direction[0] *= -1;
2025 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002026 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002027 ignoreView, solution)) {
2028 return true;
2029 }
2030 // Switch the direction back
2031 direction[0] *= -1;
2032 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002033
2034 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002035 // to find a solution by pushing along the perpendicular axis.
2036
2037 // Swap the components
2038 int temp = direction[1];
2039 direction[1] = direction[0];
2040 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002041 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002042 ignoreView, solution)) {
2043 return true;
2044 }
2045
2046 // Then we try the opposite direction
2047 direction[0] *= -1;
2048 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002049 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002050 ignoreView, solution)) {
2051 return true;
2052 }
2053 // Switch the direction back
2054 direction[0] *= -1;
2055 direction[1] *= -1;
2056
2057 // Swap the components back
2058 temp = direction[1];
2059 direction[1] = direction[0];
2060 direction[0] = temp;
2061 }
2062 return false;
2063 }
2064
Adam Cohen482ed822012-03-02 14:15:13 -08002065 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002066 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002067 // Return early if get invalid cell positions
2068 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002069
Adam Cohen8baab352012-03-20 17:39:21 -07002070 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002071 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002072
Adam Cohen8baab352012-03-20 17:39:21 -07002073 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002074 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002075 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002076 if (c != null) {
2077 c.x = cellX;
2078 c.y = cellY;
2079 }
Adam Cohen482ed822012-03-02 14:15:13 -08002080 }
Adam Cohen482ed822012-03-02 14:15:13 -08002081 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2082 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002083 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002084 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002085 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002086 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002087 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002088 if (Rect.intersects(r0, r1)) {
2089 if (!lp.canReorder) {
2090 return false;
2091 }
2092 mIntersectingViews.add(child);
2093 }
2094 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002095
Winson Chung5f8afe62013-08-12 16:19:28 -07002096 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002097 // we try to find a solution such that no displaced item travels through another item
2098 // without also displacing that item.
2099 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002100 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002101 return true;
2102 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002103
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002104 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002105 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002106 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002107 return true;
2108 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002109
Adam Cohen482ed822012-03-02 14:15:13 -08002110 // Ok, they couldn't move as a block, let's move them individually
2111 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002112 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002113 return false;
2114 }
2115 }
2116 return true;
2117 }
2118
2119 /*
2120 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2121 * the provided point and the provided cell
2122 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002123 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002124 double angle = Math.atan(((float) deltaY) / deltaX);
2125
2126 result[0] = 0;
2127 result[1] = 0;
2128 if (Math.abs(Math.cos(angle)) > 0.5f) {
2129 result[0] = (int) Math.signum(deltaX);
2130 }
2131 if (Math.abs(Math.sin(angle)) > 0.5f) {
2132 result[1] = (int) Math.signum(deltaY);
2133 }
2134 }
2135
Adam Cohen8baab352012-03-20 17:39:21 -07002136 private void copyOccupiedArray(boolean[][] occupied) {
2137 for (int i = 0; i < mCountX; i++) {
2138 for (int j = 0; j < mCountY; j++) {
2139 occupied[i][j] = mOccupied[i][j];
2140 }
2141 }
2142 }
2143
Adam Cohen482ed822012-03-02 14:15:13 -08002144 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
2145 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002146 // Copy the current state into the solution. This solution will be manipulated as necessary.
2147 copyCurrentStateToSolution(solution, false);
2148 // Copy the current occupied array into the temporary occupied array. This array will be
2149 // manipulated as necessary to find a solution.
2150 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002151
2152 // We find the nearest cell into which we would place the dragged item, assuming there's
2153 // nothing in its way.
2154 int result[] = new int[2];
2155 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2156
2157 boolean success = false;
2158 // First we try the exact nearest position of the item being dragged,
2159 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002160 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2161 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002162
2163 if (!success) {
2164 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2165 // x, then 1 in y etc.
2166 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
2167 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
2168 dragView, false, solution);
2169 } else if (spanY > minSpanY) {
2170 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
2171 dragView, true, solution);
2172 }
2173 solution.isSolution = false;
2174 } else {
2175 solution.isSolution = true;
2176 solution.dragViewX = result[0];
2177 solution.dragViewY = result[1];
2178 solution.dragViewSpanX = spanX;
2179 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002180 }
2181 return solution;
2182 }
2183
2184 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002185 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002186 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002187 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002188 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002189 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002190 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002191 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002192 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002193 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002194 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002195 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002196 }
2197 }
2198
2199 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2200 for (int i = 0; i < mCountX; i++) {
2201 for (int j = 0; j < mCountY; j++) {
2202 mTmpOccupied[i][j] = false;
2203 }
2204 }
2205
Michael Jurkaa52570f2012-03-20 03:18:20 -07002206 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002207 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002208 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002209 if (child == dragView) continue;
2210 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002211 CellAndSpan c = solution.map.get(child);
2212 if (c != null) {
2213 lp.tmpCellX = c.x;
2214 lp.tmpCellY = c.y;
2215 lp.cellHSpan = c.spanX;
2216 lp.cellVSpan = c.spanY;
2217 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002218 }
2219 }
2220 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2221 solution.dragViewSpanY, mTmpOccupied, true);
2222 }
2223
2224 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2225 commitDragView) {
2226
2227 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2228 for (int i = 0; i < mCountX; i++) {
2229 for (int j = 0; j < mCountY; j++) {
2230 occupied[i][j] = false;
2231 }
2232 }
2233
Michael Jurkaa52570f2012-03-20 03:18:20 -07002234 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002235 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002236 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002237 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002238 CellAndSpan c = solution.map.get(child);
2239 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002240 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2241 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002242 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002243 }
2244 }
2245 if (commitDragView) {
2246 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2247 solution.dragViewSpanY, occupied, true);
2248 }
2249 }
2250
Adam Cohen19f37922012-03-21 11:59:11 -07002251 // This method starts or changes the reorder hint animations
2252 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2253 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002254 for (int i = 0; i < childCount; i++) {
2255 View child = mShortcutsAndWidgets.getChildAt(i);
2256 if (child == dragView) continue;
2257 CellAndSpan c = solution.map.get(child);
2258 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2259 if (c != null) {
2260 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2261 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002262 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002263 }
2264 }
2265 }
2266
2267 // Class which represents the reorder hint animations. These animations show that an item is
2268 // in a temporary state, and hint at where the item will return to.
2269 class ReorderHintAnimation {
2270 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002271 float finalDeltaX;
2272 float finalDeltaY;
2273 float initDeltaX;
2274 float initDeltaY;
2275 float finalScale;
2276 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002277 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002278 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002279
2280 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2281 int spanX, int spanY) {
2282 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2283 final int x0 = mTmpPoint[0];
2284 final int y0 = mTmpPoint[1];
2285 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2286 final int x1 = mTmpPoint[0];
2287 final int y1 = mTmpPoint[1];
2288 final int dX = x1 - x0;
2289 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002290 finalDeltaX = 0;
2291 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002292 if (dX == dY && dX == 0) {
2293 } else {
2294 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002295 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002296 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002297 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002298 } else {
2299 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002300 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002301 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002302 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002303 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002304 }
2305 }
Adam Cohend024f982012-05-23 18:26:45 -07002306 initDeltaX = child.getTranslationX();
2307 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002308 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002309 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002310 this.child = child;
2311 }
2312
Adam Cohend024f982012-05-23 18:26:45 -07002313 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002314 if (mShakeAnimators.containsKey(child)) {
2315 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002316 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002317 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002318 if (finalDeltaX == 0 && finalDeltaY == 0) {
2319 completeAnimationImmediately();
2320 return;
2321 }
Adam Cohen19f37922012-03-21 11:59:11 -07002322 }
Adam Cohend024f982012-05-23 18:26:45 -07002323 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002324 return;
2325 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002326 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002327 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002328 va.setRepeatMode(ValueAnimator.REVERSE);
2329 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002330 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002331 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002332 va.addUpdateListener(new AnimatorUpdateListener() {
2333 @Override
2334 public void onAnimationUpdate(ValueAnimator animation) {
2335 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002336 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2337 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002338 child.setTranslationX(x);
2339 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002340 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002341 child.setScaleX(s);
2342 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002343 }
2344 });
2345 va.addListener(new AnimatorListenerAdapter() {
2346 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002347 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002348 initDeltaX = 0;
2349 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002350 initScale = getChildrenScale();
Adam Cohen19f37922012-03-21 11:59:11 -07002351 }
2352 });
Adam Cohen19f37922012-03-21 11:59:11 -07002353 mShakeAnimators.put(child, this);
2354 va.start();
2355 }
2356
Adam Cohend024f982012-05-23 18:26:45 -07002357 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002358 if (a != null) {
2359 a.cancel();
2360 }
Adam Cohen19f37922012-03-21 11:59:11 -07002361 }
Adam Cohene7587d22012-05-24 18:50:02 -07002362
Brandon Keely50e6e562012-05-08 16:28:49 -07002363 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002364 if (a != null) {
2365 a.cancel();
2366 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002367
Michael Jurka2ecf9952012-06-18 12:52:28 -07002368 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002369 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002370 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002371 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2372 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002373 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2374 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002375 );
2376 s.setDuration(REORDER_ANIMATION_DURATION);
2377 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2378 s.start();
2379 }
Adam Cohen19f37922012-03-21 11:59:11 -07002380 }
2381
2382 private void completeAndClearReorderHintAnimations() {
2383 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002384 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002385 }
2386 mShakeAnimators.clear();
2387 }
2388
Adam Cohen482ed822012-03-02 14:15:13 -08002389 private void commitTempPlacement() {
2390 for (int i = 0; i < mCountX; i++) {
2391 for (int j = 0; j < mCountY; j++) {
2392 mOccupied[i][j] = mTmpOccupied[i][j];
2393 }
2394 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002395 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002396 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002397 View child = mShortcutsAndWidgets.getChildAt(i);
2398 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2399 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002400 // We do a null check here because the item info can be null in the case of the
2401 // AllApps button in the hotseat.
2402 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002403 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2404 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2405 info.requiresDbUpdate = true;
2406 }
Adam Cohen2acce882012-03-28 19:03:19 -07002407 info.cellX = lp.cellX = lp.tmpCellX;
2408 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002409 info.spanX = lp.cellHSpan;
2410 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002411 }
Adam Cohen482ed822012-03-02 14:15:13 -08002412 }
Adam Cohen2acce882012-03-28 19:03:19 -07002413 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002414 }
2415
2416 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002417 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002418 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002419 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002420 lp.useTmpCoords = useTempCoords;
2421 }
2422 }
2423
Adam Cohen482ed822012-03-02 14:15:13 -08002424 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2425 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2426 int[] result = new int[2];
2427 int[] resultSpan = new int[2];
2428 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2429 resultSpan);
2430 if (result[0] >= 0 && result[1] >= 0) {
2431 copyCurrentStateToSolution(solution, false);
2432 solution.dragViewX = result[0];
2433 solution.dragViewY = result[1];
2434 solution.dragViewSpanX = resultSpan[0];
2435 solution.dragViewSpanY = resultSpan[1];
2436 solution.isSolution = true;
2437 } else {
2438 solution.isSolution = false;
2439 }
2440 return solution;
2441 }
2442
2443 public void prepareChildForDrag(View child) {
2444 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002445 }
2446
Adam Cohen19f37922012-03-21 11:59:11 -07002447 /* This seems like it should be obvious and straight-forward, but when the direction vector
2448 needs to match with the notion of the dragView pushing other views, we have to employ
2449 a slightly more subtle notion of the direction vector. The question is what two points is
2450 the vector between? The center of the dragView and its desired destination? Not quite, as
2451 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2452 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2453 or right, which helps make pushing feel right.
2454 */
2455 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2456 int spanY, View dragView, int[] resultDirection) {
2457 int[] targetDestination = new int[2];
2458
2459 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2460 Rect dragRect = new Rect();
2461 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2462 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2463
2464 Rect dropRegionRect = new Rect();
2465 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2466 dragView, dropRegionRect, mIntersectingViews);
2467
2468 int dropRegionSpanX = dropRegionRect.width();
2469 int dropRegionSpanY = dropRegionRect.height();
2470
2471 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2472 dropRegionRect.height(), dropRegionRect);
2473
2474 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2475 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2476
2477 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2478 deltaX = 0;
2479 }
2480 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2481 deltaY = 0;
2482 }
2483
2484 if (deltaX == 0 && deltaY == 0) {
2485 // No idea what to do, give a random direction.
2486 resultDirection[0] = 1;
2487 resultDirection[1] = 0;
2488 } else {
2489 computeDirectionVector(deltaX, deltaY, resultDirection);
2490 }
2491 }
2492
2493 // For a given cell and span, fetch the set of views intersecting the region.
2494 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2495 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2496 if (boundingRect != null) {
2497 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2498 }
2499 intersectingViews.clear();
2500 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2501 Rect r1 = new Rect();
2502 final int count = mShortcutsAndWidgets.getChildCount();
2503 for (int i = 0; i < count; i++) {
2504 View child = mShortcutsAndWidgets.getChildAt(i);
2505 if (child == dragView) continue;
2506 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2507 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2508 if (Rect.intersects(r0, r1)) {
2509 mIntersectingViews.add(child);
2510 if (boundingRect != null) {
2511 boundingRect.union(r1);
2512 }
2513 }
2514 }
2515 }
2516
2517 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2518 View dragView, int[] result) {
2519 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2520 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2521 mIntersectingViews);
2522 return !mIntersectingViews.isEmpty();
2523 }
2524
2525 void revertTempState() {
2526 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2527 final int count = mShortcutsAndWidgets.getChildCount();
2528 for (int i = 0; i < count; i++) {
2529 View child = mShortcutsAndWidgets.getChildAt(i);
2530 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2531 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2532 lp.tmpCellX = lp.cellX;
2533 lp.tmpCellY = lp.cellY;
2534 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2535 0, false, false);
2536 }
2537 }
2538 completeAndClearReorderHintAnimations();
2539 setItemPlacementDirty(false);
2540 }
2541
Adam Cohenbebf0422012-04-11 18:06:28 -07002542 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2543 View dragView, int[] direction, boolean commit) {
2544 int[] pixelXY = new int[2];
2545 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2546
2547 // First we determine if things have moved enough to cause a different layout
2548 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2549 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2550
2551 setUseTempCoords(true);
2552 if (swapSolution != null && swapSolution.isSolution) {
2553 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2554 // committing anything or animating anything as we just want to determine if a solution
2555 // exists
2556 copySolutionToTempState(swapSolution, dragView);
2557 setItemPlacementDirty(true);
2558 animateItemsToSolution(swapSolution, dragView, commit);
2559
2560 if (commit) {
2561 commitTempPlacement();
2562 completeAndClearReorderHintAnimations();
2563 setItemPlacementDirty(false);
2564 } else {
2565 beginOrAdjustHintAnimations(swapSolution, dragView,
2566 REORDER_ANIMATION_DURATION);
2567 }
2568 mShortcutsAndWidgets.requestLayout();
2569 }
2570 return swapSolution.isSolution;
2571 }
2572
Adam Cohen482ed822012-03-02 14:15:13 -08002573 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2574 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002575 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002576 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002577
2578 if (resultSpan == null) {
2579 resultSpan = new int[2];
2580 }
2581
Adam Cohen19f37922012-03-21 11:59:11 -07002582 // When we are checking drop validity or actually dropping, we don't recompute the
2583 // direction vector, since we want the solution to match the preview, and it's possible
2584 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002585 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2586 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002587 mDirectionVector[0] = mPreviousReorderDirection[0];
2588 mDirectionVector[1] = mPreviousReorderDirection[1];
2589 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002590 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2591 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2592 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002593 }
2594 } else {
2595 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2596 mPreviousReorderDirection[0] = mDirectionVector[0];
2597 mPreviousReorderDirection[1] = mDirectionVector[1];
2598 }
2599
Adam Cohen482ed822012-03-02 14:15:13 -08002600 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2601 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2602
2603 // We attempt the approach which doesn't shuffle views at all
2604 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2605 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2606
2607 ItemConfiguration finalSolution = null;
2608 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2609 finalSolution = swapSolution;
2610 } else if (noShuffleSolution.isSolution) {
2611 finalSolution = noShuffleSolution;
2612 }
2613
2614 boolean foundSolution = true;
2615 if (!DESTRUCTIVE_REORDER) {
2616 setUseTempCoords(true);
2617 }
2618
2619 if (finalSolution != null) {
2620 result[0] = finalSolution.dragViewX;
2621 result[1] = finalSolution.dragViewY;
2622 resultSpan[0] = finalSolution.dragViewSpanX;
2623 resultSpan[1] = finalSolution.dragViewSpanY;
2624
2625 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2626 // committing anything or animating anything as we just want to determine if a solution
2627 // exists
2628 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2629 if (!DESTRUCTIVE_REORDER) {
2630 copySolutionToTempState(finalSolution, dragView);
2631 }
2632 setItemPlacementDirty(true);
2633 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2634
Adam Cohen19f37922012-03-21 11:59:11 -07002635 if (!DESTRUCTIVE_REORDER &&
2636 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002637 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002638 completeAndClearReorderHintAnimations();
2639 setItemPlacementDirty(false);
2640 } else {
2641 beginOrAdjustHintAnimations(finalSolution, dragView,
2642 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002643 }
2644 }
2645 } else {
2646 foundSolution = false;
2647 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2648 }
2649
2650 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2651 setUseTempCoords(false);
2652 }
Adam Cohen482ed822012-03-02 14:15:13 -08002653
Michael Jurkaa52570f2012-03-20 03:18:20 -07002654 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002655 return result;
2656 }
2657
Adam Cohen19f37922012-03-21 11:59:11 -07002658 void setItemPlacementDirty(boolean dirty) {
2659 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002660 }
Adam Cohen19f37922012-03-21 11:59:11 -07002661 boolean isItemPlacementDirty() {
2662 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002663 }
2664
2665 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002666 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002667 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2668 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohen482ed822012-03-02 14:15:13 -08002669 boolean isSolution = false;
2670 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2671
Adam Cohenf3900c22012-11-16 18:28:11 -08002672 void save() {
2673 // Copy current state into savedMap
2674 for (View v: map.keySet()) {
2675 map.get(v).copy(savedMap.get(v));
2676 }
2677 }
2678
2679 void restore() {
2680 // Restore current state from savedMap
2681 for (View v: savedMap.keySet()) {
2682 savedMap.get(v).copy(map.get(v));
2683 }
2684 }
2685
2686 void add(View v, CellAndSpan cs) {
2687 map.put(v, cs);
2688 savedMap.put(v, new CellAndSpan());
2689 sortedViews.add(v);
2690 }
2691
Adam Cohen482ed822012-03-02 14:15:13 -08002692 int area() {
2693 return dragViewSpanX * dragViewSpanY;
2694 }
Adam Cohen8baab352012-03-20 17:39:21 -07002695 }
2696
2697 private class CellAndSpan {
2698 int x, y;
2699 int spanX, spanY;
2700
Adam Cohenf3900c22012-11-16 18:28:11 -08002701 public CellAndSpan() {
2702 }
2703
2704 public void copy(CellAndSpan copy) {
2705 copy.x = x;
2706 copy.y = y;
2707 copy.spanX = spanX;
2708 copy.spanY = spanY;
2709 }
2710
Adam Cohen8baab352012-03-20 17:39:21 -07002711 public CellAndSpan(int x, int y, int spanX, int spanY) {
2712 this.x = x;
2713 this.y = y;
2714 this.spanX = spanX;
2715 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002716 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002717
2718 public String toString() {
2719 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2720 }
2721
Adam Cohen482ed822012-03-02 14:15:13 -08002722 }
2723
Adam Cohendf035382011-04-11 17:22:04 -07002724 /**
2725 * Find a vacant area that will fit the given bounds nearest the requested
2726 * cell location. Uses Euclidean distance to score multiple vacant areas.
2727 *
2728 * @param pixelX The X location at which you want to search for a vacant area.
2729 * @param pixelY The Y location at which you want to search for a vacant area.
2730 * @param spanX Horizontal span of the object.
2731 * @param spanY Vertical span of the object.
2732 * @param ignoreView Considers space occupied by this view as unoccupied
2733 * @param result Previously returned value to possibly recycle.
2734 * @return The X, Y cell of a vacant area that can contain this object,
2735 * nearest the requested location.
2736 */
2737 int[] findNearestVacantArea(
2738 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2739 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2740 }
2741
2742 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002743 * Find a vacant area that will fit the given bounds nearest the requested
2744 * cell location. Uses Euclidean distance to score multiple vacant areas.
2745 *
2746 * @param pixelX The X location at which you want to search for a vacant area.
2747 * @param pixelY The Y location at which you want to search for a vacant area.
2748 * @param minSpanX The minimum horizontal span required
2749 * @param minSpanY The minimum vertical span required
2750 * @param spanX Horizontal span of the object.
2751 * @param spanY Vertical span of the object.
2752 * @param ignoreView Considers space occupied by this view as unoccupied
2753 * @param result Previously returned value to possibly recycle.
2754 * @return The X, Y cell of a vacant area that can contain this object,
2755 * nearest the requested location.
2756 */
2757 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2758 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002759 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2760 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002761 }
2762
2763 /**
Adam Cohendf035382011-04-11 17:22:04 -07002764 * Find a starting cell position that will fit the given bounds nearest the requested
2765 * cell location. Uses Euclidean distance to score multiple vacant areas.
2766 *
2767 * @param pixelX The X location at which you want to search for a vacant area.
2768 * @param pixelY The Y location at which you want to search for a vacant area.
2769 * @param spanX Horizontal span of the object.
2770 * @param spanY Vertical span of the object.
2771 * @param ignoreView Considers space occupied by this view as unoccupied
2772 * @param result Previously returned value to possibly recycle.
2773 * @return The X, Y cell of a vacant area that can contain this object,
2774 * nearest the requested location.
2775 */
2776 int[] findNearestArea(
2777 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2778 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2779 }
2780
Michael Jurka0280c3b2010-09-17 15:00:07 -07002781 boolean existsEmptyCell() {
2782 return findCellForSpan(null, 1, 1);
2783 }
2784
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002785 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002786 * Finds the upper-left coordinate of the first rectangle in the grid that can
2787 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2788 * then this method will only return coordinates for rectangles that contain the cell
2789 * (intersectX, intersectY)
2790 *
2791 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2792 * can be found.
2793 * @param spanX The horizontal span of the cell we want to find.
2794 * @param spanY The vertical span of the cell we want to find.
2795 *
2796 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002797 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002798 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002799 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002800 }
2801
2802 /**
2803 * Like above, but ignores any cells occupied by the item "ignoreView"
2804 *
2805 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2806 * can be found.
2807 * @param spanX The horizontal span of the cell we want to find.
2808 * @param spanY The vertical span of the cell we want to find.
2809 * @param ignoreView The home screen item we should treat as not occupying any space
2810 * @return
2811 */
2812 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002813 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2814 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002815 }
2816
2817 /**
2818 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2819 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2820 *
2821 * @param spanX The horizontal span of the cell we want to find.
2822 * @param spanY The vertical span of the cell we want to find.
2823 * @param ignoreView The home screen item we should treat as not occupying any space
2824 * @param intersectX The X coordinate of the cell that we should try to overlap
2825 * @param intersectX The Y coordinate of the cell that we should try to overlap
2826 *
2827 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2828 */
2829 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2830 int intersectX, int intersectY) {
2831 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002832 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002833 }
2834
2835 /**
2836 * The superset of the above two methods
2837 */
2838 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002839 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002840 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002841 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002842
Michael Jurka28750fb2010-09-24 17:43:49 -07002843 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002844 while (true) {
2845 int startX = 0;
2846 if (intersectX >= 0) {
2847 startX = Math.max(startX, intersectX - (spanX - 1));
2848 }
2849 int endX = mCountX - (spanX - 1);
2850 if (intersectX >= 0) {
2851 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2852 }
2853 int startY = 0;
2854 if (intersectY >= 0) {
2855 startY = Math.max(startY, intersectY - (spanY - 1));
2856 }
2857 int endY = mCountY - (spanY - 1);
2858 if (intersectY >= 0) {
2859 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2860 }
2861
Winson Chungbbc60d82010-11-11 16:34:41 -08002862 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002863 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002864 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002865 for (int i = 0; i < spanX; i++) {
2866 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002867 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002868 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002869 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002870 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002871 continue inner;
2872 }
2873 }
2874 }
2875 if (cellXY != null) {
2876 cellXY[0] = x;
2877 cellXY[1] = y;
2878 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002879 foundCell = true;
2880 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002881 }
2882 }
2883 if (intersectX == -1 && intersectY == -1) {
2884 break;
2885 } else {
2886 // if we failed to find anything, try again but without any requirements of
2887 // intersecting
2888 intersectX = -1;
2889 intersectY = -1;
2890 continue;
2891 }
2892 }
2893
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002894 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002895 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002896 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002897 }
2898
2899 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002900 * A drag event has begun over this layout.
2901 * It may have begun over this layout (in which case onDragChild is called first),
2902 * or it may have begun on another layout.
2903 */
2904 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002905 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002906 mDragging = true;
2907 }
2908
2909 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002910 * Called when drag has left this CellLayout or has been completed (successfully or not)
2911 */
2912 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002913 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002914 // This can actually be called when we aren't in a drag, e.g. when adding a new
2915 // item to this layout via the customize drawer.
2916 // Guard against that case.
2917 if (mDragging) {
2918 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002919 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002920
2921 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002922 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002923 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2924 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002925 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002926 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002927 }
2928
2929 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002930 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002931 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002932 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002933 *
2934 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002935 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002936 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002937 if (child != null) {
2938 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002939 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002940 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002942 }
2943
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002945 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002946 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002947 * @param cellX X coordinate of upper left corner expressed as a cell position
2948 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002949 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002951 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002952 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002953 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002954 final int cellWidth = mCellWidth;
2955 final int cellHeight = mCellHeight;
2956 final int widthGap = mWidthGap;
2957 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002958
Winson Chung4b825dcd2011-06-19 12:41:22 -07002959 final int hStartPadding = getPaddingLeft();
2960 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002961
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2963 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2964
2965 int x = hStartPadding + cellX * (cellWidth + widthGap);
2966 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002967
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002968 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002972 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002974 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 * @param width Width in pixels
2976 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002977 * @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 -08002978 */
Winson Chung66700732013-08-20 16:56:15 -07002979 public static int[] rectToCell(int width, int height, int[] result) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002980 LauncherAppState app = LauncherAppState.getInstance();
2981 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -07002982 Rect padding = grid.getWorkspacePadding(grid.isLandscape ?
2983 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
Winson Chung5f8afe62013-08-12 16:19:28 -07002984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 // Always assume we're working with the smallest span to make sure we
2986 // reserve enough space in both orientations.
Winson Chung66700732013-08-20 16:56:15 -07002987 int parentWidth = grid.calculateCellWidth(grid.widthPx
2988 - padding.left - padding.right, (int) grid.numColumns);
2989 int parentHeight = grid.calculateCellHeight(grid.heightPx
2990 - padding.top - padding.bottom, (int) grid.numRows);
2991 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002993 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002994 int spanX = (int) Math.ceil(width / (float) smallerSize);
2995 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002996
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002997 if (result == null) {
2998 return new int[] { spanX, spanY };
2999 }
3000 result[0] = spanX;
3001 result[1] = spanY;
3002 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003 }
3004
Michael Jurkaf12c75c2011-01-25 22:41:40 -08003005 public int[] cellSpansToSize(int hSpans, int vSpans) {
3006 int[] size = new int[2];
3007 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
3008 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
3009 return size;
3010 }
3011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003012 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08003013 * Calculate the grid spans needed to fit given item
3014 */
3015 public void calculateSpans(ItemInfo info) {
3016 final int minWidth;
3017 final int minHeight;
3018
3019 if (info instanceof LauncherAppWidgetInfo) {
3020 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3021 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3022 } else if (info instanceof PendingAddWidgetInfo) {
3023 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3024 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3025 } else {
3026 // It's not a widget, so it must be 1x1
3027 info.spanX = info.spanY = 1;
3028 return;
3029 }
3030 int[] spans = rectToCell(minWidth, minHeight, null);
3031 info.spanX = spans[0];
3032 info.spanY = spans[1];
3033 }
3034
3035 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003036 * Find the first vacant cell, if there is one.
3037 *
3038 * @param vacant Holds the x and y coordinate of the vacant cell
3039 * @param spanX Horizontal cell span.
3040 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003041 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042 * @return True if a vacant cell was found
3043 */
3044 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003045
Michael Jurka0280c3b2010-09-17 15:00:07 -07003046 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003047 }
3048
3049 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3050 int xCount, int yCount, boolean[][] occupied) {
3051
Adam Cohen2801caf2011-05-13 20:57:39 -07003052 for (int y = 0; y < yCount; y++) {
3053 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003054 boolean available = !occupied[x][y];
3055out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3056 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3057 available = available && !occupied[i][j];
3058 if (!available) break out;
3059 }
3060 }
3061
3062 if (available) {
3063 vacant[0] = x;
3064 vacant[1] = y;
3065 return true;
3066 }
3067 }
3068 }
3069
3070 return false;
3071 }
3072
Michael Jurka0280c3b2010-09-17 15:00:07 -07003073 private void clearOccupiedCells() {
3074 for (int x = 0; x < mCountX; x++) {
3075 for (int y = 0; y < mCountY; y++) {
3076 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003077 }
3078 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003079 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003080
Adam Cohend41fbf52012-02-16 23:53:59 -08003081 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003082 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003083 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003085
Adam Cohend4844c32011-02-18 19:25:06 -08003086 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003087 markCellsAsOccupiedForView(view, mOccupied);
3088 }
3089 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003090 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003091 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003092 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003093 }
3094
Adam Cohend4844c32011-02-18 19:25:06 -08003095 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003096 markCellsAsUnoccupiedForView(view, mOccupied);
3097 }
3098 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003099 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003100 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003101 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003102 }
3103
Adam Cohen482ed822012-03-02 14:15:13 -08003104 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3105 boolean value) {
3106 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003107 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3108 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003109 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003110 }
3111 }
3112 }
3113
Adam Cohen2801caf2011-05-13 20:57:39 -07003114 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003115 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003116 (Math.max((mCountX - 1), 0) * mWidthGap);
3117 }
3118
3119 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003120 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003121 (Math.max((mCountY - 1), 0) * mHeightGap);
3122 }
3123
Michael Jurka66d72172011-04-12 16:29:25 -07003124 public boolean isOccupied(int x, int y) {
3125 if (x < mCountX && y < mCountY) {
3126 return mOccupied[x][y];
3127 } else {
3128 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3129 }
3130 }
3131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003132 @Override
3133 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3134 return new CellLayout.LayoutParams(getContext(), attrs);
3135 }
3136
3137 @Override
3138 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3139 return p instanceof CellLayout.LayoutParams;
3140 }
3141
3142 @Override
3143 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3144 return new CellLayout.LayoutParams(p);
3145 }
3146
Winson Chungaafa03c2010-06-11 17:34:16 -07003147 public static class CellLayoutAnimationController extends LayoutAnimationController {
3148 public CellLayoutAnimationController(Animation animation, float delay) {
3149 super(animation, delay);
3150 }
3151
3152 @Override
3153 protected long getDelayForView(View view) {
3154 return (int) (Math.random() * 150);
3155 }
3156 }
3157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003158 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3159 /**
3160 * Horizontal location of the item in the grid.
3161 */
3162 @ViewDebug.ExportedProperty
3163 public int cellX;
3164
3165 /**
3166 * Vertical location of the item in the grid.
3167 */
3168 @ViewDebug.ExportedProperty
3169 public int cellY;
3170
3171 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003172 * Temporary horizontal location of the item in the grid during reorder
3173 */
3174 public int tmpCellX;
3175
3176 /**
3177 * Temporary vertical location of the item in the grid during reorder
3178 */
3179 public int tmpCellY;
3180
3181 /**
3182 * Indicates that the temporary coordinates should be used to layout the items
3183 */
3184 public boolean useTmpCoords;
3185
3186 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003187 * Number of cells spanned horizontally by the item.
3188 */
3189 @ViewDebug.ExportedProperty
3190 public int cellHSpan;
3191
3192 /**
3193 * Number of cells spanned vertically by the item.
3194 */
3195 @ViewDebug.ExportedProperty
3196 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003197
Adam Cohen1b607ed2011-03-03 17:26:50 -08003198 /**
3199 * Indicates whether the item will set its x, y, width and height parameters freely,
3200 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3201 */
Adam Cohend4844c32011-02-18 19:25:06 -08003202 public boolean isLockedToGrid = true;
3203
Adam Cohen482ed822012-03-02 14:15:13 -08003204 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07003205 * Indicates that this item should use the full extents of its parent.
3206 */
3207 public boolean isFullscreen = false;
3208
3209 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003210 * Indicates whether this item can be reordered. Always true except in the case of the
3211 * the AllApps button.
3212 */
3213 public boolean canReorder = true;
3214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003215 // X coordinate of the view in the layout.
3216 @ViewDebug.ExportedProperty
3217 int x;
3218 // Y coordinate of the view in the layout.
3219 @ViewDebug.ExportedProperty
3220 int y;
3221
Romain Guy84f296c2009-11-04 15:00:44 -08003222 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003224 public LayoutParams(Context c, AttributeSet attrs) {
3225 super(c, attrs);
3226 cellHSpan = 1;
3227 cellVSpan = 1;
3228 }
3229
3230 public LayoutParams(ViewGroup.LayoutParams source) {
3231 super(source);
3232 cellHSpan = 1;
3233 cellVSpan = 1;
3234 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003235
3236 public LayoutParams(LayoutParams source) {
3237 super(source);
3238 this.cellX = source.cellX;
3239 this.cellY = source.cellY;
3240 this.cellHSpan = source.cellHSpan;
3241 this.cellVSpan = source.cellVSpan;
3242 }
3243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003245 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003246 this.cellX = cellX;
3247 this.cellY = cellY;
3248 this.cellHSpan = cellHSpan;
3249 this.cellVSpan = cellVSpan;
3250 }
3251
Adam Cohen2374abf2013-04-16 14:56:57 -07003252 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3253 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003254 if (isLockedToGrid) {
3255 final int myCellHSpan = cellHSpan;
3256 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003257 int myCellX = useTmpCoords ? tmpCellX : cellX;
3258 int myCellY = useTmpCoords ? tmpCellY : cellY;
3259
3260 if (invertHorizontally) {
3261 myCellX = colCount - myCellX - cellHSpan;
3262 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003263
Adam Cohend4844c32011-02-18 19:25:06 -08003264 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3265 leftMargin - rightMargin;
3266 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3267 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003268 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3269 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003270 }
3271 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003272
Winson Chungaafa03c2010-06-11 17:34:16 -07003273 public String toString() {
3274 return "(" + this.cellX + ", " + this.cellY + ")";
3275 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003276
3277 public void setWidth(int width) {
3278 this.width = width;
3279 }
3280
3281 public int getWidth() {
3282 return width;
3283 }
3284
3285 public void setHeight(int height) {
3286 this.height = height;
3287 }
3288
3289 public int getHeight() {
3290 return height;
3291 }
3292
3293 public void setX(int x) {
3294 this.x = x;
3295 }
3296
3297 public int getX() {
3298 return x;
3299 }
3300
3301 public void setY(int y) {
3302 this.y = y;
3303 }
3304
3305 public int getY() {
3306 return y;
3307 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003308 }
3309
Michael Jurka0280c3b2010-09-17 15:00:07 -07003310 // This class stores info for two purposes:
3311 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3312 // its spanX, spanY, and the screen it is on
3313 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3314 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3315 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003316 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003317 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003318 int cellX = -1;
3319 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003320 int spanX;
3321 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003322 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003323 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003325 @Override
3326 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003327 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3328 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003329 }
3330 }
Michael Jurkad771c962011-08-09 15:00:48 -07003331
3332 public boolean lastDownOnOccupiedCell() {
3333 return mLastDownOnOccupiedCell;
3334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003335}