blob: 3c418046466240cf2b09a930ab97c48395656416 [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 Chungaafa03c2010-06-11 17:34:16 -070067
Adam Cohend22015c2010-07-26 22:02:18 -070068 private int mCountX;
69 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070
Adam Cohen234c4cd2011-07-17 21:03:04 -070071 private int mOriginalWidthGap;
72 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073 private int mWidthGap;
74 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070075 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080076 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
78 private final Rect mRect = new Rect();
79 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070080
Patrick Dubroyde7658b2010-09-27 11:15:43 -070081 // These are temporary variables to prevent having to allocate a new object just to
82 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070083 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070084 private final int[] mTmpPoint = new int[2];
Adam Cohen69ce2e52011-07-03 19:25:21 -070085 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070086
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080088 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070089 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Michael Jurkadee05892010-07-27 10:01:56 -070091 private OnTouchListener mInterceptTouchListener;
92
Adam Cohen69ce2e52011-07-03 19:25:21 -070093 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070094 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070095
Adam Cohenb5ba0972011-09-07 18:02:31 -070096 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070097 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070098 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070099
Michael Jurka33945b22010-12-21 18:19:38 -0800100 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -0800101 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700102 private Drawable mOverScrollForegroundDrawable;
103 private Drawable mOverScrollLeft;
104 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700105 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700106 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700107 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700108
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700109 // These values allow a fixed measurement to be set on the CellLayout.
110 private int mFixedWidth = -1;
111 private int mFixedHeight = -1;
112
Michael Jurka33945b22010-12-21 18:19:38 -0800113 // If we're actively dragging something over this screen, mIsDragOverlapping is true
114 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700115 private final Point mDragCenter = new Point();
Adam Cohendedbd962013-07-11 14:21:49 -0700116 boolean mUseActiveGlowBackground = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700117
Winson Chung150fbab2010-09-29 17:14:26 -0700118 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700119 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800120 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700121 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700122 private InterruptibleInOutAnimator[] mDragOutlineAnims =
123 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700124
125 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700126 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700127 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700128
Patrick Dubroy96864c32011-03-10 17:17:23 -0800129 private BubbleTextView mPressedOrFocusedIcon;
130
Adam Cohen482ed822012-03-02 14:15:13 -0800131 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
132 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700133 private HashMap<View, ReorderHintAnimation>
134 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
135
136 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700137
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700138 // When a drag operation is in progress, holds the nearest cell to the touch point
139 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Joe Onorato4be866d2010-10-10 11:26:02 -0700141 private boolean mDragging = false;
142
Patrick Dubroyce34a972010-10-19 10:34:32 -0700143 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700144 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700145
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800146 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700147 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800148
Adam Cohen482ed822012-03-02 14:15:13 -0800149 public static final int MODE_DRAG_OVER = 0;
150 public static final int MODE_ON_DROP = 1;
151 public static final int MODE_ON_DROP_EXTERNAL = 2;
152 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700153 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800154 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
155
Adam Cohena897f392012-04-27 18:12:05 -0700156 static final int LANDSCAPE = 0;
157 static final int PORTRAIT = 1;
158
Adam Cohen7bdfc972012-05-22 16:50:35 -0700159 private static final float REORDER_HINT_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700160 private static final int REORDER_ANIMATION_DURATION = 150;
161 private float mReorderHintAnimationMagnitude;
162
Adam Cohen482ed822012-03-02 14:15:13 -0800163 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
164 private Rect mOccupiedRect = new Rect();
165 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700166 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700167 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700168 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800169
Romain Guy8a0bff52012-05-06 13:14:33 -0700170 private final static PorterDuffXfermode sAddBlendMode =
171 new PorterDuffXfermode(PorterDuff.Mode.ADD);
Michael Jurkaca993832012-06-29 15:17:04 -0700172 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 public CellLayout(Context context) {
175 this(context, null);
176 }
177
178 public CellLayout(Context context, AttributeSet attrs) {
179 this(context, attrs, 0);
180 }
181
182 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
183 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700184 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700185
186 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
187 // the user where a dragged item will land when dropped.
188 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800189 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700190 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700191
Winson Chung892c74d2013-08-22 16:15:50 -0700192 LauncherAppState app = LauncherAppState.getInstance();
193 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
195
Winson Chung5f8afe62013-08-12 16:19:28 -0700196 mCellWidth = -1;
197 mCellHeight = -1;
198 mWidthGap = mOriginalWidthGap = 0;
199 mHeightGap = mOriginalHeightGap = 0;
200 mMaxGap = Integer.MAX_VALUE;
Winson Chung892c74d2013-08-22 16:15:50 -0700201 mCountX = (int) grid.numColumns;
202 mCountY = (int) grid.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700203 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800204 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700205 mPreviousReorderDirection[0] = INVALID_DIRECTION;
206 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
208 a.recycle();
209
210 setAlwaysDrawnWithCacheEnabled(false);
211
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700212 final Resources res = getResources();
Winson Chung5f8afe62013-08-12 16:19:28 -0700213 mHotseatScale = (float) grid.hotseatIconSize / grid.iconSize;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700214
Winson Chung967289b2011-06-30 18:09:30 -0700215 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700216 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800217
Adam Cohenb5ba0972011-09-07 18:02:31 -0700218 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
219 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
220 mForegroundPadding =
221 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800222
Adam Cohen19f37922012-03-21 11:59:11 -0700223 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700224 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700225
Winson Chungb26f3d62011-06-02 10:49:29 -0700226 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700227 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700228
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700229 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700230 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700231 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700232 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800233 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700234 }
235
236 // When dragging things around the home screens, we show a green outline of
237 // where the item will land. The outlines gradually fade out, leaving a trail
238 // behind the drag path.
239 // Set up all the animations that are used to implement this fading.
240 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700241 final float fromAlphaValue = 0;
242 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700243
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700244 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700245
246 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700247 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100248 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700249 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700250 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700251 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700252 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700253 final Bitmap outline = (Bitmap)anim.getTag();
254
255 // If an animation is started and then stopped very quickly, we can still
256 // get spurious updates we've cleared the tag. Guard against this.
257 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700258 @SuppressWarnings("all") // suppress dead code warning
259 final boolean debug = false;
260 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700261 Object val = animation.getAnimatedValue();
262 Log.d(TAG, "anim " + thisIndex + " update: " + val +
263 ", isStopped " + anim.isStopped());
264 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700265 // Try to prevent it from continuing to run
266 animation.cancel();
267 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700268 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800269 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700270 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700271 }
272 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 // The animation holds a reference to the drag outline bitmap as long is it's
274 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700275 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700276 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700277 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700278 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700279 anim.setTag(null);
280 }
281 }
282 });
283 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700284 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700285
Michael Jurka18014792010-10-14 09:01:34 -0700286 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700287 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800288
Michael Jurkaa52570f2012-03-20 03:18:20 -0700289 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700290 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700291 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700292
Michael Jurkaa52570f2012-03-20 03:18:20 -0700293 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700294 }
295
Adam Cohen2801caf2011-05-13 20:57:39 -0700296 public void enableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700297 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_HARDWARE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700298 }
299
300 public void disableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700301 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700302 }
303
304 public void buildHardwareLayer() {
305 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700306 }
307
Adam Cohen307fe232012-08-16 17:55:58 -0700308 public float getChildrenScale() {
309 return mIsHotseat ? mHotseatScale : 1.0f;
310 }
311
Winson Chung5f8afe62013-08-12 16:19:28 -0700312 public void setCellDimensions(int width, int height) {
313 mCellWidth = width;
314 mCellHeight = height;
315 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
316 mCountX, mCountY);
317 }
318
Adam Cohen2801caf2011-05-13 20:57:39 -0700319 public void setGridSize(int x, int y) {
320 mCountX = x;
321 mCountY = y;
322 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800323 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700324 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700325 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700326 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700327 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700328 }
329
Adam Cohen2374abf2013-04-16 14:56:57 -0700330 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
331 public void setInvertIfRtl(boolean invert) {
332 mShortcutsAndWidgets.setInvertIfRtl(invert);
333 }
334
Patrick Dubroy96864c32011-03-10 17:17:23 -0800335 private void invalidateBubbleTextView(BubbleTextView icon) {
336 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700337 invalidate(icon.getLeft() + getPaddingLeft() - padding,
338 icon.getTop() + getPaddingTop() - padding,
339 icon.getRight() + getPaddingLeft() + padding,
340 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800341 }
342
Adam Cohenb5ba0972011-09-07 18:02:31 -0700343 void setOverScrollAmount(float r, boolean left) {
344 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
345 mOverScrollForegroundDrawable = mOverScrollLeft;
346 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
347 mOverScrollForegroundDrawable = mOverScrollRight;
348 }
349
350 mForegroundAlpha = (int) Math.round((r * 255));
351 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
352 invalidate();
353 }
354
Patrick Dubroy96864c32011-03-10 17:17:23 -0800355 void setPressedOrFocusedIcon(BubbleTextView icon) {
356 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
357 // requires an expanded clip rect (due to the glow's blur radius)
358 BubbleTextView oldIcon = mPressedOrFocusedIcon;
359 mPressedOrFocusedIcon = icon;
360 if (oldIcon != null) {
361 invalidateBubbleTextView(oldIcon);
362 }
363 if (mPressedOrFocusedIcon != null) {
364 invalidateBubbleTextView(mPressedOrFocusedIcon);
365 }
366 }
367
Michael Jurka33945b22010-12-21 18:19:38 -0800368 void setIsDragOverlapping(boolean isDragOverlapping) {
369 if (mIsDragOverlapping != isDragOverlapping) {
370 mIsDragOverlapping = isDragOverlapping;
Adam Cohendedbd962013-07-11 14:21:49 -0700371 setUseActiveGlowBackground(mIsDragOverlapping);
Michael Jurka33945b22010-12-21 18:19:38 -0800372 invalidate();
373 }
374 }
375
Adam Cohendedbd962013-07-11 14:21:49 -0700376 void setUseActiveGlowBackground(boolean use) {
377 mUseActiveGlowBackground = use;
378 }
379
Michael Jurka33945b22010-12-21 18:19:38 -0800380 boolean getIsDragOverlapping() {
381 return mIsDragOverlapping;
382 }
383
Adam Cohenebea84d2011-11-09 17:20:41 -0800384 protected void setOverscrollTransformsDirty(boolean dirty) {
385 mScrollingTransformsDirty = dirty;
386 }
387
388 protected void resetOverscrollTransforms() {
389 if (mScrollingTransformsDirty) {
390 setOverscrollTransformsDirty(false);
391 setTranslationX(0);
392 setRotationY(0);
393 // It doesn't matter if we pass true or false here, the important thing is that we
394 // pass 0, which results in the overscroll drawable not being drawn any more.
395 setOverScrollAmount(0, false);
396 setPivotX(getMeasuredWidth() / 2);
397 setPivotY(getMeasuredHeight() / 2);
398 }
399 }
400
Adam Cohen307fe232012-08-16 17:55:58 -0700401 public void scaleRect(Rect r, float scale) {
402 if (scale != 1.0f) {
403 r.left = (int) (r.left * scale + 0.5f);
404 r.top = (int) (r.top * scale + 0.5f);
405 r.right = (int) (r.right * scale + 0.5f);
406 r.bottom = (int) (r.bottom * scale + 0.5f);
407 }
408 }
409
410 Rect temp = new Rect();
411 void scaleRectAboutCenter(Rect in, Rect out, float scale) {
412 int cx = in.centerX();
413 int cy = in.centerY();
414 out.set(in);
415 out.offset(-cx, -cy);
416 scaleRect(out, scale);
417 out.offset(cx, cy);
418 }
419
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700420 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700421 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700422 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
423 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
424 // When we're small, we are either drawn normally or in the "accepts drops" state (during
425 // a drag). However, we also drag the mini hover background *over* one of those two
426 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700427 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700428 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800429
Adam Cohendedbd962013-07-11 14:21:49 -0700430 if (mUseActiveGlowBackground) {
Michael Jurka33945b22010-12-21 18:19:38 -0800431 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700432 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700433 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700434 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700435 }
Michael Jurka33945b22010-12-21 18:19:38 -0800436
437 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
438 bg.setBounds(mBackgroundRect);
439 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700440 }
Romain Guya6abce82009-11-10 02:54:41 -0800441
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700442 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700443 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700444 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700445 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800446 final Rect r = mDragOutlines[i];
Adam Cohen307fe232012-08-16 17:55:58 -0700447 scaleRectAboutCenter(r, temp, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700448 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700449 paint.setAlpha((int)(alpha + .5f));
Adam Cohen307fe232012-08-16 17:55:58 -0700450 canvas.drawBitmap(b, null, temp, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700451 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700452 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800453
454 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
455 // requires an expanded clip rect (due to the glow's blur radius)
456 if (mPressedOrFocusedIcon != null) {
457 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
458 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
459 if (b != null) {
460 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700461 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
462 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800463 null);
464 }
465 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700466
Adam Cohen482ed822012-03-02 14:15:13 -0800467 if (DEBUG_VISUALIZE_OCCUPIED) {
468 int[] pt = new int[2];
469 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700470 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800471 for (int i = 0; i < mCountX; i++) {
472 for (int j = 0; j < mCountY; j++) {
473 if (mOccupied[i][j]) {
474 cellToPoint(i, j, pt);
475 canvas.save();
476 canvas.translate(pt[0], pt[1]);
477 cd.draw(canvas);
478 canvas.restore();
479 }
480 }
481 }
482 }
483
Andrew Flynn850d2e72012-04-26 16:51:20 -0700484 int previewOffset = FolderRingAnimator.sPreviewSize;
485
Adam Cohen69ce2e52011-07-03 19:25:21 -0700486 // The folder outer / inner ring image(s)
Winson Chung5f8afe62013-08-12 16:19:28 -0700487 LauncherAppState app = LauncherAppState.getInstance();
488 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700489 for (int i = 0; i < mFolderOuterRings.size(); i++) {
490 FolderRingAnimator fra = mFolderOuterRings.get(i);
491
492 // Draw outer ring
493 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
Winson Chung5f8afe62013-08-12 16:19:28 -0700494 int width = (int) (fra.getOuterRingSize() * getChildrenScale());
Adam Cohen69ce2e52011-07-03 19:25:21 -0700495 int height = width;
496 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700497 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen69ce2e52011-07-03 19:25:21 -0700498
499 int centerX = mTempLocation[0] + mCellWidth / 2;
Winson Chung5f8afe62013-08-12 16:19:28 -0700500 int centerY = mTempLocation[1] + previewOffset / 2 +
501 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700502
503 canvas.save();
504 canvas.translate(centerX - width / 2, centerY - height / 2);
505 d.setBounds(0, 0, width, height);
506 d.draw(canvas);
507 canvas.restore();
508
509 // Draw inner ring
510 d = FolderRingAnimator.sSharedInnerRingDrawable;
Winson Chung5f8afe62013-08-12 16:19:28 -0700511 width = (int) (fra.getInnerRingSize() * getChildrenScale());
Adam Cohen69ce2e52011-07-03 19:25:21 -0700512 height = width;
513 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700514 child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen69ce2e52011-07-03 19:25:21 -0700515
516 centerX = mTempLocation[0] + mCellWidth / 2;
Winson Chung5f8afe62013-08-12 16:19:28 -0700517 centerY = mTempLocation[1] + previewOffset / 2 +
518 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700519 canvas.save();
520 canvas.translate(centerX - width / 2, centerY - width / 2);
521 d.setBounds(0, 0, width, height);
522 d.draw(canvas);
523 canvas.restore();
524 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700525
526 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
527 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
528 int width = d.getIntrinsicWidth();
529 int height = d.getIntrinsicHeight();
530
531 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700532 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Adam Cohenc51934b2011-07-26 21:07:43 -0700533 int centerX = mTempLocation[0] + mCellWidth / 2;
Winson Chung5f8afe62013-08-12 16:19:28 -0700534 int centerY = mTempLocation[1] + previewOffset / 2 +
535 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700536
537 canvas.save();
538 canvas.translate(centerX - width / 2, centerY - width / 2);
539 d.setBounds(0, 0, width, height);
540 d.draw(canvas);
541 canvas.restore();
542 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700543 }
544
Adam Cohenb5ba0972011-09-07 18:02:31 -0700545 @Override
546 protected void dispatchDraw(Canvas canvas) {
547 super.dispatchDraw(canvas);
548 if (mForegroundAlpha > 0) {
549 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
550 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700551 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700552 mOverScrollForegroundDrawable.draw(canvas);
553 p.setXfermode(null);
554 }
555 }
556
Adam Cohen69ce2e52011-07-03 19:25:21 -0700557 public void showFolderAccept(FolderRingAnimator fra) {
558 mFolderOuterRings.add(fra);
559 }
560
561 public void hideFolderAccept(FolderRingAnimator fra) {
562 if (mFolderOuterRings.contains(fra)) {
563 mFolderOuterRings.remove(fra);
564 }
565 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700566 }
567
Adam Cohenc51934b2011-07-26 21:07:43 -0700568 public void setFolderLeaveBehindCell(int x, int y) {
569 mFolderLeaveBehindCell[0] = x;
570 mFolderLeaveBehindCell[1] = y;
571 invalidate();
572 }
573
574 public void clearFolderLeaveBehind() {
575 mFolderLeaveBehindCell[0] = -1;
576 mFolderLeaveBehindCell[1] = -1;
577 invalidate();
578 }
579
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700580 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700581 public boolean shouldDelayChildPressedState() {
582 return false;
583 }
584
Adam Cohen1462de32012-07-24 22:34:36 -0700585 public void restoreInstanceState(SparseArray<Parcelable> states) {
586 dispatchRestoreInstanceState(states);
587 }
588
Michael Jurkae6235dd2011-10-04 15:02:05 -0700589 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700590 public void cancelLongPress() {
591 super.cancelLongPress();
592
593 // Cancel long press for all children
594 final int count = getChildCount();
595 for (int i = 0; i < count; i++) {
596 final View child = getChildAt(i);
597 child.cancelLongPress();
598 }
599 }
600
Michael Jurkadee05892010-07-27 10:01:56 -0700601 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
602 mInterceptTouchListener = listener;
603 }
604
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700606 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 }
608
609 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700610 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
612
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800613 public void setIsHotseat(boolean isHotseat) {
614 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700615 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800616 }
617
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800618 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700619 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700620 final LayoutParams lp = params;
621
Andrew Flynnde38e422012-05-08 11:22:15 -0700622 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800623 if (child instanceof BubbleTextView) {
624 BubbleTextView bubbleChild = (BubbleTextView) child;
625
Andrew Flynnde38e422012-05-08 11:22:15 -0700626 Resources res = getResources();
Winson Chung5f8afe62013-08-12 16:19:28 -0700627 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800628 }
629
Adam Cohen307fe232012-08-16 17:55:58 -0700630 child.setScaleX(getChildrenScale());
631 child.setScaleY(getChildrenScale());
632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 // Generate an id for each view, this assumes we have at most 256x256 cells
634 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700635 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700636 // If the horizontal or vertical span is set to -1, it is taken to
637 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700638 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
639 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640
Winson Chungaafa03c2010-06-11 17:34:16 -0700641 child.setId(childId);
642
Michael Jurkaa52570f2012-03-20 03:18:20 -0700643 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700644
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700645 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700646
Winson Chungaafa03c2010-06-11 17:34:16 -0700647 return true;
648 }
649 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700653 public void removeAllViews() {
654 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700655 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656 }
657
658 @Override
659 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700660 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700661 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700662 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700663 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 }
665
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700666 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700667 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700668 }
669
Michael Jurka0280c3b2010-09-17 15:00:07 -0700670 @Override
671 public void removeView(View view) {
672 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700673 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 }
675
676 @Override
677 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700678 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
679 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 }
681
682 @Override
683 public void removeViewInLayout(View view) {
684 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700685 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700686 }
687
688 @Override
689 public void removeViews(int start, int count) {
690 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700691 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700692 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700693 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700694 }
695
696 @Override
697 public void removeViewsInLayout(int start, int count) {
698 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700699 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700700 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700701 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800702 }
703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 @Override
705 protected void onAttachedToWindow() {
706 super.onAttachedToWindow();
Adam Cohendcd297f2013-06-18 13:13:40 -0700707 if (getParent() instanceof Workspace) {
708 Workspace workspace = (Workspace) getParent();
709 mCellInfo.screenId = workspace.getIdForScreen(this);
710 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 }
712
Michael Jurkaaf442092010-06-10 17:01:57 -0700713 public void setTagToCellInfoForPoint(int touchX, int touchY) {
714 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800715 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 final int x = touchX + getScrollX();
717 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700718 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700719
720 boolean found = false;
721 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700722 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800723 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700724
Adam Cohen1b607ed2011-03-03 17:26:50 -0800725 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
726 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700727 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700728
Winson Chungeecf02d2012-03-02 17:14:58 -0800729 float scale = child.getScaleX();
730 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
731 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700732 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
733 // offset that by this CellLayout's padding to test an (x,y) point that is relative
734 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800736 frame.inset((int) (frame.width() * (1f - scale) / 2),
737 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700738
Michael Jurkaaf442092010-06-10 17:01:57 -0700739 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700740 cellInfo.cell = child;
741 cellInfo.cellX = lp.cellX;
742 cellInfo.cellY = lp.cellY;
743 cellInfo.spanX = lp.cellHSpan;
744 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700745 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700746 break;
747 }
748 }
749 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700750
Michael Jurkad771c962011-08-09 15:00:48 -0700751 mLastDownOnOccupiedCell = found;
752
Michael Jurkaaf442092010-06-10 17:01:57 -0700753 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700754 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700755 pointToCellExact(x, y, cellXY);
756
Michael Jurkaaf442092010-06-10 17:01:57 -0700757 cellInfo.cell = null;
758 cellInfo.cellX = cellXY[0];
759 cellInfo.cellY = cellXY[1];
760 cellInfo.spanX = 1;
761 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700762 }
763 setTag(cellInfo);
764 }
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 @Override
767 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700768 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
769 // even in the case where we return early. Not clearing here was causing bugs whereby on
770 // long-press we'd end up picking up an item from a previous drag operation.
771 final int action = ev.getAction();
772
773 if (action == MotionEvent.ACTION_DOWN) {
774 clearTagCellInfo();
775 }
776
Michael Jurkadee05892010-07-27 10:01:56 -0700777 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
778 return true;
779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780
781 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700782 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800784
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 return false;
786 }
787
Adam Cohenc1997fd2011-08-15 18:26:39 -0700788 private void clearTagCellInfo() {
789 final CellInfo cellInfo = mCellInfo;
790 cellInfo.cell = null;
791 cellInfo.cellX = -1;
792 cellInfo.cellY = -1;
793 cellInfo.spanX = 0;
794 cellInfo.spanY = 0;
795 setTag(cellInfo);
796 }
797
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700799 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 }
801
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700802 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700803 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 * @param x X coordinate of the point
805 * @param y Y coordinate of the point
806 * @param result Array of 2 ints to hold the x and y coordinate of the cell
807 */
808 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700809 final int hStartPadding = getPaddingLeft();
810 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811
812 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
813 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
814
Adam Cohend22015c2010-07-26 22:02:18 -0700815 final int xAxis = mCountX;
816 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817
818 if (result[0] < 0) result[0] = 0;
819 if (result[0] >= xAxis) result[0] = xAxis - 1;
820 if (result[1] < 0) result[1] = 0;
821 if (result[1] >= yAxis) result[1] = yAxis - 1;
822 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700823
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 /**
825 * Given a point, return the cell that most closely encloses that point
826 * @param x X coordinate of the point
827 * @param y Y coordinate of the point
828 * @param result Array of 2 ints to hold the x and y coordinate of the cell
829 */
830 void pointToCellRounded(int x, int y, int[] result) {
831 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
832 }
833
834 /**
835 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700836 *
837 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700839 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 * @param result Array of 2 ints to hold the x and y coordinate of the point
841 */
842 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700843 final int hStartPadding = getPaddingLeft();
844 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845
846 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
847 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
848 }
849
Adam Cohene3e27a82011-04-15 12:07:39 -0700850 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800851 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700852 *
853 * @param cellX X coordinate of the cell
854 * @param cellY Y coordinate of the cell
855 *
856 * @param result Array of 2 ints to hold the x and y coordinate of the point
857 */
858 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700859 regionToCenterPoint(cellX, cellY, 1, 1, result);
860 }
861
862 /**
863 * Given a cell coordinate and span return the point that represents the center of the regio
864 *
865 * @param cellX X coordinate of the cell
866 * @param cellY Y coordinate of the cell
867 *
868 * @param result Array of 2 ints to hold the x and y coordinate of the point
869 */
870 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700871 final int hStartPadding = getPaddingLeft();
872 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700873 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
874 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
875 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
876 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700877 }
878
Adam Cohen19f37922012-03-21 11:59:11 -0700879 /**
880 * Given a cell coordinate and span fills out a corresponding pixel rect
881 *
882 * @param cellX X coordinate of the cell
883 * @param cellY Y coordinate of the cell
884 * @param result Rect in which to write the result
885 */
886 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
887 final int hStartPadding = getPaddingLeft();
888 final int vStartPadding = getPaddingTop();
889 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
890 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
891 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
892 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
893 }
894
Adam Cohen482ed822012-03-02 14:15:13 -0800895 public float getDistanceFromCell(float x, float y, int[] cell) {
896 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
897 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
898 Math.pow(y - mTmpPoint[1], 2));
899 return distance;
900 }
901
Romain Guy84f296c2009-11-04 15:00:44 -0800902 int getCellWidth() {
903 return mCellWidth;
904 }
905
906 int getCellHeight() {
907 return mCellHeight;
908 }
909
Adam Cohend4844c32011-02-18 19:25:06 -0800910 int getWidthGap() {
911 return mWidthGap;
912 }
913
914 int getHeightGap() {
915 return mHeightGap;
916 }
917
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700918 Rect getContentRect(Rect r) {
919 if (r == null) {
920 r = new Rect();
921 }
922 int left = getPaddingLeft();
923 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700924 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
925 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700926 r.set(left, top, right, bottom);
927 return r;
928 }
929
Winson Chung66700732013-08-20 16:56:15 -0700930 static void getMetrics(Rect metrics, int paddedMeasureWidth,
931 int paddedMeasureHeight, int countX, int countY) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700932 LauncherAppState app = LauncherAppState.getInstance();
933 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -0700934 metrics.set(grid.calculateCellWidth(paddedMeasureWidth, countX),
935 grid.calculateCellHeight(paddedMeasureHeight, countY), 0, 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700936 }
937
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700938 public void setFixedSize(int width, int height) {
939 mFixedWidth = width;
940 mFixedHeight = height;
941 }
942
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 @Override
944 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700945 LauncherAppState app = LauncherAppState.getInstance();
946 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
947
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700950 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
951 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
952 if (mCellWidth < 0 || mCellHeight < 0) {
953 mCellWidth = grid.calculateCellWidth(widthSize, mCountX);
954 mCellHeight = grid.calculateCellHeight(heightSize, mCountY);
955 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
956 mHeightGap, mCountX, mCountY);
957 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700958
Winson Chung5f8afe62013-08-12 16:19:28 -0700959 int newWidth = widthSize;
960 int newHeight = heightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700961 if (mFixedWidth > 0 && mFixedHeight > 0) {
962 newWidth = mFixedWidth;
963 newHeight = mFixedHeight;
964 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
966 }
967
Adam Cohend22015c2010-07-26 22:02:18 -0700968 int numWidthGaps = mCountX - 1;
969 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970
Adam Cohen234c4cd2011-07-17 21:03:04 -0700971 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700972 int hSpace = widthSize - getPaddingLeft() - getPaddingRight();
973 int vSpace = heightSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700974 int hFreeSpace = hSpace - (mCountX * mCellWidth);
975 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700976 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
977 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700978 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
979 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700980 } else {
981 mWidthGap = mOriginalWidthGap;
982 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700983 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800984 int count = getChildCount();
Winson Chung5f8afe62013-08-12 16:19:28 -0700985 int maxWidth = 0;
986 int maxHeight = 0;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800987 for (int i = 0; i < count; i++) {
988 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700989 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
990 getPaddingRight(), MeasureSpec.EXACTLY);
991 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
992 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800993 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700994 maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
995 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
Michael Jurka8c920dd2011-01-20 14:16:56 -0800996 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700997 setMeasuredDimension(maxWidth, maxHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 }
999
1000 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001001 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001004 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001005 child.layout(getPaddingLeft(), getPaddingTop(),
1006 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 }
1008 }
1009
1010 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001011 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1012 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001013 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001014 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001015 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001016 }
1017
1018 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001020 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 }
1022
1023 @Override
1024 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001025 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 }
1027
Michael Jurka5f1c5092010-09-03 14:15:02 -07001028 public float getBackgroundAlpha() {
1029 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001030 }
1031
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001032 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001033 if (mBackgroundAlphaMultiplier != multiplier) {
1034 mBackgroundAlphaMultiplier = multiplier;
1035 invalidate();
1036 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001037 }
1038
Adam Cohenddb82192010-11-10 16:32:54 -08001039 public float getBackgroundAlphaMultiplier() {
1040 return mBackgroundAlphaMultiplier;
1041 }
1042
Michael Jurka5f1c5092010-09-03 14:15:02 -07001043 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001044 if (mBackgroundAlpha != alpha) {
1045 mBackgroundAlpha = alpha;
1046 invalidate();
1047 }
Michael Jurkadee05892010-07-27 10:01:56 -07001048 }
1049
Michael Jurkaa52570f2012-03-20 03:18:20 -07001050 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001051 final int childCount = getChildCount();
1052 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001053 getChildAt(i).setAlpha(alpha);
1054 }
1055 }
1056
Michael Jurkaa52570f2012-03-20 03:18:20 -07001057 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1058 if (getChildCount() > 0) {
1059 return (ShortcutAndWidgetContainer) getChildAt(0);
1060 }
1061 return null;
1062 }
1063
Patrick Dubroy440c3602010-07-13 17:50:32 -07001064 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001065 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001066 }
1067
Adam Cohen76fc0852011-06-17 13:26:23 -07001068 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001069 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001070 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001071 boolean[][] occupied = mOccupied;
1072 if (!permanent) {
1073 occupied = mTmpOccupied;
1074 }
1075
Adam Cohen19f37922012-03-21 11:59:11 -07001076 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001077 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1078 final ItemInfo info = (ItemInfo) child.getTag();
1079
1080 // We cancel any existing animations
1081 if (mReorderAnimators.containsKey(lp)) {
1082 mReorderAnimators.get(lp).cancel();
1083 mReorderAnimators.remove(lp);
1084 }
1085
Adam Cohen482ed822012-03-02 14:15:13 -08001086 final int oldX = lp.x;
1087 final int oldY = lp.y;
1088 if (adjustOccupied) {
1089 occupied[lp.cellX][lp.cellY] = false;
1090 occupied[cellX][cellY] = true;
1091 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001092 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001093 if (permanent) {
1094 lp.cellX = info.cellX = cellX;
1095 lp.cellY = info.cellY = cellY;
1096 } else {
1097 lp.tmpCellX = cellX;
1098 lp.tmpCellY = cellY;
1099 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001100 clc.setupLp(lp);
1101 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001102 final int newX = lp.x;
1103 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001104
Adam Cohen76fc0852011-06-17 13:26:23 -07001105 lp.x = oldX;
1106 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001107
Adam Cohen482ed822012-03-02 14:15:13 -08001108 // Exit early if we're not actually moving the view
1109 if (oldX == newX && oldY == newY) {
1110 lp.isLockedToGrid = true;
1111 return true;
1112 }
1113
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001114 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001115 va.setDuration(duration);
1116 mReorderAnimators.put(lp, va);
1117
1118 va.addUpdateListener(new AnimatorUpdateListener() {
1119 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001120 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001121 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001122 lp.x = (int) ((1 - r) * oldX + r * newX);
1123 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001124 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001125 }
1126 });
Adam Cohen482ed822012-03-02 14:15:13 -08001127 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001128 boolean cancelled = false;
1129 public void onAnimationEnd(Animator animation) {
1130 // If the animation was cancelled, it means that another animation
1131 // has interrupted this one, and we don't want to lock the item into
1132 // place just yet.
1133 if (!cancelled) {
1134 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001135 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001136 }
1137 if (mReorderAnimators.containsKey(lp)) {
1138 mReorderAnimators.remove(lp);
1139 }
1140 }
1141 public void onAnimationCancel(Animator animation) {
1142 cancelled = true;
1143 }
1144 });
Adam Cohen482ed822012-03-02 14:15:13 -08001145 va.setStartDelay(delay);
1146 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001147 return true;
1148 }
1149 return false;
1150 }
1151
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001152 /**
1153 * Estimate where the top left cell of the dragged item will land if it is dropped.
1154 *
1155 * @param originX The X value of the top left corner of the item
1156 * @param originY The Y value of the top left corner of the item
1157 * @param spanX The number of horizontal cells that the item spans
1158 * @param spanY The number of vertical cells that the item spans
1159 * @param result The estimated drop cell X and Y.
1160 */
1161 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001162 final int countX = mCountX;
1163 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001164
Michael Jurkaa63c4522010-08-19 13:52:27 -07001165 // pointToCellRounded takes the top left of a cell but will pad that with
1166 // cellWidth/2 and cellHeight/2 when finding the matching cell
1167 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001168
1169 // If the item isn't fully on this screen, snap to the edges
1170 int rightOverhang = result[0] + spanX - countX;
1171 if (rightOverhang > 0) {
1172 result[0] -= rightOverhang; // Snap to right
1173 }
1174 result[0] = Math.max(0, result[0]); // Snap to left
1175 int bottomOverhang = result[1] + spanY - countY;
1176 if (bottomOverhang > 0) {
1177 result[1] -= bottomOverhang; // Snap to bottom
1178 }
1179 result[1] = Math.max(0, result[1]); // Snap to top
1180 }
1181
Adam Cohen482ed822012-03-02 14:15:13 -08001182 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1183 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001184 final int oldDragCellX = mDragCell[0];
1185 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001186
Winson Chungb8c69f32011-10-19 21:36:08 -07001187 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001188 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1189 } else {
1190 mDragCenter.set(originX, originY);
1191 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001192
Adam Cohen2801caf2011-05-13 20:57:39 -07001193 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001194 return;
1195 }
1196
Adam Cohen482ed822012-03-02 14:15:13 -08001197 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1198 mDragCell[0] = cellX;
1199 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001200 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001201 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001202 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001203
Joe Onorato4be866d2010-10-10 11:26:02 -07001204 int left = topLeft[0];
1205 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001206
Winson Chung892c74d2013-08-22 16:15:50 -07001207 // Offset icons by their padding
1208 if (v instanceof BubbleTextView) {
1209 top += v.getPaddingTop();
1210 }
1211
Winson Chungb8c69f32011-10-19 21:36:08 -07001212 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001213 // When drawing the drag outline, it did not account for margin offsets
1214 // added by the view's parent.
1215 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1216 left += lp.leftMargin;
1217 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001218
Adam Cohen99e8b402011-03-25 19:23:43 -07001219 // Offsets due to the size difference between the View and the dragOutline.
1220 // There is a size difference to account for the outer blur, which may lie
1221 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001222 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001223 // We center about the x axis
1224 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1225 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001226 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001227 if (dragOffset != null && dragRegion != null) {
1228 // Center the drag region *horizontally* in the cell and apply a drag
1229 // outline offset
1230 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1231 - dragRegion.width()) / 2;
1232 top += dragOffset.y;
1233 } else {
1234 // Center the drag outline in the cell
1235 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1236 - dragOutline.getWidth()) / 2;
1237 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1238 - dragOutline.getHeight()) / 2;
1239 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001240 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001241 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001242 mDragOutlineAnims[oldIndex].animateOut();
1243 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001244 Rect r = mDragOutlines[mDragOutlineCurrent];
1245 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1246 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001247 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001248 }
Winson Chung150fbab2010-09-29 17:14:26 -07001249
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001250 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1251 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001252 }
1253 }
1254
Adam Cohene0310962011-04-18 16:15:31 -07001255 public void clearDragOutlines() {
1256 final int oldIndex = mDragOutlineCurrent;
1257 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001258 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001259 }
1260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001262 * Find a vacant area that will fit the given bounds nearest the requested
1263 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001264 *
Romain Guy51afc022009-05-04 18:03:43 -07001265 * @param pixelX The X location at which you want to search for a vacant area.
1266 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001267 * @param spanX Horizontal span of the object.
1268 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001269 * @param result Array in which to place the result, or null (in which case a new array will
1270 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001271 * @return The X, Y cell of a vacant area that can contain this object,
1272 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001274 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1275 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001276 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001277 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001278
Michael Jurka6a1435d2010-09-27 17:35:12 -07001279 /**
1280 * Find a vacant area that will fit the given bounds nearest the requested
1281 * cell location. Uses Euclidean distance to score multiple vacant areas.
1282 *
1283 * @param pixelX The X location at which you want to search for a vacant area.
1284 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001285 * @param minSpanX The minimum horizontal span required
1286 * @param minSpanY The minimum vertical span required
1287 * @param spanX Horizontal span of the object.
1288 * @param spanY Vertical span of the object.
1289 * @param result Array in which to place the result, or null (in which case a new array will
1290 * be allocated)
1291 * @return The X, Y cell of a vacant area that can contain this object,
1292 * nearest the requested location.
1293 */
1294 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1295 int spanY, int[] result, int[] resultSpan) {
1296 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1297 result, resultSpan);
1298 }
1299
1300 /**
1301 * Find a vacant area that will fit the given bounds nearest the requested
1302 * cell location. Uses Euclidean distance to score multiple vacant areas.
1303 *
1304 * @param pixelX The X location at which you want to search for a vacant area.
1305 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001306 * @param spanX Horizontal span of the object.
1307 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001308 * @param ignoreOccupied If true, the result can be an occupied cell
1309 * @param result Array in which to place the result, or null (in which case a new array will
1310 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001311 * @return The X, Y cell of a vacant area that can contain this object,
1312 * nearest the requested location.
1313 */
Adam Cohendf035382011-04-11 17:22:04 -07001314 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1315 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001316 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001317 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001318 }
1319
1320 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1321 private void lazyInitTempRectStack() {
1322 if (mTempRectStack.isEmpty()) {
1323 for (int i = 0; i < mCountX * mCountY; i++) {
1324 mTempRectStack.push(new Rect());
1325 }
1326 }
1327 }
Adam Cohen482ed822012-03-02 14:15:13 -08001328
Adam Cohend41fbf52012-02-16 23:53:59 -08001329 private void recycleTempRects(Stack<Rect> used) {
1330 while (!used.isEmpty()) {
1331 mTempRectStack.push(used.pop());
1332 }
1333 }
1334
1335 /**
1336 * Find a vacant area that will fit the given bounds nearest the requested
1337 * cell location. Uses Euclidean distance to score multiple vacant areas.
1338 *
1339 * @param pixelX The X location at which you want to search for a vacant area.
1340 * @param pixelY The Y location at which you want to search for a vacant area.
1341 * @param minSpanX The minimum horizontal span required
1342 * @param minSpanY The minimum vertical span required
1343 * @param spanX Horizontal span of the object.
1344 * @param spanY Vertical span of the object.
1345 * @param ignoreOccupied If true, the result can be an occupied cell
1346 * @param result Array in which to place the result, or null (in which case a new array will
1347 * be allocated)
1348 * @return The X, Y cell of a vacant area that can contain this object,
1349 * nearest the requested location.
1350 */
1351 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001352 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1353 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001354 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001355 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001356 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001357
Adam Cohene3e27a82011-04-15 12:07:39 -07001358 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1359 // to the center of the item, but we are searching based on the top-left cell, so
1360 // we translate the point over to correspond to the top-left.
1361 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1362 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1363
Jeff Sharkey70864282009-04-07 21:08:40 -07001364 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001365 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001366 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001367 final Rect bestRect = new Rect(-1, -1, -1, -1);
1368 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001369
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001370 final int countX = mCountX;
1371 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001372
Adam Cohend41fbf52012-02-16 23:53:59 -08001373 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1374 spanX < minSpanX || spanY < minSpanY) {
1375 return bestXY;
1376 }
1377
1378 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001379 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001380 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1381 int ySize = -1;
1382 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001383 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001384 // First, let's see if this thing fits anywhere
1385 for (int i = 0; i < minSpanX; i++) {
1386 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001387 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001388 continue inner;
1389 }
Michael Jurkac28de512010-08-13 11:27:44 -07001390 }
1391 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001392 xSize = minSpanX;
1393 ySize = minSpanY;
1394
1395 // We know that the item will fit at _some_ acceptable size, now let's see
1396 // how big we can make it. We'll alternate between incrementing x and y spans
1397 // until we hit a limit.
1398 boolean incX = true;
1399 boolean hitMaxX = xSize >= spanX;
1400 boolean hitMaxY = ySize >= spanY;
1401 while (!(hitMaxX && hitMaxY)) {
1402 if (incX && !hitMaxX) {
1403 for (int j = 0; j < ySize; j++) {
1404 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1405 // We can't move out horizontally
1406 hitMaxX = true;
1407 }
1408 }
1409 if (!hitMaxX) {
1410 xSize++;
1411 }
1412 } else if (!hitMaxY) {
1413 for (int i = 0; i < xSize; i++) {
1414 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1415 // We can't move out vertically
1416 hitMaxY = true;
1417 }
1418 }
1419 if (!hitMaxY) {
1420 ySize++;
1421 }
1422 }
1423 hitMaxX |= xSize >= spanX;
1424 hitMaxY |= ySize >= spanY;
1425 incX = !incX;
1426 }
1427 incX = true;
1428 hitMaxX = xSize >= spanX;
1429 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001430 }
Winson Chung0be025d2011-05-23 17:45:09 -07001431 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001432 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433
Adam Cohend41fbf52012-02-16 23:53:59 -08001434 // We verify that the current rect is not a sub-rect of any of our previous
1435 // candidates. In this case, the current rect is disqualified in favour of the
1436 // containing rect.
1437 Rect currentRect = mTempRectStack.pop();
1438 currentRect.set(x, y, x + xSize, y + ySize);
1439 boolean contained = false;
1440 for (Rect r : validRegions) {
1441 if (r.contains(currentRect)) {
1442 contained = true;
1443 break;
1444 }
1445 }
1446 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001447 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1448 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001449
Adam Cohend41fbf52012-02-16 23:53:59 -08001450 if ((distance <= bestDistance && !contained) ||
1451 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001452 bestDistance = distance;
1453 bestXY[0] = x;
1454 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001455 if (resultSpan != null) {
1456 resultSpan[0] = xSize;
1457 resultSpan[1] = ySize;
1458 }
1459 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 }
1462 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001463 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001464 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465
Adam Cohenc0dcf592011-06-01 15:30:43 -07001466 // Return -1, -1 if no suitable location found
1467 if (bestDistance == Double.MAX_VALUE) {
1468 bestXY[0] = -1;
1469 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001470 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001471 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001472 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001474
Adam Cohen482ed822012-03-02 14:15:13 -08001475 /**
1476 * Find a vacant area that will fit the given bounds nearest the requested
1477 * cell location, and will also weigh in a suggested direction vector of the
1478 * desired location. This method computers distance based on unit grid distances,
1479 * not pixel distances.
1480 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001481 * @param cellX The X cell nearest to which you want to search for a vacant area.
1482 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001483 * @param spanX Horizontal span of the object.
1484 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001485 * @param direction The favored direction in which the views should move from x, y
1486 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1487 * matches exactly. Otherwise we find the best matching direction.
1488 * @param occoupied The array which represents which cells in the CellLayout are occupied
1489 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001490 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001491 * @param result Array in which to place the result, or null (in which case a new array will
1492 * be allocated)
1493 * @return The X, Y cell of a vacant area that can contain this object,
1494 * nearest the requested location.
1495 */
1496 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001497 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001498 // Keep track of best-scoring drop area
1499 final int[] bestXY = result != null ? result : new int[2];
1500 float bestDistance = Float.MAX_VALUE;
1501 int bestDirectionScore = Integer.MIN_VALUE;
1502
1503 final int countX = mCountX;
1504 final int countY = mCountY;
1505
1506 for (int y = 0; y < countY - (spanY - 1); y++) {
1507 inner:
1508 for (int x = 0; x < countX - (spanX - 1); x++) {
1509 // First, let's see if this thing fits anywhere
1510 for (int i = 0; i < spanX; i++) {
1511 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001512 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001513 continue inner;
1514 }
1515 }
1516 }
1517
1518 float distance = (float)
1519 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1520 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001521 computeDirectionVector(x - cellX, y - cellY, curDirection);
1522 // The direction score is just the dot product of the two candidate direction
1523 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001524 int curDirectionScore = direction[0] * curDirection[0] +
1525 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001526 boolean exactDirectionOnly = false;
1527 boolean directionMatches = direction[0] == curDirection[0] &&
1528 direction[0] == curDirection[0];
1529 if ((directionMatches || !exactDirectionOnly) &&
1530 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001531 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1532 bestDistance = distance;
1533 bestDirectionScore = curDirectionScore;
1534 bestXY[0] = x;
1535 bestXY[1] = y;
1536 }
1537 }
1538 }
1539
1540 // Return -1, -1 if no suitable location found
1541 if (bestDistance == Float.MAX_VALUE) {
1542 bestXY[0] = -1;
1543 bestXY[1] = -1;
1544 }
1545 return bestXY;
1546 }
1547
1548 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001549 int[] direction, ItemConfiguration currentState) {
1550 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001551 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001552 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001553 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1554
Adam Cohen8baab352012-03-20 17:39:21 -07001555 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001556
1557 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001558 c.x = mTempLocation[0];
1559 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001560 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001561 }
Adam Cohen8baab352012-03-20 17:39:21 -07001562 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001563 return success;
1564 }
1565
Adam Cohenf3900c22012-11-16 18:28:11 -08001566 /**
1567 * This helper class defines a cluster of views. It helps with defining complex edges
1568 * of the cluster and determining how those edges interact with other views. The edges
1569 * essentially define a fine-grained boundary around the cluster of views -- like a more
1570 * precise version of a bounding box.
1571 */
1572 private class ViewCluster {
1573 final static int LEFT = 0;
1574 final static int TOP = 1;
1575 final static int RIGHT = 2;
1576 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001577
Adam Cohenf3900c22012-11-16 18:28:11 -08001578 ArrayList<View> views;
1579 ItemConfiguration config;
1580 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001581
Adam Cohenf3900c22012-11-16 18:28:11 -08001582 int[] leftEdge = new int[mCountY];
1583 int[] rightEdge = new int[mCountY];
1584 int[] topEdge = new int[mCountX];
1585 int[] bottomEdge = new int[mCountX];
1586 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1587
1588 @SuppressWarnings("unchecked")
1589 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1590 this.views = (ArrayList<View>) views.clone();
1591 this.config = config;
1592 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001593 }
1594
Adam Cohenf3900c22012-11-16 18:28:11 -08001595 void resetEdges() {
1596 for (int i = 0; i < mCountX; i++) {
1597 topEdge[i] = -1;
1598 bottomEdge[i] = -1;
1599 }
1600 for (int i = 0; i < mCountY; i++) {
1601 leftEdge[i] = -1;
1602 rightEdge[i] = -1;
1603 }
1604 leftEdgeDirty = true;
1605 rightEdgeDirty = true;
1606 bottomEdgeDirty = true;
1607 topEdgeDirty = true;
1608 boundingRectDirty = true;
1609 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001610
Adam Cohenf3900c22012-11-16 18:28:11 -08001611 void computeEdge(int which, int[] edge) {
1612 int count = views.size();
1613 for (int i = 0; i < count; i++) {
1614 CellAndSpan cs = config.map.get(views.get(i));
1615 switch (which) {
1616 case LEFT:
1617 int left = cs.x;
1618 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1619 if (left < edge[j] || edge[j] < 0) {
1620 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001621 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001622 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001623 break;
1624 case RIGHT:
1625 int right = cs.x + cs.spanX;
1626 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1627 if (right > edge[j]) {
1628 edge[j] = right;
1629 }
1630 }
1631 break;
1632 case TOP:
1633 int top = cs.y;
1634 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1635 if (top < edge[j] || edge[j] < 0) {
1636 edge[j] = top;
1637 }
1638 }
1639 break;
1640 case BOTTOM:
1641 int bottom = cs.y + cs.spanY;
1642 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1643 if (bottom > edge[j]) {
1644 edge[j] = bottom;
1645 }
1646 }
1647 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001648 }
1649 }
1650 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001651
1652 boolean isViewTouchingEdge(View v, int whichEdge) {
1653 CellAndSpan cs = config.map.get(v);
1654
1655 int[] edge = getEdge(whichEdge);
1656
1657 switch (whichEdge) {
1658 case LEFT:
1659 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1660 if (edge[i] == cs.x + cs.spanX) {
1661 return true;
1662 }
1663 }
1664 break;
1665 case RIGHT:
1666 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1667 if (edge[i] == cs.x) {
1668 return true;
1669 }
1670 }
1671 break;
1672 case TOP:
1673 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1674 if (edge[i] == cs.y + cs.spanY) {
1675 return true;
1676 }
1677 }
1678 break;
1679 case BOTTOM:
1680 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1681 if (edge[i] == cs.y) {
1682 return true;
1683 }
1684 }
1685 break;
1686 }
1687 return false;
1688 }
1689
1690 void shift(int whichEdge, int delta) {
1691 for (View v: views) {
1692 CellAndSpan c = config.map.get(v);
1693 switch (whichEdge) {
1694 case LEFT:
1695 c.x -= delta;
1696 break;
1697 case RIGHT:
1698 c.x += delta;
1699 break;
1700 case TOP:
1701 c.y -= delta;
1702 break;
1703 case BOTTOM:
1704 default:
1705 c.y += delta;
1706 break;
1707 }
1708 }
1709 resetEdges();
1710 }
1711
1712 public void addView(View v) {
1713 views.add(v);
1714 resetEdges();
1715 }
1716
1717 public Rect getBoundingRect() {
1718 if (boundingRectDirty) {
1719 boolean first = true;
1720 for (View v: views) {
1721 CellAndSpan c = config.map.get(v);
1722 if (first) {
1723 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1724 first = false;
1725 } else {
1726 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1727 }
1728 }
1729 }
1730 return boundingRect;
1731 }
1732
1733 public int[] getEdge(int which) {
1734 switch (which) {
1735 case LEFT:
1736 return getLeftEdge();
1737 case RIGHT:
1738 return getRightEdge();
1739 case TOP:
1740 return getTopEdge();
1741 case BOTTOM:
1742 default:
1743 return getBottomEdge();
1744 }
1745 }
1746
1747 public int[] getLeftEdge() {
1748 if (leftEdgeDirty) {
1749 computeEdge(LEFT, leftEdge);
1750 }
1751 return leftEdge;
1752 }
1753
1754 public int[] getRightEdge() {
1755 if (rightEdgeDirty) {
1756 computeEdge(RIGHT, rightEdge);
1757 }
1758 return rightEdge;
1759 }
1760
1761 public int[] getTopEdge() {
1762 if (topEdgeDirty) {
1763 computeEdge(TOP, topEdge);
1764 }
1765 return topEdge;
1766 }
1767
1768 public int[] getBottomEdge() {
1769 if (bottomEdgeDirty) {
1770 computeEdge(BOTTOM, bottomEdge);
1771 }
1772 return bottomEdge;
1773 }
1774
1775 PositionComparator comparator = new PositionComparator();
1776 class PositionComparator implements Comparator<View> {
1777 int whichEdge = 0;
1778 public int compare(View left, View right) {
1779 CellAndSpan l = config.map.get(left);
1780 CellAndSpan r = config.map.get(right);
1781 switch (whichEdge) {
1782 case LEFT:
1783 return (r.x + r.spanX) - (l.x + l.spanX);
1784 case RIGHT:
1785 return l.x - r.x;
1786 case TOP:
1787 return (r.y + r.spanY) - (l.y + l.spanY);
1788 case BOTTOM:
1789 default:
1790 return l.y - r.y;
1791 }
1792 }
1793 }
1794
1795 public void sortConfigurationForEdgePush(int edge) {
1796 comparator.whichEdge = edge;
1797 Collections.sort(config.sortedViews, comparator);
1798 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001799 }
1800
Adam Cohenf3900c22012-11-16 18:28:11 -08001801 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1802 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001803
Adam Cohenf3900c22012-11-16 18:28:11 -08001804 ViewCluster cluster = new ViewCluster(views, currentState);
1805 Rect clusterRect = cluster.getBoundingRect();
1806 int whichEdge;
1807 int pushDistance;
1808 boolean fail = false;
1809
1810 // Determine the edge of the cluster that will be leading the push and how far
1811 // the cluster must be shifted.
1812 if (direction[0] < 0) {
1813 whichEdge = ViewCluster.LEFT;
1814 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001815 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001816 whichEdge = ViewCluster.RIGHT;
1817 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1818 } else if (direction[1] < 0) {
1819 whichEdge = ViewCluster.TOP;
1820 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1821 } else {
1822 whichEdge = ViewCluster.BOTTOM;
1823 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001824 }
1825
Adam Cohenf3900c22012-11-16 18:28:11 -08001826 // Break early for invalid push distance.
1827 if (pushDistance <= 0) {
1828 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001829 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001830
1831 // Mark the occupied state as false for the group of views we want to move.
1832 for (View v: views) {
1833 CellAndSpan c = currentState.map.get(v);
1834 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1835 }
1836
1837 // We save the current configuration -- if we fail to find a solution we will revert
1838 // to the initial state. The process of finding a solution modifies the configuration
1839 // in place, hence the need for revert in the failure case.
1840 currentState.save();
1841
1842 // The pushing algorithm is simplified by considering the views in the order in which
1843 // they would be pushed by the cluster. For example, if the cluster is leading with its
1844 // left edge, we consider sort the views by their right edge, from right to left.
1845 cluster.sortConfigurationForEdgePush(whichEdge);
1846
1847 while (pushDistance > 0 && !fail) {
1848 for (View v: currentState.sortedViews) {
1849 // For each view that isn't in the cluster, we see if the leading edge of the
1850 // cluster is contacting the edge of that view. If so, we add that view to the
1851 // cluster.
1852 if (!cluster.views.contains(v) && v != dragView) {
1853 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1854 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1855 if (!lp.canReorder) {
1856 // The push solution includes the all apps button, this is not viable.
1857 fail = true;
1858 break;
1859 }
1860 cluster.addView(v);
1861 CellAndSpan c = currentState.map.get(v);
1862
1863 // Adding view to cluster, mark it as not occupied.
1864 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1865 }
1866 }
1867 }
1868 pushDistance--;
1869
1870 // The cluster has been completed, now we move the whole thing over in the appropriate
1871 // direction.
1872 cluster.shift(whichEdge, 1);
1873 }
1874
1875 boolean foundSolution = false;
1876 clusterRect = cluster.getBoundingRect();
1877
1878 // Due to the nature of the algorithm, the only check required to verify a valid solution
1879 // is to ensure that completed shifted cluster lies completely within the cell layout.
1880 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1881 clusterRect.bottom <= mCountY) {
1882 foundSolution = true;
1883 } else {
1884 currentState.restore();
1885 }
1886
1887 // In either case, we set the occupied array as marked for the location of the views
1888 for (View v: cluster.views) {
1889 CellAndSpan c = currentState.map.get(v);
1890 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1891 }
1892
1893 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001894 }
1895
Adam Cohen482ed822012-03-02 14:15:13 -08001896 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001897 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001898 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001899
Adam Cohen8baab352012-03-20 17:39:21 -07001900 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001901 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001902 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001903 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001904 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001905 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001906 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001907 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001908 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001909 }
1910 }
Adam Cohen8baab352012-03-20 17:39:21 -07001911
Adam Cohen8baab352012-03-20 17:39:21 -07001912 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001913 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001914 CellAndSpan c = currentState.map.get(v);
1915 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1916 }
1917
Adam Cohen47a876d2012-03-19 13:21:41 -07001918 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1919 int top = boundingRect.top;
1920 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001921 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001922 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001923 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001924 CellAndSpan c = currentState.map.get(v);
1925 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001926 }
1927
Adam Cohen482ed822012-03-02 14:15:13 -08001928 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1929
Adam Cohenf3900c22012-11-16 18:28:11 -08001930 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1931 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001932
Adam Cohen8baab352012-03-20 17:39:21 -07001933 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001934 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001935 int deltaX = mTempLocation[0] - boundingRect.left;
1936 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001937 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001938 CellAndSpan c = currentState.map.get(v);
1939 c.x += deltaX;
1940 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001941 }
1942 success = true;
1943 }
Adam Cohen8baab352012-03-20 17:39:21 -07001944
1945 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001946 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001947 CellAndSpan c = currentState.map.get(v);
1948 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001949 }
1950 return success;
1951 }
1952
1953 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1954 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1955 }
1956
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001957 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1958 // to push items in each of the cardinal directions, in an order based on the direction vector
1959 // passed.
1960 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1961 int[] direction, View ignoreView, ItemConfiguration solution) {
1962 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001963 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001964 // separately in each of the components.
1965 int temp = direction[1];
1966 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001967
1968 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001969 ignoreView, solution)) {
1970 return true;
1971 }
1972 direction[1] = temp;
1973 temp = direction[0];
1974 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001975
1976 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001977 ignoreView, solution)) {
1978 return true;
1979 }
1980 // Revert the direction
1981 direction[0] = temp;
1982
1983 // Now we try pushing in each component of the opposite direction
1984 direction[0] *= -1;
1985 direction[1] *= -1;
1986 temp = direction[1];
1987 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001988 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001989 ignoreView, solution)) {
1990 return true;
1991 }
1992
1993 direction[1] = temp;
1994 temp = direction[0];
1995 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001996 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001997 ignoreView, solution)) {
1998 return true;
1999 }
2000 // revert the direction
2001 direction[0] = temp;
2002 direction[0] *= -1;
2003 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002004
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002005 } else {
2006 // If the direction vector has a single non-zero component, we push first in the
2007 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002008 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002009 ignoreView, solution)) {
2010 return true;
2011 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002012 // Then we try the opposite direction
2013 direction[0] *= -1;
2014 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002015 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002016 ignoreView, solution)) {
2017 return true;
2018 }
2019 // Switch the direction back
2020 direction[0] *= -1;
2021 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07002022
2023 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002024 // to find a solution by pushing along the perpendicular axis.
2025
2026 // Swap the components
2027 int temp = direction[1];
2028 direction[1] = direction[0];
2029 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002030 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002031 ignoreView, solution)) {
2032 return true;
2033 }
2034
2035 // Then we try the opposite direction
2036 direction[0] *= -1;
2037 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002038 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002039 ignoreView, solution)) {
2040 return true;
2041 }
2042 // Switch the direction back
2043 direction[0] *= -1;
2044 direction[1] *= -1;
2045
2046 // Swap the components back
2047 temp = direction[1];
2048 direction[1] = direction[0];
2049 direction[0] = temp;
2050 }
2051 return false;
2052 }
2053
Adam Cohen482ed822012-03-02 14:15:13 -08002054 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002055 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002056 // Return early if get invalid cell positions
2057 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002058
Adam Cohen8baab352012-03-20 17:39:21 -07002059 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002060 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002061
Adam Cohen8baab352012-03-20 17:39:21 -07002062 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002063 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002064 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002065 if (c != null) {
2066 c.x = cellX;
2067 c.y = cellY;
2068 }
Adam Cohen482ed822012-03-02 14:15:13 -08002069 }
Adam Cohen482ed822012-03-02 14:15:13 -08002070 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2071 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002072 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002073 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002074 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002075 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002076 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002077 if (Rect.intersects(r0, r1)) {
2078 if (!lp.canReorder) {
2079 return false;
2080 }
2081 mIntersectingViews.add(child);
2082 }
2083 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002084
Winson Chung5f8afe62013-08-12 16:19:28 -07002085 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002086 // we try to find a solution such that no displaced item travels through another item
2087 // without also displacing that item.
2088 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002089 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002090 return true;
2091 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002092
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002093 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002094 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002095 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002096 return true;
2097 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002098
Adam Cohen482ed822012-03-02 14:15:13 -08002099 // Ok, they couldn't move as a block, let's move them individually
2100 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002101 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002102 return false;
2103 }
2104 }
2105 return true;
2106 }
2107
2108 /*
2109 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2110 * the provided point and the provided cell
2111 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002112 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002113 double angle = Math.atan(((float) deltaY) / deltaX);
2114
2115 result[0] = 0;
2116 result[1] = 0;
2117 if (Math.abs(Math.cos(angle)) > 0.5f) {
2118 result[0] = (int) Math.signum(deltaX);
2119 }
2120 if (Math.abs(Math.sin(angle)) > 0.5f) {
2121 result[1] = (int) Math.signum(deltaY);
2122 }
2123 }
2124
Adam Cohen8baab352012-03-20 17:39:21 -07002125 private void copyOccupiedArray(boolean[][] occupied) {
2126 for (int i = 0; i < mCountX; i++) {
2127 for (int j = 0; j < mCountY; j++) {
2128 occupied[i][j] = mOccupied[i][j];
2129 }
2130 }
2131 }
2132
Adam Cohen482ed822012-03-02 14:15:13 -08002133 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
2134 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002135 // Copy the current state into the solution. This solution will be manipulated as necessary.
2136 copyCurrentStateToSolution(solution, false);
2137 // Copy the current occupied array into the temporary occupied array. This array will be
2138 // manipulated as necessary to find a solution.
2139 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002140
2141 // We find the nearest cell into which we would place the dragged item, assuming there's
2142 // nothing in its way.
2143 int result[] = new int[2];
2144 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2145
2146 boolean success = false;
2147 // First we try the exact nearest position of the item being dragged,
2148 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002149 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2150 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002151
2152 if (!success) {
2153 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2154 // x, then 1 in y etc.
2155 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
2156 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
2157 dragView, false, solution);
2158 } else if (spanY > minSpanY) {
2159 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
2160 dragView, true, solution);
2161 }
2162 solution.isSolution = false;
2163 } else {
2164 solution.isSolution = true;
2165 solution.dragViewX = result[0];
2166 solution.dragViewY = result[1];
2167 solution.dragViewSpanX = spanX;
2168 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002169 }
2170 return solution;
2171 }
2172
2173 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002174 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002175 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002176 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002177 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002178 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002179 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002180 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002181 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002182 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002183 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002184 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002185 }
2186 }
2187
2188 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2189 for (int i = 0; i < mCountX; i++) {
2190 for (int j = 0; j < mCountY; j++) {
2191 mTmpOccupied[i][j] = false;
2192 }
2193 }
2194
Michael Jurkaa52570f2012-03-20 03:18:20 -07002195 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002196 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002197 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002198 if (child == dragView) continue;
2199 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002200 CellAndSpan c = solution.map.get(child);
2201 if (c != null) {
2202 lp.tmpCellX = c.x;
2203 lp.tmpCellY = c.y;
2204 lp.cellHSpan = c.spanX;
2205 lp.cellVSpan = c.spanY;
2206 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002207 }
2208 }
2209 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2210 solution.dragViewSpanY, mTmpOccupied, true);
2211 }
2212
2213 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2214 commitDragView) {
2215
2216 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2217 for (int i = 0; i < mCountX; i++) {
2218 for (int j = 0; j < mCountY; j++) {
2219 occupied[i][j] = false;
2220 }
2221 }
2222
Michael Jurkaa52570f2012-03-20 03:18:20 -07002223 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002224 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002225 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002226 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002227 CellAndSpan c = solution.map.get(child);
2228 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002229 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2230 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002231 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002232 }
2233 }
2234 if (commitDragView) {
2235 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2236 solution.dragViewSpanY, occupied, true);
2237 }
2238 }
2239
Adam Cohen19f37922012-03-21 11:59:11 -07002240 // This method starts or changes the reorder hint animations
2241 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2242 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002243 for (int i = 0; i < childCount; i++) {
2244 View child = mShortcutsAndWidgets.getChildAt(i);
2245 if (child == dragView) continue;
2246 CellAndSpan c = solution.map.get(child);
2247 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2248 if (c != null) {
2249 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2250 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002251 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002252 }
2253 }
2254 }
2255
2256 // Class which represents the reorder hint animations. These animations show that an item is
2257 // in a temporary state, and hint at where the item will return to.
2258 class ReorderHintAnimation {
2259 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002260 float finalDeltaX;
2261 float finalDeltaY;
2262 float initDeltaX;
2263 float initDeltaY;
2264 float finalScale;
2265 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002266 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002267 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002268
2269 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2270 int spanX, int spanY) {
2271 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2272 final int x0 = mTmpPoint[0];
2273 final int y0 = mTmpPoint[1];
2274 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2275 final int x1 = mTmpPoint[0];
2276 final int y1 = mTmpPoint[1];
2277 final int dX = x1 - x0;
2278 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002279 finalDeltaX = 0;
2280 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002281 if (dX == dY && dX == 0) {
2282 } else {
2283 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002284 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002285 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002286 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002287 } else {
2288 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002289 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002290 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002291 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002292 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002293 }
2294 }
Adam Cohend024f982012-05-23 18:26:45 -07002295 initDeltaX = child.getTranslationX();
2296 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002297 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002298 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002299 this.child = child;
2300 }
2301
Adam Cohend024f982012-05-23 18:26:45 -07002302 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002303 if (mShakeAnimators.containsKey(child)) {
2304 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002305 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002306 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002307 if (finalDeltaX == 0 && finalDeltaY == 0) {
2308 completeAnimationImmediately();
2309 return;
2310 }
Adam Cohen19f37922012-03-21 11:59:11 -07002311 }
Adam Cohend024f982012-05-23 18:26:45 -07002312 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002313 return;
2314 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002315 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002316 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002317 va.setRepeatMode(ValueAnimator.REVERSE);
2318 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002319 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002320 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002321 va.addUpdateListener(new AnimatorUpdateListener() {
2322 @Override
2323 public void onAnimationUpdate(ValueAnimator animation) {
2324 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002325 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2326 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002327 child.setTranslationX(x);
2328 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002329 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002330 child.setScaleX(s);
2331 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002332 }
2333 });
2334 va.addListener(new AnimatorListenerAdapter() {
2335 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002336 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002337 initDeltaX = 0;
2338 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002339 initScale = getChildrenScale();
Adam Cohen19f37922012-03-21 11:59:11 -07002340 }
2341 });
Adam Cohen19f37922012-03-21 11:59:11 -07002342 mShakeAnimators.put(child, this);
2343 va.start();
2344 }
2345
Adam Cohend024f982012-05-23 18:26:45 -07002346 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002347 if (a != null) {
2348 a.cancel();
2349 }
Adam Cohen19f37922012-03-21 11:59:11 -07002350 }
Adam Cohene7587d22012-05-24 18:50:02 -07002351
Brandon Keely50e6e562012-05-08 16:28:49 -07002352 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002353 if (a != null) {
2354 a.cancel();
2355 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002356
Michael Jurka2ecf9952012-06-18 12:52:28 -07002357 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002358 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002359 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002360 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2361 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002362 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2363 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002364 );
2365 s.setDuration(REORDER_ANIMATION_DURATION);
2366 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2367 s.start();
2368 }
Adam Cohen19f37922012-03-21 11:59:11 -07002369 }
2370
2371 private void completeAndClearReorderHintAnimations() {
2372 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002373 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002374 }
2375 mShakeAnimators.clear();
2376 }
2377
Adam Cohen482ed822012-03-02 14:15:13 -08002378 private void commitTempPlacement() {
2379 for (int i = 0; i < mCountX; i++) {
2380 for (int j = 0; j < mCountY; j++) {
2381 mOccupied[i][j] = mTmpOccupied[i][j];
2382 }
2383 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002384 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002385 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002386 View child = mShortcutsAndWidgets.getChildAt(i);
2387 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2388 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002389 // We do a null check here because the item info can be null in the case of the
2390 // AllApps button in the hotseat.
2391 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002392 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2393 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2394 info.requiresDbUpdate = true;
2395 }
Adam Cohen2acce882012-03-28 19:03:19 -07002396 info.cellX = lp.cellX = lp.tmpCellX;
2397 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002398 info.spanX = lp.cellHSpan;
2399 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002400 }
Adam Cohen482ed822012-03-02 14:15:13 -08002401 }
Adam Cohen2acce882012-03-28 19:03:19 -07002402 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002403 }
2404
2405 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002406 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002407 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002408 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002409 lp.useTmpCoords = useTempCoords;
2410 }
2411 }
2412
Adam Cohen482ed822012-03-02 14:15:13 -08002413 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2414 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2415 int[] result = new int[2];
2416 int[] resultSpan = new int[2];
2417 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2418 resultSpan);
2419 if (result[0] >= 0 && result[1] >= 0) {
2420 copyCurrentStateToSolution(solution, false);
2421 solution.dragViewX = result[0];
2422 solution.dragViewY = result[1];
2423 solution.dragViewSpanX = resultSpan[0];
2424 solution.dragViewSpanY = resultSpan[1];
2425 solution.isSolution = true;
2426 } else {
2427 solution.isSolution = false;
2428 }
2429 return solution;
2430 }
2431
2432 public void prepareChildForDrag(View child) {
2433 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002434 }
2435
Adam Cohen19f37922012-03-21 11:59:11 -07002436 /* This seems like it should be obvious and straight-forward, but when the direction vector
2437 needs to match with the notion of the dragView pushing other views, we have to employ
2438 a slightly more subtle notion of the direction vector. The question is what two points is
2439 the vector between? The center of the dragView and its desired destination? Not quite, as
2440 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2441 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2442 or right, which helps make pushing feel right.
2443 */
2444 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2445 int spanY, View dragView, int[] resultDirection) {
2446 int[] targetDestination = new int[2];
2447
2448 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2449 Rect dragRect = new Rect();
2450 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2451 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2452
2453 Rect dropRegionRect = new Rect();
2454 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2455 dragView, dropRegionRect, mIntersectingViews);
2456
2457 int dropRegionSpanX = dropRegionRect.width();
2458 int dropRegionSpanY = dropRegionRect.height();
2459
2460 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2461 dropRegionRect.height(), dropRegionRect);
2462
2463 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2464 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2465
2466 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2467 deltaX = 0;
2468 }
2469 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2470 deltaY = 0;
2471 }
2472
2473 if (deltaX == 0 && deltaY == 0) {
2474 // No idea what to do, give a random direction.
2475 resultDirection[0] = 1;
2476 resultDirection[1] = 0;
2477 } else {
2478 computeDirectionVector(deltaX, deltaY, resultDirection);
2479 }
2480 }
2481
2482 // For a given cell and span, fetch the set of views intersecting the region.
2483 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2484 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2485 if (boundingRect != null) {
2486 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2487 }
2488 intersectingViews.clear();
2489 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2490 Rect r1 = new Rect();
2491 final int count = mShortcutsAndWidgets.getChildCount();
2492 for (int i = 0; i < count; i++) {
2493 View child = mShortcutsAndWidgets.getChildAt(i);
2494 if (child == dragView) continue;
2495 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2496 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2497 if (Rect.intersects(r0, r1)) {
2498 mIntersectingViews.add(child);
2499 if (boundingRect != null) {
2500 boundingRect.union(r1);
2501 }
2502 }
2503 }
2504 }
2505
2506 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2507 View dragView, int[] result) {
2508 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2509 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2510 mIntersectingViews);
2511 return !mIntersectingViews.isEmpty();
2512 }
2513
2514 void revertTempState() {
2515 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2516 final int count = mShortcutsAndWidgets.getChildCount();
2517 for (int i = 0; i < count; i++) {
2518 View child = mShortcutsAndWidgets.getChildAt(i);
2519 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2520 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2521 lp.tmpCellX = lp.cellX;
2522 lp.tmpCellY = lp.cellY;
2523 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2524 0, false, false);
2525 }
2526 }
2527 completeAndClearReorderHintAnimations();
2528 setItemPlacementDirty(false);
2529 }
2530
Adam Cohenbebf0422012-04-11 18:06:28 -07002531 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2532 View dragView, int[] direction, boolean commit) {
2533 int[] pixelXY = new int[2];
2534 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2535
2536 // First we determine if things have moved enough to cause a different layout
2537 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2538 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2539
2540 setUseTempCoords(true);
2541 if (swapSolution != null && swapSolution.isSolution) {
2542 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2543 // committing anything or animating anything as we just want to determine if a solution
2544 // exists
2545 copySolutionToTempState(swapSolution, dragView);
2546 setItemPlacementDirty(true);
2547 animateItemsToSolution(swapSolution, dragView, commit);
2548
2549 if (commit) {
2550 commitTempPlacement();
2551 completeAndClearReorderHintAnimations();
2552 setItemPlacementDirty(false);
2553 } else {
2554 beginOrAdjustHintAnimations(swapSolution, dragView,
2555 REORDER_ANIMATION_DURATION);
2556 }
2557 mShortcutsAndWidgets.requestLayout();
2558 }
2559 return swapSolution.isSolution;
2560 }
2561
Adam Cohen482ed822012-03-02 14:15:13 -08002562 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2563 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002564 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002565 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002566
2567 if (resultSpan == null) {
2568 resultSpan = new int[2];
2569 }
2570
Adam Cohen19f37922012-03-21 11:59:11 -07002571 // When we are checking drop validity or actually dropping, we don't recompute the
2572 // direction vector, since we want the solution to match the preview, and it's possible
2573 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002574 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2575 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002576 mDirectionVector[0] = mPreviousReorderDirection[0];
2577 mDirectionVector[1] = mPreviousReorderDirection[1];
2578 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002579 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2580 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2581 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002582 }
2583 } else {
2584 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2585 mPreviousReorderDirection[0] = mDirectionVector[0];
2586 mPreviousReorderDirection[1] = mDirectionVector[1];
2587 }
2588
Adam Cohen482ed822012-03-02 14:15:13 -08002589 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2590 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2591
2592 // We attempt the approach which doesn't shuffle views at all
2593 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2594 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2595
2596 ItemConfiguration finalSolution = null;
2597 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2598 finalSolution = swapSolution;
2599 } else if (noShuffleSolution.isSolution) {
2600 finalSolution = noShuffleSolution;
2601 }
2602
2603 boolean foundSolution = true;
2604 if (!DESTRUCTIVE_REORDER) {
2605 setUseTempCoords(true);
2606 }
2607
2608 if (finalSolution != null) {
2609 result[0] = finalSolution.dragViewX;
2610 result[1] = finalSolution.dragViewY;
2611 resultSpan[0] = finalSolution.dragViewSpanX;
2612 resultSpan[1] = finalSolution.dragViewSpanY;
2613
2614 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2615 // committing anything or animating anything as we just want to determine if a solution
2616 // exists
2617 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2618 if (!DESTRUCTIVE_REORDER) {
2619 copySolutionToTempState(finalSolution, dragView);
2620 }
2621 setItemPlacementDirty(true);
2622 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2623
Adam Cohen19f37922012-03-21 11:59:11 -07002624 if (!DESTRUCTIVE_REORDER &&
2625 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002626 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002627 completeAndClearReorderHintAnimations();
2628 setItemPlacementDirty(false);
2629 } else {
2630 beginOrAdjustHintAnimations(finalSolution, dragView,
2631 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002632 }
2633 }
2634 } else {
2635 foundSolution = false;
2636 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2637 }
2638
2639 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2640 setUseTempCoords(false);
2641 }
Adam Cohen482ed822012-03-02 14:15:13 -08002642
Michael Jurkaa52570f2012-03-20 03:18:20 -07002643 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002644 return result;
2645 }
2646
Adam Cohen19f37922012-03-21 11:59:11 -07002647 void setItemPlacementDirty(boolean dirty) {
2648 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002649 }
Adam Cohen19f37922012-03-21 11:59:11 -07002650 boolean isItemPlacementDirty() {
2651 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002652 }
2653
2654 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002655 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002656 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2657 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohen482ed822012-03-02 14:15:13 -08002658 boolean isSolution = false;
2659 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2660
Adam Cohenf3900c22012-11-16 18:28:11 -08002661 void save() {
2662 // Copy current state into savedMap
2663 for (View v: map.keySet()) {
2664 map.get(v).copy(savedMap.get(v));
2665 }
2666 }
2667
2668 void restore() {
2669 // Restore current state from savedMap
2670 for (View v: savedMap.keySet()) {
2671 savedMap.get(v).copy(map.get(v));
2672 }
2673 }
2674
2675 void add(View v, CellAndSpan cs) {
2676 map.put(v, cs);
2677 savedMap.put(v, new CellAndSpan());
2678 sortedViews.add(v);
2679 }
2680
Adam Cohen482ed822012-03-02 14:15:13 -08002681 int area() {
2682 return dragViewSpanX * dragViewSpanY;
2683 }
Adam Cohen8baab352012-03-20 17:39:21 -07002684 }
2685
2686 private class CellAndSpan {
2687 int x, y;
2688 int spanX, spanY;
2689
Adam Cohenf3900c22012-11-16 18:28:11 -08002690 public CellAndSpan() {
2691 }
2692
2693 public void copy(CellAndSpan copy) {
2694 copy.x = x;
2695 copy.y = y;
2696 copy.spanX = spanX;
2697 copy.spanY = spanY;
2698 }
2699
Adam Cohen8baab352012-03-20 17:39:21 -07002700 public CellAndSpan(int x, int y, int spanX, int spanY) {
2701 this.x = x;
2702 this.y = y;
2703 this.spanX = spanX;
2704 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002705 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002706
2707 public String toString() {
2708 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2709 }
2710
Adam Cohen482ed822012-03-02 14:15:13 -08002711 }
2712
Adam Cohendf035382011-04-11 17:22:04 -07002713 /**
2714 * Find a vacant area that will fit the given bounds nearest the requested
2715 * cell location. Uses Euclidean distance to score multiple vacant areas.
2716 *
2717 * @param pixelX The X location at which you want to search for a vacant area.
2718 * @param pixelY The Y location at which you want to search for a vacant area.
2719 * @param spanX Horizontal span of the object.
2720 * @param spanY Vertical span of the object.
2721 * @param ignoreView Considers space occupied by this view as unoccupied
2722 * @param result Previously returned value to possibly recycle.
2723 * @return The X, Y cell of a vacant area that can contain this object,
2724 * nearest the requested location.
2725 */
2726 int[] findNearestVacantArea(
2727 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2728 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2729 }
2730
2731 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002732 * Find a vacant area that will fit the given bounds nearest the requested
2733 * cell location. Uses Euclidean distance to score multiple vacant areas.
2734 *
2735 * @param pixelX The X location at which you want to search for a vacant area.
2736 * @param pixelY The Y location at which you want to search for a vacant area.
2737 * @param minSpanX The minimum horizontal span required
2738 * @param minSpanY The minimum vertical span required
2739 * @param spanX Horizontal span of the object.
2740 * @param spanY Vertical span of the object.
2741 * @param ignoreView Considers space occupied by this view as unoccupied
2742 * @param result Previously returned value to possibly recycle.
2743 * @return The X, Y cell of a vacant area that can contain this object,
2744 * nearest the requested location.
2745 */
2746 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2747 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002748 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2749 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002750 }
2751
2752 /**
Adam Cohendf035382011-04-11 17:22:04 -07002753 * Find a starting cell position that will fit the given bounds nearest the requested
2754 * cell location. Uses Euclidean distance to score multiple vacant areas.
2755 *
2756 * @param pixelX The X location at which you want to search for a vacant area.
2757 * @param pixelY The Y location at which you want to search for a vacant area.
2758 * @param spanX Horizontal span of the object.
2759 * @param spanY Vertical span of the object.
2760 * @param ignoreView Considers space occupied by this view as unoccupied
2761 * @param result Previously returned value to possibly recycle.
2762 * @return The X, Y cell of a vacant area that can contain this object,
2763 * nearest the requested location.
2764 */
2765 int[] findNearestArea(
2766 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2767 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2768 }
2769
Michael Jurka0280c3b2010-09-17 15:00:07 -07002770 boolean existsEmptyCell() {
2771 return findCellForSpan(null, 1, 1);
2772 }
2773
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002774 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002775 * Finds the upper-left coordinate of the first rectangle in the grid that can
2776 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2777 * then this method will only return coordinates for rectangles that contain the cell
2778 * (intersectX, intersectY)
2779 *
2780 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2781 * can be found.
2782 * @param spanX The horizontal span of the cell we want to find.
2783 * @param spanY The vertical span of the cell we want to find.
2784 *
2785 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002786 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002787 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002788 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002789 }
2790
2791 /**
2792 * Like above, but ignores any cells occupied by the item "ignoreView"
2793 *
2794 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2795 * can be found.
2796 * @param spanX The horizontal span of the cell we want to find.
2797 * @param spanY The vertical span of the cell we want to find.
2798 * @param ignoreView The home screen item we should treat as not occupying any space
2799 * @return
2800 */
2801 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002802 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2803 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002804 }
2805
2806 /**
2807 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2808 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2809 *
2810 * @param spanX The horizontal span of the cell we want to find.
2811 * @param spanY The vertical span of the cell we want to find.
2812 * @param ignoreView The home screen item we should treat as not occupying any space
2813 * @param intersectX The X coordinate of the cell that we should try to overlap
2814 * @param intersectX The Y coordinate of the cell that we should try to overlap
2815 *
2816 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2817 */
2818 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2819 int intersectX, int intersectY) {
2820 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002821 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002822 }
2823
2824 /**
2825 * The superset of the above two methods
2826 */
2827 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002828 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002829 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002830 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002831
Michael Jurka28750fb2010-09-24 17:43:49 -07002832 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002833 while (true) {
2834 int startX = 0;
2835 if (intersectX >= 0) {
2836 startX = Math.max(startX, intersectX - (spanX - 1));
2837 }
2838 int endX = mCountX - (spanX - 1);
2839 if (intersectX >= 0) {
2840 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2841 }
2842 int startY = 0;
2843 if (intersectY >= 0) {
2844 startY = Math.max(startY, intersectY - (spanY - 1));
2845 }
2846 int endY = mCountY - (spanY - 1);
2847 if (intersectY >= 0) {
2848 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2849 }
2850
Winson Chungbbc60d82010-11-11 16:34:41 -08002851 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002852 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002853 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002854 for (int i = 0; i < spanX; i++) {
2855 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002856 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002857 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002858 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002859 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002860 continue inner;
2861 }
2862 }
2863 }
2864 if (cellXY != null) {
2865 cellXY[0] = x;
2866 cellXY[1] = y;
2867 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002868 foundCell = true;
2869 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002870 }
2871 }
2872 if (intersectX == -1 && intersectY == -1) {
2873 break;
2874 } else {
2875 // if we failed to find anything, try again but without any requirements of
2876 // intersecting
2877 intersectX = -1;
2878 intersectY = -1;
2879 continue;
2880 }
2881 }
2882
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002883 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002884 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002885 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002886 }
2887
2888 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002889 * A drag event has begun over this layout.
2890 * It may have begun over this layout (in which case onDragChild is called first),
2891 * or it may have begun on another layout.
2892 */
2893 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002894 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002895 mDragging = true;
2896 }
2897
2898 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002899 * Called when drag has left this CellLayout or has been completed (successfully or not)
2900 */
2901 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002902 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002903 // This can actually be called when we aren't in a drag, e.g. when adding a new
2904 // item to this layout via the customize drawer.
2905 // Guard against that case.
2906 if (mDragging) {
2907 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002908 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002909
2910 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002911 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002912 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2913 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002914 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002915 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002916 }
2917
2918 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002919 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002920 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002921 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 *
2923 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002924 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002925 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002926 if (child != null) {
2927 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002928 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002929 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 }
2932
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002933 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002935 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002936 * @param cellX X coordinate of upper left corner expressed as a cell position
2937 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002938 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002940 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002941 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002942 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002943 final int cellWidth = mCellWidth;
2944 final int cellHeight = mCellHeight;
2945 final int widthGap = mWidthGap;
2946 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002947
Winson Chung4b825dcd2011-06-19 12:41:22 -07002948 final int hStartPadding = getPaddingLeft();
2949 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002950
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002951 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2952 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2953
2954 int x = hStartPadding + cellX * (cellWidth + widthGap);
2955 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002956
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002957 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002958 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002959
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002960 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002961 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002963 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002964 * @param width Width in pixels
2965 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002966 * @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 -08002967 */
Winson Chung66700732013-08-20 16:56:15 -07002968 public static int[] rectToCell(int width, int height, int[] result) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002969 LauncherAppState app = LauncherAppState.getInstance();
2970 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung66700732013-08-20 16:56:15 -07002971 Rect padding = grid.getWorkspacePadding(grid.isLandscape ?
2972 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
Winson Chung5f8afe62013-08-12 16:19:28 -07002973
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 // Always assume we're working with the smallest span to make sure we
2975 // reserve enough space in both orientations.
Winson Chung66700732013-08-20 16:56:15 -07002976 int parentWidth = grid.calculateCellWidth(grid.widthPx
2977 - padding.left - padding.right, (int) grid.numColumns);
2978 int parentHeight = grid.calculateCellHeight(grid.heightPx
2979 - padding.top - padding.bottom, (int) grid.numRows);
2980 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002981
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002982 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002983 int spanX = (int) Math.ceil(width / (float) smallerSize);
2984 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002985
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002986 if (result == null) {
2987 return new int[] { spanX, spanY };
2988 }
2989 result[0] = spanX;
2990 result[1] = spanY;
2991 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002992 }
2993
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002994 public int[] cellSpansToSize(int hSpans, int vSpans) {
2995 int[] size = new int[2];
2996 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2997 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2998 return size;
2999 }
3000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003001 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08003002 * Calculate the grid spans needed to fit given item
3003 */
3004 public void calculateSpans(ItemInfo info) {
3005 final int minWidth;
3006 final int minHeight;
3007
3008 if (info instanceof LauncherAppWidgetInfo) {
3009 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3010 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3011 } else if (info instanceof PendingAddWidgetInfo) {
3012 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3013 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3014 } else {
3015 // It's not a widget, so it must be 1x1
3016 info.spanX = info.spanY = 1;
3017 return;
3018 }
3019 int[] spans = rectToCell(minWidth, minHeight, null);
3020 info.spanX = spans[0];
3021 info.spanY = spans[1];
3022 }
3023
3024 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003025 * Find the first vacant cell, if there is one.
3026 *
3027 * @param vacant Holds the x and y coordinate of the vacant cell
3028 * @param spanX Horizontal cell span.
3029 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003030 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003031 * @return True if a vacant cell was found
3032 */
3033 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034
Michael Jurka0280c3b2010-09-17 15:00:07 -07003035 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003036 }
3037
3038 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3039 int xCount, int yCount, boolean[][] occupied) {
3040
Adam Cohen2801caf2011-05-13 20:57:39 -07003041 for (int y = 0; y < yCount; y++) {
3042 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003043 boolean available = !occupied[x][y];
3044out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3045 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3046 available = available && !occupied[i][j];
3047 if (!available) break out;
3048 }
3049 }
3050
3051 if (available) {
3052 vacant[0] = x;
3053 vacant[1] = y;
3054 return true;
3055 }
3056 }
3057 }
3058
3059 return false;
3060 }
3061
Michael Jurka0280c3b2010-09-17 15:00:07 -07003062 private void clearOccupiedCells() {
3063 for (int x = 0; x < mCountX; x++) {
3064 for (int y = 0; y < mCountY; y++) {
3065 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003066 }
3067 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003069
Adam Cohend41fbf52012-02-16 23:53:59 -08003070 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003071 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003072 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003074
Adam Cohend4844c32011-02-18 19:25:06 -08003075 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003076 markCellsAsOccupiedForView(view, mOccupied);
3077 }
3078 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003079 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003080 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003081 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003082 }
3083
Adam Cohend4844c32011-02-18 19:25:06 -08003084 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003085 markCellsAsUnoccupiedForView(view, mOccupied);
3086 }
3087 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003088 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003089 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003090 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003091 }
3092
Adam Cohen482ed822012-03-02 14:15:13 -08003093 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3094 boolean value) {
3095 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003096 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3097 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003098 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099 }
3100 }
3101 }
3102
Adam Cohen2801caf2011-05-13 20:57:39 -07003103 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003104 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003105 (Math.max((mCountX - 1), 0) * mWidthGap);
3106 }
3107
3108 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003109 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003110 (Math.max((mCountY - 1), 0) * mHeightGap);
3111 }
3112
Michael Jurka66d72172011-04-12 16:29:25 -07003113 public boolean isOccupied(int x, int y) {
3114 if (x < mCountX && y < mCountY) {
3115 return mOccupied[x][y];
3116 } else {
3117 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3118 }
3119 }
3120
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003121 @Override
3122 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3123 return new CellLayout.LayoutParams(getContext(), attrs);
3124 }
3125
3126 @Override
3127 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3128 return p instanceof CellLayout.LayoutParams;
3129 }
3130
3131 @Override
3132 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3133 return new CellLayout.LayoutParams(p);
3134 }
3135
Winson Chungaafa03c2010-06-11 17:34:16 -07003136 public static class CellLayoutAnimationController extends LayoutAnimationController {
3137 public CellLayoutAnimationController(Animation animation, float delay) {
3138 super(animation, delay);
3139 }
3140
3141 @Override
3142 protected long getDelayForView(View view) {
3143 return (int) (Math.random() * 150);
3144 }
3145 }
3146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3148 /**
3149 * Horizontal location of the item in the grid.
3150 */
3151 @ViewDebug.ExportedProperty
3152 public int cellX;
3153
3154 /**
3155 * Vertical location of the item in the grid.
3156 */
3157 @ViewDebug.ExportedProperty
3158 public int cellY;
3159
3160 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003161 * Temporary horizontal location of the item in the grid during reorder
3162 */
3163 public int tmpCellX;
3164
3165 /**
3166 * Temporary vertical location of the item in the grid during reorder
3167 */
3168 public int tmpCellY;
3169
3170 /**
3171 * Indicates that the temporary coordinates should be used to layout the items
3172 */
3173 public boolean useTmpCoords;
3174
3175 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003176 * Number of cells spanned horizontally by the item.
3177 */
3178 @ViewDebug.ExportedProperty
3179 public int cellHSpan;
3180
3181 /**
3182 * Number of cells spanned vertically by the item.
3183 */
3184 @ViewDebug.ExportedProperty
3185 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003186
Adam Cohen1b607ed2011-03-03 17:26:50 -08003187 /**
3188 * Indicates whether the item will set its x, y, width and height parameters freely,
3189 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3190 */
Adam Cohend4844c32011-02-18 19:25:06 -08003191 public boolean isLockedToGrid = true;
3192
Adam Cohen482ed822012-03-02 14:15:13 -08003193 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07003194 * Indicates that this item should use the full extents of its parent.
3195 */
3196 public boolean isFullscreen = false;
3197
3198 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003199 * Indicates whether this item can be reordered. Always true except in the case of the
3200 * the AllApps button.
3201 */
3202 public boolean canReorder = true;
3203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003204 // X coordinate of the view in the layout.
3205 @ViewDebug.ExportedProperty
3206 int x;
3207 // Y coordinate of the view in the layout.
3208 @ViewDebug.ExportedProperty
3209 int y;
3210
Romain Guy84f296c2009-11-04 15:00:44 -08003211 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213 public LayoutParams(Context c, AttributeSet attrs) {
3214 super(c, attrs);
3215 cellHSpan = 1;
3216 cellVSpan = 1;
3217 }
3218
3219 public LayoutParams(ViewGroup.LayoutParams source) {
3220 super(source);
3221 cellHSpan = 1;
3222 cellVSpan = 1;
3223 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003224
3225 public LayoutParams(LayoutParams source) {
3226 super(source);
3227 this.cellX = source.cellX;
3228 this.cellY = source.cellY;
3229 this.cellHSpan = source.cellHSpan;
3230 this.cellVSpan = source.cellVSpan;
3231 }
3232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003233 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003234 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003235 this.cellX = cellX;
3236 this.cellY = cellY;
3237 this.cellHSpan = cellHSpan;
3238 this.cellVSpan = cellVSpan;
3239 }
3240
Adam Cohen2374abf2013-04-16 14:56:57 -07003241 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3242 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003243 if (isLockedToGrid) {
3244 final int myCellHSpan = cellHSpan;
3245 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003246 int myCellX = useTmpCoords ? tmpCellX : cellX;
3247 int myCellY = useTmpCoords ? tmpCellY : cellY;
3248
3249 if (invertHorizontally) {
3250 myCellX = colCount - myCellX - cellHSpan;
3251 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003252
Adam Cohend4844c32011-02-18 19:25:06 -08003253 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3254 leftMargin - rightMargin;
3255 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3256 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003257 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3258 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003259 }
3260 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003261
Winson Chungaafa03c2010-06-11 17:34:16 -07003262 public String toString() {
3263 return "(" + this.cellX + ", " + this.cellY + ")";
3264 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003265
3266 public void setWidth(int width) {
3267 this.width = width;
3268 }
3269
3270 public int getWidth() {
3271 return width;
3272 }
3273
3274 public void setHeight(int height) {
3275 this.height = height;
3276 }
3277
3278 public int getHeight() {
3279 return height;
3280 }
3281
3282 public void setX(int x) {
3283 this.x = x;
3284 }
3285
3286 public int getX() {
3287 return x;
3288 }
3289
3290 public void setY(int y) {
3291 this.y = y;
3292 }
3293
3294 public int getY() {
3295 return y;
3296 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003297 }
3298
Michael Jurka0280c3b2010-09-17 15:00:07 -07003299 // This class stores info for two purposes:
3300 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3301 // its spanX, spanY, and the screen it is on
3302 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3303 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3304 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003305 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003306 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003307 int cellX = -1;
3308 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003309 int spanX;
3310 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003311 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003312 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003313
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003314 @Override
3315 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003316 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3317 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003318 }
3319 }
Michael Jurkad771c962011-08-09 15:00:48 -07003320
3321 public boolean lastDownOnOccupiedCell() {
3322 return mLastDownOnOccupiedCell;
3323 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003324}