blob: ff7bcc43b745b964d062d027f3d169f8b42daba5 [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;
Adam Cohenc9735cf2015-01-23 16:11:55 -080025import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040027import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070029import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080031import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070033import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080035import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070036import android.graphics.drawable.Drawable;
Sunny Goyal2805e632015-05-20 15:35:32 -070037import android.graphics.drawable.TransitionDrawable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080038import android.os.Build;
Adam Cohen1462de32012-07-24 22:34:36 -070039import android.os.Parcelable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080040import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070042import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070043import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.view.MotionEvent;
45import android.view.View;
46import android.view.ViewDebug;
47import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080048import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070049import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Sunny Goyal4b6eb262015-05-14 19:24:40 -070051import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.FolderIcon.FolderRingAnimator;
Sunny Goyale9b651e2015-04-24 11:44:51 -070053import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
54import com.android.launcher3.accessibility.FolderAccessibilityHelper;
55import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Adam Cohen091440a2015-03-18 14:16:05 -070056import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -070057import com.android.launcher3.widget.PendingAddWidgetInfo;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070058
Adam Cohen69ce2e52011-07-03 19:25:21 -070059import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070060import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080061import java.util.Collections;
62import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070063import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080064import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070065
Sunny Goyal4b6eb262015-05-14 19:24:40 -070066public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070067 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
68 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
69
Winson Chungaafa03c2010-06-11 17:34:16 -070070 static final String TAG = "CellLayout";
71
Adam Cohen2acce882012-03-28 19:03:19 -070072 private Launcher mLauncher;
Adam Cohen091440a2015-03-18 14:16:05 -070073 @Thunk int mCellWidth;
74 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070075 private int mFixedCellWidth;
76 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070077
Adam Cohen091440a2015-03-18 14:16:05 -070078 @Thunk int mCountX;
79 @Thunk int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
Adam Cohen234c4cd2011-07-17 21:03:04 -070081 private int mOriginalWidthGap;
82 private int mOriginalHeightGap;
Adam Cohen091440a2015-03-18 14:16:05 -070083 @Thunk int mWidthGap;
84 @Thunk int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070085 private int mMaxGap;
Adam Cohen917e3882013-10-31 15:03:35 -070086 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070087 private boolean mIsDragTarget = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Patrick Dubroyde7658b2010-09-27 11:15:43 -070089 // These are temporary variables to prevent having to allocate a new object just to
90 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070091 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070092 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080095 boolean[][] mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096
Michael Jurkadee05892010-07-27 10:01:56 -070097 private OnTouchListener mInterceptTouchListener;
98
Adam Cohen69ce2e52011-07-03 19:25:21 -070099 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -0700100 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -0700101
Sunny Goyal2805e632015-05-20 15:35:32 -0700102 private static final float FOREGROUND_ALPHA_DAMPER = 0.65f;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700103 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700104 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700105
Sunny Goyal2805e632015-05-20 15:35:32 -0700106 private static final int BACKGROUND_ACTIVATE_DURATION = 120;
107 private final TransitionDrawable mBackground;
108
109 private final Drawable mOverScrollLeft;
110 private final Drawable mOverScrollRight;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700111 private Drawable mOverScrollForegroundDrawable;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700112
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700113 // These values allow a fixed measurement to be set on the CellLayout.
114 private int mFixedWidth = -1;
115 private int mFixedHeight = -1;
116
Michael Jurka33945b22010-12-21 18:19:38 -0800117 // If we're actively dragging something over this screen, mIsDragOverlapping is true
118 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700119
Winson Chung150fbab2010-09-29 17:14:26 -0700120 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700121 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk Rect[] mDragOutlines = new Rect[4];
123 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700124 private InterruptibleInOutAnimator[] mDragOutlineAnims =
125 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700126
127 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700128 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700129 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700130
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700131 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800132
Sunny Goyal316490e2015-06-02 09:38:28 -0700133 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
134 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700135
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 Cohenfa3c58f2013-12-06 16:10:55 -0800149 public static final int MODE_SHOW_REORDER_HINT = 0;
150 public static final int MODE_DRAG_OVER = 1;
151 public static final int MODE_ON_DROP = 2;
152 public static final int MODE_ON_DROP_EXTERNAL = 3;
153 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700154 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800155 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
156
Adam Cohena897f392012-04-27 18:12:05 -0700157 static final int LANDSCAPE = 0;
158 static final int PORTRAIT = 1;
159
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800160 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700161 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohen091440a2015-03-18 14:16:05 -0700162 @Thunk float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700163
Adam Cohen482ed822012-03-02 14:15:13 -0800164 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
165 private Rect mOccupiedRect = new Rect();
166 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700167 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700168 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800169
Sunny Goyal2805e632015-05-20 15:35:32 -0700170 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700171
Michael Jurkaca993832012-06-29 15:17:04 -0700172 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700173
Adam Cohenc9735cf2015-01-23 16:11:55 -0800174 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700175 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800176 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 public CellLayout(Context context) {
179 this(context, null);
180 }
181
182 public CellLayout(Context context, AttributeSet attrs) {
183 this(context, attrs, 0);
184 }
185
186 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
187 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700188
189 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
190 // the user where a dragged item will land when dropped.
191 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800192 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700193 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700194
Adam Cohen2e6da152015-05-06 11:42:25 -0700195 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
197
Winson Chung11a1a532013-09-13 11:14:45 -0700198 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800199 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700200 mWidthGap = mOriginalWidthGap = 0;
201 mHeightGap = mOriginalHeightGap = 0;
202 mMaxGap = Integer.MAX_VALUE;
Adam Cohen2e6da152015-05-06 11:42:25 -0700203 mCountX = (int) grid.inv.numColumns;
204 mCountY = (int) grid.inv.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700205 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800206 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700207 mPreviousReorderDirection[0] = INVALID_DIRECTION;
208 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
210 a.recycle();
211
212 setAlwaysDrawnWithCacheEnabled(false);
213
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700214 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700215 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700216
Sunny Goyal2805e632015-05-20 15:35:32 -0700217 mBackground = (TransitionDrawable) res.getDrawable(R.drawable.bg_screenpanel);
218 mBackground.setCallback(this);
Michael Jurka33945b22010-12-21 18:19:38 -0800219
Adam Cohenb5ba0972011-09-07 18:02:31 -0700220 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
221 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
Michael Jurka33945b22010-12-21 18:19:38 -0800222
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800223 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700224 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700225
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700226 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700227 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700228 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700229 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800230 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700231 }
232
233 // When dragging things around the home screens, we show a green outline of
234 // where the item will land. The outlines gradually fade out, leaving a trail
235 // behind the drag path.
236 // Set up all the animations that are used to implement this fading.
237 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700238 final float fromAlphaValue = 0;
239 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700240
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700241 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700242
243 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700244 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100245 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700246 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700247 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700248 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700249 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700250 final Bitmap outline = (Bitmap)anim.getTag();
251
252 // If an animation is started and then stopped very quickly, we can still
253 // get spurious updates we've cleared the tag. Guard against this.
254 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700255 @SuppressWarnings("all") // suppress dead code warning
256 final boolean debug = false;
257 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700258 Object val = animation.getAnimatedValue();
259 Log.d(TAG, "anim " + thisIndex + " update: " + val +
260 ", isStopped " + anim.isStopped());
261 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700262 // Try to prevent it from continuing to run
263 animation.cancel();
264 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700265 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800266 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700267 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700268 }
269 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700270 // The animation holds a reference to the drag outline bitmap as long is it's
271 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700272 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700273 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700274 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700275 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700276 anim.setTag(null);
277 }
278 }
279 });
280 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700281 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700282
Michael Jurkaa52570f2012-03-20 03:18:20 -0700283 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700284 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700285 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700286
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700287 mTouchFeedbackView = new ClickShadowView(context);
288 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700289 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700290 }
291
Adam Cohenc9735cf2015-01-23 16:11:55 -0800292 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700293 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800294 mUseTouchHelper = enable;
295 if (!enable) {
296 ViewCompat.setAccessibilityDelegate(this, null);
297 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
298 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
299 setOnClickListener(mLauncher);
300 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700301 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
302 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
303 mTouchHelper = new WorkspaceAccessibilityHelper(this);
304 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
305 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
306 mTouchHelper = new FolderAccessibilityHelper(this);
307 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800308 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
309 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
310 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
311 setOnClickListener(mTouchHelper);
312 }
313
314 // Invalidate the accessibility hierarchy
315 if (getParent() != null) {
316 getParent().notifySubtreeAccessibilityStateChanged(
317 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
318 }
319 }
320
321 @Override
322 public boolean dispatchHoverEvent(MotionEvent event) {
323 // Always attempt to dispatch hover events to accessibility first.
324 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
325 return true;
326 }
327 return super.dispatchHoverEvent(event);
328 }
329
330 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800331 public boolean onInterceptTouchEvent(MotionEvent ev) {
332 if (mUseTouchHelper ||
333 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
334 return true;
335 }
336 return false;
337 }
338
Chris Craik01f2d7f2013-10-01 14:41:56 -0700339 public void enableHardwareLayer(boolean hasLayer) {
340 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700341 }
342
343 public void buildHardwareLayer() {
344 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700345 }
346
Adam Cohen307fe232012-08-16 17:55:58 -0700347 public float getChildrenScale() {
348 return mIsHotseat ? mHotseatScale : 1.0f;
349 }
350
Winson Chung5f8afe62013-08-12 16:19:28 -0700351 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700352 mFixedCellWidth = mCellWidth = width;
353 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700354 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
355 mCountX, mCountY);
356 }
357
Adam Cohen2801caf2011-05-13 20:57:39 -0700358 public void setGridSize(int x, int y) {
359 mCountX = x;
360 mCountY = y;
361 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800362 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700363 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700364 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700365 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700366 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700367 }
368
Adam Cohen2374abf2013-04-16 14:56:57 -0700369 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
370 public void setInvertIfRtl(boolean invert) {
371 mShortcutsAndWidgets.setInvertIfRtl(invert);
372 }
373
Adam Cohen917e3882013-10-31 15:03:35 -0700374 public void setDropPending(boolean pending) {
375 mDropPending = pending;
376 }
377
378 public boolean isDropPending() {
379 return mDropPending;
380 }
381
Adam Cohenb5ba0972011-09-07 18:02:31 -0700382 void setOverScrollAmount(float r, boolean left) {
383 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
384 mOverScrollForegroundDrawable = mOverScrollLeft;
385 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
386 mOverScrollForegroundDrawable = mOverScrollRight;
387 }
388
Adam Cohen02dcfcc2013-10-01 12:37:33 -0700389 r *= FOREGROUND_ALPHA_DAMPER;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700390 mForegroundAlpha = (int) Math.round((r * 255));
391 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
392 invalidate();
393 }
394
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700395 @Override
396 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700397 if (icon == null || background == null) {
398 mTouchFeedbackView.setBitmap(null);
399 mTouchFeedbackView.animate().cancel();
400 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700401 if (mTouchFeedbackView.setBitmap(background)) {
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700402 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets);
403 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700404 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800405 }
406 }
407
Adam Cohenc50438c2014-08-19 17:43:05 -0700408 void disableDragTarget() {
409 mIsDragTarget = false;
410 }
411
412 boolean isDragTarget() {
413 return mIsDragTarget;
414 }
415
416 void setIsDragOverlapping(boolean isDragOverlapping) {
417 if (mIsDragOverlapping != isDragOverlapping) {
418 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700419 if (mIsDragOverlapping) {
420 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
421 } else {
422 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
423 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700424 invalidate();
425 }
426 }
427
Michael Jurka33945b22010-12-21 18:19:38 -0800428 boolean getIsDragOverlapping() {
429 return mIsDragOverlapping;
430 }
431
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700432 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700433 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700434 if (!mIsDragTarget) {
435 return;
436 }
437
Michael Jurka3e7c7632010-10-02 16:01:03 -0700438 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
439 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
440 // When we're small, we are either drawn normally or in the "accepts drops" state (during
441 // a drag). However, we also drag the mini hover background *over* one of those two
442 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700443 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700444 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700445 }
Romain Guya6abce82009-11-10 02:54:41 -0800446
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700447 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700448 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700449 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700450 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800451 final Rect r = mDragOutlines[i];
Winson Chung3a6e7f32013-10-09 15:50:52 -0700452 mTempRect.set(r);
453 Utilities.scaleRectAboutCenter(mTempRect, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700454 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700455 paint.setAlpha((int)(alpha + .5f));
Winson Chung3a6e7f32013-10-09 15:50:52 -0700456 canvas.drawBitmap(b, null, mTempRect, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700457 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700458 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800459
Adam Cohen482ed822012-03-02 14:15:13 -0800460 if (DEBUG_VISUALIZE_OCCUPIED) {
461 int[] pt = new int[2];
462 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700463 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800464 for (int i = 0; i < mCountX; i++) {
465 for (int j = 0; j < mCountY; j++) {
466 if (mOccupied[i][j]) {
467 cellToPoint(i, j, pt);
468 canvas.save();
469 canvas.translate(pt[0], pt[1]);
470 cd.draw(canvas);
471 canvas.restore();
472 }
473 }
474 }
475 }
476
Andrew Flynn850d2e72012-04-26 16:51:20 -0700477 int previewOffset = FolderRingAnimator.sPreviewSize;
478
Adam Cohen69ce2e52011-07-03 19:25:21 -0700479 // The folder outer / inner ring image(s)
Adam Cohen2e6da152015-05-06 11:42:25 -0700480 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700481 for (int i = 0; i < mFolderOuterRings.size(); i++) {
482 FolderRingAnimator fra = mFolderOuterRings.get(i);
483
Adam Cohen5108bc02013-09-20 17:04:51 -0700484 Drawable d;
485 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700486 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700487 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700488
Winson Chung89f97052013-09-20 11:32:26 -0700489 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700490 int centerX = mTempLocation[0] + mCellWidth / 2;
491 int centerY = mTempLocation[1] + previewOffset / 2 +
492 child.getPaddingTop() + grid.folderBackgroundOffset;
493
Adam Cohen5108bc02013-09-20 17:04:51 -0700494 // Draw outer ring, if it exists
495 if (FolderIcon.HAS_OUTER_RING) {
496 d = FolderRingAnimator.sSharedOuterRingDrawable;
497 width = (int) (fra.getOuterRingSize() * getChildrenScale());
498 height = width;
499 canvas.save();
500 canvas.translate(centerX - width / 2, centerY - height / 2);
501 d.setBounds(0, 0, width, height);
502 d.draw(canvas);
503 canvas.restore();
504 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700505
Winson Chung89f97052013-09-20 11:32:26 -0700506 // Draw inner ring
507 d = FolderRingAnimator.sSharedInnerRingDrawable;
508 width = (int) (fra.getInnerRingSize() * getChildrenScale());
509 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700510 canvas.save();
511 canvas.translate(centerX - width / 2, centerY - width / 2);
512 d.setBounds(0, 0, width, height);
513 d.draw(canvas);
514 canvas.restore();
515 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700516 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700517
518 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
519 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
520 int width = d.getIntrinsicWidth();
521 int height = d.getIntrinsicHeight();
522
523 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700524 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700525 if (child != null) {
526 int centerX = mTempLocation[0] + mCellWidth / 2;
527 int centerY = mTempLocation[1] + previewOffset / 2 +
528 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700529
Winson Chung89f97052013-09-20 11:32:26 -0700530 canvas.save();
531 canvas.translate(centerX - width / 2, centerY - width / 2);
532 d.setBounds(0, 0, width, height);
533 d.draw(canvas);
534 canvas.restore();
535 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700536 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700537 }
538
Adam Cohenb5ba0972011-09-07 18:02:31 -0700539 @Override
540 protected void dispatchDraw(Canvas canvas) {
541 super.dispatchDraw(canvas);
542 if (mForegroundAlpha > 0) {
Adam Cohenb5ba0972011-09-07 18:02:31 -0700543 mOverScrollForegroundDrawable.draw(canvas);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700544 }
545 }
546
Adam Cohen69ce2e52011-07-03 19:25:21 -0700547 public void showFolderAccept(FolderRingAnimator fra) {
548 mFolderOuterRings.add(fra);
549 }
550
551 public void hideFolderAccept(FolderRingAnimator fra) {
552 if (mFolderOuterRings.contains(fra)) {
553 mFolderOuterRings.remove(fra);
554 }
555 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700556 }
557
Adam Cohenc51934b2011-07-26 21:07:43 -0700558 public void setFolderLeaveBehindCell(int x, int y) {
559 mFolderLeaveBehindCell[0] = x;
560 mFolderLeaveBehindCell[1] = y;
561 invalidate();
562 }
563
564 public void clearFolderLeaveBehind() {
565 mFolderLeaveBehindCell[0] = -1;
566 mFolderLeaveBehindCell[1] = -1;
567 invalidate();
568 }
569
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700570 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700571 public boolean shouldDelayChildPressedState() {
572 return false;
573 }
574
Adam Cohen1462de32012-07-24 22:34:36 -0700575 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700576 try {
577 dispatchRestoreInstanceState(states);
578 } catch (IllegalArgumentException ex) {
579 if (LauncherAppState.isDogfoodBuild()) {
580 throw ex;
581 }
582 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
583 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
584 }
Adam Cohen1462de32012-07-24 22:34:36 -0700585 }
586
Michael Jurkae6235dd2011-10-04 15:02:05 -0700587 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700588 public void cancelLongPress() {
589 super.cancelLongPress();
590
591 // Cancel long press for all children
592 final int count = getChildCount();
593 for (int i = 0; i < count; i++) {
594 final View child = getChildAt(i);
595 child.cancelLongPress();
596 }
597 }
598
Michael Jurkadee05892010-07-27 10:01:56 -0700599 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
600 mInterceptTouchListener = listener;
601 }
602
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800603 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700604 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 }
606
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800607 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700608 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 }
610
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800611 public void setIsHotseat(boolean isHotseat) {
612 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700613 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800614 }
615
Sunny Goyale9b651e2015-04-24 11:44:51 -0700616 public boolean isHotseat() {
617 return mIsHotseat;
618 }
619
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800620 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700621 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700622 final LayoutParams lp = params;
623
Andrew Flynnde38e422012-05-08 11:22:15 -0700624 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800625 if (child instanceof BubbleTextView) {
626 BubbleTextView bubbleChild = (BubbleTextView) child;
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);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700642 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700643
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700644 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700645
Winson Chungaafa03c2010-06-11 17:34:16 -0700646 return true;
647 }
648 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700650
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700652 public void removeAllViews() {
653 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700654 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700655 }
656
657 @Override
658 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700659 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700660 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700661 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700662 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700663 }
664
665 @Override
666 public void removeView(View view) {
667 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700668 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700669 }
670
671 @Override
672 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700673 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
674 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700675 }
676
677 @Override
678 public void removeViewInLayout(View view) {
679 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700680 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700681 }
682
683 @Override
684 public void removeViews(int start, int count) {
685 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700686 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700687 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700688 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700689 }
690
691 @Override
692 public void removeViewsInLayout(int start, int count) {
693 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700694 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700695 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700696 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800697 }
698
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700699 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700700 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 * @param x X coordinate of the point
702 * @param y Y coordinate of the point
703 * @param result Array of 2 ints to hold the x and y coordinate of the cell
704 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700705 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700706 final int hStartPadding = getPaddingLeft();
707 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708
709 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
710 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
711
Adam Cohend22015c2010-07-26 22:02:18 -0700712 final int xAxis = mCountX;
713 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714
715 if (result[0] < 0) result[0] = 0;
716 if (result[0] >= xAxis) result[0] = xAxis - 1;
717 if (result[1] < 0) result[1] = 0;
718 if (result[1] >= yAxis) result[1] = yAxis - 1;
719 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700720
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 /**
722 * Given a point, return the cell that most closely encloses that point
723 * @param x X coordinate of the point
724 * @param y Y coordinate of the point
725 * @param result Array of 2 ints to hold the x and y coordinate of the cell
726 */
727 void pointToCellRounded(int x, int y, int[] result) {
728 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
729 }
730
731 /**
732 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700733 *
734 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700736 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 * @param result Array of 2 ints to hold the x and y coordinate of the point
738 */
739 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700740 final int hStartPadding = getPaddingLeft();
741 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742
743 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
744 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
745 }
746
Adam Cohene3e27a82011-04-15 12:07:39 -0700747 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800748 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700749 *
750 * @param cellX X coordinate of the cell
751 * @param cellY Y coordinate of the cell
752 *
753 * @param result Array of 2 ints to hold the x and y coordinate of the point
754 */
755 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700756 regionToCenterPoint(cellX, cellY, 1, 1, result);
757 }
758
759 /**
760 * Given a cell coordinate and span return the point that represents the center of the regio
761 *
762 * @param cellX X coordinate of the cell
763 * @param cellY Y coordinate of the cell
764 *
765 * @param result Array of 2 ints to hold the x and y coordinate of the point
766 */
767 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700768 final int hStartPadding = getPaddingLeft();
769 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700770 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
771 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
772 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
773 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700774 }
775
Adam Cohen19f37922012-03-21 11:59:11 -0700776 /**
777 * Given a cell coordinate and span fills out a corresponding pixel rect
778 *
779 * @param cellX X coordinate of the cell
780 * @param cellY Y coordinate of the cell
781 * @param result Rect in which to write the result
782 */
783 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
784 final int hStartPadding = getPaddingLeft();
785 final int vStartPadding = getPaddingTop();
786 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
787 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
788 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
789 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
790 }
791
Adam Cohen482ed822012-03-02 14:15:13 -0800792 public float getDistanceFromCell(float x, float y, int[] cell) {
793 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700794 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800795 }
796
Romain Guy84f296c2009-11-04 15:00:44 -0800797 int getCellWidth() {
798 return mCellWidth;
799 }
800
801 int getCellHeight() {
802 return mCellHeight;
803 }
804
Adam Cohend4844c32011-02-18 19:25:06 -0800805 int getWidthGap() {
806 return mWidthGap;
807 }
808
809 int getHeightGap() {
810 return mHeightGap;
811 }
812
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700813 public void setFixedSize(int width, int height) {
814 mFixedWidth = width;
815 mFixedHeight = height;
816 }
817
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 @Override
819 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700822 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
823 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700824 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
825 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700826 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700827 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
828 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700829 if (cw != mCellWidth || ch != mCellHeight) {
830 mCellWidth = cw;
831 mCellHeight = ch;
832 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
833 mHeightGap, mCountX, mCountY);
834 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700835 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700836
Winson Chung2d75f122013-09-23 16:53:31 -0700837 int newWidth = childWidthSize;
838 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700839 if (mFixedWidth > 0 && mFixedHeight > 0) {
840 newWidth = mFixedWidth;
841 newHeight = mFixedHeight;
842 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
844 }
845
Adam Cohend22015c2010-07-26 22:02:18 -0700846 int numWidthGaps = mCountX - 1;
847 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848
Adam Cohen234c4cd2011-07-17 21:03:04 -0700849 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700850 int hSpace = childWidthSize;
851 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700852 int hFreeSpace = hSpace - (mCountX * mCellWidth);
853 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700854 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
855 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700856 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
857 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700858 } else {
859 mWidthGap = mOriginalWidthGap;
860 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700861 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700862
863 // Make the feedback view large enough to hold the blur bitmap.
864 mTouchFeedbackView.measure(
865 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
866 MeasureSpec.EXACTLY),
867 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
868 MeasureSpec.EXACTLY));
869
870 mShortcutsAndWidgets.measure(
871 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
872 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
873
874 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
875 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700876 if (mFixedWidth > 0 && mFixedHeight > 0) {
877 setMeasuredDimension(maxWidth, maxHeight);
878 } else {
879 setMeasuredDimension(widthSize, heightSize);
880 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 }
882
883 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700884 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -0700885 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
886 (mCountX * mCellWidth);
887 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
888 int top = getPaddingTop();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700889
890 mTouchFeedbackView.layout(left, top,
891 left + mTouchFeedbackView.getMeasuredWidth(),
892 top + mTouchFeedbackView.getMeasuredHeight());
893 mShortcutsAndWidgets.layout(left, top,
894 left + r - l,
895 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 }
897
898 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700899 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
900 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -0700901
902 // Expand the background drawing bounds by the padding baked into the background drawable
Sunny Goyal2805e632015-05-20 15:35:32 -0700903 mBackground.getPadding(mTempRect);
904 mBackground.setBounds(-mTempRect.left, -mTempRect.top,
905 w + mTempRect.right, h + mTempRect.bottom);
Winson Chung82a9bd22013-10-08 16:02:34 -0700906
Sunny Goyal2805e632015-05-20 15:35:32 -0700907 mOverScrollLeft.setBounds(0, 0, w, h);
908 mOverScrollRight.setBounds(0, 0, w, h);
Michael Jurkadee05892010-07-27 10:01:56 -0700909 }
910
911 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700913 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 }
915
916 @Override
917 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700918 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 }
920
Michael Jurka5f1c5092010-09-03 14:15:02 -0700921 public float getBackgroundAlpha() {
922 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700923 }
924
Michael Jurka5f1c5092010-09-03 14:15:02 -0700925 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800926 if (mBackgroundAlpha != alpha) {
927 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700928 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800929 }
Michael Jurkadee05892010-07-27 10:01:56 -0700930 }
931
Sunny Goyal2805e632015-05-20 15:35:32 -0700932 @Override
933 protected boolean verifyDrawable(Drawable who) {
934 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
935 }
936
Michael Jurkaa52570f2012-03-20 03:18:20 -0700937 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700938 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700939 }
940
Michael Jurkaa52570f2012-03-20 03:18:20 -0700941 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700942 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700943 }
944
Patrick Dubroy440c3602010-07-13 17:50:32 -0700945 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700946 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700947 }
948
Adam Cohen76fc0852011-06-17 13:26:23 -0700949 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800950 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700951 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800952 boolean[][] occupied = mOccupied;
953 if (!permanent) {
954 occupied = mTmpOccupied;
955 }
956
Adam Cohen19f37922012-03-21 11:59:11 -0700957 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700958 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
959 final ItemInfo info = (ItemInfo) child.getTag();
960
961 // We cancel any existing animations
962 if (mReorderAnimators.containsKey(lp)) {
963 mReorderAnimators.get(lp).cancel();
964 mReorderAnimators.remove(lp);
965 }
966
Adam Cohen482ed822012-03-02 14:15:13 -0800967 final int oldX = lp.x;
968 final int oldY = lp.y;
969 if (adjustOccupied) {
970 occupied[lp.cellX][lp.cellY] = false;
971 occupied[cellX][cellY] = true;
972 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700973 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800974 if (permanent) {
975 lp.cellX = info.cellX = cellX;
976 lp.cellY = info.cellY = cellY;
977 } else {
978 lp.tmpCellX = cellX;
979 lp.tmpCellY = cellY;
980 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700981 clc.setupLp(lp);
982 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800983 final int newX = lp.x;
984 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700985
Adam Cohen76fc0852011-06-17 13:26:23 -0700986 lp.x = oldX;
987 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700988
Adam Cohen482ed822012-03-02 14:15:13 -0800989 // Exit early if we're not actually moving the view
990 if (oldX == newX && oldY == newY) {
991 lp.isLockedToGrid = true;
992 return true;
993 }
994
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100995 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800996 va.setDuration(duration);
997 mReorderAnimators.put(lp, va);
998
999 va.addUpdateListener(new AnimatorUpdateListener() {
1000 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001001 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001002 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001003 lp.x = (int) ((1 - r) * oldX + r * newX);
1004 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001005 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001006 }
1007 });
Adam Cohen482ed822012-03-02 14:15:13 -08001008 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001009 boolean cancelled = false;
1010 public void onAnimationEnd(Animator animation) {
1011 // If the animation was cancelled, it means that another animation
1012 // has interrupted this one, and we don't want to lock the item into
1013 // place just yet.
1014 if (!cancelled) {
1015 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001016 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001017 }
1018 if (mReorderAnimators.containsKey(lp)) {
1019 mReorderAnimators.remove(lp);
1020 }
1021 }
1022 public void onAnimationCancel(Animator animation) {
1023 cancelled = true;
1024 }
1025 });
Adam Cohen482ed822012-03-02 14:15:13 -08001026 va.setStartDelay(delay);
1027 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001028 return true;
1029 }
1030 return false;
1031 }
1032
Adam Cohen482ed822012-03-02 14:15:13 -08001033 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1034 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001035 final int oldDragCellX = mDragCell[0];
1036 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001037
Adam Cohen2801caf2011-05-13 20:57:39 -07001038 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001039 return;
1040 }
1041
Adam Cohen482ed822012-03-02 14:15:13 -08001042 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1043 mDragCell[0] = cellX;
1044 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001045 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001046 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001047 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001048
Joe Onorato4be866d2010-10-10 11:26:02 -07001049 int left = topLeft[0];
1050 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001051
Winson Chungb8c69f32011-10-19 21:36:08 -07001052 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001053 // When drawing the drag outline, it did not account for margin offsets
1054 // added by the view's parent.
1055 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1056 left += lp.leftMargin;
1057 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001058
Adam Cohen99e8b402011-03-25 19:23:43 -07001059 // Offsets due to the size difference between the View and the dragOutline.
1060 // There is a size difference to account for the outer blur, which may lie
1061 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001062 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001063 // We center about the x axis
1064 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1065 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001066 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001067 if (dragOffset != null && dragRegion != null) {
1068 // Center the drag region *horizontally* in the cell and apply a drag
1069 // outline offset
1070 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1071 - dragRegion.width()) / 2;
Winson Chung69737c32013-10-08 17:00:19 -07001072 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1073 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1074 top += dragOffset.y + cellPaddingY;
Winson Chungb8c69f32011-10-19 21:36:08 -07001075 } else {
1076 // Center the drag outline in the cell
1077 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1078 - dragOutline.getWidth()) / 2;
1079 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1080 - dragOutline.getHeight()) / 2;
1081 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001082 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001083 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001084 mDragOutlineAnims[oldIndex].animateOut();
1085 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001086 Rect r = mDragOutlines[mDragOutlineCurrent];
1087 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1088 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001089 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001090 }
Winson Chung150fbab2010-09-29 17:14:26 -07001091
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001092 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1093 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001094 }
1095 }
1096
Adam Cohene0310962011-04-18 16:15:31 -07001097 public void clearDragOutlines() {
1098 final int oldIndex = mDragOutlineCurrent;
1099 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001100 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001101 }
1102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001104 * Find a vacant area that will fit the given bounds nearest the requested
1105 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001106 *
Romain Guy51afc022009-05-04 18:03:43 -07001107 * @param pixelX The X location at which you want to search for a vacant area.
1108 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001109 * @param spanX Horizontal span of the object.
1110 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001111 * @param result Array in which to place the result, or null (in which case a new array will
1112 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001113 * @return The X, Y cell of a vacant area that can contain this object,
1114 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001116 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1117 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, spanX, spanY, result, null);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001118 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001119
Michael Jurka6a1435d2010-09-27 17:35:12 -07001120 /**
1121 * Find a vacant area that will fit the given bounds nearest the requested
1122 * cell location. Uses Euclidean distance to score multiple vacant areas.
1123 *
1124 * @param pixelX The X location at which you want to search for a vacant area.
1125 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001126 * @param minSpanX The minimum horizontal span required
1127 * @param minSpanY The minimum vertical span required
1128 * @param spanX Horizontal span of the object.
1129 * @param spanY Vertical span of the object.
1130 * @param result Array in which to place the result, or null (in which case a new array will
1131 * be allocated)
1132 * @return The X, Y cell of a vacant area that can contain this object,
1133 * nearest the requested location.
1134 */
1135 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1136 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001137 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001138 result, resultSpan);
1139 }
1140
Adam Cohend41fbf52012-02-16 23:53:59 -08001141 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1142 private void lazyInitTempRectStack() {
1143 if (mTempRectStack.isEmpty()) {
1144 for (int i = 0; i < mCountX * mCountY; i++) {
1145 mTempRectStack.push(new Rect());
1146 }
1147 }
1148 }
Adam Cohen482ed822012-03-02 14:15:13 -08001149
Adam Cohend41fbf52012-02-16 23:53:59 -08001150 private void recycleTempRects(Stack<Rect> used) {
1151 while (!used.isEmpty()) {
1152 mTempRectStack.push(used.pop());
1153 }
1154 }
1155
1156 /**
1157 * Find a vacant area that will fit the given bounds nearest the requested
1158 * cell location. Uses Euclidean distance to score multiple vacant areas.
1159 *
1160 * @param pixelX The X location at which you want to search for a vacant area.
1161 * @param pixelY The Y location at which you want to search for a vacant area.
1162 * @param minSpanX The minimum horizontal span required
1163 * @param minSpanY The minimum vertical span required
1164 * @param spanX Horizontal span of the object.
1165 * @param spanY Vertical span of the object.
1166 * @param ignoreOccupied If true, the result can be an occupied cell
1167 * @param result Array in which to place the result, or null (in which case a new array will
1168 * be allocated)
1169 * @return The X, Y cell of a vacant area that can contain this object,
1170 * nearest the requested location.
1171 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001172 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1173 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001174 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001175
Adam Cohene3e27a82011-04-15 12:07:39 -07001176 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1177 // to the center of the item, but we are searching based on the top-left cell, so
1178 // we translate the point over to correspond to the top-left.
1179 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1180 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1181
Jeff Sharkey70864282009-04-07 21:08:40 -07001182 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001183 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001184 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001185 final Rect bestRect = new Rect(-1, -1, -1, -1);
1186 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001187
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001188 final int countX = mCountX;
1189 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001190
Adam Cohend41fbf52012-02-16 23:53:59 -08001191 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1192 spanX < minSpanX || spanY < minSpanY) {
1193 return bestXY;
1194 }
1195
1196 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001197 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001198 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1199 int ySize = -1;
1200 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001201 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001202 // First, let's see if this thing fits anywhere
1203 for (int i = 0; i < minSpanX; i++) {
1204 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001205 if (mOccupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001206 continue inner;
1207 }
Michael Jurkac28de512010-08-13 11:27:44 -07001208 }
1209 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001210 xSize = minSpanX;
1211 ySize = minSpanY;
1212
1213 // We know that the item will fit at _some_ acceptable size, now let's see
1214 // how big we can make it. We'll alternate between incrementing x and y spans
1215 // until we hit a limit.
1216 boolean incX = true;
1217 boolean hitMaxX = xSize >= spanX;
1218 boolean hitMaxY = ySize >= spanY;
1219 while (!(hitMaxX && hitMaxY)) {
1220 if (incX && !hitMaxX) {
1221 for (int j = 0; j < ySize; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001222 if (x + xSize > countX -1 || mOccupied[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001223 // We can't move out horizontally
1224 hitMaxX = true;
1225 }
1226 }
1227 if (!hitMaxX) {
1228 xSize++;
1229 }
1230 } else if (!hitMaxY) {
1231 for (int i = 0; i < xSize; i++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001232 if (y + ySize > countY - 1 || mOccupied[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001233 // We can't move out vertically
1234 hitMaxY = true;
1235 }
1236 }
1237 if (!hitMaxY) {
1238 ySize++;
1239 }
1240 }
1241 hitMaxX |= xSize >= spanX;
1242 hitMaxY |= ySize >= spanY;
1243 incX = !incX;
1244 }
1245 incX = true;
1246 hitMaxX = xSize >= spanX;
1247 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001248 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001249 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001250 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251
Adam Cohend41fbf52012-02-16 23:53:59 -08001252 // We verify that the current rect is not a sub-rect of any of our previous
1253 // candidates. In this case, the current rect is disqualified in favour of the
1254 // containing rect.
1255 Rect currentRect = mTempRectStack.pop();
1256 currentRect.set(x, y, x + xSize, y + ySize);
1257 boolean contained = false;
1258 for (Rect r : validRegions) {
1259 if (r.contains(currentRect)) {
1260 contained = true;
1261 break;
1262 }
1263 }
1264 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001265 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001266
Adam Cohend41fbf52012-02-16 23:53:59 -08001267 if ((distance <= bestDistance && !contained) ||
1268 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001269 bestDistance = distance;
1270 bestXY[0] = x;
1271 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001272 if (resultSpan != null) {
1273 resultSpan[0] = xSize;
1274 resultSpan[1] = ySize;
1275 }
1276 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001277 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 }
1279 }
1280
Adam Cohenc0dcf592011-06-01 15:30:43 -07001281 // Return -1, -1 if no suitable location found
1282 if (bestDistance == Double.MAX_VALUE) {
1283 bestXY[0] = -1;
1284 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001285 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001286 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001287 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001289
Adam Cohen482ed822012-03-02 14:15:13 -08001290 /**
1291 * Find a vacant area that will fit the given bounds nearest the requested
1292 * cell location, and will also weigh in a suggested direction vector of the
1293 * desired location. This method computers distance based on unit grid distances,
1294 * not pixel distances.
1295 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001296 * @param cellX The X cell nearest to which you want to search for a vacant area.
1297 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001298 * @param spanX Horizontal span of the object.
1299 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001300 * @param direction The favored direction in which the views should move from x, y
1301 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1302 * matches exactly. Otherwise we find the best matching direction.
1303 * @param occoupied The array which represents which cells in the CellLayout are occupied
1304 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001305 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001306 * @param result Array in which to place the result, or null (in which case a new array will
1307 * be allocated)
1308 * @return The X, Y cell of a vacant area that can contain this object,
1309 * nearest the requested location.
1310 */
1311 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001312 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001313 // Keep track of best-scoring drop area
1314 final int[] bestXY = result != null ? result : new int[2];
1315 float bestDistance = Float.MAX_VALUE;
1316 int bestDirectionScore = Integer.MIN_VALUE;
1317
1318 final int countX = mCountX;
1319 final int countY = mCountY;
1320
1321 for (int y = 0; y < countY - (spanY - 1); y++) {
1322 inner:
1323 for (int x = 0; x < countX - (spanX - 1); x++) {
1324 // First, let's see if this thing fits anywhere
1325 for (int i = 0; i < spanX; i++) {
1326 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001327 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001328 continue inner;
1329 }
1330 }
1331 }
1332
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001333 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001334 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001335 computeDirectionVector(x - cellX, y - cellY, curDirection);
1336 // The direction score is just the dot product of the two candidate direction
1337 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001338 int curDirectionScore = direction[0] * curDirection[0] +
1339 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001340 boolean exactDirectionOnly = false;
1341 boolean directionMatches = direction[0] == curDirection[0] &&
1342 direction[0] == curDirection[0];
1343 if ((directionMatches || !exactDirectionOnly) &&
1344 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001345 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1346 bestDistance = distance;
1347 bestDirectionScore = curDirectionScore;
1348 bestXY[0] = x;
1349 bestXY[1] = y;
1350 }
1351 }
1352 }
1353
1354 // Return -1, -1 if no suitable location found
1355 if (bestDistance == Float.MAX_VALUE) {
1356 bestXY[0] = -1;
1357 bestXY[1] = -1;
1358 }
1359 return bestXY;
1360 }
1361
1362 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001363 int[] direction, ItemConfiguration currentState) {
1364 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001365 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001366 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001367 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1368
Adam Cohen8baab352012-03-20 17:39:21 -07001369 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001370
1371 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001372 c.x = mTempLocation[0];
1373 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001374 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001375 }
Adam Cohen8baab352012-03-20 17:39:21 -07001376 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001377 return success;
1378 }
1379
Adam Cohenf3900c22012-11-16 18:28:11 -08001380 /**
1381 * This helper class defines a cluster of views. It helps with defining complex edges
1382 * of the cluster and determining how those edges interact with other views. The edges
1383 * essentially define a fine-grained boundary around the cluster of views -- like a more
1384 * precise version of a bounding box.
1385 */
1386 private class ViewCluster {
1387 final static int LEFT = 0;
1388 final static int TOP = 1;
1389 final static int RIGHT = 2;
1390 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001391
Adam Cohenf3900c22012-11-16 18:28:11 -08001392 ArrayList<View> views;
1393 ItemConfiguration config;
1394 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001395
Adam Cohenf3900c22012-11-16 18:28:11 -08001396 int[] leftEdge = new int[mCountY];
1397 int[] rightEdge = new int[mCountY];
1398 int[] topEdge = new int[mCountX];
1399 int[] bottomEdge = new int[mCountX];
1400 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1401
1402 @SuppressWarnings("unchecked")
1403 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1404 this.views = (ArrayList<View>) views.clone();
1405 this.config = config;
1406 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001407 }
1408
Adam Cohenf3900c22012-11-16 18:28:11 -08001409 void resetEdges() {
1410 for (int i = 0; i < mCountX; i++) {
1411 topEdge[i] = -1;
1412 bottomEdge[i] = -1;
1413 }
1414 for (int i = 0; i < mCountY; i++) {
1415 leftEdge[i] = -1;
1416 rightEdge[i] = -1;
1417 }
1418 leftEdgeDirty = true;
1419 rightEdgeDirty = true;
1420 bottomEdgeDirty = true;
1421 topEdgeDirty = true;
1422 boundingRectDirty = true;
1423 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001424
Adam Cohenf3900c22012-11-16 18:28:11 -08001425 void computeEdge(int which, int[] edge) {
1426 int count = views.size();
1427 for (int i = 0; i < count; i++) {
1428 CellAndSpan cs = config.map.get(views.get(i));
1429 switch (which) {
1430 case LEFT:
1431 int left = cs.x;
1432 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1433 if (left < edge[j] || edge[j] < 0) {
1434 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001435 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001436 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001437 break;
1438 case RIGHT:
1439 int right = cs.x + cs.spanX;
1440 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1441 if (right > edge[j]) {
1442 edge[j] = right;
1443 }
1444 }
1445 break;
1446 case TOP:
1447 int top = cs.y;
1448 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1449 if (top < edge[j] || edge[j] < 0) {
1450 edge[j] = top;
1451 }
1452 }
1453 break;
1454 case BOTTOM:
1455 int bottom = cs.y + cs.spanY;
1456 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1457 if (bottom > edge[j]) {
1458 edge[j] = bottom;
1459 }
1460 }
1461 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001462 }
1463 }
1464 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001465
1466 boolean isViewTouchingEdge(View v, int whichEdge) {
1467 CellAndSpan cs = config.map.get(v);
1468
1469 int[] edge = getEdge(whichEdge);
1470
1471 switch (whichEdge) {
1472 case LEFT:
1473 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1474 if (edge[i] == cs.x + cs.spanX) {
1475 return true;
1476 }
1477 }
1478 break;
1479 case RIGHT:
1480 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1481 if (edge[i] == cs.x) {
1482 return true;
1483 }
1484 }
1485 break;
1486 case TOP:
1487 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1488 if (edge[i] == cs.y + cs.spanY) {
1489 return true;
1490 }
1491 }
1492 break;
1493 case BOTTOM:
1494 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1495 if (edge[i] == cs.y) {
1496 return true;
1497 }
1498 }
1499 break;
1500 }
1501 return false;
1502 }
1503
1504 void shift(int whichEdge, int delta) {
1505 for (View v: views) {
1506 CellAndSpan c = config.map.get(v);
1507 switch (whichEdge) {
1508 case LEFT:
1509 c.x -= delta;
1510 break;
1511 case RIGHT:
1512 c.x += delta;
1513 break;
1514 case TOP:
1515 c.y -= delta;
1516 break;
1517 case BOTTOM:
1518 default:
1519 c.y += delta;
1520 break;
1521 }
1522 }
1523 resetEdges();
1524 }
1525
1526 public void addView(View v) {
1527 views.add(v);
1528 resetEdges();
1529 }
1530
1531 public Rect getBoundingRect() {
1532 if (boundingRectDirty) {
1533 boolean first = true;
1534 for (View v: views) {
1535 CellAndSpan c = config.map.get(v);
1536 if (first) {
1537 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1538 first = false;
1539 } else {
1540 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1541 }
1542 }
1543 }
1544 return boundingRect;
1545 }
1546
1547 public int[] getEdge(int which) {
1548 switch (which) {
1549 case LEFT:
1550 return getLeftEdge();
1551 case RIGHT:
1552 return getRightEdge();
1553 case TOP:
1554 return getTopEdge();
1555 case BOTTOM:
1556 default:
1557 return getBottomEdge();
1558 }
1559 }
1560
1561 public int[] getLeftEdge() {
1562 if (leftEdgeDirty) {
1563 computeEdge(LEFT, leftEdge);
1564 }
1565 return leftEdge;
1566 }
1567
1568 public int[] getRightEdge() {
1569 if (rightEdgeDirty) {
1570 computeEdge(RIGHT, rightEdge);
1571 }
1572 return rightEdge;
1573 }
1574
1575 public int[] getTopEdge() {
1576 if (topEdgeDirty) {
1577 computeEdge(TOP, topEdge);
1578 }
1579 return topEdge;
1580 }
1581
1582 public int[] getBottomEdge() {
1583 if (bottomEdgeDirty) {
1584 computeEdge(BOTTOM, bottomEdge);
1585 }
1586 return bottomEdge;
1587 }
1588
1589 PositionComparator comparator = new PositionComparator();
1590 class PositionComparator implements Comparator<View> {
1591 int whichEdge = 0;
1592 public int compare(View left, View right) {
1593 CellAndSpan l = config.map.get(left);
1594 CellAndSpan r = config.map.get(right);
1595 switch (whichEdge) {
1596 case LEFT:
1597 return (r.x + r.spanX) - (l.x + l.spanX);
1598 case RIGHT:
1599 return l.x - r.x;
1600 case TOP:
1601 return (r.y + r.spanY) - (l.y + l.spanY);
1602 case BOTTOM:
1603 default:
1604 return l.y - r.y;
1605 }
1606 }
1607 }
1608
1609 public void sortConfigurationForEdgePush(int edge) {
1610 comparator.whichEdge = edge;
1611 Collections.sort(config.sortedViews, comparator);
1612 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001613 }
1614
Adam Cohenf3900c22012-11-16 18:28:11 -08001615 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1616 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001617
Adam Cohenf3900c22012-11-16 18:28:11 -08001618 ViewCluster cluster = new ViewCluster(views, currentState);
1619 Rect clusterRect = cluster.getBoundingRect();
1620 int whichEdge;
1621 int pushDistance;
1622 boolean fail = false;
1623
1624 // Determine the edge of the cluster that will be leading the push and how far
1625 // the cluster must be shifted.
1626 if (direction[0] < 0) {
1627 whichEdge = ViewCluster.LEFT;
1628 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001629 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001630 whichEdge = ViewCluster.RIGHT;
1631 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1632 } else if (direction[1] < 0) {
1633 whichEdge = ViewCluster.TOP;
1634 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1635 } else {
1636 whichEdge = ViewCluster.BOTTOM;
1637 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001638 }
1639
Adam Cohenf3900c22012-11-16 18:28:11 -08001640 // Break early for invalid push distance.
1641 if (pushDistance <= 0) {
1642 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001643 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001644
1645 // Mark the occupied state as false for the group of views we want to move.
1646 for (View v: views) {
1647 CellAndSpan c = currentState.map.get(v);
1648 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1649 }
1650
1651 // We save the current configuration -- if we fail to find a solution we will revert
1652 // to the initial state. The process of finding a solution modifies the configuration
1653 // in place, hence the need for revert in the failure case.
1654 currentState.save();
1655
1656 // The pushing algorithm is simplified by considering the views in the order in which
1657 // they would be pushed by the cluster. For example, if the cluster is leading with its
1658 // left edge, we consider sort the views by their right edge, from right to left.
1659 cluster.sortConfigurationForEdgePush(whichEdge);
1660
1661 while (pushDistance > 0 && !fail) {
1662 for (View v: currentState.sortedViews) {
1663 // For each view that isn't in the cluster, we see if the leading edge of the
1664 // cluster is contacting the edge of that view. If so, we add that view to the
1665 // cluster.
1666 if (!cluster.views.contains(v) && v != dragView) {
1667 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1668 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1669 if (!lp.canReorder) {
1670 // The push solution includes the all apps button, this is not viable.
1671 fail = true;
1672 break;
1673 }
1674 cluster.addView(v);
1675 CellAndSpan c = currentState.map.get(v);
1676
1677 // Adding view to cluster, mark it as not occupied.
1678 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1679 }
1680 }
1681 }
1682 pushDistance--;
1683
1684 // The cluster has been completed, now we move the whole thing over in the appropriate
1685 // direction.
1686 cluster.shift(whichEdge, 1);
1687 }
1688
1689 boolean foundSolution = false;
1690 clusterRect = cluster.getBoundingRect();
1691
1692 // Due to the nature of the algorithm, the only check required to verify a valid solution
1693 // is to ensure that completed shifted cluster lies completely within the cell layout.
1694 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1695 clusterRect.bottom <= mCountY) {
1696 foundSolution = true;
1697 } else {
1698 currentState.restore();
1699 }
1700
1701 // In either case, we set the occupied array as marked for the location of the views
1702 for (View v: cluster.views) {
1703 CellAndSpan c = currentState.map.get(v);
1704 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1705 }
1706
1707 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001708 }
1709
Adam Cohen482ed822012-03-02 14:15:13 -08001710 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001711 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001712 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001713
Adam Cohen8baab352012-03-20 17:39:21 -07001714 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001715 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001716 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001717 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001718 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001719 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001720 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001721 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001722 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001723 }
1724 }
Adam Cohen8baab352012-03-20 17:39:21 -07001725
Adam Cohen8baab352012-03-20 17:39:21 -07001726 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001727 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001728 CellAndSpan c = currentState.map.get(v);
1729 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1730 }
1731
Adam Cohen47a876d2012-03-19 13:21:41 -07001732 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1733 int top = boundingRect.top;
1734 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001735 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001736 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001737 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001738 CellAndSpan c = currentState.map.get(v);
1739 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001740 }
1741
Adam Cohen482ed822012-03-02 14:15:13 -08001742 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1743
Adam Cohenf3900c22012-11-16 18:28:11 -08001744 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1745 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001746
Adam Cohen8baab352012-03-20 17:39:21 -07001747 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001748 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001749 int deltaX = mTempLocation[0] - boundingRect.left;
1750 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001751 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001752 CellAndSpan c = currentState.map.get(v);
1753 c.x += deltaX;
1754 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001755 }
1756 success = true;
1757 }
Adam Cohen8baab352012-03-20 17:39:21 -07001758
1759 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001760 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001761 CellAndSpan c = currentState.map.get(v);
1762 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001763 }
1764 return success;
1765 }
1766
1767 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1768 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1769 }
1770
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001771 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1772 // to push items in each of the cardinal directions, in an order based on the direction vector
1773 // passed.
1774 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1775 int[] direction, View ignoreView, ItemConfiguration solution) {
1776 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001777 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001778 // separately in each of the components.
1779 int temp = direction[1];
1780 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001781
1782 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001783 ignoreView, solution)) {
1784 return true;
1785 }
1786 direction[1] = temp;
1787 temp = direction[0];
1788 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001789
1790 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001791 ignoreView, solution)) {
1792 return true;
1793 }
1794 // Revert the direction
1795 direction[0] = temp;
1796
1797 // Now we try pushing in each component of the opposite direction
1798 direction[0] *= -1;
1799 direction[1] *= -1;
1800 temp = direction[1];
1801 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001802 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001803 ignoreView, solution)) {
1804 return true;
1805 }
1806
1807 direction[1] = temp;
1808 temp = direction[0];
1809 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001810 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001811 ignoreView, solution)) {
1812 return true;
1813 }
1814 // revert the direction
1815 direction[0] = temp;
1816 direction[0] *= -1;
1817 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001818
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001819 } else {
1820 // If the direction vector has a single non-zero component, we push first in the
1821 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001822 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001823 ignoreView, solution)) {
1824 return true;
1825 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001826 // Then we try the opposite direction
1827 direction[0] *= -1;
1828 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001829 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001830 ignoreView, solution)) {
1831 return true;
1832 }
1833 // Switch the direction back
1834 direction[0] *= -1;
1835 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001836
1837 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001838 // to find a solution by pushing along the perpendicular axis.
1839
1840 // Swap the components
1841 int temp = direction[1];
1842 direction[1] = direction[0];
1843 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001844 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001845 ignoreView, solution)) {
1846 return true;
1847 }
1848
1849 // Then we try the opposite direction
1850 direction[0] *= -1;
1851 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001852 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001853 ignoreView, solution)) {
1854 return true;
1855 }
1856 // Switch the direction back
1857 direction[0] *= -1;
1858 direction[1] *= -1;
1859
1860 // Swap the components back
1861 temp = direction[1];
1862 direction[1] = direction[0];
1863 direction[0] = temp;
1864 }
1865 return false;
1866 }
1867
Adam Cohen482ed822012-03-02 14:15:13 -08001868 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001869 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001870 // Return early if get invalid cell positions
1871 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001872
Adam Cohen8baab352012-03-20 17:39:21 -07001873 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001874 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001875
Adam Cohen8baab352012-03-20 17:39:21 -07001876 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001877 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001878 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001879 if (c != null) {
1880 c.x = cellX;
1881 c.y = cellY;
1882 }
Adam Cohen482ed822012-03-02 14:15:13 -08001883 }
Adam Cohen482ed822012-03-02 14:15:13 -08001884 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1885 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001886 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001887 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001888 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001889 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001890 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001891 if (Rect.intersects(r0, r1)) {
1892 if (!lp.canReorder) {
1893 return false;
1894 }
1895 mIntersectingViews.add(child);
1896 }
1897 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001898
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001899 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1900
Winson Chung5f8afe62013-08-12 16:19:28 -07001901 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001902 // we try to find a solution such that no displaced item travels through another item
1903 // without also displacing that item.
1904 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001905 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001906 return true;
1907 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001908
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001909 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001910 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001911 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001912 return true;
1913 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001914
Adam Cohen482ed822012-03-02 14:15:13 -08001915 // Ok, they couldn't move as a block, let's move them individually
1916 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001917 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001918 return false;
1919 }
1920 }
1921 return true;
1922 }
1923
1924 /*
1925 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1926 * the provided point and the provided cell
1927 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001928 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001929 double angle = Math.atan(((float) deltaY) / deltaX);
1930
1931 result[0] = 0;
1932 result[1] = 0;
1933 if (Math.abs(Math.cos(angle)) > 0.5f) {
1934 result[0] = (int) Math.signum(deltaX);
1935 }
1936 if (Math.abs(Math.sin(angle)) > 0.5f) {
1937 result[1] = (int) Math.signum(deltaY);
1938 }
1939 }
1940
Adam Cohen8baab352012-03-20 17:39:21 -07001941 private void copyOccupiedArray(boolean[][] occupied) {
1942 for (int i = 0; i < mCountX; i++) {
1943 for (int j = 0; j < mCountY; j++) {
1944 occupied[i][j] = mOccupied[i][j];
1945 }
1946 }
1947 }
1948
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001949 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001950 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1951 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001952 // Copy the current state into the solution. This solution will be manipulated as necessary.
1953 copyCurrentStateToSolution(solution, false);
1954 // Copy the current occupied array into the temporary occupied array. This array will be
1955 // manipulated as necessary to find a solution.
1956 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001957
1958 // We find the nearest cell into which we would place the dragged item, assuming there's
1959 // nothing in its way.
1960 int result[] = new int[2];
1961 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1962
1963 boolean success = false;
1964 // First we try the exact nearest position of the item being dragged,
1965 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001966 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1967 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001968
1969 if (!success) {
1970 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1971 // x, then 1 in y etc.
1972 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001973 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1974 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001975 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001976 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1977 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001978 }
1979 solution.isSolution = false;
1980 } else {
1981 solution.isSolution = true;
1982 solution.dragViewX = result[0];
1983 solution.dragViewY = result[1];
1984 solution.dragViewSpanX = spanX;
1985 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001986 }
1987 return solution;
1988 }
1989
1990 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001991 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001992 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001993 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001994 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001995 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001996 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001997 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001998 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001999 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002000 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002001 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002002 }
2003 }
2004
2005 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2006 for (int i = 0; i < mCountX; i++) {
2007 for (int j = 0; j < mCountY; j++) {
2008 mTmpOccupied[i][j] = false;
2009 }
2010 }
2011
Michael Jurkaa52570f2012-03-20 03:18:20 -07002012 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002013 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002014 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002015 if (child == dragView) continue;
2016 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002017 CellAndSpan c = solution.map.get(child);
2018 if (c != null) {
2019 lp.tmpCellX = c.x;
2020 lp.tmpCellY = c.y;
2021 lp.cellHSpan = c.spanX;
2022 lp.cellVSpan = c.spanY;
2023 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002024 }
2025 }
2026 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2027 solution.dragViewSpanY, mTmpOccupied, true);
2028 }
2029
2030 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2031 commitDragView) {
2032
2033 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2034 for (int i = 0; i < mCountX; i++) {
2035 for (int j = 0; j < mCountY; j++) {
2036 occupied[i][j] = false;
2037 }
2038 }
2039
Michael Jurkaa52570f2012-03-20 03:18:20 -07002040 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002041 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002042 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002043 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002044 CellAndSpan c = solution.map.get(child);
2045 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002046 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2047 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002048 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002049 }
2050 }
2051 if (commitDragView) {
2052 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2053 solution.dragViewSpanY, occupied, true);
2054 }
2055 }
2056
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002057
2058 // This method starts or changes the reorder preview animations
2059 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2060 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002061 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002062 for (int i = 0; i < childCount; i++) {
2063 View child = mShortcutsAndWidgets.getChildAt(i);
2064 if (child == dragView) continue;
2065 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002066 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2067 != null && !solution.intersectingViews.contains(child);
2068
Adam Cohen19f37922012-03-21 11:59:11 -07002069 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002070 if (c != null && !skip) {
2071 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2072 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002073 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002074 }
2075 }
2076 }
2077
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002078 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002079 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002080 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002081 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002082 float finalDeltaX;
2083 float finalDeltaY;
2084 float initDeltaX;
2085 float initDeltaY;
2086 float finalScale;
2087 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002088 int mode;
2089 boolean repeating = false;
2090 private static final int PREVIEW_DURATION = 300;
2091 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2092
2093 public static final int MODE_HINT = 0;
2094 public static final int MODE_PREVIEW = 1;
2095
Adam Cohene7587d22012-05-24 18:50:02 -07002096 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002097
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002098 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2099 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002100 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2101 final int x0 = mTmpPoint[0];
2102 final int y0 = mTmpPoint[1];
2103 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2104 final int x1 = mTmpPoint[0];
2105 final int y1 = mTmpPoint[1];
2106 final int dX = x1 - x0;
2107 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002108 finalDeltaX = 0;
2109 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002110 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002111 if (dX == dY && dX == 0) {
2112 } else {
2113 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002114 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002115 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002116 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002117 } else {
2118 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002119 finalDeltaX = (int) (- dir * Math.signum(dX) *
2120 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2121 finalDeltaY = (int) (- dir * Math.signum(dY) *
2122 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002123 }
2124 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002125 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002126 initDeltaX = child.getTranslationX();
2127 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002128 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002129 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002130 this.child = child;
2131 }
2132
Adam Cohend024f982012-05-23 18:26:45 -07002133 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002134 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002135 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002136 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002137 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002138 if (finalDeltaX == 0 && finalDeltaY == 0) {
2139 completeAnimationImmediately();
2140 return;
2141 }
Adam Cohen19f37922012-03-21 11:59:11 -07002142 }
Adam Cohend024f982012-05-23 18:26:45 -07002143 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002144 return;
2145 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002146 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002147 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002148 va.setRepeatMode(ValueAnimator.REVERSE);
2149 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002150 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002151 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002152 va.addUpdateListener(new AnimatorUpdateListener() {
2153 @Override
2154 public void onAnimationUpdate(ValueAnimator animation) {
2155 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002156 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2157 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2158 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002159 child.setTranslationX(x);
2160 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002161 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002162 child.setScaleX(s);
2163 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002164 }
2165 });
2166 va.addListener(new AnimatorListenerAdapter() {
2167 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002168 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002169 initDeltaX = 0;
2170 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002171 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002172 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002173 }
2174 });
Adam Cohen19f37922012-03-21 11:59:11 -07002175 mShakeAnimators.put(child, this);
2176 va.start();
2177 }
2178
Adam Cohend024f982012-05-23 18:26:45 -07002179 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002180 if (a != null) {
2181 a.cancel();
2182 }
Adam Cohen19f37922012-03-21 11:59:11 -07002183 }
Adam Cohene7587d22012-05-24 18:50:02 -07002184
Adam Cohen091440a2015-03-18 14:16:05 -07002185 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002186 if (a != null) {
2187 a.cancel();
2188 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002189
Michael Jurka2ecf9952012-06-18 12:52:28 -07002190 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002191 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002192 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002193 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2194 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002195 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2196 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002197 );
2198 s.setDuration(REORDER_ANIMATION_DURATION);
2199 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2200 s.start();
2201 }
Adam Cohen19f37922012-03-21 11:59:11 -07002202 }
2203
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002204 private void completeAndClearReorderPreviewAnimations() {
2205 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002206 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002207 }
2208 mShakeAnimators.clear();
2209 }
2210
Adam Cohen482ed822012-03-02 14:15:13 -08002211 private void commitTempPlacement() {
2212 for (int i = 0; i < mCountX; i++) {
2213 for (int j = 0; j < mCountY; j++) {
2214 mOccupied[i][j] = mTmpOccupied[i][j];
2215 }
2216 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002217 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002218 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002219 View child = mShortcutsAndWidgets.getChildAt(i);
2220 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2221 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002222 // We do a null check here because the item info can be null in the case of the
2223 // AllApps button in the hotseat.
2224 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002225 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2226 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2227 info.requiresDbUpdate = true;
2228 }
Adam Cohen2acce882012-03-28 19:03:19 -07002229 info.cellX = lp.cellX = lp.tmpCellX;
2230 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002231 info.spanX = lp.cellHSpan;
2232 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002233 }
Adam Cohen482ed822012-03-02 14:15:13 -08002234 }
Adam Cohen2acce882012-03-28 19:03:19 -07002235 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002236 }
2237
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002238 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002239 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002240 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002241 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002242 lp.useTmpCoords = useTempCoords;
2243 }
2244 }
2245
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002246 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002247 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2248 int[] result = new int[2];
2249 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002250 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002251 resultSpan);
2252 if (result[0] >= 0 && result[1] >= 0) {
2253 copyCurrentStateToSolution(solution, false);
2254 solution.dragViewX = result[0];
2255 solution.dragViewY = result[1];
2256 solution.dragViewSpanX = resultSpan[0];
2257 solution.dragViewSpanY = resultSpan[1];
2258 solution.isSolution = true;
2259 } else {
2260 solution.isSolution = false;
2261 }
2262 return solution;
2263 }
2264
2265 public void prepareChildForDrag(View child) {
2266 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002267 }
2268
Adam Cohen19f37922012-03-21 11:59:11 -07002269 /* This seems like it should be obvious and straight-forward, but when the direction vector
2270 needs to match with the notion of the dragView pushing other views, we have to employ
2271 a slightly more subtle notion of the direction vector. The question is what two points is
2272 the vector between? The center of the dragView and its desired destination? Not quite, as
2273 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2274 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2275 or right, which helps make pushing feel right.
2276 */
2277 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2278 int spanY, View dragView, int[] resultDirection) {
2279 int[] targetDestination = new int[2];
2280
2281 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2282 Rect dragRect = new Rect();
2283 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2284 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2285
2286 Rect dropRegionRect = new Rect();
2287 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2288 dragView, dropRegionRect, mIntersectingViews);
2289
2290 int dropRegionSpanX = dropRegionRect.width();
2291 int dropRegionSpanY = dropRegionRect.height();
2292
2293 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2294 dropRegionRect.height(), dropRegionRect);
2295
2296 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2297 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2298
2299 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2300 deltaX = 0;
2301 }
2302 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2303 deltaY = 0;
2304 }
2305
2306 if (deltaX == 0 && deltaY == 0) {
2307 // No idea what to do, give a random direction.
2308 resultDirection[0] = 1;
2309 resultDirection[1] = 0;
2310 } else {
2311 computeDirectionVector(deltaX, deltaY, resultDirection);
2312 }
2313 }
2314
2315 // For a given cell and span, fetch the set of views intersecting the region.
2316 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2317 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2318 if (boundingRect != null) {
2319 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2320 }
2321 intersectingViews.clear();
2322 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2323 Rect r1 = new Rect();
2324 final int count = mShortcutsAndWidgets.getChildCount();
2325 for (int i = 0; i < count; i++) {
2326 View child = mShortcutsAndWidgets.getChildAt(i);
2327 if (child == dragView) continue;
2328 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2329 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2330 if (Rect.intersects(r0, r1)) {
2331 mIntersectingViews.add(child);
2332 if (boundingRect != null) {
2333 boundingRect.union(r1);
2334 }
2335 }
2336 }
2337 }
2338
2339 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2340 View dragView, int[] result) {
2341 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2342 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2343 mIntersectingViews);
2344 return !mIntersectingViews.isEmpty();
2345 }
2346
2347 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002348 completeAndClearReorderPreviewAnimations();
2349 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2350 final int count = mShortcutsAndWidgets.getChildCount();
2351 for (int i = 0; i < count; i++) {
2352 View child = mShortcutsAndWidgets.getChildAt(i);
2353 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2354 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2355 lp.tmpCellX = lp.cellX;
2356 lp.tmpCellY = lp.cellY;
2357 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2358 0, false, false);
2359 }
Adam Cohen19f37922012-03-21 11:59:11 -07002360 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002361 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002362 }
Adam Cohen19f37922012-03-21 11:59:11 -07002363 }
2364
Adam Cohenbebf0422012-04-11 18:06:28 -07002365 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2366 View dragView, int[] direction, boolean commit) {
2367 int[] pixelXY = new int[2];
2368 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2369
2370 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002371 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002372 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2373
2374 setUseTempCoords(true);
2375 if (swapSolution != null && swapSolution.isSolution) {
2376 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2377 // committing anything or animating anything as we just want to determine if a solution
2378 // exists
2379 copySolutionToTempState(swapSolution, dragView);
2380 setItemPlacementDirty(true);
2381 animateItemsToSolution(swapSolution, dragView, commit);
2382
2383 if (commit) {
2384 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002385 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002386 setItemPlacementDirty(false);
2387 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002388 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2389 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002390 }
2391 mShortcutsAndWidgets.requestLayout();
2392 }
2393 return swapSolution.isSolution;
2394 }
2395
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002396 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002397 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002398 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002399 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002400
2401 if (resultSpan == null) {
2402 resultSpan = new int[2];
2403 }
2404
Adam Cohen19f37922012-03-21 11:59:11 -07002405 // When we are checking drop validity or actually dropping, we don't recompute the
2406 // direction vector, since we want the solution to match the preview, and it's possible
2407 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002408 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2409 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002410 mDirectionVector[0] = mPreviousReorderDirection[0];
2411 mDirectionVector[1] = mPreviousReorderDirection[1];
2412 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002413 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2414 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2415 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002416 }
2417 } else {
2418 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2419 mPreviousReorderDirection[0] = mDirectionVector[0];
2420 mPreviousReorderDirection[1] = mDirectionVector[1];
2421 }
2422
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002423 // Find a solution involving pushing / displacing any items in the way
2424 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002425 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2426
2427 // We attempt the approach which doesn't shuffle views at all
2428 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2429 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2430
2431 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002432
2433 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2434 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002435 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2436 finalSolution = swapSolution;
2437 } else if (noShuffleSolution.isSolution) {
2438 finalSolution = noShuffleSolution;
2439 }
2440
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002441 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002442 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002443 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2444 ReorderPreviewAnimation.MODE_HINT);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002445 result[0] = finalSolution.dragViewX;
2446 result[1] = finalSolution.dragViewY;
2447 resultSpan[0] = finalSolution.dragViewSpanX;
2448 resultSpan[1] = finalSolution.dragViewSpanY;
2449 } else {
2450 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2451 }
2452 return result;
2453 }
2454
Adam Cohen482ed822012-03-02 14:15:13 -08002455 boolean foundSolution = true;
2456 if (!DESTRUCTIVE_REORDER) {
2457 setUseTempCoords(true);
2458 }
2459
2460 if (finalSolution != null) {
2461 result[0] = finalSolution.dragViewX;
2462 result[1] = finalSolution.dragViewY;
2463 resultSpan[0] = finalSolution.dragViewSpanX;
2464 resultSpan[1] = finalSolution.dragViewSpanY;
2465
2466 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2467 // committing anything or animating anything as we just want to determine if a solution
2468 // exists
2469 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2470 if (!DESTRUCTIVE_REORDER) {
2471 copySolutionToTempState(finalSolution, dragView);
2472 }
2473 setItemPlacementDirty(true);
2474 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2475
Adam Cohen19f37922012-03-21 11:59:11 -07002476 if (!DESTRUCTIVE_REORDER &&
2477 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002478 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002479 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002480 setItemPlacementDirty(false);
2481 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002482 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2483 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002484 }
2485 }
2486 } else {
2487 foundSolution = false;
2488 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2489 }
2490
2491 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2492 setUseTempCoords(false);
2493 }
Adam Cohen482ed822012-03-02 14:15:13 -08002494
Michael Jurkaa52570f2012-03-20 03:18:20 -07002495 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002496 return result;
2497 }
2498
Adam Cohen19f37922012-03-21 11:59:11 -07002499 void setItemPlacementDirty(boolean dirty) {
2500 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002501 }
Adam Cohen19f37922012-03-21 11:59:11 -07002502 boolean isItemPlacementDirty() {
2503 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002504 }
2505
Adam Cohen091440a2015-03-18 14:16:05 -07002506 @Thunk class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002507 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002508 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2509 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002510 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002511 boolean isSolution = false;
2512 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2513
Adam Cohenf3900c22012-11-16 18:28:11 -08002514 void save() {
2515 // Copy current state into savedMap
2516 for (View v: map.keySet()) {
2517 map.get(v).copy(savedMap.get(v));
2518 }
2519 }
2520
2521 void restore() {
2522 // Restore current state from savedMap
2523 for (View v: savedMap.keySet()) {
2524 savedMap.get(v).copy(map.get(v));
2525 }
2526 }
2527
2528 void add(View v, CellAndSpan cs) {
2529 map.put(v, cs);
2530 savedMap.put(v, new CellAndSpan());
2531 sortedViews.add(v);
2532 }
2533
Adam Cohen482ed822012-03-02 14:15:13 -08002534 int area() {
2535 return dragViewSpanX * dragViewSpanY;
2536 }
Adam Cohen8baab352012-03-20 17:39:21 -07002537 }
2538
2539 private class CellAndSpan {
2540 int x, y;
2541 int spanX, spanY;
2542
Adam Cohenf3900c22012-11-16 18:28:11 -08002543 public CellAndSpan() {
2544 }
2545
2546 public void copy(CellAndSpan copy) {
2547 copy.x = x;
2548 copy.y = y;
2549 copy.spanX = spanX;
2550 copy.spanY = spanY;
2551 }
2552
Adam Cohen8baab352012-03-20 17:39:21 -07002553 public CellAndSpan(int x, int y, int spanX, int spanY) {
2554 this.x = x;
2555 this.y = y;
2556 this.spanX = spanX;
2557 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002558 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002559
2560 public String toString() {
2561 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2562 }
2563
Adam Cohen482ed822012-03-02 14:15:13 -08002564 }
2565
Adam Cohendf035382011-04-11 17:22:04 -07002566 /**
Adam Cohendf035382011-04-11 17:22:04 -07002567 * Find a starting cell position that will fit the given bounds nearest the requested
2568 * cell location. Uses Euclidean distance to score multiple vacant areas.
2569 *
2570 * @param pixelX The X location at which you want to search for a vacant area.
2571 * @param pixelY The Y location at which you want to search for a vacant area.
2572 * @param spanX Horizontal span of the object.
2573 * @param spanY Vertical span of the object.
2574 * @param ignoreView Considers space occupied by this view as unoccupied
2575 * @param result Previously returned value to possibly recycle.
2576 * @return The X, Y cell of a vacant area that can contain this object,
2577 * nearest the requested location.
2578 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002579 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2580 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002581 }
2582
Michael Jurka0280c3b2010-09-17 15:00:07 -07002583 boolean existsEmptyCell() {
2584 return findCellForSpan(null, 1, 1);
2585 }
2586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002588 * Finds the upper-left coordinate of the first rectangle in the grid that can
2589 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2590 * then this method will only return coordinates for rectangles that contain the cell
2591 * (intersectX, intersectY)
2592 *
2593 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2594 * can be found.
2595 * @param spanX The horizontal span of the cell we want to find.
2596 * @param spanY The vertical span of the cell we want to find.
2597 *
2598 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002599 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002600 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Michael Jurka28750fb2010-09-24 17:43:49 -07002601 boolean foundCell = false;
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002602 final int endX = mCountX - (spanX - 1);
2603 final int endY = mCountY - (spanY - 1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002604
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002605 for (int y = 0; y < endY && !foundCell; y++) {
2606 inner:
2607 for (int x = 0; x < endX; x++) {
2608 for (int i = 0; i < spanX; i++) {
2609 for (int j = 0; j < spanY; j++) {
2610 if (mOccupied[x + i][y + j]) {
2611 // small optimization: we can skip to after the column we just found
2612 // an occupied cell
2613 x += i;
2614 continue inner;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002615 }
2616 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002617 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002618 if (cellXY != null) {
2619 cellXY[0] = x;
2620 cellXY[1] = y;
2621 }
2622 foundCell = true;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002623 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002624 }
2625 }
2626
Michael Jurka28750fb2010-09-24 17:43:49 -07002627 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002628 }
2629
2630 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002631 * A drag event has begun over this layout.
2632 * It may have begun over this layout (in which case onDragChild is called first),
2633 * or it may have begun on another layout.
2634 */
2635 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002636 mDragging = true;
2637 }
2638
2639 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002640 * Called when drag has left this CellLayout or has been completed (successfully or not)
2641 */
2642 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002643 // This can actually be called when we aren't in a drag, e.g. when adding a new
2644 // item to this layout via the customize drawer.
2645 // Guard against that case.
2646 if (mDragging) {
2647 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002648 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002649
2650 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002651 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002652 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2653 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002654 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002655 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002656 }
2657
2658 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002659 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002660 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002661 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002662 *
2663 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002664 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002665 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002666 if (child != null) {
2667 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002668 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002669 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002671 }
2672
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002673 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002674 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002675 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002676 * @param cellX X coordinate of upper left corner expressed as a cell position
2677 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002678 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002680 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002682 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002683 final int cellWidth = mCellWidth;
2684 final int cellHeight = mCellHeight;
2685 final int widthGap = mWidthGap;
2686 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002687
Winson Chung4b825dcd2011-06-19 12:41:22 -07002688 final int hStartPadding = getPaddingLeft();
2689 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002690
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002691 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2692 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2693
2694 int x = hStartPadding + cellX * (cellWidth + widthGap);
2695 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002696
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002697 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002698 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002699
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002700 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002701 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002702 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002703 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002704 * @param width Width in pixels
2705 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002706 * @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 -08002707 */
Adam Cohen2e6da152015-05-06 11:42:25 -07002708 public static int[] rectToCell(Launcher launcher, int width, int height, int[] result) {
Adam Cohen2e6da152015-05-06 11:42:25 -07002709 DeviceProfile grid = launcher.getDeviceProfile();
Sunny Goyalc6205602015-05-21 20:46:33 -07002710 Rect padding = grid.getWorkspacePadding(Utilities.isRtl(launcher.getResources()));
Winson Chung5f8afe62013-08-12 16:19:28 -07002711
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002712 // Always assume we're working with the smallest span to make sure we
2713 // reserve enough space in both orientations.
Sunny Goyalc6205602015-05-21 20:46:33 -07002714 int parentWidth = DeviceProfile.calculateCellWidth(grid.widthPx
Adam Cohen2e6da152015-05-06 11:42:25 -07002715 - padding.left - padding.right, (int) grid.inv.numColumns);
Sunny Goyalc6205602015-05-21 20:46:33 -07002716 int parentHeight = DeviceProfile.calculateCellHeight(grid.heightPx
Adam Cohen2e6da152015-05-06 11:42:25 -07002717 - padding.top - padding.bottom, (int) grid.inv.numRows);
Winson Chung66700732013-08-20 16:56:15 -07002718 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002719
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002721 int spanX = (int) Math.ceil(width / (float) smallerSize);
2722 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002723
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002724 if (result == null) {
2725 return new int[] { spanX, spanY };
2726 }
2727 result[0] = spanX;
2728 result[1] = spanY;
2729 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002730 }
2731
2732 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002733 * Calculate the grid spans needed to fit given item
2734 */
2735 public void calculateSpans(ItemInfo info) {
2736 final int minWidth;
2737 final int minHeight;
2738
2739 if (info instanceof LauncherAppWidgetInfo) {
2740 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2741 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2742 } else if (info instanceof PendingAddWidgetInfo) {
2743 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2744 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2745 } else {
2746 // It's not a widget, so it must be 1x1
2747 info.spanX = info.spanY = 1;
2748 return;
2749 }
Adam Cohen2e6da152015-05-06 11:42:25 -07002750 int[] spans = rectToCell(mLauncher, minWidth, minHeight, null);
Patrick Dubroy047379a2010-12-19 22:02:04 -08002751 info.spanX = spans[0];
2752 info.spanY = spans[1];
2753 }
2754
Michael Jurka0280c3b2010-09-17 15:00:07 -07002755 private void clearOccupiedCells() {
2756 for (int x = 0; x < mCountX; x++) {
2757 for (int y = 0; y < mCountY; y++) {
2758 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002759 }
2760 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002761 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002762
Adam Cohend4844c32011-02-18 19:25:06 -08002763 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002764 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002765 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002766 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002767 }
2768
Adam Cohend4844c32011-02-18 19:25:06 -08002769 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002770 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002771 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002772 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002773 }
2774
Adam Cohen482ed822012-03-02 14:15:13 -08002775 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2776 boolean value) {
2777 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002778 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2779 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002780 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002781 }
2782 }
2783 }
2784
Adam Cohen2801caf2011-05-13 20:57:39 -07002785 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002786 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002787 (Math.max((mCountX - 1), 0) * mWidthGap);
2788 }
2789
2790 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002791 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002792 (Math.max((mCountY - 1), 0) * mHeightGap);
2793 }
2794
Michael Jurka66d72172011-04-12 16:29:25 -07002795 public boolean isOccupied(int x, int y) {
2796 if (x < mCountX && y < mCountY) {
2797 return mOccupied[x][y];
2798 } else {
2799 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2800 }
2801 }
2802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002803 @Override
2804 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2805 return new CellLayout.LayoutParams(getContext(), attrs);
2806 }
2807
2808 @Override
2809 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2810 return p instanceof CellLayout.LayoutParams;
2811 }
2812
2813 @Override
2814 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2815 return new CellLayout.LayoutParams(p);
2816 }
2817
2818 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2819 /**
2820 * Horizontal location of the item in the grid.
2821 */
2822 @ViewDebug.ExportedProperty
2823 public int cellX;
2824
2825 /**
2826 * Vertical location of the item in the grid.
2827 */
2828 @ViewDebug.ExportedProperty
2829 public int cellY;
2830
2831 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002832 * Temporary horizontal location of the item in the grid during reorder
2833 */
2834 public int tmpCellX;
2835
2836 /**
2837 * Temporary vertical location of the item in the grid during reorder
2838 */
2839 public int tmpCellY;
2840
2841 /**
2842 * Indicates that the temporary coordinates should be used to layout the items
2843 */
2844 public boolean useTmpCoords;
2845
2846 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002847 * Number of cells spanned horizontally by the item.
2848 */
2849 @ViewDebug.ExportedProperty
2850 public int cellHSpan;
2851
2852 /**
2853 * Number of cells spanned vertically by the item.
2854 */
2855 @ViewDebug.ExportedProperty
2856 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002857
Adam Cohen1b607ed2011-03-03 17:26:50 -08002858 /**
2859 * Indicates whether the item will set its x, y, width and height parameters freely,
2860 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2861 */
Adam Cohend4844c32011-02-18 19:25:06 -08002862 public boolean isLockedToGrid = true;
2863
Adam Cohen482ed822012-03-02 14:15:13 -08002864 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002865 * Indicates that this item should use the full extents of its parent.
2866 */
2867 public boolean isFullscreen = false;
2868
2869 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002870 * Indicates whether this item can be reordered. Always true except in the case of the
2871 * the AllApps button.
2872 */
2873 public boolean canReorder = true;
2874
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875 // X coordinate of the view in the layout.
2876 @ViewDebug.ExportedProperty
2877 int x;
2878 // Y coordinate of the view in the layout.
2879 @ViewDebug.ExportedProperty
2880 int y;
2881
Romain Guy84f296c2009-11-04 15:00:44 -08002882 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002883
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002884 public LayoutParams(Context c, AttributeSet attrs) {
2885 super(c, attrs);
2886 cellHSpan = 1;
2887 cellVSpan = 1;
2888 }
2889
2890 public LayoutParams(ViewGroup.LayoutParams source) {
2891 super(source);
2892 cellHSpan = 1;
2893 cellVSpan = 1;
2894 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002895
2896 public LayoutParams(LayoutParams source) {
2897 super(source);
2898 this.cellX = source.cellX;
2899 this.cellY = source.cellY;
2900 this.cellHSpan = source.cellHSpan;
2901 this.cellVSpan = source.cellVSpan;
2902 }
2903
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002904 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002905 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002906 this.cellX = cellX;
2907 this.cellY = cellY;
2908 this.cellHSpan = cellHSpan;
2909 this.cellVSpan = cellVSpan;
2910 }
2911
Adam Cohen2374abf2013-04-16 14:56:57 -07002912 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2913 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002914 if (isLockedToGrid) {
2915 final int myCellHSpan = cellHSpan;
2916 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002917 int myCellX = useTmpCoords ? tmpCellX : cellX;
2918 int myCellY = useTmpCoords ? tmpCellY : cellY;
2919
2920 if (invertHorizontally) {
2921 myCellX = colCount - myCellX - cellHSpan;
2922 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002923
Adam Cohend4844c32011-02-18 19:25:06 -08002924 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2925 leftMargin - rightMargin;
2926 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2927 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002928 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2929 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002930 }
2931 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002932
Winson Chungaafa03c2010-06-11 17:34:16 -07002933 public String toString() {
2934 return "(" + this.cellX + ", " + this.cellY + ")";
2935 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002936
2937 public void setWidth(int width) {
2938 this.width = width;
2939 }
2940
2941 public int getWidth() {
2942 return width;
2943 }
2944
2945 public void setHeight(int height) {
2946 this.height = height;
2947 }
2948
2949 public int getHeight() {
2950 return height;
2951 }
2952
2953 public void setX(int x) {
2954 this.x = x;
2955 }
2956
2957 public int getX() {
2958 return x;
2959 }
2960
2961 public void setY(int y) {
2962 this.y = y;
2963 }
2964
2965 public int getY() {
2966 return y;
2967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 }
2969
Michael Jurka0280c3b2010-09-17 15:00:07 -07002970 // This class stores info for two purposes:
2971 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2972 // its spanX, spanY, and the screen it is on
2973 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2974 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2975 // the CellLayout that was long clicked
Sunny Goyal83a8f042015-05-19 12:52:12 -07002976 public static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002978 int cellX = -1;
2979 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002980 int spanX;
2981 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07002982 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002983 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002984
Sunny Goyal83a8f042015-05-19 12:52:12 -07002985 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002986 cell = v;
2987 cellX = info.cellX;
2988 cellY = info.cellY;
2989 spanX = info.spanX;
2990 spanY = info.spanY;
2991 screenId = info.screenId;
2992 container = info.container;
2993 }
2994
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002995 @Override
2996 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002997 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2998 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002999 }
3000 }
Michael Jurkad771c962011-08-09 15:00:48 -07003001
Sunny Goyala9116722015-04-29 13:55:58 -07003002 public boolean findVacantCell(int spanX, int spanY, int[] outXY) {
3003 return Utilities.findVacantCell(outXY, spanX, spanY, mCountX, mCountY, mOccupied);
3004 }
Sunny Goyal9ca9c132015-04-29 14:57:22 -07003005
3006 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
3007 int x2 = x + spanX - 1;
3008 int y2 = y + spanY - 1;
3009 if (x < 0 || y < 0 || x2 >= mCountX || y2 >= mCountY) {
3010 return false;
3011 }
3012 for (int i = x; i <= x2; i++) {
3013 for (int j = y; j <= y2; j++) {
3014 if (mOccupied[i][j]) {
3015 return false;
3016 }
3017 }
3018 }
3019
3020 return true;
3021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003022}