blob: 81f9af2e1cc5dbb542eff6d5e55edc77a064a33b [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Brandon Keely50e6e562012-05-08 16:28:49 -070021import android.animation.AnimatorSet;
Chet Haase00397b12010-10-07 11:13:10 -070022import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070023import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070028import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080030import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070032import android.graphics.Point;
Adam Cohenb5ba0972011-09-07 18:02:31 -070033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080036import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070037import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070038import android.graphics.drawable.NinePatchDrawable;
Adam Cohen1462de32012-07-24 22:34:36 -070039import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070042import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.R;
52import com.android.launcher3.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070053
Adam Cohen69ce2e52011-07-03 19:25:21 -070054import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080056import java.util.Collections;
57import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070058import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080059import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070060
Michael Jurkabdb5c532011-02-01 15:05:06 -080061public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070062 static final String TAG = "CellLayout";
63
Adam Cohen2acce882012-03-28 19:03:19 -070064 private Launcher mLauncher;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 private int mCellWidth;
66 private int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070067 private int mFixedCellWidth;
68 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070069
Adam Cohend22015c2010-07-26 22:02:18 -070070 private int mCountX;
71 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
Adam Cohen234c4cd2011-07-17 21:03:04 -070073 private int mOriginalWidthGap;
74 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075 private int mWidthGap;
76 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070077 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080078 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
80 private final Rect mRect = new Rect();
81 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070082
Patrick Dubroyde7658b2010-09-27 11:15:43 -070083 // These are temporary variables to prevent having to allocate a new object just to
84 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070085 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070086 private final int[] mTmpPoint = new int[2];
Adam Cohen69ce2e52011-07-03 19:25:21 -070087 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070088
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080090 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070091 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Michael Jurkadee05892010-07-27 10:01:56 -070093 private OnTouchListener mInterceptTouchListener;
94
Adam Cohen69ce2e52011-07-03 19:25:21 -070095 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070096 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070097
Adam Cohen02dcfcc2013-10-01 12:37:33 -070098 private float FOREGROUND_ALPHA_DAMPER = 0.65f;
Adam Cohenb5ba0972011-09-07 18:02:31 -070099 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700100 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700101 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -0700102
Michael Jurka33945b22010-12-21 18:19:38 -0800103 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -0800104 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700105 private Drawable mOverScrollForegroundDrawable;
106 private Drawable mOverScrollLeft;
107 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700108 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700109 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700110 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700111
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700112 // These values allow a fixed measurement to be set on the CellLayout.
113 private int mFixedWidth = -1;
114 private int mFixedHeight = -1;
115
Michael Jurka33945b22010-12-21 18:19:38 -0800116 // If we're actively dragging something over this screen, mIsDragOverlapping is true
117 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700118 private final Point mDragCenter = new Point();
Adam Cohendedbd962013-07-11 14:21:49 -0700119 boolean mUseActiveGlowBackground = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700120
Winson Chung150fbab2010-09-29 17:14:26 -0700121 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700122 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800123 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700124 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700125 private InterruptibleInOutAnimator[] mDragOutlineAnims =
126 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700127
128 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700129 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700130 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700131
Patrick Dubroy96864c32011-03-10 17:17:23 -0800132 private BubbleTextView mPressedOrFocusedIcon;
133
Adam Cohen482ed822012-03-02 14:15:13 -0800134 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
135 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700136 private HashMap<View, ReorderHintAnimation>
137 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
138
139 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700140
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700141 // When a drag operation is in progress, holds the nearest cell to the touch point
142 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Joe Onorato4be866d2010-10-10 11:26:02 -0700144 private boolean mDragging = false;
145
Patrick Dubroyce34a972010-10-19 10:34:32 -0700146 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700147 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700148
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800149 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700150 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800151
Adam Cohen482ed822012-03-02 14:15:13 -0800152 public static final int MODE_DRAG_OVER = 0;
153 public static final int MODE_ON_DROP = 1;
154 public static final int MODE_ON_DROP_EXTERNAL = 2;
155 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700156 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800157 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
158
Adam Cohena897f392012-04-27 18:12:05 -0700159 static final int LANDSCAPE = 0;
160 static final int PORTRAIT = 1;
161
Adam Cohen7bdfc972012-05-22 16:50:35 -0700162 private static final float REORDER_HINT_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700163 private static final int REORDER_ANIMATION_DURATION = 150;
164 private float mReorderHintAnimationMagnitude;
165
Adam Cohen482ed822012-03-02 14:15:13 -0800166 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
167 private Rect mOccupiedRect = new Rect();
168 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700169 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700170 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700171 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800172
Romain Guy8a0bff52012-05-06 13:14:33 -0700173 private final static PorterDuffXfermode sAddBlendMode =
174 new PorterDuffXfermode(PorterDuff.Mode.ADD);
Michael Jurkaca993832012-06-29 15:17:04 -0700175 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 public CellLayout(Context context) {
178 this(context, null);
179 }
180
181 public CellLayout(Context context, AttributeSet attrs) {
182 this(context, attrs, 0);
183 }
184
185 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
186 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700187 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700188
189 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
190 // the user where a dragged item will land when dropped.
191 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800192 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700193 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700194
Winson Chung892c74d2013-08-22 16:15:50 -0700195 LauncherAppState app = LauncherAppState.getInstance();
196 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
198
Winson Chung11a1a532013-09-13 11:14:45 -0700199 mCellWidth = mCellHeight = -1;
200 mFixedCellHeight = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700201 mWidthGap = mOriginalWidthGap = 0;
202 mHeightGap = mOriginalHeightGap = 0;
203 mMaxGap = Integer.MAX_VALUE;
Winson Chung892c74d2013-08-22 16:15:50 -0700204 mCountX = (int) grid.numColumns;
205 mCountY = (int) grid.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700206 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800207 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700208 mPreviousReorderDirection[0] = INVALID_DIRECTION;
209 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
211 a.recycle();
212
213 setAlwaysDrawnWithCacheEnabled(false);
214
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700215 final Resources res = getResources();
Winson Chung5f8afe62013-08-12 16:19:28 -0700216 mHotseatScale = (float) grid.hotseatIconSize / grid.iconSize;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700217
Adam Cohen410f3cd2013-09-22 12:09:32 -0700218 mNormalBackground = res.getDrawable(R.drawable.screenpanel);
219 mActiveGlowBackground = res.getDrawable(R.drawable.screenpanel_hover);
Michael Jurka33945b22010-12-21 18:19:38 -0800220
Adam Cohenb5ba0972011-09-07 18:02:31 -0700221 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
222 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
223 mForegroundPadding =
224 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800225
Adam Cohen19f37922012-03-21 11:59:11 -0700226 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700227 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700228
Winson Chungb26f3d62011-06-02 10:49:29 -0700229 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700230 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700231
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700232 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700233 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700234 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700235 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800236 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700237 }
238
239 // When dragging things around the home screens, we show a green outline of
240 // where the item will land. The outlines gradually fade out, leaving a trail
241 // behind the drag path.
242 // Set up all the animations that are used to implement this fading.
243 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700244 final float fromAlphaValue = 0;
245 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700246
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700247 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700248
249 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700250 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100251 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700252 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700253 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700254 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700255 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700256 final Bitmap outline = (Bitmap)anim.getTag();
257
258 // If an animation is started and then stopped very quickly, we can still
259 // get spurious updates we've cleared the tag. Guard against this.
260 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700261 @SuppressWarnings("all") // suppress dead code warning
262 final boolean debug = false;
263 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700264 Object val = animation.getAnimatedValue();
265 Log.d(TAG, "anim " + thisIndex + " update: " + val +
266 ", isStopped " + anim.isStopped());
267 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 // Try to prevent it from continuing to run
269 animation.cancel();
270 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700271 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800272 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700274 }
275 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700276 // The animation holds a reference to the drag outline bitmap as long is it's
277 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700278 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700279 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700280 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700281 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700282 anim.setTag(null);
283 }
284 }
285 });
286 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700287 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700288
Michael Jurka18014792010-10-14 09:01:34 -0700289 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700290 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800291
Michael Jurkaa52570f2012-03-20 03:18:20 -0700292 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700293 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700294 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700295
Michael Jurkaa52570f2012-03-20 03:18:20 -0700296 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700297 }
298
Adam Cohen2801caf2011-05-13 20:57:39 -0700299 public void enableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700300 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_HARDWARE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700301 }
302
303 public void disableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700304 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700305 }
306
307 public void buildHardwareLayer() {
308 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700309 }
310
Adam Cohen307fe232012-08-16 17:55:58 -0700311 public float getChildrenScale() {
312 return mIsHotseat ? mHotseatScale : 1.0f;
313 }
314
Winson Chung5f8afe62013-08-12 16:19:28 -0700315 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700316 mFixedCellWidth = mCellWidth = width;
317 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700318 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
319 mCountX, mCountY);
320 }
321
Adam Cohen2801caf2011-05-13 20:57:39 -0700322 public void setGridSize(int x, int y) {
323 mCountX = x;
324 mCountY = y;
325 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800326 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700327 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700328 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700329 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700330 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700331 }
332
Adam Cohen2374abf2013-04-16 14:56:57 -0700333 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
334 public void setInvertIfRtl(boolean invert) {
335 mShortcutsAndWidgets.setInvertIfRtl(invert);
336 }
337
Patrick Dubroy96864c32011-03-10 17:17:23 -0800338 private void invalidateBubbleTextView(BubbleTextView icon) {
339 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700340 invalidate(icon.getLeft() + getPaddingLeft() - padding,
341 icon.getTop() + getPaddingTop() - padding,
342 icon.getRight() + getPaddingLeft() + padding,
343 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800344 }
345
Adam Cohenb5ba0972011-09-07 18:02:31 -0700346 void setOverScrollAmount(float r, boolean left) {
347 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
348 mOverScrollForegroundDrawable = mOverScrollLeft;
349 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
350 mOverScrollForegroundDrawable = mOverScrollRight;
351 }
352
Adam Cohen02dcfcc2013-10-01 12:37:33 -0700353 r *= FOREGROUND_ALPHA_DAMPER;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700354 mForegroundAlpha = (int) Math.round((r * 255));
355 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
356 invalidate();
357 }
358
Patrick Dubroy96864c32011-03-10 17:17:23 -0800359 void setPressedOrFocusedIcon(BubbleTextView icon) {
360 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
361 // requires an expanded clip rect (due to the glow's blur radius)
362 BubbleTextView oldIcon = mPressedOrFocusedIcon;
363 mPressedOrFocusedIcon = icon;
364 if (oldIcon != null) {
365 invalidateBubbleTextView(oldIcon);
366 }
367 if (mPressedOrFocusedIcon != null) {
368 invalidateBubbleTextView(mPressedOrFocusedIcon);
369 }
370 }
371
Michael Jurka33945b22010-12-21 18:19:38 -0800372 void setIsDragOverlapping(boolean isDragOverlapping) {
373 if (mIsDragOverlapping != isDragOverlapping) {
374 mIsDragOverlapping = isDragOverlapping;
Adam Cohendedbd962013-07-11 14:21:49 -0700375 setUseActiveGlowBackground(mIsDragOverlapping);
Michael Jurka33945b22010-12-21 18:19:38 -0800376 invalidate();
377 }
378 }
379
Adam Cohendedbd962013-07-11 14:21:49 -0700380 void setUseActiveGlowBackground(boolean use) {
381 mUseActiveGlowBackground = use;
382 }
383
Michael Jurka33945b22010-12-21 18:19:38 -0800384 boolean getIsDragOverlapping() {
385 return mIsDragOverlapping;
386 }
387
Adam Cohenebea84d2011-11-09 17:20:41 -0800388 protected void setOverscrollTransformsDirty(boolean dirty) {
389 mScrollingTransformsDirty = dirty;
390 }
391
392 protected void resetOverscrollTransforms() {
393 if (mScrollingTransformsDirty) {
394 setOverscrollTransformsDirty(false);
395 setTranslationX(0);
396 setRotationY(0);
397 // It doesn't matter if we pass true or false here, the important thing is that we
398 // pass 0, which results in the overscroll drawable not being drawn any more.
399 setOverScrollAmount(0, false);
400 setPivotX(getMeasuredWidth() / 2);
401 setPivotY(getMeasuredHeight() / 2);
402 }
403 }
404
Adam Cohen307fe232012-08-16 17:55:58 -0700405 public void scaleRect(Rect r, float scale) {
406 if (scale != 1.0f) {
407 r.left = (int) (r.left * scale + 0.5f);
408 r.top = (int) (r.top * scale + 0.5f);
409 r.right = (int) (r.right * scale + 0.5f);
410 r.bottom = (int) (r.bottom * scale + 0.5f);
411 }
412 }
413
414 Rect temp = new Rect();
415 void scaleRectAboutCenter(Rect in, Rect out, float scale) {
416 int cx = in.centerX();
417 int cy = in.centerY();
418 out.set(in);
419 out.offset(-cx, -cy);
420 scaleRect(out, scale);
421 out.offset(cx, cy);
422 }
423
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700424 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700425 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700426 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
427 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
428 // When we're small, we are either drawn normally or in the "accepts drops" state (during
429 // a drag). However, we also drag the mini hover background *over* one of those two
430 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700431 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700432 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800433
Adam Cohendedbd962013-07-11 14:21:49 -0700434 if (mUseActiveGlowBackground) {
Michael Jurka33945b22010-12-21 18:19:38 -0800435 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700436 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700437 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700438 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700439 }
Michael Jurka33945b22010-12-21 18:19:38 -0800440
441 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
442 bg.setBounds(mBackgroundRect);
443 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700444 }
Romain Guya6abce82009-11-10 02:54:41 -0800445
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700446 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700447 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700448 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700449 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800450 final Rect r = mDragOutlines[i];
Adam Cohen307fe232012-08-16 17:55:58 -0700451 scaleRectAboutCenter(r, temp, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700452 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700453 paint.setAlpha((int)(alpha + .5f));
Adam Cohen307fe232012-08-16 17:55:58 -0700454 canvas.drawBitmap(b, null, temp, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700455 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700456 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800457
458 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
459 // requires an expanded clip rect (due to the glow's blur radius)
460 if (mPressedOrFocusedIcon != null) {
461 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
462 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
463 if (b != null) {
464 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700465 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
466 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800467 null);
468 }
469 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700470
Adam Cohen482ed822012-03-02 14:15:13 -0800471 if (DEBUG_VISUALIZE_OCCUPIED) {
472 int[] pt = new int[2];
473 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700474 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800475 for (int i = 0; i < mCountX; i++) {
476 for (int j = 0; j < mCountY; j++) {
477 if (mOccupied[i][j]) {
478 cellToPoint(i, j, pt);
479 canvas.save();
480 canvas.translate(pt[0], pt[1]);
481 cd.draw(canvas);
482 canvas.restore();
483 }
484 }
485 }
486 }
487
Andrew Flynn850d2e72012-04-26 16:51:20 -0700488 int previewOffset = FolderRingAnimator.sPreviewSize;
489
Adam Cohen69ce2e52011-07-03 19:25:21 -0700490 // The folder outer / inner ring image(s)
Winson Chung5f8afe62013-08-12 16:19:28 -0700491 LauncherAppState app = LauncherAppState.getInstance();
492 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700493 for (int i = 0; i < mFolderOuterRings.size(); i++) {
494 FolderRingAnimator fra = mFolderOuterRings.get(i);
495
Adam Cohen5108bc02013-09-20 17:04:51 -0700496 Drawable d;
497 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700498 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700499 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen5108bc02013-09-20 17:04:51 -0700500 int centerX = mTempLocation[0] + mCellWidth / 2;
501 int centerY = mTempLocation[1] + previewOffset / 2 +
502 child.getPaddingTop() + grid.folderBackgroundOffset;
Winson Chung89f97052013-09-20 11:32:26 -0700503 if (child != null) {
Adam Cohen5108bc02013-09-20 17:04:51 -0700504 // Draw outer ring, if it exists
505 if (FolderIcon.HAS_OUTER_RING) {
506 d = FolderRingAnimator.sSharedOuterRingDrawable;
507 width = (int) (fra.getOuterRingSize() * getChildrenScale());
508 height = width;
509 canvas.save();
510 canvas.translate(centerX - width / 2, centerY - height / 2);
511 d.setBounds(0, 0, width, height);
512 d.draw(canvas);
513 canvas.restore();
514 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700515
Winson Chung89f97052013-09-20 11:32:26 -0700516 // Draw inner ring
517 d = FolderRingAnimator.sSharedInnerRingDrawable;
518 width = (int) (fra.getInnerRingSize() * getChildrenScale());
519 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700520 canvas.save();
521 canvas.translate(centerX - width / 2, centerY - width / 2);
522 d.setBounds(0, 0, width, height);
523 d.draw(canvas);
524 canvas.restore();
525 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700526 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700527
528 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
529 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
530 int width = d.getIntrinsicWidth();
531 int height = d.getIntrinsicHeight();
532
533 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700534 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700535 if (child != null) {
536 int centerX = mTempLocation[0] + mCellWidth / 2;
537 int centerY = mTempLocation[1] + previewOffset / 2 +
538 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700539
Winson Chung89f97052013-09-20 11:32:26 -0700540 canvas.save();
541 canvas.translate(centerX - width / 2, centerY - width / 2);
542 d.setBounds(0, 0, width, height);
543 d.draw(canvas);
544 canvas.restore();
545 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700546 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700547 }
548
Adam Cohenb5ba0972011-09-07 18:02:31 -0700549 @Override
550 protected void dispatchDraw(Canvas canvas) {
551 super.dispatchDraw(canvas);
552 if (mForegroundAlpha > 0) {
553 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
554 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700555 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700556 mOverScrollForegroundDrawable.draw(canvas);
557 p.setXfermode(null);
558 }
559 }
560
Adam Cohen69ce2e52011-07-03 19:25:21 -0700561 public void showFolderAccept(FolderRingAnimator fra) {
562 mFolderOuterRings.add(fra);
563 }
564
565 public void hideFolderAccept(FolderRingAnimator fra) {
566 if (mFolderOuterRings.contains(fra)) {
567 mFolderOuterRings.remove(fra);
568 }
569 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700570 }
571
Adam Cohenc51934b2011-07-26 21:07:43 -0700572 public void setFolderLeaveBehindCell(int x, int y) {
573 mFolderLeaveBehindCell[0] = x;
574 mFolderLeaveBehindCell[1] = y;
575 invalidate();
576 }
577
578 public void clearFolderLeaveBehind() {
579 mFolderLeaveBehindCell[0] = -1;
580 mFolderLeaveBehindCell[1] = -1;
581 invalidate();
582 }
583
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700584 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700585 public boolean shouldDelayChildPressedState() {
586 return false;
587 }
588
Adam Cohen1462de32012-07-24 22:34:36 -0700589 public void restoreInstanceState(SparseArray<Parcelable> states) {
590 dispatchRestoreInstanceState(states);
591 }
592
Michael Jurkae6235dd2011-10-04 15:02:05 -0700593 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700594 public void cancelLongPress() {
595 super.cancelLongPress();
596
597 // Cancel long press for all children
598 final int count = getChildCount();
599 for (int i = 0; i < count; i++) {
600 final View child = getChildAt(i);
601 child.cancelLongPress();
602 }
603 }
604
Michael Jurkadee05892010-07-27 10:01:56 -0700605 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
606 mInterceptTouchListener = listener;
607 }
608
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700610 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
612
613 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700614 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 }
616
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800617 public void setIsHotseat(boolean isHotseat) {
618 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700619 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800620 }
621
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800622 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700623 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700624 final LayoutParams lp = params;
625
Andrew Flynnde38e422012-05-08 11:22:15 -0700626 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800627 if (child instanceof BubbleTextView) {
628 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700629 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800630 }
631
Adam Cohen307fe232012-08-16 17:55:58 -0700632 child.setScaleX(getChildrenScale());
633 child.setScaleY(getChildrenScale());
634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 // Generate an id for each view, this assumes we have at most 256x256 cells
636 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700637 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700638 // If the horizontal or vertical span is set to -1, it is taken to
639 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700640 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
641 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 child.setId(childId);
644
Michael Jurkaa52570f2012-03-20 03:18:20 -0700645 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700646
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700647 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700648
Winson Chungaafa03c2010-06-11 17:34:16 -0700649 return true;
650 }
651 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700655 public void removeAllViews() {
656 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700657 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700658 }
659
660 @Override
661 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700662 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700663 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700664 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700665 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 }
667
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700668 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700669 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700670 }
671
Michael Jurka0280c3b2010-09-17 15:00:07 -0700672 @Override
673 public void removeView(View view) {
674 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700675 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 }
677
678 @Override
679 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700680 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
681 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700682 }
683
684 @Override
685 public void removeViewInLayout(View view) {
686 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700687 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700688 }
689
690 @Override
691 public void removeViews(int start, int count) {
692 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700693 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700694 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700695 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700696 }
697
698 @Override
699 public void removeViewsInLayout(int start, int count) {
700 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700701 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700702 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700703 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800704 }
705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 @Override
707 protected void onAttachedToWindow() {
708 super.onAttachedToWindow();
Adam Cohendcd297f2013-06-18 13:13:40 -0700709 if (getParent() instanceof Workspace) {
710 Workspace workspace = (Workspace) getParent();
711 mCellInfo.screenId = workspace.getIdForScreen(this);
712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 }
714
Michael Jurkaaf442092010-06-10 17:01:57 -0700715 public void setTagToCellInfoForPoint(int touchX, int touchY) {
716 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800717 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700718 final int x = touchX + getScrollX();
719 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700720 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700721
722 boolean found = false;
723 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700724 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800725 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700726
Adam Cohen1b607ed2011-03-03 17:26:50 -0800727 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
728 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700729 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700730
Winson Chungeecf02d2012-03-02 17:14:58 -0800731 float scale = child.getScaleX();
732 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
733 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700734 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
735 // offset that by this CellLayout's padding to test an (x,y) point that is relative
736 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800738 frame.inset((int) (frame.width() * (1f - scale) / 2),
739 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700740
Michael Jurkaaf442092010-06-10 17:01:57 -0700741 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700742 cellInfo.cell = child;
743 cellInfo.cellX = lp.cellX;
744 cellInfo.cellY = lp.cellY;
745 cellInfo.spanX = lp.cellHSpan;
746 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700747 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700748 break;
749 }
750 }
751 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700752
Michael Jurkad771c962011-08-09 15:00:48 -0700753 mLastDownOnOccupiedCell = found;
754
Michael Jurkaaf442092010-06-10 17:01:57 -0700755 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700756 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700757 pointToCellExact(x, y, cellXY);
758
Michael Jurkaaf442092010-06-10 17:01:57 -0700759 cellInfo.cell = null;
760 cellInfo.cellX = cellXY[0];
761 cellInfo.cellY = cellXY[1];
762 cellInfo.spanX = 1;
763 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700764 }
765 setTag(cellInfo);
766 }
767
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 @Override
769 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700770 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
771 // even in the case where we return early. Not clearing here was causing bugs whereby on
772 // long-press we'd end up picking up an item from a previous drag operation.
773 final int action = ev.getAction();
774
775 if (action == MotionEvent.ACTION_DOWN) {
776 clearTagCellInfo();
777 }
778
Michael Jurkadee05892010-07-27 10:01:56 -0700779 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
780 return true;
781 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782
783 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700784 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800786
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 return false;
788 }
789
Adam Cohenc1997fd2011-08-15 18:26:39 -0700790 private void clearTagCellInfo() {
791 final CellInfo cellInfo = mCellInfo;
792 cellInfo.cell = null;
793 cellInfo.cellX = -1;
794 cellInfo.cellY = -1;
795 cellInfo.spanX = 0;
796 cellInfo.spanY = 0;
797 setTag(cellInfo);
798 }
799
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700801 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 }
803
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700804 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700805 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 * @param x X coordinate of the point
807 * @param y Y coordinate of the point
808 * @param result Array of 2 ints to hold the x and y coordinate of the cell
809 */
810 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700811 final int hStartPadding = getPaddingLeft();
812 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813
814 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
815 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
816
Adam Cohend22015c2010-07-26 22:02:18 -0700817 final int xAxis = mCountX;
818 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819
820 if (result[0] < 0) result[0] = 0;
821 if (result[0] >= xAxis) result[0] = xAxis - 1;
822 if (result[1] < 0) result[1] = 0;
823 if (result[1] >= yAxis) result[1] = yAxis - 1;
824 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700825
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 /**
827 * Given a point, return the cell that most closely encloses that point
828 * @param x X coordinate of the point
829 * @param y Y coordinate of the point
830 * @param result Array of 2 ints to hold the x and y coordinate of the cell
831 */
832 void pointToCellRounded(int x, int y, int[] result) {
833 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
834 }
835
836 /**
837 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700838 *
839 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700841 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 * @param result Array of 2 ints to hold the x and y coordinate of the point
843 */
844 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700845 final int hStartPadding = getPaddingLeft();
846 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847
848 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
849 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
850 }
851
Adam Cohene3e27a82011-04-15 12:07:39 -0700852 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800853 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700854 *
855 * @param cellX X coordinate of the cell
856 * @param cellY Y coordinate of the cell
857 *
858 * @param result Array of 2 ints to hold the x and y coordinate of the point
859 */
860 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700861 regionToCenterPoint(cellX, cellY, 1, 1, result);
862 }
863
864 /**
865 * Given a cell coordinate and span return the point that represents the center of the regio
866 *
867 * @param cellX X coordinate of the cell
868 * @param cellY Y coordinate of the cell
869 *
870 * @param result Array of 2 ints to hold the x and y coordinate of the point
871 */
872 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700873 final int hStartPadding = getPaddingLeft();
874 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700875 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
876 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
877 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
878 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700879 }
880
Adam Cohen19f37922012-03-21 11:59:11 -0700881 /**
882 * Given a cell coordinate and span fills out a corresponding pixel rect
883 *
884 * @param cellX X coordinate of the cell
885 * @param cellY Y coordinate of the cell
886 * @param result Rect in which to write the result
887 */
888 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
889 final int hStartPadding = getPaddingLeft();
890 final int vStartPadding = getPaddingTop();
891 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
892 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
893 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
894 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
895 }
896
Adam Cohen482ed822012-03-02 14:15:13 -0800897 public float getDistanceFromCell(float x, float y, int[] cell) {
898 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
899 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
900 Math.pow(y - mTmpPoint[1], 2));
901 return distance;
902 }
903
Romain Guy84f296c2009-11-04 15:00:44 -0800904 int getCellWidth() {
905 return mCellWidth;
906 }
907
908 int getCellHeight() {
909 return mCellHeight;
910 }
911
Adam Cohend4844c32011-02-18 19:25:06 -0800912 int getWidthGap() {
913 return mWidthGap;
914 }
915
916 int getHeightGap() {
917 return mHeightGap;
918 }
919
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700920 Rect getContentRect(Rect r) {
921 if (r == null) {
922 r = new Rect();
923 }
924 int left = getPaddingLeft();
925 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700926 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
927 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700928 r.set(left, top, right, bottom);
929 return r;
930 }
931
Winson Chungfe411c82013-09-26 16:07:17 -0700932 /** Return a rect that has the cellWidth/cellHeight (left, top), and
933 * widthGap/heightGap (right, bottom) */
Winson Chung66700732013-08-20 16:56:15 -0700934 static void getMetrics(Rect metrics, int paddedMeasureWidth,
935 int paddedMeasureHeight, int countX, int countY) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700936 LauncherAppState app = LauncherAppState.getInstance();
937 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -0700938 metrics.set(grid.calculateCellWidth(paddedMeasureWidth, countX),
939 grid.calculateCellHeight(paddedMeasureHeight, countY), 0, 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700940 }
941
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700942 public void setFixedSize(int width, int height) {
943 mFixedWidth = width;
944 mFixedHeight = height;
945 }
946
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 @Override
948 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700949 LauncherAppState app = LauncherAppState.getInstance();
950 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
951
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700954 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
955 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700956 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
957 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700958 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700959 int cw = grid.calculateCellWidth(childWidthSize, mCountX);
960 int ch = grid.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700961 if (cw != mCellWidth || ch != mCellHeight) {
962 mCellWidth = cw;
963 mCellHeight = ch;
964 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
965 mHeightGap, mCountX, mCountY);
966 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700967 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700968
Winson Chung2d75f122013-09-23 16:53:31 -0700969 int newWidth = childWidthSize;
970 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700971 if (mFixedWidth > 0 && mFixedHeight > 0) {
972 newWidth = mFixedWidth;
973 newHeight = mFixedHeight;
974 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
976 }
977
Adam Cohend22015c2010-07-26 22:02:18 -0700978 int numWidthGaps = mCountX - 1;
979 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980
Adam Cohen234c4cd2011-07-17 21:03:04 -0700981 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700982 int hSpace = childWidthSize;
983 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700984 int hFreeSpace = hSpace - (mCountX * mCellWidth);
985 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700986 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
987 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700988 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
989 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700990 } else {
991 mWidthGap = mOriginalWidthGap;
992 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700993 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800994 int count = getChildCount();
Winson Chung5f8afe62013-08-12 16:19:28 -0700995 int maxWidth = 0;
996 int maxHeight = 0;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800997 for (int i = 0; i < count; i++) {
998 View child = getChildAt(i);
Winson Chung2d75f122013-09-23 16:53:31 -0700999 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth,
1000 MeasureSpec.EXACTLY);
1001 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
1002 MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -08001003 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -07001004 maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
1005 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
Michael Jurka8c920dd2011-01-20 14:16:56 -08001006 }
Winson Chung2d75f122013-09-23 16:53:31 -07001007 if (mFixedWidth > 0 && mFixedHeight > 0) {
1008 setMeasuredDimension(maxWidth, maxHeight);
1009 } else {
1010 setMeasuredDimension(widthSize, heightSize);
1011 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 }
1013
1014 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001015 protected void onLayout(boolean changed, int l, int t, int r, int b) {
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 int left = getPaddingLeft();
1020 int top = getPaddingTop();
1021 child.layout(left, top,
1022 left + r - l,
1023 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 }
1025 }
1026
1027 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001028 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1029 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001030 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001031 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001032 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001033 }
1034
1035 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001037 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 }
1039
1040 @Override
1041 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001042 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 }
1044
Michael Jurka5f1c5092010-09-03 14:15:02 -07001045 public float getBackgroundAlpha() {
1046 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001047 }
1048
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001049 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001050 if (mBackgroundAlphaMultiplier != multiplier) {
1051 mBackgroundAlphaMultiplier = multiplier;
1052 invalidate();
1053 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001054 }
1055
Adam Cohenddb82192010-11-10 16:32:54 -08001056 public float getBackgroundAlphaMultiplier() {
1057 return mBackgroundAlphaMultiplier;
1058 }
1059
Michael Jurka5f1c5092010-09-03 14:15:02 -07001060 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001061 if (mBackgroundAlpha != alpha) {
1062 mBackgroundAlpha = alpha;
1063 invalidate();
1064 }
Michael Jurkadee05892010-07-27 10:01:56 -07001065 }
1066
Michael Jurkaa52570f2012-03-20 03:18:20 -07001067 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001068 final int childCount = getChildCount();
1069 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001070 getChildAt(i).setAlpha(alpha);
1071 }
1072 }
1073
Michael Jurkaa52570f2012-03-20 03:18:20 -07001074 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1075 if (getChildCount() > 0) {
1076 return (ShortcutAndWidgetContainer) getChildAt(0);
1077 }
1078 return null;
1079 }
1080
Patrick Dubroy440c3602010-07-13 17:50:32 -07001081 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001082 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001083 }
1084
Adam Cohen76fc0852011-06-17 13:26:23 -07001085 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001086 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001087 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001088 boolean[][] occupied = mOccupied;
1089 if (!permanent) {
1090 occupied = mTmpOccupied;
1091 }
1092
Adam Cohen19f37922012-03-21 11:59:11 -07001093 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001094 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1095 final ItemInfo info = (ItemInfo) child.getTag();
1096
1097 // We cancel any existing animations
1098 if (mReorderAnimators.containsKey(lp)) {
1099 mReorderAnimators.get(lp).cancel();
1100 mReorderAnimators.remove(lp);
1101 }
1102
Adam Cohen482ed822012-03-02 14:15:13 -08001103 final int oldX = lp.x;
1104 final int oldY = lp.y;
1105 if (adjustOccupied) {
1106 occupied[lp.cellX][lp.cellY] = false;
1107 occupied[cellX][cellY] = true;
1108 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001109 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001110 if (permanent) {
1111 lp.cellX = info.cellX = cellX;
1112 lp.cellY = info.cellY = cellY;
1113 } else {
1114 lp.tmpCellX = cellX;
1115 lp.tmpCellY = cellY;
1116 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001117 clc.setupLp(lp);
1118 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001119 final int newX = lp.x;
1120 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001121
Adam Cohen76fc0852011-06-17 13:26:23 -07001122 lp.x = oldX;
1123 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001124
Adam Cohen482ed822012-03-02 14:15:13 -08001125 // Exit early if we're not actually moving the view
1126 if (oldX == newX && oldY == newY) {
1127 lp.isLockedToGrid = true;
1128 return true;
1129 }
1130
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001131 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001132 va.setDuration(duration);
1133 mReorderAnimators.put(lp, va);
1134
1135 va.addUpdateListener(new AnimatorUpdateListener() {
1136 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001137 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001138 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001139 lp.x = (int) ((1 - r) * oldX + r * newX);
1140 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001141 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001142 }
1143 });
Adam Cohen482ed822012-03-02 14:15:13 -08001144 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001145 boolean cancelled = false;
1146 public void onAnimationEnd(Animator animation) {
1147 // If the animation was cancelled, it means that another animation
1148 // has interrupted this one, and we don't want to lock the item into
1149 // place just yet.
1150 if (!cancelled) {
1151 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001152 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001153 }
1154 if (mReorderAnimators.containsKey(lp)) {
1155 mReorderAnimators.remove(lp);
1156 }
1157 }
1158 public void onAnimationCancel(Animator animation) {
1159 cancelled = true;
1160 }
1161 });
Adam Cohen482ed822012-03-02 14:15:13 -08001162 va.setStartDelay(delay);
1163 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001164 return true;
1165 }
1166 return false;
1167 }
1168
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001169 /**
1170 * Estimate where the top left cell of the dragged item will land if it is dropped.
1171 *
1172 * @param originX The X value of the top left corner of the item
1173 * @param originY The Y value of the top left corner of the item
1174 * @param spanX The number of horizontal cells that the item spans
1175 * @param spanY The number of vertical cells that the item spans
1176 * @param result The estimated drop cell X and Y.
1177 */
1178 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001179 final int countX = mCountX;
1180 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001181
Michael Jurkaa63c4522010-08-19 13:52:27 -07001182 // pointToCellRounded takes the top left of a cell but will pad that with
1183 // cellWidth/2 and cellHeight/2 when finding the matching cell
1184 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001185
1186 // If the item isn't fully on this screen, snap to the edges
1187 int rightOverhang = result[0] + spanX - countX;
1188 if (rightOverhang > 0) {
1189 result[0] -= rightOverhang; // Snap to right
1190 }
1191 result[0] = Math.max(0, result[0]); // Snap to left
1192 int bottomOverhang = result[1] + spanY - countY;
1193 if (bottomOverhang > 0) {
1194 result[1] -= bottomOverhang; // Snap to bottom
1195 }
1196 result[1] = Math.max(0, result[1]); // Snap to top
1197 }
1198
Adam Cohen482ed822012-03-02 14:15:13 -08001199 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1200 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001201 final int oldDragCellX = mDragCell[0];
1202 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001203
Winson Chungb8c69f32011-10-19 21:36:08 -07001204 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001205 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1206 } else {
1207 mDragCenter.set(originX, originY);
1208 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001209
Adam Cohen2801caf2011-05-13 20:57:39 -07001210 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001211 return;
1212 }
1213
Adam Cohen482ed822012-03-02 14:15:13 -08001214 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1215 mDragCell[0] = cellX;
1216 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001217 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001218 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001219 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001220
Joe Onorato4be866d2010-10-10 11:26:02 -07001221 int left = topLeft[0];
1222 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001223
Winson Chung892c74d2013-08-22 16:15:50 -07001224 // Offset icons by their padding
1225 if (v instanceof BubbleTextView) {
1226 top += v.getPaddingTop();
1227 }
1228
Winson Chungb8c69f32011-10-19 21:36:08 -07001229 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001230 // When drawing the drag outline, it did not account for margin offsets
1231 // added by the view's parent.
1232 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1233 left += lp.leftMargin;
1234 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001235
Adam Cohen99e8b402011-03-25 19:23:43 -07001236 // Offsets due to the size difference between the View and the dragOutline.
1237 // There is a size difference to account for the outer blur, which may lie
1238 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001239 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001240 // We center about the x axis
1241 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1242 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001243 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001244 if (dragOffset != null && dragRegion != null) {
1245 // Center the drag region *horizontally* in the cell and apply a drag
1246 // outline offset
1247 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1248 - dragRegion.width()) / 2;
1249 top += dragOffset.y;
1250 } else {
1251 // Center the drag outline in the cell
1252 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1253 - dragOutline.getWidth()) / 2;
1254 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1255 - dragOutline.getHeight()) / 2;
1256 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001257 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001258 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001259 mDragOutlineAnims[oldIndex].animateOut();
1260 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001261 Rect r = mDragOutlines[mDragOutlineCurrent];
1262 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1263 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001264 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001265 }
Winson Chung150fbab2010-09-29 17:14:26 -07001266
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001267 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1268 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001269 }
1270 }
1271
Adam Cohene0310962011-04-18 16:15:31 -07001272 public void clearDragOutlines() {
1273 final int oldIndex = mDragOutlineCurrent;
1274 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001275 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001276 }
1277
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001279 * Find a vacant area that will fit the given bounds nearest the requested
1280 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001281 *
Romain Guy51afc022009-05-04 18:03:43 -07001282 * @param pixelX The X location at which you want to search for a vacant area.
1283 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001284 * @param spanX Horizontal span of the object.
1285 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001286 * @param result Array in which to place the result, or null (in which case a new array will
1287 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001288 * @return The X, Y cell of a vacant area that can contain this object,
1289 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001291 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1292 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001293 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001294 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001295
Michael Jurka6a1435d2010-09-27 17:35:12 -07001296 /**
1297 * Find a vacant area that will fit the given bounds nearest the requested
1298 * cell location. Uses Euclidean distance to score multiple vacant areas.
1299 *
1300 * @param pixelX The X location at which you want to search for a vacant area.
1301 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001302 * @param minSpanX The minimum horizontal span required
1303 * @param minSpanY The minimum vertical span required
1304 * @param spanX Horizontal span of the object.
1305 * @param spanY Vertical span of the object.
1306 * @param result Array in which to place the result, or null (in which case a new array will
1307 * be allocated)
1308 * @return The X, Y cell of a vacant area that can contain this object,
1309 * nearest the requested location.
1310 */
1311 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1312 int spanY, int[] result, int[] resultSpan) {
1313 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1314 result, resultSpan);
1315 }
1316
1317 /**
1318 * Find a vacant area that will fit the given bounds nearest the requested
1319 * cell location. Uses Euclidean distance to score multiple vacant areas.
1320 *
1321 * @param pixelX The X location at which you want to search for a vacant area.
1322 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001323 * @param spanX Horizontal span of the object.
1324 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001325 * @param ignoreOccupied If true, the result can be an occupied cell
1326 * @param result Array in which to place the result, or null (in which case a new array will
1327 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001328 * @return The X, Y cell of a vacant area that can contain this object,
1329 * nearest the requested location.
1330 */
Adam Cohendf035382011-04-11 17:22:04 -07001331 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1332 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001333 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001334 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001335 }
1336
1337 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1338 private void lazyInitTempRectStack() {
1339 if (mTempRectStack.isEmpty()) {
1340 for (int i = 0; i < mCountX * mCountY; i++) {
1341 mTempRectStack.push(new Rect());
1342 }
1343 }
1344 }
Adam Cohen482ed822012-03-02 14:15:13 -08001345
Adam Cohend41fbf52012-02-16 23:53:59 -08001346 private void recycleTempRects(Stack<Rect> used) {
1347 while (!used.isEmpty()) {
1348 mTempRectStack.push(used.pop());
1349 }
1350 }
1351
1352 /**
1353 * Find a vacant area that will fit the given bounds nearest the requested
1354 * cell location. Uses Euclidean distance to score multiple vacant areas.
1355 *
1356 * @param pixelX The X location at which you want to search for a vacant area.
1357 * @param pixelY The Y location at which you want to search for a vacant area.
1358 * @param minSpanX The minimum horizontal span required
1359 * @param minSpanY The minimum vertical span required
1360 * @param spanX Horizontal span of the object.
1361 * @param spanY Vertical span of the object.
1362 * @param ignoreOccupied If true, the result can be an occupied cell
1363 * @param result Array in which to place the result, or null (in which case a new array will
1364 * be allocated)
1365 * @return The X, Y cell of a vacant area that can contain this object,
1366 * nearest the requested location.
1367 */
1368 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001369 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1370 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001371 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001372 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001373 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001374
Adam Cohene3e27a82011-04-15 12:07:39 -07001375 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1376 // to the center of the item, but we are searching based on the top-left cell, so
1377 // we translate the point over to correspond to the top-left.
1378 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1379 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1380
Jeff Sharkey70864282009-04-07 21:08:40 -07001381 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001382 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001383 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001384 final Rect bestRect = new Rect(-1, -1, -1, -1);
1385 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001386
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001387 final int countX = mCountX;
1388 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001389
Adam Cohend41fbf52012-02-16 23:53:59 -08001390 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1391 spanX < minSpanX || spanY < minSpanY) {
1392 return bestXY;
1393 }
1394
1395 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001396 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001397 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1398 int ySize = -1;
1399 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001400 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001401 // First, let's see if this thing fits anywhere
1402 for (int i = 0; i < minSpanX; i++) {
1403 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001404 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001405 continue inner;
1406 }
Michael Jurkac28de512010-08-13 11:27:44 -07001407 }
1408 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001409 xSize = minSpanX;
1410 ySize = minSpanY;
1411
1412 // We know that the item will fit at _some_ acceptable size, now let's see
1413 // how big we can make it. We'll alternate between incrementing x and y spans
1414 // until we hit a limit.
1415 boolean incX = true;
1416 boolean hitMaxX = xSize >= spanX;
1417 boolean hitMaxY = ySize >= spanY;
1418 while (!(hitMaxX && hitMaxY)) {
1419 if (incX && !hitMaxX) {
1420 for (int j = 0; j < ySize; j++) {
1421 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1422 // We can't move out horizontally
1423 hitMaxX = true;
1424 }
1425 }
1426 if (!hitMaxX) {
1427 xSize++;
1428 }
1429 } else if (!hitMaxY) {
1430 for (int i = 0; i < xSize; i++) {
1431 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1432 // We can't move out vertically
1433 hitMaxY = true;
1434 }
1435 }
1436 if (!hitMaxY) {
1437 ySize++;
1438 }
1439 }
1440 hitMaxX |= xSize >= spanX;
1441 hitMaxY |= ySize >= spanY;
1442 incX = !incX;
1443 }
1444 incX = true;
1445 hitMaxX = xSize >= spanX;
1446 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001447 }
Winson Chung0be025d2011-05-23 17:45:09 -07001448 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001449 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450
Adam Cohend41fbf52012-02-16 23:53:59 -08001451 // We verify that the current rect is not a sub-rect of any of our previous
1452 // candidates. In this case, the current rect is disqualified in favour of the
1453 // containing rect.
1454 Rect currentRect = mTempRectStack.pop();
1455 currentRect.set(x, y, x + xSize, y + ySize);
1456 boolean contained = false;
1457 for (Rect r : validRegions) {
1458 if (r.contains(currentRect)) {
1459 contained = true;
1460 break;
1461 }
1462 }
1463 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001464 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1465 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001466
Adam Cohend41fbf52012-02-16 23:53:59 -08001467 if ((distance <= bestDistance && !contained) ||
1468 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001469 bestDistance = distance;
1470 bestXY[0] = x;
1471 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001472 if (resultSpan != null) {
1473 resultSpan[0] = xSize;
1474 resultSpan[1] = ySize;
1475 }
1476 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
1479 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001480 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001481 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482
Adam Cohenc0dcf592011-06-01 15:30:43 -07001483 // Return -1, -1 if no suitable location found
1484 if (bestDistance == Double.MAX_VALUE) {
1485 bestXY[0] = -1;
1486 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001487 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001488 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001489 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001491
Adam Cohen482ed822012-03-02 14:15:13 -08001492 /**
1493 * Find a vacant area that will fit the given bounds nearest the requested
1494 * cell location, and will also weigh in a suggested direction vector of the
1495 * desired location. This method computers distance based on unit grid distances,
1496 * not pixel distances.
1497 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001498 * @param cellX The X cell nearest to which you want to search for a vacant area.
1499 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001500 * @param spanX Horizontal span of the object.
1501 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001502 * @param direction The favored direction in which the views should move from x, y
1503 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1504 * matches exactly. Otherwise we find the best matching direction.
1505 * @param occoupied The array which represents which cells in the CellLayout are occupied
1506 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001507 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001508 * @param result Array in which to place the result, or null (in which case a new array will
1509 * be allocated)
1510 * @return The X, Y cell of a vacant area that can contain this object,
1511 * nearest the requested location.
1512 */
1513 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001514 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001515 // Keep track of best-scoring drop area
1516 final int[] bestXY = result != null ? result : new int[2];
1517 float bestDistance = Float.MAX_VALUE;
1518 int bestDirectionScore = Integer.MIN_VALUE;
1519
1520 final int countX = mCountX;
1521 final int countY = mCountY;
1522
1523 for (int y = 0; y < countY - (spanY - 1); y++) {
1524 inner:
1525 for (int x = 0; x < countX - (spanX - 1); x++) {
1526 // First, let's see if this thing fits anywhere
1527 for (int i = 0; i < spanX; i++) {
1528 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001529 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001530 continue inner;
1531 }
1532 }
1533 }
1534
1535 float distance = (float)
1536 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1537 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001538 computeDirectionVector(x - cellX, y - cellY, curDirection);
1539 // The direction score is just the dot product of the two candidate direction
1540 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001541 int curDirectionScore = direction[0] * curDirection[0] +
1542 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001543 boolean exactDirectionOnly = false;
1544 boolean directionMatches = direction[0] == curDirection[0] &&
1545 direction[0] == curDirection[0];
1546 if ((directionMatches || !exactDirectionOnly) &&
1547 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001548 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1549 bestDistance = distance;
1550 bestDirectionScore = curDirectionScore;
1551 bestXY[0] = x;
1552 bestXY[1] = y;
1553 }
1554 }
1555 }
1556
1557 // Return -1, -1 if no suitable location found
1558 if (bestDistance == Float.MAX_VALUE) {
1559 bestXY[0] = -1;
1560 bestXY[1] = -1;
1561 }
1562 return bestXY;
1563 }
1564
1565 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001566 int[] direction, ItemConfiguration currentState) {
1567 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001568 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001569 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001570 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1571
Adam Cohen8baab352012-03-20 17:39:21 -07001572 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001573
1574 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001575 c.x = mTempLocation[0];
1576 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001577 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001578 }
Adam Cohen8baab352012-03-20 17:39:21 -07001579 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001580 return success;
1581 }
1582
Adam Cohenf3900c22012-11-16 18:28:11 -08001583 /**
1584 * This helper class defines a cluster of views. It helps with defining complex edges
1585 * of the cluster and determining how those edges interact with other views. The edges
1586 * essentially define a fine-grained boundary around the cluster of views -- like a more
1587 * precise version of a bounding box.
1588 */
1589 private class ViewCluster {
1590 final static int LEFT = 0;
1591 final static int TOP = 1;
1592 final static int RIGHT = 2;
1593 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001594
Adam Cohenf3900c22012-11-16 18:28:11 -08001595 ArrayList<View> views;
1596 ItemConfiguration config;
1597 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001598
Adam Cohenf3900c22012-11-16 18:28:11 -08001599 int[] leftEdge = new int[mCountY];
1600 int[] rightEdge = new int[mCountY];
1601 int[] topEdge = new int[mCountX];
1602 int[] bottomEdge = new int[mCountX];
1603 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1604
1605 @SuppressWarnings("unchecked")
1606 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1607 this.views = (ArrayList<View>) views.clone();
1608 this.config = config;
1609 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001610 }
1611
Adam Cohenf3900c22012-11-16 18:28:11 -08001612 void resetEdges() {
1613 for (int i = 0; i < mCountX; i++) {
1614 topEdge[i] = -1;
1615 bottomEdge[i] = -1;
1616 }
1617 for (int i = 0; i < mCountY; i++) {
1618 leftEdge[i] = -1;
1619 rightEdge[i] = -1;
1620 }
1621 leftEdgeDirty = true;
1622 rightEdgeDirty = true;
1623 bottomEdgeDirty = true;
1624 topEdgeDirty = true;
1625 boundingRectDirty = true;
1626 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001627
Adam Cohenf3900c22012-11-16 18:28:11 -08001628 void computeEdge(int which, int[] edge) {
1629 int count = views.size();
1630 for (int i = 0; i < count; i++) {
1631 CellAndSpan cs = config.map.get(views.get(i));
1632 switch (which) {
1633 case LEFT:
1634 int left = cs.x;
1635 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1636 if (left < edge[j] || edge[j] < 0) {
1637 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001638 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001639 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001640 break;
1641 case RIGHT:
1642 int right = cs.x + cs.spanX;
1643 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1644 if (right > edge[j]) {
1645 edge[j] = right;
1646 }
1647 }
1648 break;
1649 case TOP:
1650 int top = cs.y;
1651 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1652 if (top < edge[j] || edge[j] < 0) {
1653 edge[j] = top;
1654 }
1655 }
1656 break;
1657 case BOTTOM:
1658 int bottom = cs.y + cs.spanY;
1659 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1660 if (bottom > edge[j]) {
1661 edge[j] = bottom;
1662 }
1663 }
1664 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001665 }
1666 }
1667 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001668
1669 boolean isViewTouchingEdge(View v, int whichEdge) {
1670 CellAndSpan cs = config.map.get(v);
1671
1672 int[] edge = getEdge(whichEdge);
1673
1674 switch (whichEdge) {
1675 case LEFT:
1676 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1677 if (edge[i] == cs.x + cs.spanX) {
1678 return true;
1679 }
1680 }
1681 break;
1682 case RIGHT:
1683 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1684 if (edge[i] == cs.x) {
1685 return true;
1686 }
1687 }
1688 break;
1689 case TOP:
1690 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1691 if (edge[i] == cs.y + cs.spanY) {
1692 return true;
1693 }
1694 }
1695 break;
1696 case BOTTOM:
1697 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1698 if (edge[i] == cs.y) {
1699 return true;
1700 }
1701 }
1702 break;
1703 }
1704 return false;
1705 }
1706
1707 void shift(int whichEdge, int delta) {
1708 for (View v: views) {
1709 CellAndSpan c = config.map.get(v);
1710 switch (whichEdge) {
1711 case LEFT:
1712 c.x -= delta;
1713 break;
1714 case RIGHT:
1715 c.x += delta;
1716 break;
1717 case TOP:
1718 c.y -= delta;
1719 break;
1720 case BOTTOM:
1721 default:
1722 c.y += delta;
1723 break;
1724 }
1725 }
1726 resetEdges();
1727 }
1728
1729 public void addView(View v) {
1730 views.add(v);
1731 resetEdges();
1732 }
1733
1734 public Rect getBoundingRect() {
1735 if (boundingRectDirty) {
1736 boolean first = true;
1737 for (View v: views) {
1738 CellAndSpan c = config.map.get(v);
1739 if (first) {
1740 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1741 first = false;
1742 } else {
1743 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1744 }
1745 }
1746 }
1747 return boundingRect;
1748 }
1749
1750 public int[] getEdge(int which) {
1751 switch (which) {
1752 case LEFT:
1753 return getLeftEdge();
1754 case RIGHT:
1755 return getRightEdge();
1756 case TOP:
1757 return getTopEdge();
1758 case BOTTOM:
1759 default:
1760 return getBottomEdge();
1761 }
1762 }
1763
1764 public int[] getLeftEdge() {
1765 if (leftEdgeDirty) {
1766 computeEdge(LEFT, leftEdge);
1767 }
1768 return leftEdge;
1769 }
1770
1771 public int[] getRightEdge() {
1772 if (rightEdgeDirty) {
1773 computeEdge(RIGHT, rightEdge);
1774 }
1775 return rightEdge;
1776 }
1777
1778 public int[] getTopEdge() {
1779 if (topEdgeDirty) {
1780 computeEdge(TOP, topEdge);
1781 }
1782 return topEdge;
1783 }
1784
1785 public int[] getBottomEdge() {
1786 if (bottomEdgeDirty) {
1787 computeEdge(BOTTOM, bottomEdge);
1788 }
1789 return bottomEdge;
1790 }
1791
1792 PositionComparator comparator = new PositionComparator();
1793 class PositionComparator implements Comparator<View> {
1794 int whichEdge = 0;
1795 public int compare(View left, View right) {
1796 CellAndSpan l = config.map.get(left);
1797 CellAndSpan r = config.map.get(right);
1798 switch (whichEdge) {
1799 case LEFT:
1800 return (r.x + r.spanX) - (l.x + l.spanX);
1801 case RIGHT:
1802 return l.x - r.x;
1803 case TOP:
1804 return (r.y + r.spanY) - (l.y + l.spanY);
1805 case BOTTOM:
1806 default:
1807 return l.y - r.y;
1808 }
1809 }
1810 }
1811
1812 public void sortConfigurationForEdgePush(int edge) {
1813 comparator.whichEdge = edge;
1814 Collections.sort(config.sortedViews, comparator);
1815 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001816 }
1817
Adam Cohenf3900c22012-11-16 18:28:11 -08001818 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1819 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001820
Adam Cohenf3900c22012-11-16 18:28:11 -08001821 ViewCluster cluster = new ViewCluster(views, currentState);
1822 Rect clusterRect = cluster.getBoundingRect();
1823 int whichEdge;
1824 int pushDistance;
1825 boolean fail = false;
1826
1827 // Determine the edge of the cluster that will be leading the push and how far
1828 // the cluster must be shifted.
1829 if (direction[0] < 0) {
1830 whichEdge = ViewCluster.LEFT;
1831 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001832 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001833 whichEdge = ViewCluster.RIGHT;
1834 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1835 } else if (direction[1] < 0) {
1836 whichEdge = ViewCluster.TOP;
1837 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1838 } else {
1839 whichEdge = ViewCluster.BOTTOM;
1840 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001841 }
1842
Adam Cohenf3900c22012-11-16 18:28:11 -08001843 // Break early for invalid push distance.
1844 if (pushDistance <= 0) {
1845 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001846 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001847
1848 // Mark the occupied state as false for the group of views we want to move.
1849 for (View v: views) {
1850 CellAndSpan c = currentState.map.get(v);
1851 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1852 }
1853
1854 // We save the current configuration -- if we fail to find a solution we will revert
1855 // to the initial state. The process of finding a solution modifies the configuration
1856 // in place, hence the need for revert in the failure case.
1857 currentState.save();
1858
1859 // The pushing algorithm is simplified by considering the views in the order in which
1860 // they would be pushed by the cluster. For example, if the cluster is leading with its
1861 // left edge, we consider sort the views by their right edge, from right to left.
1862 cluster.sortConfigurationForEdgePush(whichEdge);
1863
1864 while (pushDistance > 0 && !fail) {
1865 for (View v: currentState.sortedViews) {
1866 // For each view that isn't in the cluster, we see if the leading edge of the
1867 // cluster is contacting the edge of that view. If so, we add that view to the
1868 // cluster.
1869 if (!cluster.views.contains(v) && v != dragView) {
1870 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1871 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1872 if (!lp.canReorder) {
1873 // The push solution includes the all apps button, this is not viable.
1874 fail = true;
1875 break;
1876 }
1877 cluster.addView(v);
1878 CellAndSpan c = currentState.map.get(v);
1879
1880 // Adding view to cluster, mark it as not occupied.
1881 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1882 }
1883 }
1884 }
1885 pushDistance--;
1886
1887 // The cluster has been completed, now we move the whole thing over in the appropriate
1888 // direction.
1889 cluster.shift(whichEdge, 1);
1890 }
1891
1892 boolean foundSolution = false;
1893 clusterRect = cluster.getBoundingRect();
1894
1895 // Due to the nature of the algorithm, the only check required to verify a valid solution
1896 // is to ensure that completed shifted cluster lies completely within the cell layout.
1897 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1898 clusterRect.bottom <= mCountY) {
1899 foundSolution = true;
1900 } else {
1901 currentState.restore();
1902 }
1903
1904 // In either case, we set the occupied array as marked for the location of the views
1905 for (View v: cluster.views) {
1906 CellAndSpan c = currentState.map.get(v);
1907 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1908 }
1909
1910 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001911 }
1912
Adam Cohen482ed822012-03-02 14:15:13 -08001913 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001914 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001915 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001916
Adam Cohen8baab352012-03-20 17:39:21 -07001917 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001918 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001919 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001920 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001921 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001922 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001923 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001924 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001925 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001926 }
1927 }
Adam Cohen8baab352012-03-20 17:39:21 -07001928
Adam Cohen8baab352012-03-20 17:39:21 -07001929 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001930 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001931 CellAndSpan c = currentState.map.get(v);
1932 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1933 }
1934
Adam Cohen47a876d2012-03-19 13:21:41 -07001935 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1936 int top = boundingRect.top;
1937 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001938 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001939 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001940 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001941 CellAndSpan c = currentState.map.get(v);
1942 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001943 }
1944
Adam Cohen482ed822012-03-02 14:15:13 -08001945 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1946
Adam Cohenf3900c22012-11-16 18:28:11 -08001947 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1948 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001949
Adam Cohen8baab352012-03-20 17:39:21 -07001950 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001951 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001952 int deltaX = mTempLocation[0] - boundingRect.left;
1953 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001954 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001955 CellAndSpan c = currentState.map.get(v);
1956 c.x += deltaX;
1957 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001958 }
1959 success = true;
1960 }
Adam Cohen8baab352012-03-20 17:39:21 -07001961
1962 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001963 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001964 CellAndSpan c = currentState.map.get(v);
1965 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001966 }
1967 return success;
1968 }
1969
1970 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1971 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1972 }
1973
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001974 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1975 // to push items in each of the cardinal directions, in an order based on the direction vector
1976 // passed.
1977 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1978 int[] direction, View ignoreView, ItemConfiguration solution) {
1979 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001980 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001981 // separately in each of the components.
1982 int temp = direction[1];
1983 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001984
1985 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001986 ignoreView, solution)) {
1987 return true;
1988 }
1989 direction[1] = temp;
1990 temp = direction[0];
1991 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001992
1993 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001994 ignoreView, solution)) {
1995 return true;
1996 }
1997 // Revert the direction
1998 direction[0] = temp;
1999
2000 // Now we try pushing in each component of the opposite direction
2001 direction[0] *= -1;
2002 direction[1] *= -1;
2003 temp = direction[1];
2004 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002005 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002006 ignoreView, solution)) {
2007 return true;
2008 }
2009
2010 direction[1] = temp;
2011 temp = direction[0];
2012 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002013 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002014 ignoreView, solution)) {
2015 return true;
2016 }
2017 // revert the direction
2018 direction[0] = temp;
2019 direction[0] *= -1;
2020 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002021
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002022 } else {
2023 // If the direction vector has a single non-zero component, we push first in the
2024 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002025 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002026 ignoreView, solution)) {
2027 return true;
2028 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002029 // Then we try the opposite direction
2030 direction[0] *= -1;
2031 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002032 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002033 ignoreView, solution)) {
2034 return true;
2035 }
2036 // Switch the direction back
2037 direction[0] *= -1;
2038 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002039
2040 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002041 // to find a solution by pushing along the perpendicular axis.
2042
2043 // Swap the components
2044 int temp = direction[1];
2045 direction[1] = direction[0];
2046 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002047 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002048 ignoreView, solution)) {
2049 return true;
2050 }
2051
2052 // Then we try the opposite direction
2053 direction[0] *= -1;
2054 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002055 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002056 ignoreView, solution)) {
2057 return true;
2058 }
2059 // Switch the direction back
2060 direction[0] *= -1;
2061 direction[1] *= -1;
2062
2063 // Swap the components back
2064 temp = direction[1];
2065 direction[1] = direction[0];
2066 direction[0] = temp;
2067 }
2068 return false;
2069 }
2070
Adam Cohen482ed822012-03-02 14:15:13 -08002071 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002072 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002073 // Return early if get invalid cell positions
2074 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002075
Adam Cohen8baab352012-03-20 17:39:21 -07002076 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002077 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002078
Adam Cohen8baab352012-03-20 17:39:21 -07002079 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002080 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002081 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002082 if (c != null) {
2083 c.x = cellX;
2084 c.y = cellY;
2085 }
Adam Cohen482ed822012-03-02 14:15:13 -08002086 }
Adam Cohen482ed822012-03-02 14:15:13 -08002087 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2088 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002089 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002090 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002091 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002092 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002093 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002094 if (Rect.intersects(r0, r1)) {
2095 if (!lp.canReorder) {
2096 return false;
2097 }
2098 mIntersectingViews.add(child);
2099 }
2100 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002101
Winson Chung5f8afe62013-08-12 16:19:28 -07002102 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002103 // we try to find a solution such that no displaced item travels through another item
2104 // without also displacing that item.
2105 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002106 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002107 return true;
2108 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002109
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002110 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002111 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002112 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002113 return true;
2114 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002115
Adam Cohen482ed822012-03-02 14:15:13 -08002116 // Ok, they couldn't move as a block, let's move them individually
2117 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002118 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002119 return false;
2120 }
2121 }
2122 return true;
2123 }
2124
2125 /*
2126 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2127 * the provided point and the provided cell
2128 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002129 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002130 double angle = Math.atan(((float) deltaY) / deltaX);
2131
2132 result[0] = 0;
2133 result[1] = 0;
2134 if (Math.abs(Math.cos(angle)) > 0.5f) {
2135 result[0] = (int) Math.signum(deltaX);
2136 }
2137 if (Math.abs(Math.sin(angle)) > 0.5f) {
2138 result[1] = (int) Math.signum(deltaY);
2139 }
2140 }
2141
Adam Cohen8baab352012-03-20 17:39:21 -07002142 private void copyOccupiedArray(boolean[][] occupied) {
2143 for (int i = 0; i < mCountX; i++) {
2144 for (int j = 0; j < mCountY; j++) {
2145 occupied[i][j] = mOccupied[i][j];
2146 }
2147 }
2148 }
2149
Adam Cohen482ed822012-03-02 14:15:13 -08002150 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
2151 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002152 // Copy the current state into the solution. This solution will be manipulated as necessary.
2153 copyCurrentStateToSolution(solution, false);
2154 // Copy the current occupied array into the temporary occupied array. This array will be
2155 // manipulated as necessary to find a solution.
2156 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002157
2158 // We find the nearest cell into which we would place the dragged item, assuming there's
2159 // nothing in its way.
2160 int result[] = new int[2];
2161 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2162
2163 boolean success = false;
2164 // First we try the exact nearest position of the item being dragged,
2165 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002166 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2167 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002168
2169 if (!success) {
2170 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2171 // x, then 1 in y etc.
2172 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
2173 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
2174 dragView, false, solution);
2175 } else if (spanY > minSpanY) {
2176 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
2177 dragView, true, solution);
2178 }
2179 solution.isSolution = false;
2180 } else {
2181 solution.isSolution = true;
2182 solution.dragViewX = result[0];
2183 solution.dragViewY = result[1];
2184 solution.dragViewSpanX = spanX;
2185 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002186 }
2187 return solution;
2188 }
2189
2190 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002191 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002192 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002193 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002194 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002195 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002196 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002197 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002198 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002199 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002200 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002201 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002202 }
2203 }
2204
2205 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2206 for (int i = 0; i < mCountX; i++) {
2207 for (int j = 0; j < mCountY; j++) {
2208 mTmpOccupied[i][j] = false;
2209 }
2210 }
2211
Michael Jurkaa52570f2012-03-20 03:18:20 -07002212 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002213 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002214 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002215 if (child == dragView) continue;
2216 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002217 CellAndSpan c = solution.map.get(child);
2218 if (c != null) {
2219 lp.tmpCellX = c.x;
2220 lp.tmpCellY = c.y;
2221 lp.cellHSpan = c.spanX;
2222 lp.cellVSpan = c.spanY;
2223 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002224 }
2225 }
2226 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2227 solution.dragViewSpanY, mTmpOccupied, true);
2228 }
2229
2230 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2231 commitDragView) {
2232
2233 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2234 for (int i = 0; i < mCountX; i++) {
2235 for (int j = 0; j < mCountY; j++) {
2236 occupied[i][j] = false;
2237 }
2238 }
2239
Michael Jurkaa52570f2012-03-20 03:18:20 -07002240 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002241 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002242 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002243 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002244 CellAndSpan c = solution.map.get(child);
2245 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002246 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2247 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002248 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002249 }
2250 }
2251 if (commitDragView) {
2252 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2253 solution.dragViewSpanY, occupied, true);
2254 }
2255 }
2256
Adam Cohen19f37922012-03-21 11:59:11 -07002257 // This method starts or changes the reorder hint animations
2258 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2259 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002260 for (int i = 0; i < childCount; i++) {
2261 View child = mShortcutsAndWidgets.getChildAt(i);
2262 if (child == dragView) continue;
2263 CellAndSpan c = solution.map.get(child);
2264 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2265 if (c != null) {
2266 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2267 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002268 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002269 }
2270 }
2271 }
2272
2273 // Class which represents the reorder hint animations. These animations show that an item is
2274 // in a temporary state, and hint at where the item will return to.
2275 class ReorderHintAnimation {
2276 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002277 float finalDeltaX;
2278 float finalDeltaY;
2279 float initDeltaX;
2280 float initDeltaY;
2281 float finalScale;
2282 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002283 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002284 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002285
2286 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2287 int spanX, int spanY) {
2288 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2289 final int x0 = mTmpPoint[0];
2290 final int y0 = mTmpPoint[1];
2291 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2292 final int x1 = mTmpPoint[0];
2293 final int y1 = mTmpPoint[1];
2294 final int dX = x1 - x0;
2295 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002296 finalDeltaX = 0;
2297 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002298 if (dX == dY && dX == 0) {
2299 } else {
2300 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002301 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002302 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002303 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002304 } else {
2305 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002306 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002307 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002308 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002309 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002310 }
2311 }
Adam Cohend024f982012-05-23 18:26:45 -07002312 initDeltaX = child.getTranslationX();
2313 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002314 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002315 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002316 this.child = child;
2317 }
2318
Adam Cohend024f982012-05-23 18:26:45 -07002319 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002320 if (mShakeAnimators.containsKey(child)) {
2321 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002322 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002323 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002324 if (finalDeltaX == 0 && finalDeltaY == 0) {
2325 completeAnimationImmediately();
2326 return;
2327 }
Adam Cohen19f37922012-03-21 11:59:11 -07002328 }
Adam Cohend024f982012-05-23 18:26:45 -07002329 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002330 return;
2331 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002332 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002333 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002334 va.setRepeatMode(ValueAnimator.REVERSE);
2335 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002336 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002337 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002338 va.addUpdateListener(new AnimatorUpdateListener() {
2339 @Override
2340 public void onAnimationUpdate(ValueAnimator animation) {
2341 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002342 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2343 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002344 child.setTranslationX(x);
2345 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002346 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002347 child.setScaleX(s);
2348 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002349 }
2350 });
2351 va.addListener(new AnimatorListenerAdapter() {
2352 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002353 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002354 initDeltaX = 0;
2355 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002356 initScale = getChildrenScale();
Adam Cohen19f37922012-03-21 11:59:11 -07002357 }
2358 });
Adam Cohen19f37922012-03-21 11:59:11 -07002359 mShakeAnimators.put(child, this);
2360 va.start();
2361 }
2362
Adam Cohend024f982012-05-23 18:26:45 -07002363 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002364 if (a != null) {
2365 a.cancel();
2366 }
Adam Cohen19f37922012-03-21 11:59:11 -07002367 }
Adam Cohene7587d22012-05-24 18:50:02 -07002368
Brandon Keely50e6e562012-05-08 16:28:49 -07002369 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002370 if (a != null) {
2371 a.cancel();
2372 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002373
Michael Jurka2ecf9952012-06-18 12:52:28 -07002374 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002375 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002376 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002377 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2378 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002379 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2380 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002381 );
2382 s.setDuration(REORDER_ANIMATION_DURATION);
2383 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2384 s.start();
2385 }
Adam Cohen19f37922012-03-21 11:59:11 -07002386 }
2387
2388 private void completeAndClearReorderHintAnimations() {
2389 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002390 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002391 }
2392 mShakeAnimators.clear();
2393 }
2394
Adam Cohen482ed822012-03-02 14:15:13 -08002395 private void commitTempPlacement() {
2396 for (int i = 0; i < mCountX; i++) {
2397 for (int j = 0; j < mCountY; j++) {
2398 mOccupied[i][j] = mTmpOccupied[i][j];
2399 }
2400 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002401 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002402 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002403 View child = mShortcutsAndWidgets.getChildAt(i);
2404 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2405 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002406 // We do a null check here because the item info can be null in the case of the
2407 // AllApps button in the hotseat.
2408 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002409 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2410 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2411 info.requiresDbUpdate = true;
2412 }
Adam Cohen2acce882012-03-28 19:03:19 -07002413 info.cellX = lp.cellX = lp.tmpCellX;
2414 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002415 info.spanX = lp.cellHSpan;
2416 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002417 }
Adam Cohen482ed822012-03-02 14:15:13 -08002418 }
Adam Cohen2acce882012-03-28 19:03:19 -07002419 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002420 }
2421
2422 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002423 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002424 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002425 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002426 lp.useTmpCoords = useTempCoords;
2427 }
2428 }
2429
Adam Cohen482ed822012-03-02 14:15:13 -08002430 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2431 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2432 int[] result = new int[2];
2433 int[] resultSpan = new int[2];
2434 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2435 resultSpan);
2436 if (result[0] >= 0 && result[1] >= 0) {
2437 copyCurrentStateToSolution(solution, false);
2438 solution.dragViewX = result[0];
2439 solution.dragViewY = result[1];
2440 solution.dragViewSpanX = resultSpan[0];
2441 solution.dragViewSpanY = resultSpan[1];
2442 solution.isSolution = true;
2443 } else {
2444 solution.isSolution = false;
2445 }
2446 return solution;
2447 }
2448
2449 public void prepareChildForDrag(View child) {
2450 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002451 }
2452
Adam Cohen19f37922012-03-21 11:59:11 -07002453 /* This seems like it should be obvious and straight-forward, but when the direction vector
2454 needs to match with the notion of the dragView pushing other views, we have to employ
2455 a slightly more subtle notion of the direction vector. The question is what two points is
2456 the vector between? The center of the dragView and its desired destination? Not quite, as
2457 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2458 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2459 or right, which helps make pushing feel right.
2460 */
2461 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2462 int spanY, View dragView, int[] resultDirection) {
2463 int[] targetDestination = new int[2];
2464
2465 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2466 Rect dragRect = new Rect();
2467 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2468 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2469
2470 Rect dropRegionRect = new Rect();
2471 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2472 dragView, dropRegionRect, mIntersectingViews);
2473
2474 int dropRegionSpanX = dropRegionRect.width();
2475 int dropRegionSpanY = dropRegionRect.height();
2476
2477 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2478 dropRegionRect.height(), dropRegionRect);
2479
2480 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2481 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2482
2483 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2484 deltaX = 0;
2485 }
2486 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2487 deltaY = 0;
2488 }
2489
2490 if (deltaX == 0 && deltaY == 0) {
2491 // No idea what to do, give a random direction.
2492 resultDirection[0] = 1;
2493 resultDirection[1] = 0;
2494 } else {
2495 computeDirectionVector(deltaX, deltaY, resultDirection);
2496 }
2497 }
2498
2499 // For a given cell and span, fetch the set of views intersecting the region.
2500 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2501 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2502 if (boundingRect != null) {
2503 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2504 }
2505 intersectingViews.clear();
2506 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2507 Rect r1 = new Rect();
2508 final int count = mShortcutsAndWidgets.getChildCount();
2509 for (int i = 0; i < count; i++) {
2510 View child = mShortcutsAndWidgets.getChildAt(i);
2511 if (child == dragView) continue;
2512 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2513 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2514 if (Rect.intersects(r0, r1)) {
2515 mIntersectingViews.add(child);
2516 if (boundingRect != null) {
2517 boundingRect.union(r1);
2518 }
2519 }
2520 }
2521 }
2522
2523 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2524 View dragView, int[] result) {
2525 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2526 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2527 mIntersectingViews);
2528 return !mIntersectingViews.isEmpty();
2529 }
2530
2531 void revertTempState() {
2532 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2533 final int count = mShortcutsAndWidgets.getChildCount();
2534 for (int i = 0; i < count; i++) {
2535 View child = mShortcutsAndWidgets.getChildAt(i);
2536 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2537 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2538 lp.tmpCellX = lp.cellX;
2539 lp.tmpCellY = lp.cellY;
2540 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2541 0, false, false);
2542 }
2543 }
2544 completeAndClearReorderHintAnimations();
2545 setItemPlacementDirty(false);
2546 }
2547
Adam Cohenbebf0422012-04-11 18:06:28 -07002548 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2549 View dragView, int[] direction, boolean commit) {
2550 int[] pixelXY = new int[2];
2551 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2552
2553 // First we determine if things have moved enough to cause a different layout
2554 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2555 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2556
2557 setUseTempCoords(true);
2558 if (swapSolution != null && swapSolution.isSolution) {
2559 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2560 // committing anything or animating anything as we just want to determine if a solution
2561 // exists
2562 copySolutionToTempState(swapSolution, dragView);
2563 setItemPlacementDirty(true);
2564 animateItemsToSolution(swapSolution, dragView, commit);
2565
2566 if (commit) {
2567 commitTempPlacement();
2568 completeAndClearReorderHintAnimations();
2569 setItemPlacementDirty(false);
2570 } else {
2571 beginOrAdjustHintAnimations(swapSolution, dragView,
2572 REORDER_ANIMATION_DURATION);
2573 }
2574 mShortcutsAndWidgets.requestLayout();
2575 }
2576 return swapSolution.isSolution;
2577 }
2578
Adam Cohen482ed822012-03-02 14:15:13 -08002579 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2580 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002581 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002582 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002583
2584 if (resultSpan == null) {
2585 resultSpan = new int[2];
2586 }
2587
Adam Cohen19f37922012-03-21 11:59:11 -07002588 // When we are checking drop validity or actually dropping, we don't recompute the
2589 // direction vector, since we want the solution to match the preview, and it's possible
2590 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002591 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2592 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002593 mDirectionVector[0] = mPreviousReorderDirection[0];
2594 mDirectionVector[1] = mPreviousReorderDirection[1];
2595 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002596 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2597 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2598 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002599 }
2600 } else {
2601 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2602 mPreviousReorderDirection[0] = mDirectionVector[0];
2603 mPreviousReorderDirection[1] = mDirectionVector[1];
2604 }
2605
Adam Cohen482ed822012-03-02 14:15:13 -08002606 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2607 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2608
2609 // We attempt the approach which doesn't shuffle views at all
2610 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2611 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2612
2613 ItemConfiguration finalSolution = null;
2614 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2615 finalSolution = swapSolution;
2616 } else if (noShuffleSolution.isSolution) {
2617 finalSolution = noShuffleSolution;
2618 }
2619
2620 boolean foundSolution = true;
2621 if (!DESTRUCTIVE_REORDER) {
2622 setUseTempCoords(true);
2623 }
2624
2625 if (finalSolution != null) {
2626 result[0] = finalSolution.dragViewX;
2627 result[1] = finalSolution.dragViewY;
2628 resultSpan[0] = finalSolution.dragViewSpanX;
2629 resultSpan[1] = finalSolution.dragViewSpanY;
2630
2631 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2632 // committing anything or animating anything as we just want to determine if a solution
2633 // exists
2634 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2635 if (!DESTRUCTIVE_REORDER) {
2636 copySolutionToTempState(finalSolution, dragView);
2637 }
2638 setItemPlacementDirty(true);
2639 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2640
Adam Cohen19f37922012-03-21 11:59:11 -07002641 if (!DESTRUCTIVE_REORDER &&
2642 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002643 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002644 completeAndClearReorderHintAnimations();
2645 setItemPlacementDirty(false);
2646 } else {
2647 beginOrAdjustHintAnimations(finalSolution, dragView,
2648 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002649 }
2650 }
2651 } else {
2652 foundSolution = false;
2653 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2654 }
2655
2656 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2657 setUseTempCoords(false);
2658 }
Adam Cohen482ed822012-03-02 14:15:13 -08002659
Michael Jurkaa52570f2012-03-20 03:18:20 -07002660 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002661 return result;
2662 }
2663
Adam Cohen19f37922012-03-21 11:59:11 -07002664 void setItemPlacementDirty(boolean dirty) {
2665 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002666 }
Adam Cohen19f37922012-03-21 11:59:11 -07002667 boolean isItemPlacementDirty() {
2668 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002669 }
2670
2671 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002672 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002673 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2674 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohen482ed822012-03-02 14:15:13 -08002675 boolean isSolution = false;
2676 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2677
Adam Cohenf3900c22012-11-16 18:28:11 -08002678 void save() {
2679 // Copy current state into savedMap
2680 for (View v: map.keySet()) {
2681 map.get(v).copy(savedMap.get(v));
2682 }
2683 }
2684
2685 void restore() {
2686 // Restore current state from savedMap
2687 for (View v: savedMap.keySet()) {
2688 savedMap.get(v).copy(map.get(v));
2689 }
2690 }
2691
2692 void add(View v, CellAndSpan cs) {
2693 map.put(v, cs);
2694 savedMap.put(v, new CellAndSpan());
2695 sortedViews.add(v);
2696 }
2697
Adam Cohen482ed822012-03-02 14:15:13 -08002698 int area() {
2699 return dragViewSpanX * dragViewSpanY;
2700 }
Adam Cohen8baab352012-03-20 17:39:21 -07002701 }
2702
2703 private class CellAndSpan {
2704 int x, y;
2705 int spanX, spanY;
2706
Adam Cohenf3900c22012-11-16 18:28:11 -08002707 public CellAndSpan() {
2708 }
2709
2710 public void copy(CellAndSpan copy) {
2711 copy.x = x;
2712 copy.y = y;
2713 copy.spanX = spanX;
2714 copy.spanY = spanY;
2715 }
2716
Adam Cohen8baab352012-03-20 17:39:21 -07002717 public CellAndSpan(int x, int y, int spanX, int spanY) {
2718 this.x = x;
2719 this.y = y;
2720 this.spanX = spanX;
2721 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002722 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002723
2724 public String toString() {
2725 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2726 }
2727
Adam Cohen482ed822012-03-02 14:15:13 -08002728 }
2729
Adam Cohendf035382011-04-11 17:22:04 -07002730 /**
2731 * Find a vacant area that will fit the given bounds nearest the requested
2732 * cell location. Uses Euclidean distance to score multiple vacant areas.
2733 *
2734 * @param pixelX The X location at which you want to search for a vacant area.
2735 * @param pixelY The Y location at which you want to search for a vacant area.
2736 * @param spanX Horizontal span of the object.
2737 * @param spanY Vertical span of the object.
2738 * @param ignoreView Considers space occupied by this view as unoccupied
2739 * @param result Previously returned value to possibly recycle.
2740 * @return The X, Y cell of a vacant area that can contain this object,
2741 * nearest the requested location.
2742 */
2743 int[] findNearestVacantArea(
2744 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2745 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2746 }
2747
2748 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002749 * Find a vacant area that will fit the given bounds nearest the requested
2750 * cell location. Uses Euclidean distance to score multiple vacant areas.
2751 *
2752 * @param pixelX The X location at which you want to search for a vacant area.
2753 * @param pixelY The Y location at which you want to search for a vacant area.
2754 * @param minSpanX The minimum horizontal span required
2755 * @param minSpanY The minimum vertical span required
2756 * @param spanX Horizontal span of the object.
2757 * @param spanY Vertical span of the object.
2758 * @param ignoreView Considers space occupied by this view as unoccupied
2759 * @param result Previously returned value to possibly recycle.
2760 * @return The X, Y cell of a vacant area that can contain this object,
2761 * nearest the requested location.
2762 */
2763 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2764 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002765 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2766 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002767 }
2768
2769 /**
Adam Cohendf035382011-04-11 17:22:04 -07002770 * Find a starting cell position that will fit the given bounds nearest the requested
2771 * cell location. Uses Euclidean distance to score multiple vacant areas.
2772 *
2773 * @param pixelX The X location at which you want to search for a vacant area.
2774 * @param pixelY The Y location at which you want to search for a vacant area.
2775 * @param spanX Horizontal span of the object.
2776 * @param spanY Vertical span of the object.
2777 * @param ignoreView Considers space occupied by this view as unoccupied
2778 * @param result Previously returned value to possibly recycle.
2779 * @return The X, Y cell of a vacant area that can contain this object,
2780 * nearest the requested location.
2781 */
2782 int[] findNearestArea(
2783 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2784 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2785 }
2786
Michael Jurka0280c3b2010-09-17 15:00:07 -07002787 boolean existsEmptyCell() {
2788 return findCellForSpan(null, 1, 1);
2789 }
2790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002791 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002792 * Finds the upper-left coordinate of the first rectangle in the grid that can
2793 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2794 * then this method will only return coordinates for rectangles that contain the cell
2795 * (intersectX, intersectY)
2796 *
2797 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2798 * can be found.
2799 * @param spanX The horizontal span of the cell we want to find.
2800 * @param spanY The vertical span of the cell we want to find.
2801 *
2802 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002803 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002804 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002805 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002806 }
2807
2808 /**
2809 * Like above, but ignores any cells occupied by the item "ignoreView"
2810 *
2811 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2812 * can be found.
2813 * @param spanX The horizontal span of the cell we want to find.
2814 * @param spanY The vertical span of the cell we want to find.
2815 * @param ignoreView The home screen item we should treat as not occupying any space
2816 * @return
2817 */
2818 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002819 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2820 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002821 }
2822
2823 /**
2824 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2825 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2826 *
2827 * @param spanX The horizontal span of the cell we want to find.
2828 * @param spanY The vertical span of the cell we want to find.
2829 * @param ignoreView The home screen item we should treat as not occupying any space
2830 * @param intersectX The X coordinate of the cell that we should try to overlap
2831 * @param intersectX The Y coordinate of the cell that we should try to overlap
2832 *
2833 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2834 */
2835 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2836 int intersectX, int intersectY) {
2837 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002838 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002839 }
2840
2841 /**
2842 * The superset of the above two methods
2843 */
2844 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002845 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002846 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002847 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002848
Michael Jurka28750fb2010-09-24 17:43:49 -07002849 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002850 while (true) {
2851 int startX = 0;
2852 if (intersectX >= 0) {
2853 startX = Math.max(startX, intersectX - (spanX - 1));
2854 }
2855 int endX = mCountX - (spanX - 1);
2856 if (intersectX >= 0) {
2857 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2858 }
2859 int startY = 0;
2860 if (intersectY >= 0) {
2861 startY = Math.max(startY, intersectY - (spanY - 1));
2862 }
2863 int endY = mCountY - (spanY - 1);
2864 if (intersectY >= 0) {
2865 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2866 }
2867
Winson Chungbbc60d82010-11-11 16:34:41 -08002868 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002869 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002870 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002871 for (int i = 0; i < spanX; i++) {
2872 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002873 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002874 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002875 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002876 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002877 continue inner;
2878 }
2879 }
2880 }
2881 if (cellXY != null) {
2882 cellXY[0] = x;
2883 cellXY[1] = y;
2884 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002885 foundCell = true;
2886 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002887 }
2888 }
2889 if (intersectX == -1 && intersectY == -1) {
2890 break;
2891 } else {
2892 // if we failed to find anything, try again but without any requirements of
2893 // intersecting
2894 intersectX = -1;
2895 intersectY = -1;
2896 continue;
2897 }
2898 }
2899
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002900 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002901 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002902 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002903 }
2904
2905 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002906 * A drag event has begun over this layout.
2907 * It may have begun over this layout (in which case onDragChild is called first),
2908 * or it may have begun on another layout.
2909 */
2910 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002911 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002912 mDragging = true;
2913 }
2914
2915 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002916 * Called when drag has left this CellLayout or has been completed (successfully or not)
2917 */
2918 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002919 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002920 // This can actually be called when we aren't in a drag, e.g. when adding a new
2921 // item to this layout via the customize drawer.
2922 // Guard against that case.
2923 if (mDragging) {
2924 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002925 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002926
2927 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002928 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002929 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2930 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002931 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002932 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002933 }
2934
2935 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002936 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002937 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002938 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939 *
2940 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002941 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002942 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002943 if (child != null) {
2944 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002945 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002946 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002947 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948 }
2949
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002951 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002952 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002953 * @param cellX X coordinate of upper left corner expressed as a cell position
2954 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002955 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002957 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002958 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002959 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002960 final int cellWidth = mCellWidth;
2961 final int cellHeight = mCellHeight;
2962 final int widthGap = mWidthGap;
2963 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002964
Winson Chung4b825dcd2011-06-19 12:41:22 -07002965 final int hStartPadding = getPaddingLeft();
2966 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002967
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2969 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2970
2971 int x = hStartPadding + cellX * (cellWidth + widthGap);
2972 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002973
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002974 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002978 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002980 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002981 * @param width Width in pixels
2982 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002983 * @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 -08002984 */
Winson Chung66700732013-08-20 16:56:15 -07002985 public static int[] rectToCell(int width, int height, int[] result) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002986 LauncherAppState app = LauncherAppState.getInstance();
2987 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -07002988 Rect padding = grid.getWorkspacePadding(grid.isLandscape ?
2989 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
Winson Chung5f8afe62013-08-12 16:19:28 -07002990
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002991 // Always assume we're working with the smallest span to make sure we
2992 // reserve enough space in both orientations.
Winson Chung66700732013-08-20 16:56:15 -07002993 int parentWidth = grid.calculateCellWidth(grid.widthPx
2994 - padding.left - padding.right, (int) grid.numColumns);
2995 int parentHeight = grid.calculateCellHeight(grid.heightPx
2996 - padding.top - padding.bottom, (int) grid.numRows);
2997 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002999 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07003000 int spanX = (int) Math.ceil(width / (float) smallerSize);
3001 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04003002
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07003003 if (result == null) {
3004 return new int[] { spanX, spanY };
3005 }
3006 result[0] = spanX;
3007 result[1] = spanY;
3008 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 }
3010
Michael Jurkaf12c75c2011-01-25 22:41:40 -08003011 public int[] cellSpansToSize(int hSpans, int vSpans) {
3012 int[] size = new int[2];
3013 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
3014 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
3015 return size;
3016 }
3017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003018 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08003019 * Calculate the grid spans needed to fit given item
3020 */
3021 public void calculateSpans(ItemInfo info) {
3022 final int minWidth;
3023 final int minHeight;
3024
3025 if (info instanceof LauncherAppWidgetInfo) {
3026 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3027 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3028 } else if (info instanceof PendingAddWidgetInfo) {
3029 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3030 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3031 } else {
3032 // It's not a widget, so it must be 1x1
3033 info.spanX = info.spanY = 1;
3034 return;
3035 }
3036 int[] spans = rectToCell(minWidth, minHeight, null);
3037 info.spanX = spans[0];
3038 info.spanY = spans[1];
3039 }
3040
3041 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042 * Find the first vacant cell, if there is one.
3043 *
3044 * @param vacant Holds the x and y coordinate of the vacant cell
3045 * @param spanX Horizontal cell span.
3046 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003047 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003048 * @return True if a vacant cell was found
3049 */
3050 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003051
Michael Jurka0280c3b2010-09-17 15:00:07 -07003052 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003053 }
3054
3055 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3056 int xCount, int yCount, boolean[][] occupied) {
3057
Adam Cohen2801caf2011-05-13 20:57:39 -07003058 for (int y = 0; y < yCount; y++) {
3059 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003060 boolean available = !occupied[x][y];
3061out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3062 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3063 available = available && !occupied[i][j];
3064 if (!available) break out;
3065 }
3066 }
3067
3068 if (available) {
3069 vacant[0] = x;
3070 vacant[1] = y;
3071 return true;
3072 }
3073 }
3074 }
3075
3076 return false;
3077 }
3078
Michael Jurka0280c3b2010-09-17 15:00:07 -07003079 private void clearOccupiedCells() {
3080 for (int x = 0; x < mCountX; x++) {
3081 for (int y = 0; y < mCountY; y++) {
3082 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003083 }
3084 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003085 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003086
Adam Cohend41fbf52012-02-16 23:53:59 -08003087 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003088 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003089 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003090 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091
Adam Cohend4844c32011-02-18 19:25:06 -08003092 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003093 markCellsAsOccupiedForView(view, mOccupied);
3094 }
3095 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003096 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003097 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003098 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003099 }
3100
Adam Cohend4844c32011-02-18 19:25:06 -08003101 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003102 markCellsAsUnoccupiedForView(view, mOccupied);
3103 }
3104 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003105 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003106 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003107 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003108 }
3109
Adam Cohen482ed822012-03-02 14:15:13 -08003110 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3111 boolean value) {
3112 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003113 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3114 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003115 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116 }
3117 }
3118 }
3119
Adam Cohen2801caf2011-05-13 20:57:39 -07003120 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003121 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003122 (Math.max((mCountX - 1), 0) * mWidthGap);
3123 }
3124
3125 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003126 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003127 (Math.max((mCountY - 1), 0) * mHeightGap);
3128 }
3129
Michael Jurka66d72172011-04-12 16:29:25 -07003130 public boolean isOccupied(int x, int y) {
3131 if (x < mCountX && y < mCountY) {
3132 return mOccupied[x][y];
3133 } else {
3134 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3135 }
3136 }
3137
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003138 @Override
3139 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3140 return new CellLayout.LayoutParams(getContext(), attrs);
3141 }
3142
3143 @Override
3144 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3145 return p instanceof CellLayout.LayoutParams;
3146 }
3147
3148 @Override
3149 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3150 return new CellLayout.LayoutParams(p);
3151 }
3152
Winson Chungaafa03c2010-06-11 17:34:16 -07003153 public static class CellLayoutAnimationController extends LayoutAnimationController {
3154 public CellLayoutAnimationController(Animation animation, float delay) {
3155 super(animation, delay);
3156 }
3157
3158 @Override
3159 protected long getDelayForView(View view) {
3160 return (int) (Math.random() * 150);
3161 }
3162 }
3163
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003164 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3165 /**
3166 * Horizontal location of the item in the grid.
3167 */
3168 @ViewDebug.ExportedProperty
3169 public int cellX;
3170
3171 /**
3172 * Vertical location of the item in the grid.
3173 */
3174 @ViewDebug.ExportedProperty
3175 public int cellY;
3176
3177 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003178 * Temporary horizontal location of the item in the grid during reorder
3179 */
3180 public int tmpCellX;
3181
3182 /**
3183 * Temporary vertical location of the item in the grid during reorder
3184 */
3185 public int tmpCellY;
3186
3187 /**
3188 * Indicates that the temporary coordinates should be used to layout the items
3189 */
3190 public boolean useTmpCoords;
3191
3192 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003193 * Number of cells spanned horizontally by the item.
3194 */
3195 @ViewDebug.ExportedProperty
3196 public int cellHSpan;
3197
3198 /**
3199 * Number of cells spanned vertically by the item.
3200 */
3201 @ViewDebug.ExportedProperty
3202 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003203
Adam Cohen1b607ed2011-03-03 17:26:50 -08003204 /**
3205 * Indicates whether the item will set its x, y, width and height parameters freely,
3206 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3207 */
Adam Cohend4844c32011-02-18 19:25:06 -08003208 public boolean isLockedToGrid = true;
3209
Adam Cohen482ed822012-03-02 14:15:13 -08003210 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07003211 * Indicates that this item should use the full extents of its parent.
3212 */
3213 public boolean isFullscreen = false;
3214
3215 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003216 * Indicates whether this item can be reordered. Always true except in the case of the
3217 * the AllApps button.
3218 */
3219 public boolean canReorder = true;
3220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003221 // X coordinate of the view in the layout.
3222 @ViewDebug.ExportedProperty
3223 int x;
3224 // Y coordinate of the view in the layout.
3225 @ViewDebug.ExportedProperty
3226 int y;
3227
Romain Guy84f296c2009-11-04 15:00:44 -08003228 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230 public LayoutParams(Context c, AttributeSet attrs) {
3231 super(c, attrs);
3232 cellHSpan = 1;
3233 cellVSpan = 1;
3234 }
3235
3236 public LayoutParams(ViewGroup.LayoutParams source) {
3237 super(source);
3238 cellHSpan = 1;
3239 cellVSpan = 1;
3240 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003241
3242 public LayoutParams(LayoutParams source) {
3243 super(source);
3244 this.cellX = source.cellX;
3245 this.cellY = source.cellY;
3246 this.cellHSpan = source.cellHSpan;
3247 this.cellVSpan = source.cellVSpan;
3248 }
3249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003250 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003251 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003252 this.cellX = cellX;
3253 this.cellY = cellY;
3254 this.cellHSpan = cellHSpan;
3255 this.cellVSpan = cellVSpan;
3256 }
3257
Adam Cohen2374abf2013-04-16 14:56:57 -07003258 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3259 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003260 if (isLockedToGrid) {
3261 final int myCellHSpan = cellHSpan;
3262 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003263 int myCellX = useTmpCoords ? tmpCellX : cellX;
3264 int myCellY = useTmpCoords ? tmpCellY : cellY;
3265
3266 if (invertHorizontally) {
3267 myCellX = colCount - myCellX - cellHSpan;
3268 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003269
Adam Cohend4844c32011-02-18 19:25:06 -08003270 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3271 leftMargin - rightMargin;
3272 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3273 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003274 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3275 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003276 }
3277 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003278
Winson Chungaafa03c2010-06-11 17:34:16 -07003279 public String toString() {
3280 return "(" + this.cellX + ", " + this.cellY + ")";
3281 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003282
3283 public void setWidth(int width) {
3284 this.width = width;
3285 }
3286
3287 public int getWidth() {
3288 return width;
3289 }
3290
3291 public void setHeight(int height) {
3292 this.height = height;
3293 }
3294
3295 public int getHeight() {
3296 return height;
3297 }
3298
3299 public void setX(int x) {
3300 this.x = x;
3301 }
3302
3303 public int getX() {
3304 return x;
3305 }
3306
3307 public void setY(int y) {
3308 this.y = y;
3309 }
3310
3311 public int getY() {
3312 return y;
3313 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003314 }
3315
Michael Jurka0280c3b2010-09-17 15:00:07 -07003316 // This class stores info for two purposes:
3317 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3318 // its spanX, spanY, and the screen it is on
3319 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3320 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3321 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003322 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003323 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003324 int cellX = -1;
3325 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003326 int spanX;
3327 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003328 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003329 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003330
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003331 @Override
3332 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003333 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3334 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003335 }
3336 }
Michael Jurkad771c962011-08-09 15:00:48 -07003337
3338 public boolean lastDownOnOccupiedCell() {
3339 return mLastDownOnOccupiedCell;
3340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003341}