blob: 2eed2649284cb006dfa389038feac6182f55681b [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;
Chet Haase00397b12010-10-07 11:13:10 -070021import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
Adam Cohenc9735cf2015-01-23 16:11:55 -080024import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Sunny Goyalc13403c2016-11-18 23:44:48 -080027import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070028import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080030import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070032import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080034import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070035import android.graphics.drawable.Drawable;
Sunny Goyal2805e632015-05-20 15:35:32 -070036import android.graphics.drawable.TransitionDrawable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080037import android.os.Build;
Adam Cohen1462de32012-07-24 22:34:36 -070038import android.os.Parcelable;
Sunny Goyalc13403c2016-11-18 23:44:48 -080039import android.support.annotation.IntDef;
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;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070052import com.android.launcher3.LauncherSettings.Favorites;
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;
Tony Wickhame0c33232016-02-08 11:37:04 -080056import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070057import com.android.launcher3.config.ProviderConfig;
Sunny Goyal26119432016-02-18 22:09:23 +000058import com.android.launcher3.folder.FolderIcon;
Sunny Goyal06e21a22016-08-11 16:02:02 -070059import com.android.launcher3.graphics.DragPreviewProvider;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070060import com.android.launcher3.util.CellAndSpan;
61import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070062import com.android.launcher3.util.ParcelableSparseArray;
Adam Cohen091440a2015-03-18 14:16:05 -070063import com.android.launcher3.util.Thunk;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070064
Sunny Goyalc13403c2016-11-18 23:44:48 -080065import java.lang.annotation.Retention;
66import java.lang.annotation.RetentionPolicy;
Adam Cohen69ce2e52011-07-03 19:25:21 -070067import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070068import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080069import java.util.Collections;
70import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070071import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080072import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070073
Sunny Goyal4b6eb262015-05-14 19:24:40 -070074public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070075 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
76 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
77
Tony Wickhama0628cc2015-10-14 15:23:04 -070078 private static final String TAG = "CellLayout";
79 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070080
Adam Cohen2acce882012-03-28 19:03:19 -070081 private Launcher mLauncher;
Sunny Goyal4ffec482016-02-09 11:28:52 -080082 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070083 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080084 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070085 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070086 private int mFixedCellWidth;
87 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070088
Sunny Goyal4ffec482016-02-09 11:28:52 -080089 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070090 private int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -080091 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070092 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093
Adam Cohen917e3882013-10-31 15:03:35 -070094 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070095 private boolean mIsDragTarget = true;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070096 private boolean mJailContent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
Patrick Dubroyde7658b2010-09-27 11:15:43 -070098 // These are temporary variables to prevent having to allocate a new object just to
99 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -0700100 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -0700101 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700102
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700103 private GridOccupancy mOccupied;
104 private GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105
Michael Jurkadee05892010-07-27 10:01:56 -0700106 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -0700107 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700108
Adam Cohenefca0272016-02-24 19:19:06 -0800109 private ArrayList<FolderIcon.PreviewBackground> mFolderBackgrounds = new ArrayList<FolderIcon.PreviewBackground>();
110 FolderIcon.PreviewBackground mFolderLeaveBehind = new FolderIcon.PreviewBackground();
111 Paint mFolderBgPaint = new Paint();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700112
Michael Jurka5f1c5092010-09-03 14:15:02 -0700113 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700114
Tony Wickham33326072016-04-04 15:05:49 -0700115 private static final int BACKGROUND_ACTIVATE_DURATION =
116 FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 120 : 0;
Sunny Goyal2805e632015-05-20 15:35:32 -0700117 private final TransitionDrawable mBackground;
118
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700119 // These values allow a fixed measurement to be set on the CellLayout.
120 private int mFixedWidth = -1;
121 private int mFixedHeight = -1;
122
Michael Jurka33945b22010-12-21 18:19:38 -0800123 // If we're actively dragging something over this screen, mIsDragOverlapping is true
124 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700125
Winson Chung150fbab2010-09-29 17:14:26 -0700126 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700127 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700128 @Thunk Rect[] mDragOutlines = new Rect[4];
129 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700130 private InterruptibleInOutAnimator[] mDragOutlineAnims =
131 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700132
133 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700134 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700135 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700136
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700137 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800138
Sunny Goyal316490e2015-06-02 09:38:28 -0700139 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
140 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700141
142 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700143
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700144 // When a drag operation is in progress, holds the nearest cell to the touch point
145 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Joe Onorato4be866d2010-10-10 11:26:02 -0700147 private boolean mDragging = false;
148
Patrick Dubroyce34a972010-10-19 10:34:32 -0700149 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700150 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700151
Sunny Goyalc13403c2016-11-18 23:44:48 -0800152 @Retention(RetentionPolicy.SOURCE)
153 @IntDef({WORKSPACE, HOTSEAT, FOLDER})
154 public @interface ContainerType{}
155 public static final int WORKSPACE = 0;
156 public static final int HOTSEAT = 1;
157 public static final int FOLDER = 2;
158
159 @ContainerType private final int mContainerType;
160
161 private final float mChildScale;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800162
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800163 public static final int MODE_SHOW_REORDER_HINT = 0;
164 public static final int MODE_DRAG_OVER = 1;
165 public static final int MODE_ON_DROP = 2;
166 public static final int MODE_ON_DROP_EXTERNAL = 3;
167 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700168 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800169 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
170
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800171 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700172 private static final int REORDER_ANIMATION_DURATION = 150;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800173 @Thunk final float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700174
Adam Cohen482ed822012-03-02 14:15:13 -0800175 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
176 private Rect mOccupiedRect = new Rect();
177 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700178 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700179 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800180
Sunny Goyal2805e632015-05-20 15:35:32 -0700181 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700182
Michael Jurkaca993832012-06-29 15:17:04 -0700183 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700184
Adam Cohenc9735cf2015-01-23 16:11:55 -0800185 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700186 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800187 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 public CellLayout(Context context) {
190 this(context, null);
191 }
192
193 public CellLayout(Context context, AttributeSet attrs) {
194 this(context, attrs, 0);
195 }
196
197 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
198 super(context, attrs, defStyle);
Sunny Goyalc13403c2016-11-18 23:44:48 -0800199 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
200 mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE);
201 a.recycle();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700202
203 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
204 // the user where a dragged item will land when dropped.
205 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800206 setClipToPadding(false);
Tony2fd02082016-10-07 12:50:01 -0700207 mLauncher = Launcher.getLauncher(context);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700208
Adam Cohen2e6da152015-05-06 11:42:25 -0700209 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chung11a1a532013-09-13 11:14:45 -0700211 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800212 mFixedCellWidth = mFixedCellHeight = -1;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700213
214 mCountX = grid.inv.numColumns;
215 mCountY = grid.inv.numRows;
216 mOccupied = new GridOccupancy(mCountX, mCountY);
217 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
218
Adam Cohen5b53f292012-03-29 14:30:35 -0700219 mPreviousReorderDirection[0] = INVALID_DIRECTION;
220 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
Adam Cohenefca0272016-02-24 19:19:06 -0800222 mFolderLeaveBehind.delegateCellX = -1;
223 mFolderLeaveBehind.delegateCellY = -1;
224
Sunny Goyalc13403c2016-11-18 23:44:48 -0800225 mChildScale = mContainerType == HOTSEAT ? grid.inv.hotseatScale : 1f;
226
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700228 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700229
Tony Wickhame0c33232016-02-08 11:37:04 -0800230 mBackground = (TransitionDrawable) res.getDrawable(
231 FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? R.drawable.bg_screenpanel
232 : R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700233 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700234 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800235
Sunny Goyalc13403c2016-11-18 23:44:48 -0800236 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700237
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700238 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700239 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700240 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700241 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800242 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700243 }
Sunny Goyalf28e6af2016-08-31 16:02:40 -0700244 mDragOutlinePaint.setColor(getResources().getColor(R.color.outline_color));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700245
246 // When dragging things around the home screens, we show a green outline of
247 // where the item will land. The outlines gradually fade out, leaving a trail
248 // behind the drag path.
249 // Set up all the animations that are used to implement this fading.
250 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700251 final float fromAlphaValue = 0;
252 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700253
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700254 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700255
256 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700257 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100258 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700259 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700260 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700261 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700262 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700263 final Bitmap outline = (Bitmap)anim.getTag();
264
265 // If an animation is started and then stopped very quickly, we can still
266 // get spurious updates we've cleared the tag. Guard against this.
267 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700268 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700269 Object val = animation.getAnimatedValue();
270 Log.d(TAG, "anim " + thisIndex + " update: " + val +
271 ", isStopped " + anim.isStopped());
272 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 // Try to prevent it from continuing to run
274 animation.cancel();
275 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700276 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800277 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700278 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700279 }
280 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700281 // The animation holds a reference to the drag outline bitmap as long is it's
282 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700283 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700284 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700285 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700286 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700287 anim.setTag(null);
288 }
289 }
290 });
291 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700292 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700293
Sunny Goyalc13403c2016-11-18 23:44:48 -0800294 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530295 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700296
Mady Mellorbb835202015-07-15 16:34:34 -0700297 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700298
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700299 mTouchFeedbackView = new ClickShadowView(context);
300 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700301 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700302 }
303
Adam Cohenc9735cf2015-01-23 16:11:55 -0800304 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700305 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800306 mUseTouchHelper = enable;
307 if (!enable) {
308 ViewCompat.setAccessibilityDelegate(this, null);
309 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
310 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
311 setOnClickListener(mLauncher);
312 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700313 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
314 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
315 mTouchHelper = new WorkspaceAccessibilityHelper(this);
316 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
317 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
318 mTouchHelper = new FolderAccessibilityHelper(this);
319 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800320 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
321 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
322 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
323 setOnClickListener(mTouchHelper);
324 }
325
326 // Invalidate the accessibility hierarchy
327 if (getParent() != null) {
328 getParent().notifySubtreeAccessibilityStateChanged(
329 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
330 }
331 }
332
333 @Override
334 public boolean dispatchHoverEvent(MotionEvent event) {
335 // Always attempt to dispatch hover events to accessibility first.
336 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
337 return true;
338 }
339 return super.dispatchHoverEvent(event);
340 }
341
342 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800343 public boolean onInterceptTouchEvent(MotionEvent ev) {
344 if (mUseTouchHelper ||
345 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
346 return true;
347 }
348 return false;
349 }
350
Mady Melloref044dd2015-06-02 15:35:07 -0700351 @Override
352 public boolean onTouchEvent(MotionEvent ev) {
353 boolean handled = super.onTouchEvent(ev);
354 // Stylus button press on a home screen should not switch between overview mode and
355 // the home screen mode, however, once in overview mode stylus button press should be
356 // enabled to allow rearranging the different home screens. So check what mode
357 // the workspace is in, and only perform stylus button presses while in overview mode.
358 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700359 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700360 return true;
361 }
362 return handled;
363 }
364
Chris Craik01f2d7f2013-10-01 14:41:56 -0700365 public void enableHardwareLayer(boolean hasLayer) {
366 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700367 }
368
369 public void buildHardwareLayer() {
370 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700371 }
372
Winson Chung5f8afe62013-08-12 16:19:28 -0700373 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700374 mFixedCellWidth = mCellWidth = width;
375 mFixedCellHeight = mCellHeight = height;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530376 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung5f8afe62013-08-12 16:19:28 -0700377 }
378
Adam Cohen2801caf2011-05-13 20:57:39 -0700379 public void setGridSize(int x, int y) {
380 mCountX = x;
381 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700382 mOccupied = new GridOccupancy(mCountX, mCountY);
383 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700384 mTempRectStack.clear();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530385 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700386 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700387 }
388
Adam Cohen2374abf2013-04-16 14:56:57 -0700389 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
390 public void setInvertIfRtl(boolean invert) {
391 mShortcutsAndWidgets.setInvertIfRtl(invert);
392 }
393
Adam Cohen917e3882013-10-31 15:03:35 -0700394 public void setDropPending(boolean pending) {
395 mDropPending = pending;
396 }
397
398 public boolean isDropPending() {
399 return mDropPending;
400 }
401
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700402 @Override
403 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700404 if (icon == null || background == null) {
405 mTouchFeedbackView.setBitmap(null);
406 mTouchFeedbackView.animate().cancel();
407 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700408 if (mTouchFeedbackView.setBitmap(background)) {
Winsonbe9798b2016-07-20 12:55:49 -0700409 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
410 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700411 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700412 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800413 }
414 }
415
Adam Cohenc50438c2014-08-19 17:43:05 -0700416 void disableDragTarget() {
417 mIsDragTarget = false;
418 }
419
Tony Wickham0f97b782015-12-02 17:55:07 -0800420 public boolean isDragTarget() {
421 return mIsDragTarget;
422 }
423
Adam Cohenc50438c2014-08-19 17:43:05 -0700424 void setIsDragOverlapping(boolean isDragOverlapping) {
425 if (mIsDragOverlapping != isDragOverlapping) {
426 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700427 if (mIsDragOverlapping) {
428 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
429 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700430 if (mBackgroundAlpha > 0f) {
431 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
432 } else {
433 mBackground.resetTransition();
434 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700435 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700436 invalidate();
437 }
438 }
439
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700440 public void disableJailContent() {
441 mJailContent = false;
442 }
443
444 @Override
445 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
446 if (mJailContent) {
447 ParcelableSparseArray jail = getJailedArray(container);
448 super.dispatchSaveInstanceState(jail);
449 container.put(R.id.cell_layout_jail_id, jail);
450 } else {
451 super.dispatchSaveInstanceState(container);
452 }
453 }
454
455 @Override
456 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
457 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
458 }
459
460 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
461 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
462 return parcelable instanceof ParcelableSparseArray ?
463 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
464 }
465
Tony Wickham0f97b782015-12-02 17:55:07 -0800466 public boolean getIsDragOverlapping() {
467 return mIsDragOverlapping;
468 }
469
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700470 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700471 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700472 if (!mIsDragTarget) {
473 return;
474 }
475
Michael Jurka3e7c7632010-10-02 16:01:03 -0700476 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
477 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
478 // When we're small, we are either drawn normally or in the "accepts drops" state (during
479 // a drag). However, we also drag the mini hover background *over* one of those two
480 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700481 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700482 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700483 }
Romain Guya6abce82009-11-10 02:54:41 -0800484
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700485 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700486 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700487 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700488 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700489 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700490 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700491 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700492 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700493 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800494
Adam Cohen482ed822012-03-02 14:15:13 -0800495 if (DEBUG_VISUALIZE_OCCUPIED) {
496 int[] pt = new int[2];
497 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700498 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800499 for (int i = 0; i < mCountX; i++) {
500 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700501 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800502 cellToPoint(i, j, pt);
503 canvas.save();
504 canvas.translate(pt[0], pt[1]);
505 cd.draw(canvas);
506 canvas.restore();
507 }
508 }
509 }
510 }
511
Adam Cohenefca0272016-02-24 19:19:06 -0800512 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
513 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
514 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
515 canvas.save();
516 canvas.translate(mTempLocation[0], mTempLocation[1]);
517 bg.drawBackground(canvas, mFolderBgPaint);
Adam Cohenf172b742016-03-30 19:28:34 -0700518 if (!bg.isClipping) {
519 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
520 }
Adam Cohenefca0272016-02-24 19:19:06 -0800521 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700522 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700523
Adam Cohenefca0272016-02-24 19:19:06 -0800524 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
525 cellToPoint(mFolderLeaveBehind.delegateCellX,
526 mFolderLeaveBehind.delegateCellY, mTempLocation);
527 canvas.save();
528 canvas.translate(mTempLocation[0], mTempLocation[1]);
529 mFolderLeaveBehind.drawLeaveBehind(canvas, mFolderBgPaint);
530 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700531 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700532 }
533
Adam Cohenefca0272016-02-24 19:19:06 -0800534 @Override
535 protected void dispatchDraw(Canvas canvas) {
536 super.dispatchDraw(canvas);
537
538 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
539 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700540 if (bg.isClipping) {
541 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
542 canvas.save();
543 canvas.translate(mTempLocation[0], mTempLocation[1]);
544 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
545 canvas.restore();
546 }
Adam Cohenefca0272016-02-24 19:19:06 -0800547 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700548 }
549
Adam Cohenefca0272016-02-24 19:19:06 -0800550 public void addFolderBackground(FolderIcon.PreviewBackground bg) {
551 mFolderBackgrounds.add(bg);
552 }
553 public void removeFolderBackground(FolderIcon.PreviewBackground bg) {
554 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700555 }
556
Adam Cohenc51934b2011-07-26 21:07:43 -0700557 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800558
559 DeviceProfile grid = mLauncher.getDeviceProfile();
560 View child = getChildAt(x, y);
561
562 mFolderLeaveBehind.setup(getResources().getDisplayMetrics(), grid, null,
563 child.getMeasuredWidth(), child.getPaddingTop());
564
565 mFolderLeaveBehind.delegateCellX = x;
566 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700567 invalidate();
568 }
569
570 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800571 mFolderLeaveBehind.delegateCellX = -1;
572 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700573 invalidate();
574 }
575
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700576 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700577 public boolean shouldDelayChildPressedState() {
578 return false;
579 }
580
Adam Cohen1462de32012-07-24 22:34:36 -0700581 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700582 try {
583 dispatchRestoreInstanceState(states);
584 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700585 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700586 throw ex;
587 }
588 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
589 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
590 }
Adam Cohen1462de32012-07-24 22:34:36 -0700591 }
592
Michael Jurkae6235dd2011-10-04 15:02:05 -0700593 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700594 public void cancelLongPress() {
595 super.cancelLongPress();
596
597 // Cancel long press for all children
598 final int count = getChildCount();
599 for (int i = 0; i < count; i++) {
600 final View child = getChildAt(i);
601 child.cancelLongPress();
602 }
603 }
604
Michael Jurkadee05892010-07-27 10:01:56 -0700605 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
606 mInterceptTouchListener = listener;
607 }
608
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800609 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700610 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
612
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800613 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700614 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 }
616
Sunny Goyalc13403c2016-11-18 23:44:48 -0800617 public boolean acceptsWidget() {
618 return mContainerType == WORKSPACE;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700619 }
620
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800621 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700622 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700623 final LayoutParams lp = params;
624
Andrew Flynnde38e422012-05-08 11:22:15 -0700625 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800626 if (child instanceof BubbleTextView) {
627 BubbleTextView bubbleChild = (BubbleTextView) child;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800628 bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800629 }
630
Sunny Goyalc13403c2016-11-18 23:44:48 -0800631 child.setScaleX(mChildScale);
632 child.setScaleY(mChildScale);
Adam Cohen307fe232012-08-16 17:55:58 -0700633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 // Generate an id for each view, this assumes we have at most 256x256 cells
635 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700636 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700637 // If the horizontal or vertical span is set to -1, it is taken to
638 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700639 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
640 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641
Winson Chungaafa03c2010-06-11 17:34:16 -0700642 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700643 if (LOGD) {
644 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
645 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700646 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700647
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700648 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700649
Winson Chungaafa03c2010-06-11 17:34:16 -0700650 return true;
651 }
652 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700657 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700658 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700659 }
660
661 @Override
662 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700663 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700664 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700665 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700666 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700667 }
668
669 @Override
670 public void removeView(View view) {
671 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700672 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700673 }
674
675 @Override
676 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700677 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
678 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700679 }
680
681 @Override
682 public void removeViewInLayout(View view) {
683 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700684 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700685 }
686
687 @Override
688 public void removeViews(int start, int count) {
689 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700690 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700691 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700692 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700693 }
694
695 @Override
696 public void removeViewsInLayout(int start, int count) {
697 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700698 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700699 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700700 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800701 }
702
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700703 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700704 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 * @param x X coordinate of the point
706 * @param y Y coordinate of the point
707 * @param result Array of 2 ints to hold the x and y coordinate of the cell
708 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700709 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700710 final int hStartPadding = getPaddingLeft();
711 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530713 result[0] = (x - hStartPadding) / mCellWidth;
714 result[1] = (y - vStartPadding) / mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715
Adam Cohend22015c2010-07-26 22:02:18 -0700716 final int xAxis = mCountX;
717 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718
719 if (result[0] < 0) result[0] = 0;
720 if (result[0] >= xAxis) result[0] = xAxis - 1;
721 if (result[1] < 0) result[1] = 0;
722 if (result[1] >= yAxis) result[1] = yAxis - 1;
723 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700724
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 /**
726 * Given a point, return the cell that most closely encloses that point
727 * @param x X coordinate of the point
728 * @param y Y coordinate of the point
729 * @param result Array of 2 ints to hold the x and y coordinate of the cell
730 */
731 void pointToCellRounded(int x, int y, int[] result) {
732 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
733 }
734
735 /**
736 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700737 *
738 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700740 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 * @param result Array of 2 ints to hold the x and y coordinate of the point
742 */
743 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700744 final int hStartPadding = getPaddingLeft();
745 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530747 result[0] = hStartPadding + cellX * mCellWidth;
748 result[1] = vStartPadding + cellY * mCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 }
750
Adam Cohene3e27a82011-04-15 12:07:39 -0700751 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800752 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700753 *
754 * @param cellX X coordinate of the cell
755 * @param cellY Y coordinate of the cell
756 *
757 * @param result Array of 2 ints to hold the x and y coordinate of the point
758 */
759 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700760 regionToCenterPoint(cellX, cellY, 1, 1, result);
761 }
762
763 /**
764 * Given a cell coordinate and span return the point that represents the center of the regio
765 *
766 * @param cellX X coordinate of the cell
767 * @param cellY Y coordinate of the cell
768 *
769 * @param result Array of 2 ints to hold the x and y coordinate of the point
770 */
771 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700772 final int hStartPadding = getPaddingLeft();
773 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530774 result[0] = hStartPadding + cellX * mCellWidth + (spanX * mCellWidth) / 2;
775 result[1] = vStartPadding + cellY * mCellHeight + (spanY * mCellHeight) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700776 }
777
Adam Cohen19f37922012-03-21 11:59:11 -0700778 /**
779 * Given a cell coordinate and span fills out a corresponding pixel rect
780 *
781 * @param cellX X coordinate of the cell
782 * @param cellY Y coordinate of the cell
783 * @param result Rect in which to write the result
784 */
785 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
786 final int hStartPadding = getPaddingLeft();
787 final int vStartPadding = getPaddingTop();
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530788 final int left = hStartPadding + cellX * mCellWidth;
789 final int top = vStartPadding + cellY * mCellHeight;
790 result.set(left, top, left + (spanX * mCellWidth), top + (spanY * mCellHeight));
Adam Cohen19f37922012-03-21 11:59:11 -0700791 }
792
Adam Cohen482ed822012-03-02 14:15:13 -0800793 public float getDistanceFromCell(float x, float y, int[] cell) {
794 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700795 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800796 }
797
Adam Cohenf9c184a2016-01-15 16:47:43 -0800798 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800799 return mCellWidth;
800 }
801
802 int getCellHeight() {
803 return mCellHeight;
804 }
805
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700806 public void setFixedSize(int width, int height) {
807 mFixedWidth = width;
808 mFixedHeight = height;
809 }
810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 @Override
812 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700815 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
816 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700817 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
818 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700819 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700820 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
821 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700822 if (cw != mCellWidth || ch != mCellHeight) {
823 mCellWidth = cw;
824 mCellHeight = ch;
Sunny Goyalaa8a8712016-11-20 15:26:01 +0530825 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700826 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700827 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700828
Winson Chung2d75f122013-09-23 16:53:31 -0700829 int newWidth = childWidthSize;
830 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700831 if (mFixedWidth > 0 && mFixedHeight > 0) {
832 newWidth = mFixedWidth;
833 newHeight = mFixedHeight;
834 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
836 }
837
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700838 // Make the feedback view large enough to hold the blur bitmap.
839 mTouchFeedbackView.measure(
840 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
841 MeasureSpec.EXACTLY),
842 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
843 MeasureSpec.EXACTLY));
844
845 mShortcutsAndWidgets.measure(
846 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
847 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
848
849 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
850 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700851 if (mFixedWidth > 0 && mFixedHeight > 0) {
852 setMeasuredDimension(maxWidth, maxHeight);
853 } else {
854 setMeasuredDimension(widthSize, heightSize);
855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 }
857
858 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700859 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800860 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
861 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
862 int left = getPaddingLeft();
863 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800864 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800865 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700866 int right = r - l - getPaddingRight();
867 if (!isFullscreen) {
868 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
869 }
870
Winson Chung38848ca2013-10-08 12:03:44 -0700871 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700872 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700873
874 mTouchFeedbackView.layout(left, top,
875 left + mTouchFeedbackView.getMeasuredWidth(),
876 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700877 mShortcutsAndWidgets.layout(left, top, right, bottom);
878
879 // Expand the background drawing bounds by the padding baked into the background drawable
880 mBackground.getPadding(mTempRect);
881 mBackground.setBounds(
882 left - mTempRect.left,
883 top - mTempRect.top,
884 right + mTempRect.right,
885 bottom + mTempRect.bottom);
886 }
887
Tony Wickhama501d492015-11-03 18:05:01 -0800888 /**
889 * Returns the amount of space left over after subtracting padding and cells. This space will be
890 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
891 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
892 */
893 public int getUnusedHorizontalSpace() {
894 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
895 }
896
Michael Jurka5f1c5092010-09-03 14:15:02 -0700897 public float getBackgroundAlpha() {
898 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700899 }
900
Michael Jurka5f1c5092010-09-03 14:15:02 -0700901 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800902 if (mBackgroundAlpha != alpha) {
903 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700904 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800905 }
Michael Jurkadee05892010-07-27 10:01:56 -0700906 }
907
Sunny Goyal2805e632015-05-20 15:35:32 -0700908 @Override
909 protected boolean verifyDrawable(Drawable who) {
910 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
911 }
912
Michael Jurkaa52570f2012-03-20 03:18:20 -0700913 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700914 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700915 }
916
Michael Jurkaa52570f2012-03-20 03:18:20 -0700917 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700918 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700919 }
920
Patrick Dubroy440c3602010-07-13 17:50:32 -0700921 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700922 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700923 }
924
Adam Cohen76fc0852011-06-17 13:26:23 -0700925 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800926 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700927 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800928
Adam Cohen19f37922012-03-21 11:59:11 -0700929 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700930 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
931 final ItemInfo info = (ItemInfo) child.getTag();
932
933 // We cancel any existing animations
934 if (mReorderAnimators.containsKey(lp)) {
935 mReorderAnimators.get(lp).cancel();
936 mReorderAnimators.remove(lp);
937 }
938
Adam Cohen482ed822012-03-02 14:15:13 -0800939 final int oldX = lp.x;
940 final int oldY = lp.y;
941 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700942 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
943 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
944 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800945 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700946 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800947 if (permanent) {
948 lp.cellX = info.cellX = cellX;
949 lp.cellY = info.cellY = cellY;
950 } else {
951 lp.tmpCellX = cellX;
952 lp.tmpCellY = cellY;
953 }
Jon Mirandae96798e2016-12-07 12:10:44 -0800954 clc.setupLp(child);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700955 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800956 final int newX = lp.x;
957 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700958
Adam Cohen76fc0852011-06-17 13:26:23 -0700959 lp.x = oldX;
960 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700961
Adam Cohen482ed822012-03-02 14:15:13 -0800962 // Exit early if we're not actually moving the view
963 if (oldX == newX && oldY == newY) {
964 lp.isLockedToGrid = true;
965 return true;
966 }
967
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100968 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800969 va.setDuration(duration);
970 mReorderAnimators.put(lp, va);
971
972 va.addUpdateListener(new AnimatorUpdateListener() {
973 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700974 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -0800975 float r = (Float) animation.getAnimatedValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700976 lp.x = (int) ((1 - r) * oldX + r * newX);
977 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700978 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700979 }
980 });
Adam Cohen482ed822012-03-02 14:15:13 -0800981 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700982 boolean cancelled = false;
983 public void onAnimationEnd(Animator animation) {
984 // If the animation was cancelled, it means that another animation
985 // has interrupted this one, and we don't want to lock the item into
986 // place just yet.
987 if (!cancelled) {
988 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800989 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700990 }
991 if (mReorderAnimators.containsKey(lp)) {
992 mReorderAnimators.remove(lp);
993 }
994 }
995 public void onAnimationCancel(Animator animation) {
996 cancelled = true;
997 }
998 });
Adam Cohen482ed822012-03-02 14:15:13 -0800999 va.setStartDelay(delay);
1000 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001001 return true;
1002 }
1003 return false;
1004 }
1005
Sunny Goyal06e21a22016-08-11 16:02:02 -07001006 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
1007 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001008 final int oldDragCellX = mDragCell[0];
1009 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001010
Hyunyoung Song0de01172016-10-05 16:27:48 -07001011 if (outlineProvider == null || outlineProvider.generatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001012 return;
1013 }
1014
Hyunyoung Song0de01172016-10-05 16:27:48 -07001015 Bitmap dragOutline = outlineProvider.generatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -08001016 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001017 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1018 Rect dragRegion = dragObject.dragView.getDragRegion();
1019
Adam Cohen482ed822012-03-02 14:15:13 -08001020 mDragCell[0] = cellX;
1021 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001022
Joe Onorato4be866d2010-10-10 11:26:02 -07001023 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001024 mDragOutlineAnims[oldIndex].animateOut();
1025 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001026 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001027
Adam Cohend41fbf52012-02-16 23:53:59 -08001028 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001029 cellToRect(cellX, cellY, spanX, spanY, r);
Jon Mirandae96798e2016-12-07 12:10:44 -08001030 if (v instanceof LauncherAppWidgetHostView) {
1031 DeviceProfile profile = mLauncher.getDeviceProfile();
1032 Utilities.shrinkRectAboutCenter(r, profile.appWidgetScale.x,
1033 profile.appWidgetScale.y);
1034 }
Sunny Goyal106bf642015-07-16 12:18:06 -07001035 } else {
1036 // Find the top left corner of the rect the object will occupy
1037 final int[] topLeft = mTmpPoint;
1038 cellToPoint(cellX, cellY, topLeft);
1039
1040 int left = topLeft[0];
1041 int top = topLeft[1];
1042
1043 if (v != null && dragOffset == null) {
1044 // When drawing the drag outline, it did not account for margin offsets
1045 // added by the view's parent.
1046 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1047 left += lp.leftMargin;
1048 top += lp.topMargin;
1049
1050 // Offsets due to the size difference between the View and the dragOutline.
1051 // There is a size difference to account for the outer blur, which may lie
1052 // outside the bounds of the view.
Jon Mirandaf7ff3fe2016-12-05 12:04:44 -08001053 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001054 // We center about the x axis
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301055 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001056 } else {
1057 if (dragOffset != null && dragRegion != null) {
1058 // Center the drag region *horizontally* in the cell and apply a drag
1059 // outline offset
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301060 left += dragOffset.x + ((mCellWidth * spanX) - dragRegion.width()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001061 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1062 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1063 top += dragOffset.y + cellPaddingY;
1064 } else {
1065 // Center the drag outline in the cell
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301066 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
1067 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001068 }
1069 }
1070 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001071 }
Winson Chung150fbab2010-09-29 17:14:26 -07001072
Jon Mirandae96798e2016-12-07 12:10:44 -08001073 Utilities.shrinkRectAboutCenter(r, mChildScale, mChildScale);
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001074 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1075 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001076
1077 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001078 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001079 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001080 }
1081 }
1082
Sunny Goyalc13403c2016-11-18 23:44:48 -08001083 public String getItemMoveDescription(int cellX, int cellY) {
1084 if (mContainerType == HOTSEAT) {
1085 return getContext().getString(R.string.move_to_hotseat_position,
1086 Math.max(cellX, cellY) + 1);
1087 } else {
1088 return getContext().getString(R.string.move_to_empty_cell,
1089 cellY + 1, cellX + 1);
1090 }
1091 }
1092
Adam Cohene0310962011-04-18 16:15:31 -07001093 public void clearDragOutlines() {
1094 final int oldIndex = mDragOutlineCurrent;
1095 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001096 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001097 }
1098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001100 * Find a vacant area that will fit the given bounds nearest the requested
1101 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001102 *
Romain Guy51afc022009-05-04 18:03:43 -07001103 * @param pixelX The X location at which you want to search for a vacant area.
1104 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001105 * @param minSpanX The minimum horizontal span required
1106 * @param minSpanY The minimum vertical span required
1107 * @param spanX Horizontal span of the object.
1108 * @param spanY Vertical span of the object.
1109 * @param result Array in which to place the result, or null (in which case a new array will
1110 * be allocated)
1111 * @return The X, Y cell of a vacant area that can contain this object,
1112 * nearest the requested location.
1113 */
1114 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1115 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001116 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001117 result, resultSpan);
1118 }
1119
Adam Cohend41fbf52012-02-16 23:53:59 -08001120 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1121 private void lazyInitTempRectStack() {
1122 if (mTempRectStack.isEmpty()) {
1123 for (int i = 0; i < mCountX * mCountY; i++) {
1124 mTempRectStack.push(new Rect());
1125 }
1126 }
1127 }
Adam Cohen482ed822012-03-02 14:15:13 -08001128
Adam Cohend41fbf52012-02-16 23:53:59 -08001129 private void recycleTempRects(Stack<Rect> used) {
1130 while (!used.isEmpty()) {
1131 mTempRectStack.push(used.pop());
1132 }
1133 }
1134
1135 /**
1136 * Find a vacant area that will fit the given bounds nearest the requested
1137 * cell location. Uses Euclidean distance to score multiple vacant areas.
1138 *
1139 * @param pixelX The X location at which you want to search for a vacant area.
1140 * @param pixelY The Y location at which you want to search for a vacant area.
1141 * @param minSpanX The minimum horizontal span required
1142 * @param minSpanY The minimum vertical span required
1143 * @param spanX Horizontal span of the object.
1144 * @param spanY Vertical span of the object.
1145 * @param ignoreOccupied If true, the result can be an occupied cell
1146 * @param result Array in which to place the result, or null (in which case a new array will
1147 * be allocated)
1148 * @return The X, Y cell of a vacant area that can contain this object,
1149 * nearest the requested location.
1150 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001151 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1152 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001153 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001154
Adam Cohene3e27a82011-04-15 12:07:39 -07001155 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1156 // to the center of the item, but we are searching based on the top-left cell, so
1157 // we translate the point over to correspond to the top-left.
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301158 pixelX -= mCellWidth * (spanX - 1) / 2f;
1159 pixelY -= mCellHeight * (spanY - 1) / 2f;
Adam Cohene3e27a82011-04-15 12:07:39 -07001160
Jeff Sharkey70864282009-04-07 21:08:40 -07001161 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001162 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001163 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001164 final Rect bestRect = new Rect(-1, -1, -1, -1);
1165 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001166
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001167 final int countX = mCountX;
1168 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001169
Adam Cohend41fbf52012-02-16 23:53:59 -08001170 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1171 spanX < minSpanX || spanY < minSpanY) {
1172 return bestXY;
1173 }
1174
1175 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001176 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001177 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1178 int ySize = -1;
1179 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001180 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001181 // First, let's see if this thing fits anywhere
1182 for (int i = 0; i < minSpanX; i++) {
1183 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001184 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001185 continue inner;
1186 }
Michael Jurkac28de512010-08-13 11:27:44 -07001187 }
1188 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001189 xSize = minSpanX;
1190 ySize = minSpanY;
1191
1192 // We know that the item will fit at _some_ acceptable size, now let's see
1193 // how big we can make it. We'll alternate between incrementing x and y spans
1194 // until we hit a limit.
1195 boolean incX = true;
1196 boolean hitMaxX = xSize >= spanX;
1197 boolean hitMaxY = ySize >= spanY;
1198 while (!(hitMaxX && hitMaxY)) {
1199 if (incX && !hitMaxX) {
1200 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001201 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001202 // We can't move out horizontally
1203 hitMaxX = true;
1204 }
1205 }
1206 if (!hitMaxX) {
1207 xSize++;
1208 }
1209 } else if (!hitMaxY) {
1210 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001211 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001212 // We can't move out vertically
1213 hitMaxY = true;
1214 }
1215 }
1216 if (!hitMaxY) {
1217 ySize++;
1218 }
1219 }
1220 hitMaxX |= xSize >= spanX;
1221 hitMaxY |= ySize >= spanY;
1222 incX = !incX;
1223 }
1224 incX = true;
1225 hitMaxX = xSize >= spanX;
1226 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001227 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001228 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001229 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230
Adam Cohend41fbf52012-02-16 23:53:59 -08001231 // We verify that the current rect is not a sub-rect of any of our previous
1232 // candidates. In this case, the current rect is disqualified in favour of the
1233 // containing rect.
1234 Rect currentRect = mTempRectStack.pop();
1235 currentRect.set(x, y, x + xSize, y + ySize);
1236 boolean contained = false;
1237 for (Rect r : validRegions) {
1238 if (r.contains(currentRect)) {
1239 contained = true;
1240 break;
1241 }
1242 }
1243 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001244 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001245
Adam Cohend41fbf52012-02-16 23:53:59 -08001246 if ((distance <= bestDistance && !contained) ||
1247 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001248 bestDistance = distance;
1249 bestXY[0] = x;
1250 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001251 if (resultSpan != null) {
1252 resultSpan[0] = xSize;
1253 resultSpan[1] = ySize;
1254 }
1255 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001256 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 }
1258 }
1259
Adam Cohenc0dcf592011-06-01 15:30:43 -07001260 // Return -1, -1 if no suitable location found
1261 if (bestDistance == Double.MAX_VALUE) {
1262 bestXY[0] = -1;
1263 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001264 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001265 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001266 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001268
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001269 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001270 * Find a vacant area that will fit the given bounds nearest the requested
1271 * cell location, and will also weigh in a suggested direction vector of the
1272 * desired location. This method computers distance based on unit grid distances,
1273 * not pixel distances.
1274 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001275 * @param cellX The X cell nearest to which you want to search for a vacant area.
1276 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001277 * @param spanX Horizontal span of the object.
1278 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001279 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001280 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001281 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001282 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001283 * @param result Array in which to place the result, or null (in which case a new array will
1284 * be allocated)
1285 * @return The X, Y cell of a vacant area that can contain this object,
1286 * nearest the requested location.
1287 */
1288 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001289 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001290 // Keep track of best-scoring drop area
1291 final int[] bestXY = result != null ? result : new int[2];
1292 float bestDistance = Float.MAX_VALUE;
1293 int bestDirectionScore = Integer.MIN_VALUE;
1294
1295 final int countX = mCountX;
1296 final int countY = mCountY;
1297
1298 for (int y = 0; y < countY - (spanY - 1); y++) {
1299 inner:
1300 for (int x = 0; x < countX - (spanX - 1); x++) {
1301 // First, let's see if this thing fits anywhere
1302 for (int i = 0; i < spanX; i++) {
1303 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001304 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001305 continue inner;
1306 }
1307 }
1308 }
1309
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001310 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001311 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001312 computeDirectionVector(x - cellX, y - cellY, curDirection);
1313 // The direction score is just the dot product of the two candidate direction
1314 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001315 int curDirectionScore = direction[0] * curDirection[0] +
1316 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001317 if (Float.compare(distance, bestDistance) < 0 ||
1318 (Float.compare(distance, bestDistance) == 0
1319 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001320 bestDistance = distance;
1321 bestDirectionScore = curDirectionScore;
1322 bestXY[0] = x;
1323 bestXY[1] = y;
1324 }
1325 }
1326 }
1327
1328 // Return -1, -1 if no suitable location found
1329 if (bestDistance == Float.MAX_VALUE) {
1330 bestXY[0] = -1;
1331 bestXY[1] = -1;
1332 }
1333 return bestXY;
1334 }
1335
1336 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001337 int[] direction, ItemConfiguration currentState) {
1338 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001339 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001340 mTmpOccupied.markCells(c, false);
1341 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001342
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001343 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1344 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001345
1346 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001347 c.cellX = mTempLocation[0];
1348 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001349 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001350 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001351 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001352 return success;
1353 }
1354
Adam Cohenf3900c22012-11-16 18:28:11 -08001355 /**
1356 * This helper class defines a cluster of views. It helps with defining complex edges
1357 * of the cluster and determining how those edges interact with other views. The edges
1358 * essentially define a fine-grained boundary around the cluster of views -- like a more
1359 * precise version of a bounding box.
1360 */
1361 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001362 final static int LEFT = 1 << 0;
1363 final static int TOP = 1 << 1;
1364 final static int RIGHT = 1 << 2;
1365 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001366
Adam Cohenf3900c22012-11-16 18:28:11 -08001367 ArrayList<View> views;
1368 ItemConfiguration config;
1369 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001370
Adam Cohenf3900c22012-11-16 18:28:11 -08001371 int[] leftEdge = new int[mCountY];
1372 int[] rightEdge = new int[mCountY];
1373 int[] topEdge = new int[mCountX];
1374 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001375 int dirtyEdges;
1376 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001377
1378 @SuppressWarnings("unchecked")
1379 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1380 this.views = (ArrayList<View>) views.clone();
1381 this.config = config;
1382 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001383 }
1384
Adam Cohenf3900c22012-11-16 18:28:11 -08001385 void resetEdges() {
1386 for (int i = 0; i < mCountX; i++) {
1387 topEdge[i] = -1;
1388 bottomEdge[i] = -1;
1389 }
1390 for (int i = 0; i < mCountY; i++) {
1391 leftEdge[i] = -1;
1392 rightEdge[i] = -1;
1393 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001394 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001395 boundingRectDirty = true;
1396 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001397
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001398 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001399 int count = views.size();
1400 for (int i = 0; i < count; i++) {
1401 CellAndSpan cs = config.map.get(views.get(i));
1402 switch (which) {
1403 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001404 int left = cs.cellX;
1405 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001406 if (left < leftEdge[j] || leftEdge[j] < 0) {
1407 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001408 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001409 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001410 break;
1411 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001412 int right = cs.cellX + cs.spanX;
1413 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001414 if (right > rightEdge[j]) {
1415 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001416 }
1417 }
1418 break;
1419 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001420 int top = cs.cellY;
1421 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001422 if (top < topEdge[j] || topEdge[j] < 0) {
1423 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001424 }
1425 }
1426 break;
1427 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001428 int bottom = cs.cellY + cs.spanY;
1429 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001430 if (bottom > bottomEdge[j]) {
1431 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001432 }
1433 }
1434 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001435 }
1436 }
1437 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001438
1439 boolean isViewTouchingEdge(View v, int whichEdge) {
1440 CellAndSpan cs = config.map.get(v);
1441
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001442 if ((dirtyEdges & whichEdge) == whichEdge) {
1443 computeEdge(whichEdge);
1444 dirtyEdges &= ~whichEdge;
1445 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001446
1447 switch (whichEdge) {
1448 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001449 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1450 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001451 return true;
1452 }
1453 }
1454 break;
1455 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001456 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1457 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001458 return true;
1459 }
1460 }
1461 break;
1462 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001463 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1464 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001465 return true;
1466 }
1467 }
1468 break;
1469 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001470 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1471 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001472 return true;
1473 }
1474 }
1475 break;
1476 }
1477 return false;
1478 }
1479
1480 void shift(int whichEdge, int delta) {
1481 for (View v: views) {
1482 CellAndSpan c = config.map.get(v);
1483 switch (whichEdge) {
1484 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001485 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001486 break;
1487 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001488 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001489 break;
1490 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001491 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001492 break;
1493 case BOTTOM:
1494 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001495 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001496 break;
1497 }
1498 }
1499 resetEdges();
1500 }
1501
1502 public void addView(View v) {
1503 views.add(v);
1504 resetEdges();
1505 }
1506
1507 public Rect getBoundingRect() {
1508 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001509 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001510 }
1511 return boundingRect;
1512 }
1513
Adam Cohenf3900c22012-11-16 18:28:11 -08001514 PositionComparator comparator = new PositionComparator();
1515 class PositionComparator implements Comparator<View> {
1516 int whichEdge = 0;
1517 public int compare(View left, View right) {
1518 CellAndSpan l = config.map.get(left);
1519 CellAndSpan r = config.map.get(right);
1520 switch (whichEdge) {
1521 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001522 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001523 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001524 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001525 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001526 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001527 case BOTTOM:
1528 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001529 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001530 }
1531 }
1532 }
1533
1534 public void sortConfigurationForEdgePush(int edge) {
1535 comparator.whichEdge = edge;
1536 Collections.sort(config.sortedViews, comparator);
1537 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001538 }
1539
Adam Cohenf3900c22012-11-16 18:28:11 -08001540 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1541 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001542
Adam Cohenf3900c22012-11-16 18:28:11 -08001543 ViewCluster cluster = new ViewCluster(views, currentState);
1544 Rect clusterRect = cluster.getBoundingRect();
1545 int whichEdge;
1546 int pushDistance;
1547 boolean fail = false;
1548
1549 // Determine the edge of the cluster that will be leading the push and how far
1550 // the cluster must be shifted.
1551 if (direction[0] < 0) {
1552 whichEdge = ViewCluster.LEFT;
1553 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001554 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001555 whichEdge = ViewCluster.RIGHT;
1556 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1557 } else if (direction[1] < 0) {
1558 whichEdge = ViewCluster.TOP;
1559 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1560 } else {
1561 whichEdge = ViewCluster.BOTTOM;
1562 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001563 }
1564
Adam Cohenf3900c22012-11-16 18:28:11 -08001565 // Break early for invalid push distance.
1566 if (pushDistance <= 0) {
1567 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001568 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001569
1570 // Mark the occupied state as false for the group of views we want to move.
1571 for (View v: views) {
1572 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001573 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001574 }
1575
1576 // We save the current configuration -- if we fail to find a solution we will revert
1577 // to the initial state. The process of finding a solution modifies the configuration
1578 // in place, hence the need for revert in the failure case.
1579 currentState.save();
1580
1581 // The pushing algorithm is simplified by considering the views in the order in which
1582 // they would be pushed by the cluster. For example, if the cluster is leading with its
1583 // left edge, we consider sort the views by their right edge, from right to left.
1584 cluster.sortConfigurationForEdgePush(whichEdge);
1585
1586 while (pushDistance > 0 && !fail) {
1587 for (View v: currentState.sortedViews) {
1588 // For each view that isn't in the cluster, we see if the leading edge of the
1589 // cluster is contacting the edge of that view. If so, we add that view to the
1590 // cluster.
1591 if (!cluster.views.contains(v) && v != dragView) {
1592 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1593 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1594 if (!lp.canReorder) {
1595 // The push solution includes the all apps button, this is not viable.
1596 fail = true;
1597 break;
1598 }
1599 cluster.addView(v);
1600 CellAndSpan c = currentState.map.get(v);
1601
1602 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001603 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001604 }
1605 }
1606 }
1607 pushDistance--;
1608
1609 // The cluster has been completed, now we move the whole thing over in the appropriate
1610 // direction.
1611 cluster.shift(whichEdge, 1);
1612 }
1613
1614 boolean foundSolution = false;
1615 clusterRect = cluster.getBoundingRect();
1616
1617 // Due to the nature of the algorithm, the only check required to verify a valid solution
1618 // is to ensure that completed shifted cluster lies completely within the cell layout.
1619 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1620 clusterRect.bottom <= mCountY) {
1621 foundSolution = true;
1622 } else {
1623 currentState.restore();
1624 }
1625
1626 // In either case, we set the occupied array as marked for the location of the views
1627 for (View v: cluster.views) {
1628 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001629 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001630 }
1631
1632 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001633 }
1634
Adam Cohen482ed822012-03-02 14:15:13 -08001635 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001636 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001637 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001638
Adam Cohen8baab352012-03-20 17:39:21 -07001639 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001640 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001641 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001642 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001643
Adam Cohen8baab352012-03-20 17:39:21 -07001644 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001645 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001646 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001647 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001648 }
1649
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001650 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001651 int top = boundingRect.top;
1652 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001653 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001654 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001655 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001656 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001657 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001658 }
1659
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001660 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001661
Adam Cohenf3900c22012-11-16 18:28:11 -08001662 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001663 boundingRect.height(), direction,
1664 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001665
Adam Cohen8baab352012-03-20 17:39:21 -07001666 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001667 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001668 int deltaX = mTempLocation[0] - boundingRect.left;
1669 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001670 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001671 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001672 c.cellX += deltaX;
1673 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001674 }
1675 success = true;
1676 }
Adam Cohen8baab352012-03-20 17:39:21 -07001677
1678 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001679 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001680 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001681 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001682 }
1683 return success;
1684 }
1685
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001686 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1687 // to push items in each of the cardinal directions, in an order based on the direction vector
1688 // passed.
1689 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1690 int[] direction, View ignoreView, ItemConfiguration solution) {
1691 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001692 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001693 // separately in each of the components.
1694 int temp = direction[1];
1695 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001696
1697 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001698 ignoreView, solution)) {
1699 return true;
1700 }
1701 direction[1] = temp;
1702 temp = direction[0];
1703 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001704
1705 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001706 ignoreView, solution)) {
1707 return true;
1708 }
1709 // Revert the direction
1710 direction[0] = temp;
1711
1712 // Now we try pushing in each component of the opposite direction
1713 direction[0] *= -1;
1714 direction[1] *= -1;
1715 temp = direction[1];
1716 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001717 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001718 ignoreView, solution)) {
1719 return true;
1720 }
1721
1722 direction[1] = temp;
1723 temp = direction[0];
1724 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001725 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001726 ignoreView, solution)) {
1727 return true;
1728 }
1729 // revert the direction
1730 direction[0] = temp;
1731 direction[0] *= -1;
1732 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001733
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001734 } else {
1735 // If the direction vector has a single non-zero component, we push first in the
1736 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001737 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001738 ignoreView, solution)) {
1739 return true;
1740 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001741 // Then we try the opposite direction
1742 direction[0] *= -1;
1743 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001744 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001745 ignoreView, solution)) {
1746 return true;
1747 }
1748 // Switch the direction back
1749 direction[0] *= -1;
1750 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001751
1752 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001753 // to find a solution by pushing along the perpendicular axis.
1754
1755 // Swap the components
1756 int temp = direction[1];
1757 direction[1] = direction[0];
1758 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001759 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001760 ignoreView, solution)) {
1761 return true;
1762 }
1763
1764 // Then we try the opposite direction
1765 direction[0] *= -1;
1766 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001767 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001768 ignoreView, solution)) {
1769 return true;
1770 }
1771 // Switch the direction back
1772 direction[0] *= -1;
1773 direction[1] *= -1;
1774
1775 // Swap the components back
1776 temp = direction[1];
1777 direction[1] = direction[0];
1778 direction[0] = temp;
1779 }
1780 return false;
1781 }
1782
Adam Cohen482ed822012-03-02 14:15:13 -08001783 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001784 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001785 // Return early if get invalid cell positions
1786 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001787
Adam Cohen8baab352012-03-20 17:39:21 -07001788 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001789 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001790
Adam Cohen8baab352012-03-20 17:39:21 -07001791 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001792 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001793 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001794 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001795 c.cellX = cellX;
1796 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001797 }
Adam Cohen482ed822012-03-02 14:15:13 -08001798 }
Adam Cohen482ed822012-03-02 14:15:13 -08001799 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1800 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001801 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001802 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001803 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001804 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001805 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001806 if (Rect.intersects(r0, r1)) {
1807 if (!lp.canReorder) {
1808 return false;
1809 }
1810 mIntersectingViews.add(child);
1811 }
1812 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001813
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001814 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1815
Winson Chung5f8afe62013-08-12 16:19:28 -07001816 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001817 // we try to find a solution such that no displaced item travels through another item
1818 // without also displacing that item.
1819 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001820 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001821 return true;
1822 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001823
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001824 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001825 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001826 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001827 return true;
1828 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001829
Adam Cohen482ed822012-03-02 14:15:13 -08001830 // Ok, they couldn't move as a block, let's move them individually
1831 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001832 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001833 return false;
1834 }
1835 }
1836 return true;
1837 }
1838
1839 /*
1840 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1841 * the provided point and the provided cell
1842 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001843 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001844 double angle = Math.atan(deltaY / deltaX);
Adam Cohen482ed822012-03-02 14:15:13 -08001845
1846 result[0] = 0;
1847 result[1] = 0;
1848 if (Math.abs(Math.cos(angle)) > 0.5f) {
1849 result[0] = (int) Math.signum(deltaX);
1850 }
1851 if (Math.abs(Math.sin(angle)) > 0.5f) {
1852 result[1] = (int) Math.signum(deltaY);
1853 }
1854 }
1855
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001856 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001857 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1858 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001859 // Copy the current state into the solution. This solution will be manipulated as necessary.
1860 copyCurrentStateToSolution(solution, false);
1861 // Copy the current occupied array into the temporary occupied array. This array will be
1862 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001863 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001864
1865 // We find the nearest cell into which we would place the dragged item, assuming there's
1866 // nothing in its way.
1867 int result[] = new int[2];
1868 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1869
1870 boolean success = false;
1871 // First we try the exact nearest position of the item being dragged,
1872 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001873 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1874 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001875
1876 if (!success) {
1877 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1878 // x, then 1 in y etc.
1879 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001880 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1881 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001882 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001883 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1884 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001885 }
1886 solution.isSolution = false;
1887 } else {
1888 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001889 solution.cellX = result[0];
1890 solution.cellY = result[1];
1891 solution.spanX = spanX;
1892 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001893 }
1894 return solution;
1895 }
1896
1897 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001898 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001899 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001900 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001901 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001902 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001903 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001904 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001905 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001906 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001907 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001908 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001909 }
1910 }
1911
1912 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001913 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001914
Michael Jurkaa52570f2012-03-20 03:18:20 -07001915 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001916 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001917 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001918 if (child == dragView) continue;
1919 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001920 CellAndSpan c = solution.map.get(child);
1921 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001922 lp.tmpCellX = c.cellX;
1923 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001924 lp.cellHSpan = c.spanX;
1925 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001926 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001927 }
1928 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001929 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001930 }
1931
1932 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1933 commitDragView) {
1934
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001935 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1936 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001937
Michael Jurkaa52570f2012-03-20 03:18:20 -07001938 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001939 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001940 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001941 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001942 CellAndSpan c = solution.map.get(child);
1943 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001944 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001945 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001946 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001947 }
1948 }
1949 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001950 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001951 }
1952 }
1953
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001954
1955 // This method starts or changes the reorder preview animations
1956 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1957 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001958 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001959 for (int i = 0; i < childCount; i++) {
1960 View child = mShortcutsAndWidgets.getChildAt(i);
1961 if (child == dragView) continue;
1962 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001963 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1964 != null && !solution.intersectingViews.contains(child);
1965
Adam Cohen19f37922012-03-21 11:59:11 -07001966 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001967 if (c != null && !skip) {
1968 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001969 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07001970 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001971 }
1972 }
1973 }
1974
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001975 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07001976 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001977 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07001978 View child;
Adam Cohend024f982012-05-23 18:26:45 -07001979 float finalDeltaX;
1980 float finalDeltaY;
1981 float initDeltaX;
1982 float initDeltaY;
1983 float finalScale;
1984 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001985 int mode;
1986 boolean repeating = false;
1987 private static final int PREVIEW_DURATION = 300;
1988 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
1989
1990 public static final int MODE_HINT = 0;
1991 public static final int MODE_PREVIEW = 1;
1992
Adam Cohene7587d22012-05-24 18:50:02 -07001993 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07001994
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001995 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
1996 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07001997 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1998 final int x0 = mTmpPoint[0];
1999 final int y0 = mTmpPoint[1];
2000 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2001 final int x1 = mTmpPoint[0];
2002 final int y1 = mTmpPoint[1];
2003 final int dX = x1 - x0;
2004 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002005 finalDeltaX = 0;
2006 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002007 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002008 if (dX == dY && dX == 0) {
2009 } else {
2010 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002011 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002012 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002013 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002014 } else {
2015 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002016 finalDeltaX = (int) (- dir * Math.signum(dX) *
2017 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2018 finalDeltaY = (int) (- dir * Math.signum(dY) *
2019 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002020 }
2021 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002022 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002023 initDeltaX = child.getTranslationX();
2024 initDeltaY = child.getTranslationY();
Sunny Goyalc13403c2016-11-18 23:44:48 -08002025 finalScale = mChildScale - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002026 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002027 this.child = child;
2028 }
2029
Adam Cohend024f982012-05-23 18:26:45 -07002030 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002031 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002032 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002033 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002034 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002035 if (finalDeltaX == 0 && finalDeltaY == 0) {
2036 completeAnimationImmediately();
2037 return;
2038 }
Adam Cohen19f37922012-03-21 11:59:11 -07002039 }
Adam Cohend024f982012-05-23 18:26:45 -07002040 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002041 return;
2042 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002043 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002044 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002045
2046 // Animations are disabled in power save mode, causing the repeated animation to jump
2047 // spastically between beginning and end states. Since this looks bad, we don't repeat
2048 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002049 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002050 va.setRepeatMode(ValueAnimator.REVERSE);
2051 va.setRepeatCount(ValueAnimator.INFINITE);
2052 }
2053
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002054 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002055 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002056 va.addUpdateListener(new AnimatorUpdateListener() {
2057 @Override
2058 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08002059 float r = (Float) animation.getAnimatedValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002060 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2061 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2062 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002063 child.setTranslationX(x);
2064 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002065 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002066 child.setScaleX(s);
2067 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002068 }
2069 });
2070 va.addListener(new AnimatorListenerAdapter() {
2071 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002072 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002073 initDeltaX = 0;
2074 initDeltaY = 0;
Sunny Goyalc13403c2016-11-18 23:44:48 -08002075 initScale = mChildScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002076 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002077 }
2078 });
Adam Cohen19f37922012-03-21 11:59:11 -07002079 mShakeAnimators.put(child, this);
2080 va.start();
2081 }
2082
Adam Cohend024f982012-05-23 18:26:45 -07002083 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002084 if (a != null) {
2085 a.cancel();
2086 }
Adam Cohen19f37922012-03-21 11:59:11 -07002087 }
Adam Cohene7587d22012-05-24 18:50:02 -07002088
Adam Cohen091440a2015-03-18 14:16:05 -07002089 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002090 if (a != null) {
2091 a.cancel();
2092 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002093
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002094 a = new LauncherViewPropertyAnimator(child)
Sunny Goyalc13403c2016-11-18 23:44:48 -08002095 .scaleX(mChildScale)
2096 .scaleY(mChildScale)
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002097 .translationX(0)
2098 .translationY(0)
2099 .setDuration(REORDER_ANIMATION_DURATION);
2100 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2101 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002102 }
Adam Cohen19f37922012-03-21 11:59:11 -07002103 }
2104
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002105 private void completeAndClearReorderPreviewAnimations() {
2106 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002107 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002108 }
2109 mShakeAnimators.clear();
2110 }
2111
Adam Cohen482ed822012-03-02 14:15:13 -08002112 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002113 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002114
2115 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2116 int container = Favorites.CONTAINER_DESKTOP;
2117
Sunny Goyalc13403c2016-11-18 23:44:48 -08002118 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002119 screenId = -1;
2120 container = Favorites.CONTAINER_HOTSEAT;
2121 }
2122
Michael Jurkaa52570f2012-03-20 03:18:20 -07002123 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002124 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002125 View child = mShortcutsAndWidgets.getChildAt(i);
2126 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2127 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002128 // We do a null check here because the item info can be null in the case of the
2129 // AllApps button in the hotseat.
2130 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002131 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2132 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2133 || info.spanY != lp.cellVSpan);
2134
Adam Cohen2acce882012-03-28 19:03:19 -07002135 info.cellX = lp.cellX = lp.tmpCellX;
2136 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002137 info.spanX = lp.cellHSpan;
2138 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002139
2140 if (requiresDbUpdate) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002141 LauncherModel.modifyItemInDatabase(getContext(), info, container, screenId,
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002142 info.cellX, info.cellY, info.spanX, info.spanY);
2143 }
Adam Cohen2acce882012-03-28 19:03:19 -07002144 }
Adam Cohen482ed822012-03-02 14:15:13 -08002145 }
2146 }
2147
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002148 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002149 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002150 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002151 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002152 lp.useTmpCoords = useTempCoords;
2153 }
2154 }
2155
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002156 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002157 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2158 int[] result = new int[2];
2159 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002160 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002161 resultSpan);
2162 if (result[0] >= 0 && result[1] >= 0) {
2163 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002164 solution.cellX = result[0];
2165 solution.cellY = result[1];
2166 solution.spanX = resultSpan[0];
2167 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002168 solution.isSolution = true;
2169 } else {
2170 solution.isSolution = false;
2171 }
2172 return solution;
2173 }
2174
Adam Cohen19f37922012-03-21 11:59:11 -07002175 /* This seems like it should be obvious and straight-forward, but when the direction vector
2176 needs to match with the notion of the dragView pushing other views, we have to employ
2177 a slightly more subtle notion of the direction vector. The question is what two points is
2178 the vector between? The center of the dragView and its desired destination? Not quite, as
2179 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2180 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2181 or right, which helps make pushing feel right.
2182 */
2183 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2184 int spanY, View dragView, int[] resultDirection) {
2185 int[] targetDestination = new int[2];
2186
2187 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2188 Rect dragRect = new Rect();
2189 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2190 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2191
2192 Rect dropRegionRect = new Rect();
2193 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2194 dragView, dropRegionRect, mIntersectingViews);
2195
2196 int dropRegionSpanX = dropRegionRect.width();
2197 int dropRegionSpanY = dropRegionRect.height();
2198
2199 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2200 dropRegionRect.height(), dropRegionRect);
2201
2202 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2203 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2204
2205 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2206 deltaX = 0;
2207 }
2208 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2209 deltaY = 0;
2210 }
2211
2212 if (deltaX == 0 && deltaY == 0) {
2213 // No idea what to do, give a random direction.
2214 resultDirection[0] = 1;
2215 resultDirection[1] = 0;
2216 } else {
2217 computeDirectionVector(deltaX, deltaY, resultDirection);
2218 }
2219 }
2220
2221 // For a given cell and span, fetch the set of views intersecting the region.
2222 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2223 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2224 if (boundingRect != null) {
2225 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2226 }
2227 intersectingViews.clear();
2228 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2229 Rect r1 = new Rect();
2230 final int count = mShortcutsAndWidgets.getChildCount();
2231 for (int i = 0; i < count; i++) {
2232 View child = mShortcutsAndWidgets.getChildAt(i);
2233 if (child == dragView) continue;
2234 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2235 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2236 if (Rect.intersects(r0, r1)) {
2237 mIntersectingViews.add(child);
2238 if (boundingRect != null) {
2239 boundingRect.union(r1);
2240 }
2241 }
2242 }
2243 }
2244
2245 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2246 View dragView, int[] result) {
2247 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2248 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2249 mIntersectingViews);
2250 return !mIntersectingViews.isEmpty();
2251 }
2252
2253 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002254 completeAndClearReorderPreviewAnimations();
2255 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2256 final int count = mShortcutsAndWidgets.getChildCount();
2257 for (int i = 0; i < count; i++) {
2258 View child = mShortcutsAndWidgets.getChildAt(i);
2259 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2260 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2261 lp.tmpCellX = lp.cellX;
2262 lp.tmpCellY = lp.cellY;
2263 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2264 0, false, false);
2265 }
Adam Cohen19f37922012-03-21 11:59:11 -07002266 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002267 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002268 }
Adam Cohen19f37922012-03-21 11:59:11 -07002269 }
2270
Adam Cohenbebf0422012-04-11 18:06:28 -07002271 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2272 View dragView, int[] direction, boolean commit) {
2273 int[] pixelXY = new int[2];
2274 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2275
2276 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002277 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002278 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2279
2280 setUseTempCoords(true);
2281 if (swapSolution != null && swapSolution.isSolution) {
2282 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2283 // committing anything or animating anything as we just want to determine if a solution
2284 // exists
2285 copySolutionToTempState(swapSolution, dragView);
2286 setItemPlacementDirty(true);
2287 animateItemsToSolution(swapSolution, dragView, commit);
2288
2289 if (commit) {
2290 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002291 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002292 setItemPlacementDirty(false);
2293 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002294 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2295 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002296 }
2297 mShortcutsAndWidgets.requestLayout();
2298 }
2299 return swapSolution.isSolution;
2300 }
2301
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002302 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002303 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002304 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002305 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002306
2307 if (resultSpan == null) {
2308 resultSpan = new int[2];
2309 }
2310
Adam Cohen19f37922012-03-21 11:59:11 -07002311 // When we are checking drop validity or actually dropping, we don't recompute the
2312 // direction vector, since we want the solution to match the preview, and it's possible
2313 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002314 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2315 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002316 mDirectionVector[0] = mPreviousReorderDirection[0];
2317 mDirectionVector[1] = mPreviousReorderDirection[1];
2318 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002319 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2320 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2321 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002322 }
2323 } else {
2324 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2325 mPreviousReorderDirection[0] = mDirectionVector[0];
2326 mPreviousReorderDirection[1] = mDirectionVector[1];
2327 }
2328
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002329 // Find a solution involving pushing / displacing any items in the way
2330 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002331 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2332
2333 // We attempt the approach which doesn't shuffle views at all
2334 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2335 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2336
2337 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002338
2339 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2340 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002341 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2342 finalSolution = swapSolution;
2343 } else if (noShuffleSolution.isSolution) {
2344 finalSolution = noShuffleSolution;
2345 }
2346
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002347 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002348 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002349 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2350 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002351 result[0] = finalSolution.cellX;
2352 result[1] = finalSolution.cellY;
2353 resultSpan[0] = finalSolution.spanX;
2354 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002355 } else {
2356 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2357 }
2358 return result;
2359 }
2360
Adam Cohen482ed822012-03-02 14:15:13 -08002361 boolean foundSolution = true;
2362 if (!DESTRUCTIVE_REORDER) {
2363 setUseTempCoords(true);
2364 }
2365
2366 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002367 result[0] = finalSolution.cellX;
2368 result[1] = finalSolution.cellY;
2369 resultSpan[0] = finalSolution.spanX;
2370 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002371
2372 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2373 // committing anything or animating anything as we just want to determine if a solution
2374 // exists
2375 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2376 if (!DESTRUCTIVE_REORDER) {
2377 copySolutionToTempState(finalSolution, dragView);
2378 }
2379 setItemPlacementDirty(true);
2380 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2381
Adam Cohen19f37922012-03-21 11:59:11 -07002382 if (!DESTRUCTIVE_REORDER &&
2383 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002384 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002385 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002386 setItemPlacementDirty(false);
2387 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002388 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2389 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002390 }
2391 }
2392 } else {
2393 foundSolution = false;
2394 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2395 }
2396
2397 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2398 setUseTempCoords(false);
2399 }
Adam Cohen482ed822012-03-02 14:15:13 -08002400
Michael Jurkaa52570f2012-03-20 03:18:20 -07002401 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002402 return result;
2403 }
2404
Adam Cohen19f37922012-03-21 11:59:11 -07002405 void setItemPlacementDirty(boolean dirty) {
2406 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002407 }
Adam Cohen19f37922012-03-21 11:59:11 -07002408 boolean isItemPlacementDirty() {
2409 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002410 }
2411
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002412 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002413 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002414 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2415 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002416 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002417 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002418
Adam Cohenf3900c22012-11-16 18:28:11 -08002419 void save() {
2420 // Copy current state into savedMap
2421 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002422 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002423 }
2424 }
2425
2426 void restore() {
2427 // Restore current state from savedMap
2428 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002429 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002430 }
2431 }
2432
2433 void add(View v, CellAndSpan cs) {
2434 map.put(v, cs);
2435 savedMap.put(v, new CellAndSpan());
2436 sortedViews.add(v);
2437 }
2438
Adam Cohen482ed822012-03-02 14:15:13 -08002439 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002440 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002441 }
2442
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002443 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2444 boolean first = true;
2445 for (View v: views) {
2446 CellAndSpan c = map.get(v);
2447 if (first) {
2448 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2449 first = false;
2450 } else {
2451 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2452 }
2453 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002454 }
Adam Cohen482ed822012-03-02 14:15:13 -08002455 }
2456
Adam Cohendf035382011-04-11 17:22:04 -07002457 /**
Adam Cohendf035382011-04-11 17:22:04 -07002458 * Find a starting cell position that will fit the given bounds nearest the requested
2459 * cell location. Uses Euclidean distance to score multiple vacant areas.
2460 *
2461 * @param pixelX The X location at which you want to search for a vacant area.
2462 * @param pixelY The Y location at which you want to search for a vacant area.
2463 * @param spanX Horizontal span of the object.
2464 * @param spanY Vertical span of the object.
2465 * @param ignoreView Considers space occupied by this view as unoccupied
2466 * @param result Previously returned value to possibly recycle.
2467 * @return The X, Y cell of a vacant area that can contain this object,
2468 * nearest the requested location.
2469 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002470 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002471 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002472 }
2473
Michael Jurka0280c3b2010-09-17 15:00:07 -07002474 boolean existsEmptyCell() {
2475 return findCellForSpan(null, 1, 1);
2476 }
2477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002479 * Finds the upper-left coordinate of the first rectangle in the grid that can
2480 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2481 * then this method will only return coordinates for rectangles that contain the cell
2482 * (intersectX, intersectY)
2483 *
2484 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2485 * can be found.
2486 * @param spanX The horizontal span of the cell we want to find.
2487 * @param spanY The vertical span of the cell we want to find.
2488 *
2489 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002490 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002491 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002492 if (cellXY == null) {
2493 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002494 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002495 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002496 }
2497
2498 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002499 * A drag event has begun over this layout.
2500 * It may have begun over this layout (in which case onDragChild is called first),
2501 * or it may have begun on another layout.
2502 */
2503 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002504 mDragging = true;
2505 }
2506
2507 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002508 * Called when drag has left this CellLayout or has been completed (successfully or not)
2509 */
2510 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002511 // This can actually be called when we aren't in a drag, e.g. when adding a new
2512 // item to this layout via the customize drawer.
2513 // Guard against that case.
2514 if (mDragging) {
2515 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002516 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002517
2518 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002519 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002520 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2521 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002522 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002523 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002524 }
2525
2526 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002527 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002528 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002529 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530 *
2531 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002532 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002533 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002534 if (child != null) {
2535 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002536 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002537 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002538 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002539 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 }
2541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002542 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002544 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 * @param cellX X coordinate of upper left corner expressed as a cell position
2546 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002547 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002549 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002550 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002551 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 final int cellWidth = mCellWidth;
2553 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002554
Winson Chung4b825dcd2011-06-19 12:41:22 -07002555 final int hStartPadding = getPaddingLeft();
2556 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002557
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302558 int width = cellHSpan * cellWidth;
2559 int height = cellVSpan * cellHeight;
2560 int x = hStartPadding + cellX * cellWidth;
2561 int y = vStartPadding + cellY * cellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002562
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002563 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002565
Adam Cohend4844c32011-02-18 19:25:06 -08002566 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002567 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002568 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002569 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002570 }
2571
Adam Cohend4844c32011-02-18 19:25:06 -08002572 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002573 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002574 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002575 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 }
2577
Adam Cohen2801caf2011-05-13 20:57:39 -07002578 public int getDesiredWidth() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302579 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth);
Adam Cohen2801caf2011-05-13 20:57:39 -07002580 }
2581
2582 public int getDesiredHeight() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302583 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight);
Adam Cohen2801caf2011-05-13 20:57:39 -07002584 }
2585
Michael Jurka66d72172011-04-12 16:29:25 -07002586 public boolean isOccupied(int x, int y) {
2587 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002588 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002589 } else {
2590 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2591 }
2592 }
2593
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002594 @Override
2595 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2596 return new CellLayout.LayoutParams(getContext(), attrs);
2597 }
2598
2599 @Override
2600 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2601 return p instanceof CellLayout.LayoutParams;
2602 }
2603
2604 @Override
2605 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2606 return new CellLayout.LayoutParams(p);
2607 }
2608
2609 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2610 /**
2611 * Horizontal location of the item in the grid.
2612 */
2613 @ViewDebug.ExportedProperty
2614 public int cellX;
2615
2616 /**
2617 * Vertical location of the item in the grid.
2618 */
2619 @ViewDebug.ExportedProperty
2620 public int cellY;
2621
2622 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002623 * Temporary horizontal location of the item in the grid during reorder
2624 */
2625 public int tmpCellX;
2626
2627 /**
2628 * Temporary vertical location of the item in the grid during reorder
2629 */
2630 public int tmpCellY;
2631
2632 /**
2633 * Indicates that the temporary coordinates should be used to layout the items
2634 */
2635 public boolean useTmpCoords;
2636
2637 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002638 * Number of cells spanned horizontally by the item.
2639 */
2640 @ViewDebug.ExportedProperty
2641 public int cellHSpan;
2642
2643 /**
2644 * Number of cells spanned vertically by the item.
2645 */
2646 @ViewDebug.ExportedProperty
2647 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002648
Adam Cohen1b607ed2011-03-03 17:26:50 -08002649 /**
2650 * Indicates whether the item will set its x, y, width and height parameters freely,
2651 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2652 */
Adam Cohend4844c32011-02-18 19:25:06 -08002653 public boolean isLockedToGrid = true;
2654
Adam Cohen482ed822012-03-02 14:15:13 -08002655 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002656 * Indicates that this item should use the full extents of its parent.
2657 */
2658 public boolean isFullscreen = false;
2659
2660 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002661 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002662 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002663 */
2664 public boolean canReorder = true;
2665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002666 // X coordinate of the view in the layout.
2667 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002668 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002669 // Y coordinate of the view in the layout.
2670 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002671 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002672
Romain Guy84f296c2009-11-04 15:00:44 -08002673 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002674
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002675 public LayoutParams(Context c, AttributeSet attrs) {
2676 super(c, attrs);
2677 cellHSpan = 1;
2678 cellVSpan = 1;
2679 }
2680
2681 public LayoutParams(ViewGroup.LayoutParams source) {
2682 super(source);
2683 cellHSpan = 1;
2684 cellVSpan = 1;
2685 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002686
2687 public LayoutParams(LayoutParams source) {
2688 super(source);
2689 this.cellX = source.cellX;
2690 this.cellY = source.cellY;
2691 this.cellHSpan = source.cellHSpan;
2692 this.cellVSpan = source.cellVSpan;
2693 }
2694
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002695 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002696 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697 this.cellX = cellX;
2698 this.cellY = cellY;
2699 this.cellHSpan = cellHSpan;
2700 this.cellVSpan = cellVSpan;
2701 }
2702
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302703 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) {
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002704 setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f);
2705 }
2706
2707 /**
2708 * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs
2709 * to be scaled.
2710 *
2711 * ie. In multi-window mode, we setup widgets so that they are measured and laid out
2712 * using their full/invariant device profile sizes.
2713 */
2714 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount,
2715 float cellScaleX, float cellScaleY) {
Adam Cohend4844c32011-02-18 19:25:06 -08002716 if (isLockedToGrid) {
2717 final int myCellHSpan = cellHSpan;
2718 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002719 int myCellX = useTmpCoords ? tmpCellX : cellX;
2720 int myCellY = useTmpCoords ? tmpCellY : cellY;
2721
2722 if (invertHorizontally) {
2723 myCellX = colCount - myCellX - cellHSpan;
2724 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002725
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002726 width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin);
2727 height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin);
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302728 x = (myCellX * cellWidth + leftMargin);
2729 y = (myCellY * cellHeight + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002730 }
2731 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002732
Winson Chungaafa03c2010-06-11 17:34:16 -07002733 public String toString() {
2734 return "(" + this.cellX + ", " + this.cellY + ")";
2735 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002736
2737 public void setWidth(int width) {
2738 this.width = width;
2739 }
2740
2741 public int getWidth() {
2742 return width;
2743 }
2744
2745 public void setHeight(int height) {
2746 this.height = height;
2747 }
2748
2749 public int getHeight() {
2750 return height;
2751 }
2752
2753 public void setX(int x) {
2754 this.x = x;
2755 }
2756
2757 public int getX() {
2758 return x;
2759 }
2760
2761 public void setY(int y) {
2762 this.y = y;
2763 }
2764
2765 public int getY() {
2766 return y;
2767 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002768 }
2769
Michael Jurka0280c3b2010-09-17 15:00:07 -07002770 // This class stores info for two purposes:
2771 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2772 // its spanX, spanY, and the screen it is on
2773 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2774 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2775 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002776 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002777 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002778 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002779 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002780
Sunny Goyal83a8f042015-05-19 12:52:12 -07002781 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002782 cellX = info.cellX;
2783 cellY = info.cellY;
2784 spanX = info.spanX;
2785 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002786 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002787 screenId = info.screenId;
2788 container = info.container;
2789 }
2790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002791 @Override
2792 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002793 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2794 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002795 }
2796 }
Michael Jurkad771c962011-08-09 15:00:48 -07002797
Tony Wickham86930612015-09-09 13:50:40 -07002798 /**
2799 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2800 * if necessary).
2801 */
2802 public boolean hasReorderSolution(ItemInfo itemInfo) {
2803 int[] cellPoint = new int[2];
2804 // Check for a solution starting at every cell.
2805 for (int cellX = 0; cellX < getCountX(); cellX++) {
2806 for (int cellY = 0; cellY < getCountY(); cellY++) {
2807 cellToPoint(cellX, cellY, cellPoint);
2808 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2809 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2810 true, new ItemConfiguration()).isSolution) {
2811 return true;
2812 }
2813 }
2814 }
2815 return false;
2816 }
2817
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002818 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002819 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002821}