blob: 11273fab6d86d309c6b29cc2ab45d29261daa1ee [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();
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001032 Utilities.shrinkRect(r, profile.appWidgetScale.x, profile.appWidgetScale.y);
Jon Mirandae96798e2016-12-07 12:10:44 -08001033 }
Sunny Goyal106bf642015-07-16 12:18:06 -07001034 } else {
1035 // Find the top left corner of the rect the object will occupy
1036 final int[] topLeft = mTmpPoint;
1037 cellToPoint(cellX, cellY, topLeft);
1038
1039 int left = topLeft[0];
1040 int top = topLeft[1];
1041
1042 if (v != null && dragOffset == null) {
1043 // When drawing the drag outline, it did not account for margin offsets
1044 // added by the view's parent.
1045 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1046 left += lp.leftMargin;
1047 top += lp.topMargin;
1048
1049 // Offsets due to the size difference between the View and the dragOutline.
1050 // There is a size difference to account for the outer blur, which may lie
1051 // outside the bounds of the view.
Jon Mirandaf7ff3fe2016-12-05 12:04:44 -08001052 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001053 // We center about the x axis
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301054 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001055 } else {
1056 if (dragOffset != null && dragRegion != null) {
1057 // Center the drag region *horizontally* in the cell and apply a drag
1058 // outline offset
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301059 left += dragOffset.x + ((mCellWidth * spanX) - dragRegion.width()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001060 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1061 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1062 top += dragOffset.y + cellPaddingY;
1063 } else {
1064 // Center the drag outline in the cell
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301065 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
1066 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001067 }
1068 }
1069 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001070 }
Winson Chung150fbab2010-09-29 17:14:26 -07001071
Jon Miranda6f6a06a2016-12-15 11:24:18 -08001072 Utilities.scaleRectAboutCenter(r, mChildScale);
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001073 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1074 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001075
1076 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001077 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001078 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001079 }
1080 }
1081
Sunny Goyalc13403c2016-11-18 23:44:48 -08001082 public String getItemMoveDescription(int cellX, int cellY) {
1083 if (mContainerType == HOTSEAT) {
1084 return getContext().getString(R.string.move_to_hotseat_position,
1085 Math.max(cellX, cellY) + 1);
1086 } else {
1087 return getContext().getString(R.string.move_to_empty_cell,
1088 cellY + 1, cellX + 1);
1089 }
1090 }
1091
Adam Cohene0310962011-04-18 16:15:31 -07001092 public void clearDragOutlines() {
1093 final int oldIndex = mDragOutlineCurrent;
1094 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001095 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001096 }
1097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001099 * Find a vacant area that will fit the given bounds nearest the requested
1100 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001101 *
Romain Guy51afc022009-05-04 18:03:43 -07001102 * @param pixelX The X location at which you want to search for a vacant area.
1103 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001104 * @param minSpanX The minimum horizontal span required
1105 * @param minSpanY The minimum vertical span required
1106 * @param spanX Horizontal span of the object.
1107 * @param spanY Vertical span of the object.
1108 * @param result Array in which to place the result, or null (in which case a new array will
1109 * be allocated)
1110 * @return The X, Y cell of a vacant area that can contain this object,
1111 * nearest the requested location.
1112 */
1113 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1114 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001115 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001116 result, resultSpan);
1117 }
1118
Adam Cohend41fbf52012-02-16 23:53:59 -08001119 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1120 private void lazyInitTempRectStack() {
1121 if (mTempRectStack.isEmpty()) {
1122 for (int i = 0; i < mCountX * mCountY; i++) {
1123 mTempRectStack.push(new Rect());
1124 }
1125 }
1126 }
Adam Cohen482ed822012-03-02 14:15:13 -08001127
Adam Cohend41fbf52012-02-16 23:53:59 -08001128 private void recycleTempRects(Stack<Rect> used) {
1129 while (!used.isEmpty()) {
1130 mTempRectStack.push(used.pop());
1131 }
1132 }
1133
1134 /**
1135 * Find a vacant area that will fit the given bounds nearest the requested
1136 * cell location. Uses Euclidean distance to score multiple vacant areas.
1137 *
1138 * @param pixelX The X location at which you want to search for a vacant area.
1139 * @param pixelY The Y location at which you want to search for a vacant area.
1140 * @param minSpanX The minimum horizontal span required
1141 * @param minSpanY The minimum vertical span required
1142 * @param spanX Horizontal span of the object.
1143 * @param spanY Vertical span of the object.
1144 * @param ignoreOccupied If true, the result can be an occupied cell
1145 * @param result Array in which to place the result, or null (in which case a new array will
1146 * be allocated)
1147 * @return The X, Y cell of a vacant area that can contain this object,
1148 * nearest the requested location.
1149 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001150 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1151 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001152 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001153
Adam Cohene3e27a82011-04-15 12:07:39 -07001154 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1155 // to the center of the item, but we are searching based on the top-left cell, so
1156 // we translate the point over to correspond to the top-left.
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301157 pixelX -= mCellWidth * (spanX - 1) / 2f;
1158 pixelY -= mCellHeight * (spanY - 1) / 2f;
Adam Cohene3e27a82011-04-15 12:07:39 -07001159
Jeff Sharkey70864282009-04-07 21:08:40 -07001160 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001161 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001162 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001163 final Rect bestRect = new Rect(-1, -1, -1, -1);
1164 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001165
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001166 final int countX = mCountX;
1167 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001168
Adam Cohend41fbf52012-02-16 23:53:59 -08001169 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1170 spanX < minSpanX || spanY < minSpanY) {
1171 return bestXY;
1172 }
1173
1174 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001175 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001176 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1177 int ySize = -1;
1178 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001179 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001180 // First, let's see if this thing fits anywhere
1181 for (int i = 0; i < minSpanX; i++) {
1182 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001183 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001184 continue inner;
1185 }
Michael Jurkac28de512010-08-13 11:27:44 -07001186 }
1187 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001188 xSize = minSpanX;
1189 ySize = minSpanY;
1190
1191 // We know that the item will fit at _some_ acceptable size, now let's see
1192 // how big we can make it. We'll alternate between incrementing x and y spans
1193 // until we hit a limit.
1194 boolean incX = true;
1195 boolean hitMaxX = xSize >= spanX;
1196 boolean hitMaxY = ySize >= spanY;
1197 while (!(hitMaxX && hitMaxY)) {
1198 if (incX && !hitMaxX) {
1199 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001200 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001201 // We can't move out horizontally
1202 hitMaxX = true;
1203 }
1204 }
1205 if (!hitMaxX) {
1206 xSize++;
1207 }
1208 } else if (!hitMaxY) {
1209 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001210 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001211 // We can't move out vertically
1212 hitMaxY = true;
1213 }
1214 }
1215 if (!hitMaxY) {
1216 ySize++;
1217 }
1218 }
1219 hitMaxX |= xSize >= spanX;
1220 hitMaxY |= ySize >= spanY;
1221 incX = !incX;
1222 }
1223 incX = true;
1224 hitMaxX = xSize >= spanX;
1225 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001226 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001227 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001228 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229
Adam Cohend41fbf52012-02-16 23:53:59 -08001230 // We verify that the current rect is not a sub-rect of any of our previous
1231 // candidates. In this case, the current rect is disqualified in favour of the
1232 // containing rect.
1233 Rect currentRect = mTempRectStack.pop();
1234 currentRect.set(x, y, x + xSize, y + ySize);
1235 boolean contained = false;
1236 for (Rect r : validRegions) {
1237 if (r.contains(currentRect)) {
1238 contained = true;
1239 break;
1240 }
1241 }
1242 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001243 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001244
Adam Cohend41fbf52012-02-16 23:53:59 -08001245 if ((distance <= bestDistance && !contained) ||
1246 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001247 bestDistance = distance;
1248 bestXY[0] = x;
1249 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001250 if (resultSpan != null) {
1251 resultSpan[0] = xSize;
1252 resultSpan[1] = ySize;
1253 }
1254 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 }
1257 }
1258
Adam Cohenc0dcf592011-06-01 15:30:43 -07001259 // Return -1, -1 if no suitable location found
1260 if (bestDistance == Double.MAX_VALUE) {
1261 bestXY[0] = -1;
1262 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001263 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001264 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001265 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001267
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001268 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001269 * Find a vacant area that will fit the given bounds nearest the requested
1270 * cell location, and will also weigh in a suggested direction vector of the
1271 * desired location. This method computers distance based on unit grid distances,
1272 * not pixel distances.
1273 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001274 * @param cellX The X cell nearest to which you want to search for a vacant area.
1275 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001276 * @param spanX Horizontal span of the object.
1277 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001278 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001279 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001280 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001281 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001282 * @param result Array in which to place the result, or null (in which case a new array will
1283 * be allocated)
1284 * @return The X, Y cell of a vacant area that can contain this object,
1285 * nearest the requested location.
1286 */
1287 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001288 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001289 // Keep track of best-scoring drop area
1290 final int[] bestXY = result != null ? result : new int[2];
1291 float bestDistance = Float.MAX_VALUE;
1292 int bestDirectionScore = Integer.MIN_VALUE;
1293
1294 final int countX = mCountX;
1295 final int countY = mCountY;
1296
1297 for (int y = 0; y < countY - (spanY - 1); y++) {
1298 inner:
1299 for (int x = 0; x < countX - (spanX - 1); x++) {
1300 // First, let's see if this thing fits anywhere
1301 for (int i = 0; i < spanX; i++) {
1302 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001303 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001304 continue inner;
1305 }
1306 }
1307 }
1308
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001309 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001310 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001311 computeDirectionVector(x - cellX, y - cellY, curDirection);
1312 // The direction score is just the dot product of the two candidate direction
1313 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001314 int curDirectionScore = direction[0] * curDirection[0] +
1315 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001316 if (Float.compare(distance, bestDistance) < 0 ||
1317 (Float.compare(distance, bestDistance) == 0
1318 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001319 bestDistance = distance;
1320 bestDirectionScore = curDirectionScore;
1321 bestXY[0] = x;
1322 bestXY[1] = y;
1323 }
1324 }
1325 }
1326
1327 // Return -1, -1 if no suitable location found
1328 if (bestDistance == Float.MAX_VALUE) {
1329 bestXY[0] = -1;
1330 bestXY[1] = -1;
1331 }
1332 return bestXY;
1333 }
1334
1335 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001336 int[] direction, ItemConfiguration currentState) {
1337 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001338 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001339 mTmpOccupied.markCells(c, false);
1340 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001341
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001342 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1343 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001344
1345 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001346 c.cellX = mTempLocation[0];
1347 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001348 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001349 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001350 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001351 return success;
1352 }
1353
Adam Cohenf3900c22012-11-16 18:28:11 -08001354 /**
1355 * This helper class defines a cluster of views. It helps with defining complex edges
1356 * of the cluster and determining how those edges interact with other views. The edges
1357 * essentially define a fine-grained boundary around the cluster of views -- like a more
1358 * precise version of a bounding box.
1359 */
1360 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001361 final static int LEFT = 1 << 0;
1362 final static int TOP = 1 << 1;
1363 final static int RIGHT = 1 << 2;
1364 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001365
Adam Cohenf3900c22012-11-16 18:28:11 -08001366 ArrayList<View> views;
1367 ItemConfiguration config;
1368 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001369
Adam Cohenf3900c22012-11-16 18:28:11 -08001370 int[] leftEdge = new int[mCountY];
1371 int[] rightEdge = new int[mCountY];
1372 int[] topEdge = new int[mCountX];
1373 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001374 int dirtyEdges;
1375 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001376
1377 @SuppressWarnings("unchecked")
1378 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1379 this.views = (ArrayList<View>) views.clone();
1380 this.config = config;
1381 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001382 }
1383
Adam Cohenf3900c22012-11-16 18:28:11 -08001384 void resetEdges() {
1385 for (int i = 0; i < mCountX; i++) {
1386 topEdge[i] = -1;
1387 bottomEdge[i] = -1;
1388 }
1389 for (int i = 0; i < mCountY; i++) {
1390 leftEdge[i] = -1;
1391 rightEdge[i] = -1;
1392 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001393 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001394 boundingRectDirty = true;
1395 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001396
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001397 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001398 int count = views.size();
1399 for (int i = 0; i < count; i++) {
1400 CellAndSpan cs = config.map.get(views.get(i));
1401 switch (which) {
1402 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001403 int left = cs.cellX;
1404 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001405 if (left < leftEdge[j] || leftEdge[j] < 0) {
1406 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001407 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001408 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001409 break;
1410 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001411 int right = cs.cellX + cs.spanX;
1412 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001413 if (right > rightEdge[j]) {
1414 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001415 }
1416 }
1417 break;
1418 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001419 int top = cs.cellY;
1420 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001421 if (top < topEdge[j] || topEdge[j] < 0) {
1422 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001423 }
1424 }
1425 break;
1426 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001427 int bottom = cs.cellY + cs.spanY;
1428 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001429 if (bottom > bottomEdge[j]) {
1430 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001431 }
1432 }
1433 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001434 }
1435 }
1436 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001437
1438 boolean isViewTouchingEdge(View v, int whichEdge) {
1439 CellAndSpan cs = config.map.get(v);
1440
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001441 if ((dirtyEdges & whichEdge) == whichEdge) {
1442 computeEdge(whichEdge);
1443 dirtyEdges &= ~whichEdge;
1444 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001445
1446 switch (whichEdge) {
1447 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001448 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1449 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001450 return true;
1451 }
1452 }
1453 break;
1454 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001455 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1456 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001457 return true;
1458 }
1459 }
1460 break;
1461 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001462 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1463 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001464 return true;
1465 }
1466 }
1467 break;
1468 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001469 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1470 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001471 return true;
1472 }
1473 }
1474 break;
1475 }
1476 return false;
1477 }
1478
1479 void shift(int whichEdge, int delta) {
1480 for (View v: views) {
1481 CellAndSpan c = config.map.get(v);
1482 switch (whichEdge) {
1483 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001484 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001485 break;
1486 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001487 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001488 break;
1489 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001490 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001491 break;
1492 case BOTTOM:
1493 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001494 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001495 break;
1496 }
1497 }
1498 resetEdges();
1499 }
1500
1501 public void addView(View v) {
1502 views.add(v);
1503 resetEdges();
1504 }
1505
1506 public Rect getBoundingRect() {
1507 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001508 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001509 }
1510 return boundingRect;
1511 }
1512
Adam Cohenf3900c22012-11-16 18:28:11 -08001513 PositionComparator comparator = new PositionComparator();
1514 class PositionComparator implements Comparator<View> {
1515 int whichEdge = 0;
1516 public int compare(View left, View right) {
1517 CellAndSpan l = config.map.get(left);
1518 CellAndSpan r = config.map.get(right);
1519 switch (whichEdge) {
1520 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001521 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001522 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001523 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001524 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001525 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001526 case BOTTOM:
1527 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001528 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001529 }
1530 }
1531 }
1532
1533 public void sortConfigurationForEdgePush(int edge) {
1534 comparator.whichEdge = edge;
1535 Collections.sort(config.sortedViews, comparator);
1536 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001537 }
1538
Adam Cohenf3900c22012-11-16 18:28:11 -08001539 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1540 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001541
Adam Cohenf3900c22012-11-16 18:28:11 -08001542 ViewCluster cluster = new ViewCluster(views, currentState);
1543 Rect clusterRect = cluster.getBoundingRect();
1544 int whichEdge;
1545 int pushDistance;
1546 boolean fail = false;
1547
1548 // Determine the edge of the cluster that will be leading the push and how far
1549 // the cluster must be shifted.
1550 if (direction[0] < 0) {
1551 whichEdge = ViewCluster.LEFT;
1552 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001553 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001554 whichEdge = ViewCluster.RIGHT;
1555 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1556 } else if (direction[1] < 0) {
1557 whichEdge = ViewCluster.TOP;
1558 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1559 } else {
1560 whichEdge = ViewCluster.BOTTOM;
1561 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001562 }
1563
Adam Cohenf3900c22012-11-16 18:28:11 -08001564 // Break early for invalid push distance.
1565 if (pushDistance <= 0) {
1566 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001567 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001568
1569 // Mark the occupied state as false for the group of views we want to move.
1570 for (View v: views) {
1571 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001572 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001573 }
1574
1575 // We save the current configuration -- if we fail to find a solution we will revert
1576 // to the initial state. The process of finding a solution modifies the configuration
1577 // in place, hence the need for revert in the failure case.
1578 currentState.save();
1579
1580 // The pushing algorithm is simplified by considering the views in the order in which
1581 // they would be pushed by the cluster. For example, if the cluster is leading with its
1582 // left edge, we consider sort the views by their right edge, from right to left.
1583 cluster.sortConfigurationForEdgePush(whichEdge);
1584
1585 while (pushDistance > 0 && !fail) {
1586 for (View v: currentState.sortedViews) {
1587 // For each view that isn't in the cluster, we see if the leading edge of the
1588 // cluster is contacting the edge of that view. If so, we add that view to the
1589 // cluster.
1590 if (!cluster.views.contains(v) && v != dragView) {
1591 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1592 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1593 if (!lp.canReorder) {
1594 // The push solution includes the all apps button, this is not viable.
1595 fail = true;
1596 break;
1597 }
1598 cluster.addView(v);
1599 CellAndSpan c = currentState.map.get(v);
1600
1601 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001602 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001603 }
1604 }
1605 }
1606 pushDistance--;
1607
1608 // The cluster has been completed, now we move the whole thing over in the appropriate
1609 // direction.
1610 cluster.shift(whichEdge, 1);
1611 }
1612
1613 boolean foundSolution = false;
1614 clusterRect = cluster.getBoundingRect();
1615
1616 // Due to the nature of the algorithm, the only check required to verify a valid solution
1617 // is to ensure that completed shifted cluster lies completely within the cell layout.
1618 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1619 clusterRect.bottom <= mCountY) {
1620 foundSolution = true;
1621 } else {
1622 currentState.restore();
1623 }
1624
1625 // In either case, we set the occupied array as marked for the location of the views
1626 for (View v: cluster.views) {
1627 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001628 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001629 }
1630
1631 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001632 }
1633
Adam Cohen482ed822012-03-02 14:15:13 -08001634 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001635 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001636 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001637
Adam Cohen8baab352012-03-20 17:39:21 -07001638 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001639 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001640 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001641 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001642
Adam Cohen8baab352012-03-20 17:39:21 -07001643 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001644 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001645 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001646 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001647 }
1648
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001649 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001650 int top = boundingRect.top;
1651 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001652 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001653 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001654 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001655 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001656 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001657 }
1658
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001659 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001660
Adam Cohenf3900c22012-11-16 18:28:11 -08001661 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001662 boundingRect.height(), direction,
1663 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001664
Adam Cohen8baab352012-03-20 17:39:21 -07001665 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001666 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001667 int deltaX = mTempLocation[0] - boundingRect.left;
1668 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001669 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001670 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001671 c.cellX += deltaX;
1672 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001673 }
1674 success = true;
1675 }
Adam Cohen8baab352012-03-20 17:39:21 -07001676
1677 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001678 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001679 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001680 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001681 }
1682 return success;
1683 }
1684
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001685 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1686 // to push items in each of the cardinal directions, in an order based on the direction vector
1687 // passed.
1688 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1689 int[] direction, View ignoreView, ItemConfiguration solution) {
1690 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001691 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001692 // separately in each of the components.
1693 int temp = direction[1];
1694 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001695
1696 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001697 ignoreView, solution)) {
1698 return true;
1699 }
1700 direction[1] = temp;
1701 temp = direction[0];
1702 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001703
1704 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001705 ignoreView, solution)) {
1706 return true;
1707 }
1708 // Revert the direction
1709 direction[0] = temp;
1710
1711 // Now we try pushing in each component of the opposite direction
1712 direction[0] *= -1;
1713 direction[1] *= -1;
1714 temp = direction[1];
1715 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001716 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001717 ignoreView, solution)) {
1718 return true;
1719 }
1720
1721 direction[1] = temp;
1722 temp = direction[0];
1723 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001724 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001725 ignoreView, solution)) {
1726 return true;
1727 }
1728 // revert the direction
1729 direction[0] = temp;
1730 direction[0] *= -1;
1731 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001732
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001733 } else {
1734 // If the direction vector has a single non-zero component, we push first in the
1735 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001736 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001737 ignoreView, solution)) {
1738 return true;
1739 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001740 // Then we try the opposite direction
1741 direction[0] *= -1;
1742 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001743 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001744 ignoreView, solution)) {
1745 return true;
1746 }
1747 // Switch the direction back
1748 direction[0] *= -1;
1749 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001750
1751 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001752 // to find a solution by pushing along the perpendicular axis.
1753
1754 // Swap the components
1755 int temp = direction[1];
1756 direction[1] = direction[0];
1757 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001758 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001759 ignoreView, solution)) {
1760 return true;
1761 }
1762
1763 // Then we try the opposite direction
1764 direction[0] *= -1;
1765 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001766 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001767 ignoreView, solution)) {
1768 return true;
1769 }
1770 // Switch the direction back
1771 direction[0] *= -1;
1772 direction[1] *= -1;
1773
1774 // Swap the components back
1775 temp = direction[1];
1776 direction[1] = direction[0];
1777 direction[0] = temp;
1778 }
1779 return false;
1780 }
1781
Adam Cohen482ed822012-03-02 14:15:13 -08001782 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001783 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001784 // Return early if get invalid cell positions
1785 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001786
Adam Cohen8baab352012-03-20 17:39:21 -07001787 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001788 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001789
Adam Cohen8baab352012-03-20 17:39:21 -07001790 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001791 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001792 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001793 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001794 c.cellX = cellX;
1795 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001796 }
Adam Cohen482ed822012-03-02 14:15:13 -08001797 }
Adam Cohen482ed822012-03-02 14:15:13 -08001798 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1799 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001800 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001801 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001802 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001803 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001804 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001805 if (Rect.intersects(r0, r1)) {
1806 if (!lp.canReorder) {
1807 return false;
1808 }
1809 mIntersectingViews.add(child);
1810 }
1811 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001812
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001813 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1814
Winson Chung5f8afe62013-08-12 16:19:28 -07001815 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001816 // we try to find a solution such that no displaced item travels through another item
1817 // without also displacing that item.
1818 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001819 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001820 return true;
1821 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001822
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001823 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001824 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001825 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001826 return true;
1827 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001828
Adam Cohen482ed822012-03-02 14:15:13 -08001829 // Ok, they couldn't move as a block, let's move them individually
1830 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001831 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001832 return false;
1833 }
1834 }
1835 return true;
1836 }
1837
1838 /*
1839 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1840 * the provided point and the provided cell
1841 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001842 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Jon Mirandae96798e2016-12-07 12:10:44 -08001843 double angle = Math.atan(deltaY / deltaX);
Adam Cohen482ed822012-03-02 14:15:13 -08001844
1845 result[0] = 0;
1846 result[1] = 0;
1847 if (Math.abs(Math.cos(angle)) > 0.5f) {
1848 result[0] = (int) Math.signum(deltaX);
1849 }
1850 if (Math.abs(Math.sin(angle)) > 0.5f) {
1851 result[1] = (int) Math.signum(deltaY);
1852 }
1853 }
1854
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001855 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001856 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1857 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001858 // Copy the current state into the solution. This solution will be manipulated as necessary.
1859 copyCurrentStateToSolution(solution, false);
1860 // Copy the current occupied array into the temporary occupied array. This array will be
1861 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001862 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001863
1864 // We find the nearest cell into which we would place the dragged item, assuming there's
1865 // nothing in its way.
1866 int result[] = new int[2];
1867 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1868
1869 boolean success = false;
1870 // First we try the exact nearest position of the item being dragged,
1871 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001872 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1873 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001874
1875 if (!success) {
1876 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1877 // x, then 1 in y etc.
1878 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001879 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1880 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001881 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001882 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1883 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001884 }
1885 solution.isSolution = false;
1886 } else {
1887 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001888 solution.cellX = result[0];
1889 solution.cellY = result[1];
1890 solution.spanX = spanX;
1891 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001892 }
1893 return solution;
1894 }
1895
1896 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001897 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001898 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001899 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001900 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001901 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001902 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001903 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001904 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001905 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001906 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001907 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001908 }
1909 }
1910
1911 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001912 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001913
Michael Jurkaa52570f2012-03-20 03:18:20 -07001914 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001915 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001916 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001917 if (child == dragView) continue;
1918 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001919 CellAndSpan c = solution.map.get(child);
1920 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001921 lp.tmpCellX = c.cellX;
1922 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001923 lp.cellHSpan = c.spanX;
1924 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001925 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001926 }
1927 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001928 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001929 }
1930
1931 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1932 commitDragView) {
1933
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001934 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1935 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001936
Michael Jurkaa52570f2012-03-20 03:18:20 -07001937 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001938 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001939 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001940 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001941 CellAndSpan c = solution.map.get(child);
1942 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001943 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001944 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001945 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001946 }
1947 }
1948 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001949 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001950 }
1951 }
1952
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001953
1954 // This method starts or changes the reorder preview animations
1955 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1956 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001957 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001958 for (int i = 0; i < childCount; i++) {
1959 View child = mShortcutsAndWidgets.getChildAt(i);
1960 if (child == dragView) continue;
1961 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001962 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1963 != null && !solution.intersectingViews.contains(child);
1964
Adam Cohen19f37922012-03-21 11:59:11 -07001965 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001966 if (c != null && !skip) {
1967 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001968 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07001969 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001970 }
1971 }
1972 }
1973
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001974 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07001975 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001976 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07001977 View child;
Adam Cohend024f982012-05-23 18:26:45 -07001978 float finalDeltaX;
1979 float finalDeltaY;
1980 float initDeltaX;
1981 float initDeltaY;
1982 float finalScale;
1983 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001984 int mode;
1985 boolean repeating = false;
1986 private static final int PREVIEW_DURATION = 300;
1987 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
1988
1989 public static final int MODE_HINT = 0;
1990 public static final int MODE_PREVIEW = 1;
1991
Adam Cohene7587d22012-05-24 18:50:02 -07001992 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07001993
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001994 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
1995 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07001996 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1997 final int x0 = mTmpPoint[0];
1998 final int y0 = mTmpPoint[1];
1999 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2000 final int x1 = mTmpPoint[0];
2001 final int y1 = mTmpPoint[1];
2002 final int dX = x1 - x0;
2003 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002004 finalDeltaX = 0;
2005 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002006 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002007 if (dX == dY && dX == 0) {
2008 } else {
2009 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002010 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002011 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002012 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002013 } else {
2014 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002015 finalDeltaX = (int) (- dir * Math.signum(dX) *
2016 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2017 finalDeltaY = (int) (- dir * Math.signum(dY) *
2018 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002019 }
2020 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002021 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002022 initDeltaX = child.getTranslationX();
2023 initDeltaY = child.getTranslationY();
Sunny Goyalc13403c2016-11-18 23:44:48 -08002024 finalScale = mChildScale - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002025 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002026 this.child = child;
2027 }
2028
Adam Cohend024f982012-05-23 18:26:45 -07002029 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002030 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002031 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002032 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002033 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002034 if (finalDeltaX == 0 && finalDeltaY == 0) {
2035 completeAnimationImmediately();
2036 return;
2037 }
Adam Cohen19f37922012-03-21 11:59:11 -07002038 }
Adam Cohend024f982012-05-23 18:26:45 -07002039 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002040 return;
2041 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002042 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002043 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002044
2045 // Animations are disabled in power save mode, causing the repeated animation to jump
2046 // spastically between beginning and end states. Since this looks bad, we don't repeat
2047 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002048 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002049 va.setRepeatMode(ValueAnimator.REVERSE);
2050 va.setRepeatCount(ValueAnimator.INFINITE);
2051 }
2052
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002053 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002054 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002055 va.addUpdateListener(new AnimatorUpdateListener() {
2056 @Override
2057 public void onAnimationUpdate(ValueAnimator animation) {
Jon Mirandae96798e2016-12-07 12:10:44 -08002058 float r = (Float) animation.getAnimatedValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002059 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2060 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2061 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002062 child.setTranslationX(x);
2063 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002064 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002065 child.setScaleX(s);
2066 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002067 }
2068 });
2069 va.addListener(new AnimatorListenerAdapter() {
2070 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002071 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002072 initDeltaX = 0;
2073 initDeltaY = 0;
Sunny Goyalc13403c2016-11-18 23:44:48 -08002074 initScale = mChildScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002075 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002076 }
2077 });
Adam Cohen19f37922012-03-21 11:59:11 -07002078 mShakeAnimators.put(child, this);
2079 va.start();
2080 }
2081
Adam Cohend024f982012-05-23 18:26:45 -07002082 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002083 if (a != null) {
2084 a.cancel();
2085 }
Adam Cohen19f37922012-03-21 11:59:11 -07002086 }
Adam Cohene7587d22012-05-24 18:50:02 -07002087
Adam Cohen091440a2015-03-18 14:16:05 -07002088 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002089 if (a != null) {
2090 a.cancel();
2091 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002092
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002093 a = new LauncherViewPropertyAnimator(child)
Sunny Goyalc13403c2016-11-18 23:44:48 -08002094 .scaleX(mChildScale)
2095 .scaleY(mChildScale)
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002096 .translationX(0)
2097 .translationY(0)
2098 .setDuration(REORDER_ANIMATION_DURATION);
2099 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2100 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002101 }
Adam Cohen19f37922012-03-21 11:59:11 -07002102 }
2103
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002104 private void completeAndClearReorderPreviewAnimations() {
2105 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002106 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002107 }
2108 mShakeAnimators.clear();
2109 }
2110
Adam Cohen482ed822012-03-02 14:15:13 -08002111 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002112 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002113
2114 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2115 int container = Favorites.CONTAINER_DESKTOP;
2116
Sunny Goyalc13403c2016-11-18 23:44:48 -08002117 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002118 screenId = -1;
2119 container = Favorites.CONTAINER_HOTSEAT;
2120 }
2121
Michael Jurkaa52570f2012-03-20 03:18:20 -07002122 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002123 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002124 View child = mShortcutsAndWidgets.getChildAt(i);
2125 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2126 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002127 // We do a null check here because the item info can be null in the case of the
2128 // AllApps button in the hotseat.
2129 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002130 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2131 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2132 || info.spanY != lp.cellVSpan);
2133
Adam Cohen2acce882012-03-28 19:03:19 -07002134 info.cellX = lp.cellX = lp.tmpCellX;
2135 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002136 info.spanX = lp.cellHSpan;
2137 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002138
2139 if (requiresDbUpdate) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002140 LauncherModel.modifyItemInDatabase(getContext(), info, container, screenId,
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002141 info.cellX, info.cellY, info.spanX, info.spanY);
2142 }
Adam Cohen2acce882012-03-28 19:03:19 -07002143 }
Adam Cohen482ed822012-03-02 14:15:13 -08002144 }
2145 }
2146
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002147 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002148 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002149 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002150 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002151 lp.useTmpCoords = useTempCoords;
2152 }
2153 }
2154
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002155 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002156 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2157 int[] result = new int[2];
2158 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002159 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002160 resultSpan);
2161 if (result[0] >= 0 && result[1] >= 0) {
2162 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002163 solution.cellX = result[0];
2164 solution.cellY = result[1];
2165 solution.spanX = resultSpan[0];
2166 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002167 solution.isSolution = true;
2168 } else {
2169 solution.isSolution = false;
2170 }
2171 return solution;
2172 }
2173
Adam Cohen19f37922012-03-21 11:59:11 -07002174 /* This seems like it should be obvious and straight-forward, but when the direction vector
2175 needs to match with the notion of the dragView pushing other views, we have to employ
2176 a slightly more subtle notion of the direction vector. The question is what two points is
2177 the vector between? The center of the dragView and its desired destination? Not quite, as
2178 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2179 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2180 or right, which helps make pushing feel right.
2181 */
2182 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2183 int spanY, View dragView, int[] resultDirection) {
2184 int[] targetDestination = new int[2];
2185
2186 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2187 Rect dragRect = new Rect();
2188 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2189 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2190
2191 Rect dropRegionRect = new Rect();
2192 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2193 dragView, dropRegionRect, mIntersectingViews);
2194
2195 int dropRegionSpanX = dropRegionRect.width();
2196 int dropRegionSpanY = dropRegionRect.height();
2197
2198 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2199 dropRegionRect.height(), dropRegionRect);
2200
2201 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2202 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2203
2204 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2205 deltaX = 0;
2206 }
2207 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2208 deltaY = 0;
2209 }
2210
2211 if (deltaX == 0 && deltaY == 0) {
2212 // No idea what to do, give a random direction.
2213 resultDirection[0] = 1;
2214 resultDirection[1] = 0;
2215 } else {
2216 computeDirectionVector(deltaX, deltaY, resultDirection);
2217 }
2218 }
2219
2220 // For a given cell and span, fetch the set of views intersecting the region.
2221 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2222 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2223 if (boundingRect != null) {
2224 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2225 }
2226 intersectingViews.clear();
2227 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2228 Rect r1 = new Rect();
2229 final int count = mShortcutsAndWidgets.getChildCount();
2230 for (int i = 0; i < count; i++) {
2231 View child = mShortcutsAndWidgets.getChildAt(i);
2232 if (child == dragView) continue;
2233 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2234 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2235 if (Rect.intersects(r0, r1)) {
2236 mIntersectingViews.add(child);
2237 if (boundingRect != null) {
2238 boundingRect.union(r1);
2239 }
2240 }
2241 }
2242 }
2243
2244 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2245 View dragView, int[] result) {
2246 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2247 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2248 mIntersectingViews);
2249 return !mIntersectingViews.isEmpty();
2250 }
2251
2252 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002253 completeAndClearReorderPreviewAnimations();
2254 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2255 final int count = mShortcutsAndWidgets.getChildCount();
2256 for (int i = 0; i < count; i++) {
2257 View child = mShortcutsAndWidgets.getChildAt(i);
2258 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2259 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2260 lp.tmpCellX = lp.cellX;
2261 lp.tmpCellY = lp.cellY;
2262 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2263 0, false, false);
2264 }
Adam Cohen19f37922012-03-21 11:59:11 -07002265 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002266 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002267 }
Adam Cohen19f37922012-03-21 11:59:11 -07002268 }
2269
Adam Cohenbebf0422012-04-11 18:06:28 -07002270 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2271 View dragView, int[] direction, boolean commit) {
2272 int[] pixelXY = new int[2];
2273 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2274
2275 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002276 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002277 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2278
2279 setUseTempCoords(true);
2280 if (swapSolution != null && swapSolution.isSolution) {
2281 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2282 // committing anything or animating anything as we just want to determine if a solution
2283 // exists
2284 copySolutionToTempState(swapSolution, dragView);
2285 setItemPlacementDirty(true);
2286 animateItemsToSolution(swapSolution, dragView, commit);
2287
2288 if (commit) {
2289 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002290 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002291 setItemPlacementDirty(false);
2292 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002293 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2294 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002295 }
2296 mShortcutsAndWidgets.requestLayout();
2297 }
2298 return swapSolution.isSolution;
2299 }
2300
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002301 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002302 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002303 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002304 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002305
2306 if (resultSpan == null) {
2307 resultSpan = new int[2];
2308 }
2309
Adam Cohen19f37922012-03-21 11:59:11 -07002310 // When we are checking drop validity or actually dropping, we don't recompute the
2311 // direction vector, since we want the solution to match the preview, and it's possible
2312 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002313 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2314 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002315 mDirectionVector[0] = mPreviousReorderDirection[0];
2316 mDirectionVector[1] = mPreviousReorderDirection[1];
2317 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002318 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2319 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2320 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002321 }
2322 } else {
2323 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2324 mPreviousReorderDirection[0] = mDirectionVector[0];
2325 mPreviousReorderDirection[1] = mDirectionVector[1];
2326 }
2327
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002328 // Find a solution involving pushing / displacing any items in the way
2329 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002330 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2331
2332 // We attempt the approach which doesn't shuffle views at all
2333 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2334 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2335
2336 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002337
2338 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2339 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002340 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2341 finalSolution = swapSolution;
2342 } else if (noShuffleSolution.isSolution) {
2343 finalSolution = noShuffleSolution;
2344 }
2345
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002346 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002347 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002348 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2349 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002350 result[0] = finalSolution.cellX;
2351 result[1] = finalSolution.cellY;
2352 resultSpan[0] = finalSolution.spanX;
2353 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002354 } else {
2355 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2356 }
2357 return result;
2358 }
2359
Adam Cohen482ed822012-03-02 14:15:13 -08002360 boolean foundSolution = true;
2361 if (!DESTRUCTIVE_REORDER) {
2362 setUseTempCoords(true);
2363 }
2364
2365 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002366 result[0] = finalSolution.cellX;
2367 result[1] = finalSolution.cellY;
2368 resultSpan[0] = finalSolution.spanX;
2369 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002370
2371 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2372 // committing anything or animating anything as we just want to determine if a solution
2373 // exists
2374 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2375 if (!DESTRUCTIVE_REORDER) {
2376 copySolutionToTempState(finalSolution, dragView);
2377 }
2378 setItemPlacementDirty(true);
2379 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2380
Adam Cohen19f37922012-03-21 11:59:11 -07002381 if (!DESTRUCTIVE_REORDER &&
2382 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002383 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002384 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002385 setItemPlacementDirty(false);
2386 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002387 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2388 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002389 }
2390 }
2391 } else {
2392 foundSolution = false;
2393 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2394 }
2395
2396 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2397 setUseTempCoords(false);
2398 }
Adam Cohen482ed822012-03-02 14:15:13 -08002399
Michael Jurkaa52570f2012-03-20 03:18:20 -07002400 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002401 return result;
2402 }
2403
Adam Cohen19f37922012-03-21 11:59:11 -07002404 void setItemPlacementDirty(boolean dirty) {
2405 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002406 }
Adam Cohen19f37922012-03-21 11:59:11 -07002407 boolean isItemPlacementDirty() {
2408 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002409 }
2410
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002411 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002412 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002413 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2414 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002415 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002416 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002417
Adam Cohenf3900c22012-11-16 18:28:11 -08002418 void save() {
2419 // Copy current state into savedMap
2420 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002421 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002422 }
2423 }
2424
2425 void restore() {
2426 // Restore current state from savedMap
2427 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002428 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002429 }
2430 }
2431
2432 void add(View v, CellAndSpan cs) {
2433 map.put(v, cs);
2434 savedMap.put(v, new CellAndSpan());
2435 sortedViews.add(v);
2436 }
2437
Adam Cohen482ed822012-03-02 14:15:13 -08002438 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002439 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002440 }
2441
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002442 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2443 boolean first = true;
2444 for (View v: views) {
2445 CellAndSpan c = map.get(v);
2446 if (first) {
2447 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2448 first = false;
2449 } else {
2450 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2451 }
2452 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002453 }
Adam Cohen482ed822012-03-02 14:15:13 -08002454 }
2455
Adam Cohendf035382011-04-11 17:22:04 -07002456 /**
Adam Cohendf035382011-04-11 17:22:04 -07002457 * Find a starting cell position that will fit the given bounds nearest the requested
2458 * cell location. Uses Euclidean distance to score multiple vacant areas.
2459 *
2460 * @param pixelX The X location at which you want to search for a vacant area.
2461 * @param pixelY The Y location at which you want to search for a vacant area.
2462 * @param spanX Horizontal span of the object.
2463 * @param spanY Vertical span of the object.
2464 * @param ignoreView Considers space occupied by this view as unoccupied
2465 * @param result Previously returned value to possibly recycle.
2466 * @return The X, Y cell of a vacant area that can contain this object,
2467 * nearest the requested location.
2468 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002469 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002470 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002471 }
2472
Michael Jurka0280c3b2010-09-17 15:00:07 -07002473 boolean existsEmptyCell() {
2474 return findCellForSpan(null, 1, 1);
2475 }
2476
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002477 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002478 * Finds the upper-left coordinate of the first rectangle in the grid that can
2479 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2480 * then this method will only return coordinates for rectangles that contain the cell
2481 * (intersectX, intersectY)
2482 *
2483 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2484 * can be found.
2485 * @param spanX The horizontal span of the cell we want to find.
2486 * @param spanY The vertical span of the cell we want to find.
2487 *
2488 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002489 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002490 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002491 if (cellXY == null) {
2492 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002493 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002494 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002495 }
2496
2497 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002498 * A drag event has begun over this layout.
2499 * It may have begun over this layout (in which case onDragChild is called first),
2500 * or it may have begun on another layout.
2501 */
2502 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002503 mDragging = true;
2504 }
2505
2506 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002507 * Called when drag has left this CellLayout or has been completed (successfully or not)
2508 */
2509 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002510 // This can actually be called when we aren't in a drag, e.g. when adding a new
2511 // item to this layout via the customize drawer.
2512 // Guard against that case.
2513 if (mDragging) {
2514 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002515 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002516
2517 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002518 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002519 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2520 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002521 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002522 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002523 }
2524
2525 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002526 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002527 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002528 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002529 *
2530 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002532 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002533 if (child != null) {
2534 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002535 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002536 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002537 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002538 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 }
2540
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002542 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002543 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002544 * @param cellX X coordinate of upper left corner expressed as a cell position
2545 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002546 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002548 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002550 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 final int cellWidth = mCellWidth;
2552 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002553
Winson Chung4b825dcd2011-06-19 12:41:22 -07002554 final int hStartPadding = getPaddingLeft();
2555 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002556
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302557 int width = cellHSpan * cellWidth;
2558 int height = cellVSpan * cellHeight;
2559 int x = hStartPadding + cellX * cellWidth;
2560 int y = vStartPadding + cellY * cellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002561
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002562 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002564
Adam Cohend4844c32011-02-18 19:25:06 -08002565 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002566 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002567 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002568 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002569 }
2570
Adam Cohend4844c32011-02-18 19:25:06 -08002571 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002572 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002573 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002574 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002575 }
2576
Adam Cohen2801caf2011-05-13 20:57:39 -07002577 public int getDesiredWidth() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302578 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth);
Adam Cohen2801caf2011-05-13 20:57:39 -07002579 }
2580
2581 public int getDesiredHeight() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302582 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight);
Adam Cohen2801caf2011-05-13 20:57:39 -07002583 }
2584
Michael Jurka66d72172011-04-12 16:29:25 -07002585 public boolean isOccupied(int x, int y) {
2586 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002587 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002588 } else {
2589 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2590 }
2591 }
2592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002593 @Override
2594 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2595 return new CellLayout.LayoutParams(getContext(), attrs);
2596 }
2597
2598 @Override
2599 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2600 return p instanceof CellLayout.LayoutParams;
2601 }
2602
2603 @Override
2604 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2605 return new CellLayout.LayoutParams(p);
2606 }
2607
2608 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2609 /**
2610 * Horizontal location of the item in the grid.
2611 */
2612 @ViewDebug.ExportedProperty
2613 public int cellX;
2614
2615 /**
2616 * Vertical location of the item in the grid.
2617 */
2618 @ViewDebug.ExportedProperty
2619 public int cellY;
2620
2621 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002622 * Temporary horizontal location of the item in the grid during reorder
2623 */
2624 public int tmpCellX;
2625
2626 /**
2627 * Temporary vertical location of the item in the grid during reorder
2628 */
2629 public int tmpCellY;
2630
2631 /**
2632 * Indicates that the temporary coordinates should be used to layout the items
2633 */
2634 public boolean useTmpCoords;
2635
2636 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002637 * Number of cells spanned horizontally by the item.
2638 */
2639 @ViewDebug.ExportedProperty
2640 public int cellHSpan;
2641
2642 /**
2643 * Number of cells spanned vertically by the item.
2644 */
2645 @ViewDebug.ExportedProperty
2646 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002647
Adam Cohen1b607ed2011-03-03 17:26:50 -08002648 /**
2649 * Indicates whether the item will set its x, y, width and height parameters freely,
2650 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2651 */
Adam Cohend4844c32011-02-18 19:25:06 -08002652 public boolean isLockedToGrid = true;
2653
Adam Cohen482ed822012-03-02 14:15:13 -08002654 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002655 * Indicates that this item should use the full extents of its parent.
2656 */
2657 public boolean isFullscreen = false;
2658
2659 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002660 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002661 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002662 */
2663 public boolean canReorder = true;
2664
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002665 // X coordinate of the view in the layout.
2666 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002667 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002668 // Y coordinate of the view in the layout.
2669 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002670 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002671
Romain Guy84f296c2009-11-04 15:00:44 -08002672 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002673
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002674 public LayoutParams(Context c, AttributeSet attrs) {
2675 super(c, attrs);
2676 cellHSpan = 1;
2677 cellVSpan = 1;
2678 }
2679
2680 public LayoutParams(ViewGroup.LayoutParams source) {
2681 super(source);
2682 cellHSpan = 1;
2683 cellVSpan = 1;
2684 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002685
2686 public LayoutParams(LayoutParams source) {
2687 super(source);
2688 this.cellX = source.cellX;
2689 this.cellY = source.cellY;
2690 this.cellHSpan = source.cellHSpan;
2691 this.cellVSpan = source.cellVSpan;
2692 }
2693
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002694 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002695 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002696 this.cellX = cellX;
2697 this.cellY = cellY;
2698 this.cellHSpan = cellHSpan;
2699 this.cellVSpan = cellVSpan;
2700 }
2701
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302702 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) {
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002703 setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f);
2704 }
2705
2706 /**
2707 * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs
2708 * to be scaled.
2709 *
2710 * ie. In multi-window mode, we setup widgets so that they are measured and laid out
2711 * using their full/invariant device profile sizes.
2712 */
2713 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount,
2714 float cellScaleX, float cellScaleY) {
Adam Cohend4844c32011-02-18 19:25:06 -08002715 if (isLockedToGrid) {
2716 final int myCellHSpan = cellHSpan;
2717 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002718 int myCellX = useTmpCoords ? tmpCellX : cellX;
2719 int myCellY = useTmpCoords ? tmpCellY : cellY;
2720
2721 if (invertHorizontally) {
2722 myCellX = colCount - myCellX - cellHSpan;
2723 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002724
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002725 width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin);
2726 height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin);
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302727 x = (myCellX * cellWidth + leftMargin);
2728 y = (myCellY * cellHeight + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002729 }
2730 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002731
Winson Chungaafa03c2010-06-11 17:34:16 -07002732 public String toString() {
2733 return "(" + this.cellX + ", " + this.cellY + ")";
2734 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002735
2736 public void setWidth(int width) {
2737 this.width = width;
2738 }
2739
2740 public int getWidth() {
2741 return width;
2742 }
2743
2744 public void setHeight(int height) {
2745 this.height = height;
2746 }
2747
2748 public int getHeight() {
2749 return height;
2750 }
2751
2752 public void setX(int x) {
2753 this.x = x;
2754 }
2755
2756 public int getX() {
2757 return x;
2758 }
2759
2760 public void setY(int y) {
2761 this.y = y;
2762 }
2763
2764 public int getY() {
2765 return y;
2766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002767 }
2768
Michael Jurka0280c3b2010-09-17 15:00:07 -07002769 // This class stores info for two purposes:
2770 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2771 // its spanX, spanY, and the screen it is on
2772 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2773 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2774 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002775 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002776 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002777 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002778 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779
Sunny Goyal83a8f042015-05-19 12:52:12 -07002780 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002781 cellX = info.cellX;
2782 cellY = info.cellY;
2783 spanX = info.spanX;
2784 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002785 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002786 screenId = info.screenId;
2787 container = info.container;
2788 }
2789
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002790 @Override
2791 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002792 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2793 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002794 }
2795 }
Michael Jurkad771c962011-08-09 15:00:48 -07002796
Tony Wickham86930612015-09-09 13:50:40 -07002797 /**
2798 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2799 * if necessary).
2800 */
2801 public boolean hasReorderSolution(ItemInfo itemInfo) {
2802 int[] cellPoint = new int[2];
2803 // Check for a solution starting at every cell.
2804 for (int cellX = 0; cellX < getCountX(); cellX++) {
2805 for (int cellY = 0; cellY < getCountY(); cellY++) {
2806 cellToPoint(cellX, cellY, cellPoint);
2807 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2808 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2809 true, new ItemConfiguration()).isSolution) {
2810 return true;
2811 }
2812 }
2813 }
2814 return false;
2815 }
2816
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002817 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002818 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002819 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002820}