blob: c0087c45e07c7f39c9e6ab666c06fce545a400c9 [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 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700954 clc.setupLp(lp);
955 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) {
Adam Cohen482ed822012-03-02 14:15:13 -0800975 float r = ((Float) animation.getAnimatedValue()).floatValue();
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);
Sunny Goyal106bf642015-07-16 12:18:06 -07001030 } else {
1031 // Find the top left corner of the rect the object will occupy
1032 final int[] topLeft = mTmpPoint;
1033 cellToPoint(cellX, cellY, topLeft);
1034
1035 int left = topLeft[0];
1036 int top = topLeft[1];
1037
1038 if (v != null && dragOffset == null) {
1039 // When drawing the drag outline, it did not account for margin offsets
1040 // added by the view's parent.
1041 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1042 left += lp.leftMargin;
1043 top += lp.topMargin;
1044
1045 // Offsets due to the size difference between the View and the dragOutline.
1046 // There is a size difference to account for the outer blur, which may lie
1047 // outside the bounds of the view.
Jon Mirandaf7ff3fe2016-12-05 12:04:44 -08001048 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001049 // We center about the x axis
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301050 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001051 } else {
1052 if (dragOffset != null && dragRegion != null) {
1053 // Center the drag region *horizontally* in the cell and apply a drag
1054 // outline offset
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301055 left += dragOffset.x + ((mCellWidth * spanX) - dragRegion.width()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001056 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1057 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1058 top += dragOffset.y + cellPaddingY;
1059 } else {
1060 // Center the drag outline in the cell
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301061 left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2;
1062 top += ((mCellHeight * spanY) - dragOutline.getHeight()) / 2;
Sunny Goyal106bf642015-07-16 12:18:06 -07001063 }
1064 }
1065 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001066 }
Winson Chung150fbab2010-09-29 17:14:26 -07001067
Sunny Goyalc13403c2016-11-18 23:44:48 -08001068 Utilities.scaleRectAboutCenter(r, mChildScale);
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001069 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1070 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001071
1072 if (dragObject.stateAnnouncer != null) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001073 dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY));
Sunny Goyale78e3d72015-09-24 11:23:31 -07001074 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001075 }
1076 }
1077
Sunny Goyalc13403c2016-11-18 23:44:48 -08001078 public String getItemMoveDescription(int cellX, int cellY) {
1079 if (mContainerType == HOTSEAT) {
1080 return getContext().getString(R.string.move_to_hotseat_position,
1081 Math.max(cellX, cellY) + 1);
1082 } else {
1083 return getContext().getString(R.string.move_to_empty_cell,
1084 cellY + 1, cellX + 1);
1085 }
1086 }
1087
Adam Cohene0310962011-04-18 16:15:31 -07001088 public void clearDragOutlines() {
1089 final int oldIndex = mDragOutlineCurrent;
1090 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001091 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001092 }
1093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001095 * Find a vacant area that will fit the given bounds nearest the requested
1096 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001097 *
Romain Guy51afc022009-05-04 18:03:43 -07001098 * @param pixelX The X location at which you want to search for a vacant area.
1099 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001100 * @param minSpanX The minimum horizontal span required
1101 * @param minSpanY The minimum vertical span required
1102 * @param spanX Horizontal span of the object.
1103 * @param spanY Vertical span of the object.
1104 * @param result Array in which to place the result, or null (in which case a new array will
1105 * be allocated)
1106 * @return The X, Y cell of a vacant area that can contain this object,
1107 * nearest the requested location.
1108 */
1109 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1110 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001111 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001112 result, resultSpan);
1113 }
1114
Adam Cohend41fbf52012-02-16 23:53:59 -08001115 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1116 private void lazyInitTempRectStack() {
1117 if (mTempRectStack.isEmpty()) {
1118 for (int i = 0; i < mCountX * mCountY; i++) {
1119 mTempRectStack.push(new Rect());
1120 }
1121 }
1122 }
Adam Cohen482ed822012-03-02 14:15:13 -08001123
Adam Cohend41fbf52012-02-16 23:53:59 -08001124 private void recycleTempRects(Stack<Rect> used) {
1125 while (!used.isEmpty()) {
1126 mTempRectStack.push(used.pop());
1127 }
1128 }
1129
1130 /**
1131 * Find a vacant area that will fit the given bounds nearest the requested
1132 * cell location. Uses Euclidean distance to score multiple vacant areas.
1133 *
1134 * @param pixelX The X location at which you want to search for a vacant area.
1135 * @param pixelY The Y location at which you want to search for a vacant area.
1136 * @param minSpanX The minimum horizontal span required
1137 * @param minSpanY The minimum vertical span required
1138 * @param spanX Horizontal span of the object.
1139 * @param spanY Vertical span of the object.
1140 * @param ignoreOccupied If true, the result can be an occupied cell
1141 * @param result Array in which to place the result, or null (in which case a new array will
1142 * be allocated)
1143 * @return The X, Y cell of a vacant area that can contain this object,
1144 * nearest the requested location.
1145 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001146 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1147 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001148 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001149
Adam Cohene3e27a82011-04-15 12:07:39 -07001150 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1151 // to the center of the item, but we are searching based on the top-left cell, so
1152 // we translate the point over to correspond to the top-left.
Sunny Goyalaa8a8712016-11-20 15:26:01 +05301153 pixelX -= mCellWidth * (spanX - 1) / 2f;
1154 pixelY -= mCellHeight * (spanY - 1) / 2f;
Adam Cohene3e27a82011-04-15 12:07:39 -07001155
Jeff Sharkey70864282009-04-07 21:08:40 -07001156 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001157 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001158 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001159 final Rect bestRect = new Rect(-1, -1, -1, -1);
1160 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001161
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001162 final int countX = mCountX;
1163 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001164
Adam Cohend41fbf52012-02-16 23:53:59 -08001165 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1166 spanX < minSpanX || spanY < minSpanY) {
1167 return bestXY;
1168 }
1169
1170 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001171 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001172 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1173 int ySize = -1;
1174 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001175 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001176 // First, let's see if this thing fits anywhere
1177 for (int i = 0; i < minSpanX; i++) {
1178 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001179 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001180 continue inner;
1181 }
Michael Jurkac28de512010-08-13 11:27:44 -07001182 }
1183 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001184 xSize = minSpanX;
1185 ySize = minSpanY;
1186
1187 // We know that the item will fit at _some_ acceptable size, now let's see
1188 // how big we can make it. We'll alternate between incrementing x and y spans
1189 // until we hit a limit.
1190 boolean incX = true;
1191 boolean hitMaxX = xSize >= spanX;
1192 boolean hitMaxY = ySize >= spanY;
1193 while (!(hitMaxX && hitMaxY)) {
1194 if (incX && !hitMaxX) {
1195 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001196 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001197 // We can't move out horizontally
1198 hitMaxX = true;
1199 }
1200 }
1201 if (!hitMaxX) {
1202 xSize++;
1203 }
1204 } else if (!hitMaxY) {
1205 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001206 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001207 // We can't move out vertically
1208 hitMaxY = true;
1209 }
1210 }
1211 if (!hitMaxY) {
1212 ySize++;
1213 }
1214 }
1215 hitMaxX |= xSize >= spanX;
1216 hitMaxY |= ySize >= spanY;
1217 incX = !incX;
1218 }
1219 incX = true;
1220 hitMaxX = xSize >= spanX;
1221 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001222 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001223 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001224 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225
Adam Cohend41fbf52012-02-16 23:53:59 -08001226 // We verify that the current rect is not a sub-rect of any of our previous
1227 // candidates. In this case, the current rect is disqualified in favour of the
1228 // containing rect.
1229 Rect currentRect = mTempRectStack.pop();
1230 currentRect.set(x, y, x + xSize, y + ySize);
1231 boolean contained = false;
1232 for (Rect r : validRegions) {
1233 if (r.contains(currentRect)) {
1234 contained = true;
1235 break;
1236 }
1237 }
1238 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001239 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001240
Adam Cohend41fbf52012-02-16 23:53:59 -08001241 if ((distance <= bestDistance && !contained) ||
1242 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001243 bestDistance = distance;
1244 bestXY[0] = x;
1245 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001246 if (resultSpan != null) {
1247 resultSpan[0] = xSize;
1248 resultSpan[1] = ySize;
1249 }
1250 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001251 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 }
1253 }
1254
Adam Cohenc0dcf592011-06-01 15:30:43 -07001255 // Return -1, -1 if no suitable location found
1256 if (bestDistance == Double.MAX_VALUE) {
1257 bestXY[0] = -1;
1258 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001259 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001260 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001261 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001263
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001264 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001265 * Find a vacant area that will fit the given bounds nearest the requested
1266 * cell location, and will also weigh in a suggested direction vector of the
1267 * desired location. This method computers distance based on unit grid distances,
1268 * not pixel distances.
1269 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001270 * @param cellX The X cell nearest to which you want to search for a vacant area.
1271 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001272 * @param spanX Horizontal span of the object.
1273 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001274 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001275 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001276 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001277 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001278 * @param result Array in which to place the result, or null (in which case a new array will
1279 * be allocated)
1280 * @return The X, Y cell of a vacant area that can contain this object,
1281 * nearest the requested location.
1282 */
1283 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001284 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001285 // Keep track of best-scoring drop area
1286 final int[] bestXY = result != null ? result : new int[2];
1287 float bestDistance = Float.MAX_VALUE;
1288 int bestDirectionScore = Integer.MIN_VALUE;
1289
1290 final int countX = mCountX;
1291 final int countY = mCountY;
1292
1293 for (int y = 0; y < countY - (spanY - 1); y++) {
1294 inner:
1295 for (int x = 0; x < countX - (spanX - 1); x++) {
1296 // First, let's see if this thing fits anywhere
1297 for (int i = 0; i < spanX; i++) {
1298 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001299 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001300 continue inner;
1301 }
1302 }
1303 }
1304
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001305 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001306 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001307 computeDirectionVector(x - cellX, y - cellY, curDirection);
1308 // The direction score is just the dot product of the two candidate direction
1309 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001310 int curDirectionScore = direction[0] * curDirection[0] +
1311 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001312 if (Float.compare(distance, bestDistance) < 0 ||
1313 (Float.compare(distance, bestDistance) == 0
1314 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001315 bestDistance = distance;
1316 bestDirectionScore = curDirectionScore;
1317 bestXY[0] = x;
1318 bestXY[1] = y;
1319 }
1320 }
1321 }
1322
1323 // Return -1, -1 if no suitable location found
1324 if (bestDistance == Float.MAX_VALUE) {
1325 bestXY[0] = -1;
1326 bestXY[1] = -1;
1327 }
1328 return bestXY;
1329 }
1330
1331 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001332 int[] direction, ItemConfiguration currentState) {
1333 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001334 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001335 mTmpOccupied.markCells(c, false);
1336 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001337
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001338 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1339 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001340
1341 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001342 c.cellX = mTempLocation[0];
1343 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001344 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001345 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001346 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001347 return success;
1348 }
1349
Adam Cohenf3900c22012-11-16 18:28:11 -08001350 /**
1351 * This helper class defines a cluster of views. It helps with defining complex edges
1352 * of the cluster and determining how those edges interact with other views. The edges
1353 * essentially define a fine-grained boundary around the cluster of views -- like a more
1354 * precise version of a bounding box.
1355 */
1356 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001357 final static int LEFT = 1 << 0;
1358 final static int TOP = 1 << 1;
1359 final static int RIGHT = 1 << 2;
1360 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001361
Adam Cohenf3900c22012-11-16 18:28:11 -08001362 ArrayList<View> views;
1363 ItemConfiguration config;
1364 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001365
Adam Cohenf3900c22012-11-16 18:28:11 -08001366 int[] leftEdge = new int[mCountY];
1367 int[] rightEdge = new int[mCountY];
1368 int[] topEdge = new int[mCountX];
1369 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001370 int dirtyEdges;
1371 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001372
1373 @SuppressWarnings("unchecked")
1374 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1375 this.views = (ArrayList<View>) views.clone();
1376 this.config = config;
1377 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001378 }
1379
Adam Cohenf3900c22012-11-16 18:28:11 -08001380 void resetEdges() {
1381 for (int i = 0; i < mCountX; i++) {
1382 topEdge[i] = -1;
1383 bottomEdge[i] = -1;
1384 }
1385 for (int i = 0; i < mCountY; i++) {
1386 leftEdge[i] = -1;
1387 rightEdge[i] = -1;
1388 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001389 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001390 boundingRectDirty = true;
1391 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001392
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001393 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001394 int count = views.size();
1395 for (int i = 0; i < count; i++) {
1396 CellAndSpan cs = config.map.get(views.get(i));
1397 switch (which) {
1398 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001399 int left = cs.cellX;
1400 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001401 if (left < leftEdge[j] || leftEdge[j] < 0) {
1402 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001403 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001404 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001405 break;
1406 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001407 int right = cs.cellX + cs.spanX;
1408 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001409 if (right > rightEdge[j]) {
1410 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001411 }
1412 }
1413 break;
1414 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001415 int top = cs.cellY;
1416 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001417 if (top < topEdge[j] || topEdge[j] < 0) {
1418 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001419 }
1420 }
1421 break;
1422 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001423 int bottom = cs.cellY + cs.spanY;
1424 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001425 if (bottom > bottomEdge[j]) {
1426 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001427 }
1428 }
1429 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001430 }
1431 }
1432 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001433
1434 boolean isViewTouchingEdge(View v, int whichEdge) {
1435 CellAndSpan cs = config.map.get(v);
1436
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001437 if ((dirtyEdges & whichEdge) == whichEdge) {
1438 computeEdge(whichEdge);
1439 dirtyEdges &= ~whichEdge;
1440 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001441
1442 switch (whichEdge) {
1443 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001444 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1445 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001446 return true;
1447 }
1448 }
1449 break;
1450 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001451 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1452 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001453 return true;
1454 }
1455 }
1456 break;
1457 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001458 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1459 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001460 return true;
1461 }
1462 }
1463 break;
1464 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001465 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1466 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001467 return true;
1468 }
1469 }
1470 break;
1471 }
1472 return false;
1473 }
1474
1475 void shift(int whichEdge, int delta) {
1476 for (View v: views) {
1477 CellAndSpan c = config.map.get(v);
1478 switch (whichEdge) {
1479 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001480 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001481 break;
1482 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001483 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001484 break;
1485 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001486 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001487 break;
1488 case BOTTOM:
1489 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001490 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001491 break;
1492 }
1493 }
1494 resetEdges();
1495 }
1496
1497 public void addView(View v) {
1498 views.add(v);
1499 resetEdges();
1500 }
1501
1502 public Rect getBoundingRect() {
1503 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001504 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001505 }
1506 return boundingRect;
1507 }
1508
Adam Cohenf3900c22012-11-16 18:28:11 -08001509 PositionComparator comparator = new PositionComparator();
1510 class PositionComparator implements Comparator<View> {
1511 int whichEdge = 0;
1512 public int compare(View left, View right) {
1513 CellAndSpan l = config.map.get(left);
1514 CellAndSpan r = config.map.get(right);
1515 switch (whichEdge) {
1516 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001517 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001518 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001519 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001520 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001521 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001522 case BOTTOM:
1523 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001524 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001525 }
1526 }
1527 }
1528
1529 public void sortConfigurationForEdgePush(int edge) {
1530 comparator.whichEdge = edge;
1531 Collections.sort(config.sortedViews, comparator);
1532 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001533 }
1534
Adam Cohenf3900c22012-11-16 18:28:11 -08001535 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1536 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001537
Adam Cohenf3900c22012-11-16 18:28:11 -08001538 ViewCluster cluster = new ViewCluster(views, currentState);
1539 Rect clusterRect = cluster.getBoundingRect();
1540 int whichEdge;
1541 int pushDistance;
1542 boolean fail = false;
1543
1544 // Determine the edge of the cluster that will be leading the push and how far
1545 // the cluster must be shifted.
1546 if (direction[0] < 0) {
1547 whichEdge = ViewCluster.LEFT;
1548 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001549 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001550 whichEdge = ViewCluster.RIGHT;
1551 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1552 } else if (direction[1] < 0) {
1553 whichEdge = ViewCluster.TOP;
1554 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1555 } else {
1556 whichEdge = ViewCluster.BOTTOM;
1557 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001558 }
1559
Adam Cohenf3900c22012-11-16 18:28:11 -08001560 // Break early for invalid push distance.
1561 if (pushDistance <= 0) {
1562 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001563 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001564
1565 // Mark the occupied state as false for the group of views we want to move.
1566 for (View v: views) {
1567 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001568 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001569 }
1570
1571 // We save the current configuration -- if we fail to find a solution we will revert
1572 // to the initial state. The process of finding a solution modifies the configuration
1573 // in place, hence the need for revert in the failure case.
1574 currentState.save();
1575
1576 // The pushing algorithm is simplified by considering the views in the order in which
1577 // they would be pushed by the cluster. For example, if the cluster is leading with its
1578 // left edge, we consider sort the views by their right edge, from right to left.
1579 cluster.sortConfigurationForEdgePush(whichEdge);
1580
1581 while (pushDistance > 0 && !fail) {
1582 for (View v: currentState.sortedViews) {
1583 // For each view that isn't in the cluster, we see if the leading edge of the
1584 // cluster is contacting the edge of that view. If so, we add that view to the
1585 // cluster.
1586 if (!cluster.views.contains(v) && v != dragView) {
1587 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1588 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1589 if (!lp.canReorder) {
1590 // The push solution includes the all apps button, this is not viable.
1591 fail = true;
1592 break;
1593 }
1594 cluster.addView(v);
1595 CellAndSpan c = currentState.map.get(v);
1596
1597 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001598 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001599 }
1600 }
1601 }
1602 pushDistance--;
1603
1604 // The cluster has been completed, now we move the whole thing over in the appropriate
1605 // direction.
1606 cluster.shift(whichEdge, 1);
1607 }
1608
1609 boolean foundSolution = false;
1610 clusterRect = cluster.getBoundingRect();
1611
1612 // Due to the nature of the algorithm, the only check required to verify a valid solution
1613 // is to ensure that completed shifted cluster lies completely within the cell layout.
1614 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1615 clusterRect.bottom <= mCountY) {
1616 foundSolution = true;
1617 } else {
1618 currentState.restore();
1619 }
1620
1621 // In either case, we set the occupied array as marked for the location of the views
1622 for (View v: cluster.views) {
1623 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001624 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001625 }
1626
1627 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001628 }
1629
Adam Cohen482ed822012-03-02 14:15:13 -08001630 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001631 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001632 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001633
Adam Cohen8baab352012-03-20 17:39:21 -07001634 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001635 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001636 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001637 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001638
Adam Cohen8baab352012-03-20 17:39:21 -07001639 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001640 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001641 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001642 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001643 }
1644
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001645 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001646 int top = boundingRect.top;
1647 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001648 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001649 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001650 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001651 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001652 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001653 }
1654
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001655 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001656
Adam Cohenf3900c22012-11-16 18:28:11 -08001657 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001658 boundingRect.height(), direction,
1659 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001660
Adam Cohen8baab352012-03-20 17:39:21 -07001661 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001662 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001663 int deltaX = mTempLocation[0] - boundingRect.left;
1664 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001665 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001666 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001667 c.cellX += deltaX;
1668 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001669 }
1670 success = true;
1671 }
Adam Cohen8baab352012-03-20 17:39:21 -07001672
1673 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001674 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001675 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001676 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001677 }
1678 return success;
1679 }
1680
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001681 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1682 // to push items in each of the cardinal directions, in an order based on the direction vector
1683 // passed.
1684 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1685 int[] direction, View ignoreView, ItemConfiguration solution) {
1686 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001687 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001688 // separately in each of the components.
1689 int temp = direction[1];
1690 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001691
1692 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001693 ignoreView, solution)) {
1694 return true;
1695 }
1696 direction[1] = temp;
1697 temp = direction[0];
1698 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001699
1700 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001701 ignoreView, solution)) {
1702 return true;
1703 }
1704 // Revert the direction
1705 direction[0] = temp;
1706
1707 // Now we try pushing in each component of the opposite direction
1708 direction[0] *= -1;
1709 direction[1] *= -1;
1710 temp = direction[1];
1711 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001712 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001713 ignoreView, solution)) {
1714 return true;
1715 }
1716
1717 direction[1] = temp;
1718 temp = direction[0];
1719 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001720 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001721 ignoreView, solution)) {
1722 return true;
1723 }
1724 // revert the direction
1725 direction[0] = temp;
1726 direction[0] *= -1;
1727 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001728
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001729 } else {
1730 // If the direction vector has a single non-zero component, we push first in the
1731 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001732 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001733 ignoreView, solution)) {
1734 return true;
1735 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001736 // Then we try the opposite direction
1737 direction[0] *= -1;
1738 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001739 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001740 ignoreView, solution)) {
1741 return true;
1742 }
1743 // Switch the direction back
1744 direction[0] *= -1;
1745 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001746
1747 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001748 // to find a solution by pushing along the perpendicular axis.
1749
1750 // Swap the components
1751 int temp = direction[1];
1752 direction[1] = direction[0];
1753 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001754 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001755 ignoreView, solution)) {
1756 return true;
1757 }
1758
1759 // Then we try the opposite direction
1760 direction[0] *= -1;
1761 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001762 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001763 ignoreView, solution)) {
1764 return true;
1765 }
1766 // Switch the direction back
1767 direction[0] *= -1;
1768 direction[1] *= -1;
1769
1770 // Swap the components back
1771 temp = direction[1];
1772 direction[1] = direction[0];
1773 direction[0] = temp;
1774 }
1775 return false;
1776 }
1777
Adam Cohen482ed822012-03-02 14:15:13 -08001778 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001779 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001780 // Return early if get invalid cell positions
1781 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001782
Adam Cohen8baab352012-03-20 17:39:21 -07001783 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001784 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001785
Adam Cohen8baab352012-03-20 17:39:21 -07001786 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001787 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001788 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001789 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001790 c.cellX = cellX;
1791 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001792 }
Adam Cohen482ed822012-03-02 14:15:13 -08001793 }
Adam Cohen482ed822012-03-02 14:15:13 -08001794 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1795 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001796 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001797 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001798 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001799 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001800 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001801 if (Rect.intersects(r0, r1)) {
1802 if (!lp.canReorder) {
1803 return false;
1804 }
1805 mIntersectingViews.add(child);
1806 }
1807 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001808
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001809 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1810
Winson Chung5f8afe62013-08-12 16:19:28 -07001811 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001812 // we try to find a solution such that no displaced item travels through another item
1813 // without also displacing that item.
1814 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001815 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001816 return true;
1817 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001818
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001819 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001820 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001821 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001822 return true;
1823 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001824
Adam Cohen482ed822012-03-02 14:15:13 -08001825 // Ok, they couldn't move as a block, let's move them individually
1826 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001827 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001828 return false;
1829 }
1830 }
1831 return true;
1832 }
1833
1834 /*
1835 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1836 * the provided point and the provided cell
1837 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001838 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001839 double angle = Math.atan(((float) deltaY) / deltaX);
1840
1841 result[0] = 0;
1842 result[1] = 0;
1843 if (Math.abs(Math.cos(angle)) > 0.5f) {
1844 result[0] = (int) Math.signum(deltaX);
1845 }
1846 if (Math.abs(Math.sin(angle)) > 0.5f) {
1847 result[1] = (int) Math.signum(deltaY);
1848 }
1849 }
1850
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001851 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001852 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1853 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001854 // Copy the current state into the solution. This solution will be manipulated as necessary.
1855 copyCurrentStateToSolution(solution, false);
1856 // Copy the current occupied array into the temporary occupied array. This array will be
1857 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001858 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001859
1860 // We find the nearest cell into which we would place the dragged item, assuming there's
1861 // nothing in its way.
1862 int result[] = new int[2];
1863 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1864
1865 boolean success = false;
1866 // First we try the exact nearest position of the item being dragged,
1867 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001868 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1869 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001870
1871 if (!success) {
1872 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1873 // x, then 1 in y etc.
1874 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001875 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1876 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001877 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001878 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1879 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001880 }
1881 solution.isSolution = false;
1882 } else {
1883 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001884 solution.cellX = result[0];
1885 solution.cellY = result[1];
1886 solution.spanX = spanX;
1887 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001888 }
1889 return solution;
1890 }
1891
1892 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001893 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001894 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001895 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001896 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001897 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001898 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001899 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001900 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001901 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001902 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001903 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001904 }
1905 }
1906
1907 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001908 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001909
Michael Jurkaa52570f2012-03-20 03:18:20 -07001910 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001911 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001912 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001913 if (child == dragView) continue;
1914 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001915 CellAndSpan c = solution.map.get(child);
1916 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001917 lp.tmpCellX = c.cellX;
1918 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001919 lp.cellHSpan = c.spanX;
1920 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001921 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001922 }
1923 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001924 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001925 }
1926
1927 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1928 commitDragView) {
1929
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001930 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1931 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001932
Michael Jurkaa52570f2012-03-20 03:18:20 -07001933 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001934 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001935 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001936 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001937 CellAndSpan c = solution.map.get(child);
1938 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001939 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001940 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001941 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001942 }
1943 }
1944 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001945 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001946 }
1947 }
1948
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001949
1950 // This method starts or changes the reorder preview animations
1951 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1952 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001953 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001954 for (int i = 0; i < childCount; i++) {
1955 View child = mShortcutsAndWidgets.getChildAt(i);
1956 if (child == dragView) continue;
1957 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001958 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1959 != null && !solution.intersectingViews.contains(child);
1960
Adam Cohen19f37922012-03-21 11:59:11 -07001961 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001962 if (c != null && !skip) {
1963 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001964 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07001965 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07001966 }
1967 }
1968 }
1969
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001970 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07001971 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001972 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07001973 View child;
Adam Cohend024f982012-05-23 18:26:45 -07001974 float finalDeltaX;
1975 float finalDeltaY;
1976 float initDeltaX;
1977 float initDeltaY;
1978 float finalScale;
1979 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001980 int mode;
1981 boolean repeating = false;
1982 private static final int PREVIEW_DURATION = 300;
1983 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
1984
1985 public static final int MODE_HINT = 0;
1986 public static final int MODE_PREVIEW = 1;
1987
Adam Cohene7587d22012-05-24 18:50:02 -07001988 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07001989
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001990 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
1991 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07001992 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1993 final int x0 = mTmpPoint[0];
1994 final int y0 = mTmpPoint[1];
1995 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1996 final int x1 = mTmpPoint[0];
1997 final int y1 = mTmpPoint[1];
1998 final int dX = x1 - x0;
1999 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002000 finalDeltaX = 0;
2001 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002002 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002003 if (dX == dY && dX == 0) {
2004 } else {
2005 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002006 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002007 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002008 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002009 } else {
2010 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002011 finalDeltaX = (int) (- dir * Math.signum(dX) *
2012 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2013 finalDeltaY = (int) (- dir * Math.signum(dY) *
2014 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002015 }
2016 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002017 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002018 initDeltaX = child.getTranslationX();
2019 initDeltaY = child.getTranslationY();
Sunny Goyalc13403c2016-11-18 23:44:48 -08002020 finalScale = mChildScale - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002021 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002022 this.child = child;
2023 }
2024
Adam Cohend024f982012-05-23 18:26:45 -07002025 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002026 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002027 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002028 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002029 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002030 if (finalDeltaX == 0 && finalDeltaY == 0) {
2031 completeAnimationImmediately();
2032 return;
2033 }
Adam Cohen19f37922012-03-21 11:59:11 -07002034 }
Adam Cohend024f982012-05-23 18:26:45 -07002035 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002036 return;
2037 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002038 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002039 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002040
2041 // Animations are disabled in power save mode, causing the repeated animation to jump
2042 // spastically between beginning and end states. Since this looks bad, we don't repeat
2043 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002044 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002045 va.setRepeatMode(ValueAnimator.REVERSE);
2046 va.setRepeatCount(ValueAnimator.INFINITE);
2047 }
2048
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002049 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002050 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002051 va.addUpdateListener(new AnimatorUpdateListener() {
2052 @Override
2053 public void onAnimationUpdate(ValueAnimator animation) {
2054 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002055 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2056 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2057 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002058 child.setTranslationX(x);
2059 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002060 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002061 child.setScaleX(s);
2062 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002063 }
2064 });
2065 va.addListener(new AnimatorListenerAdapter() {
2066 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002067 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002068 initDeltaX = 0;
2069 initDeltaY = 0;
Sunny Goyalc13403c2016-11-18 23:44:48 -08002070 initScale = mChildScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002071 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002072 }
2073 });
Adam Cohen19f37922012-03-21 11:59:11 -07002074 mShakeAnimators.put(child, this);
2075 va.start();
2076 }
2077
Adam Cohend024f982012-05-23 18:26:45 -07002078 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002079 if (a != null) {
2080 a.cancel();
2081 }
Adam Cohen19f37922012-03-21 11:59:11 -07002082 }
Adam Cohene7587d22012-05-24 18:50:02 -07002083
Adam Cohen091440a2015-03-18 14:16:05 -07002084 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002085 if (a != null) {
2086 a.cancel();
2087 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002088
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002089 a = new LauncherViewPropertyAnimator(child)
Sunny Goyalc13403c2016-11-18 23:44:48 -08002090 .scaleX(mChildScale)
2091 .scaleY(mChildScale)
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002092 .translationX(0)
2093 .translationY(0)
2094 .setDuration(REORDER_ANIMATION_DURATION);
2095 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2096 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002097 }
Adam Cohen19f37922012-03-21 11:59:11 -07002098 }
2099
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002100 private void completeAndClearReorderPreviewAnimations() {
2101 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002102 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002103 }
2104 mShakeAnimators.clear();
2105 }
2106
Adam Cohen482ed822012-03-02 14:15:13 -08002107 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002108 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002109
2110 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2111 int container = Favorites.CONTAINER_DESKTOP;
2112
Sunny Goyalc13403c2016-11-18 23:44:48 -08002113 if (mContainerType == HOTSEAT) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002114 screenId = -1;
2115 container = Favorites.CONTAINER_HOTSEAT;
2116 }
2117
Michael Jurkaa52570f2012-03-20 03:18:20 -07002118 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002119 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002120 View child = mShortcutsAndWidgets.getChildAt(i);
2121 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2122 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002123 // We do a null check here because the item info can be null in the case of the
2124 // AllApps button in the hotseat.
2125 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002126 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2127 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2128 || info.spanY != lp.cellVSpan);
2129
Adam Cohen2acce882012-03-28 19:03:19 -07002130 info.cellX = lp.cellX = lp.tmpCellX;
2131 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002132 info.spanX = lp.cellHSpan;
2133 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002134
2135 if (requiresDbUpdate) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002136 LauncherModel.modifyItemInDatabase(getContext(), info, container, screenId,
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002137 info.cellX, info.cellY, info.spanX, info.spanY);
2138 }
Adam Cohen2acce882012-03-28 19:03:19 -07002139 }
Adam Cohen482ed822012-03-02 14:15:13 -08002140 }
2141 }
2142
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002143 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002144 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002145 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002146 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002147 lp.useTmpCoords = useTempCoords;
2148 }
2149 }
2150
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002151 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002152 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2153 int[] result = new int[2];
2154 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002155 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002156 resultSpan);
2157 if (result[0] >= 0 && result[1] >= 0) {
2158 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002159 solution.cellX = result[0];
2160 solution.cellY = result[1];
2161 solution.spanX = resultSpan[0];
2162 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002163 solution.isSolution = true;
2164 } else {
2165 solution.isSolution = false;
2166 }
2167 return solution;
2168 }
2169
Adam Cohen19f37922012-03-21 11:59:11 -07002170 /* This seems like it should be obvious and straight-forward, but when the direction vector
2171 needs to match with the notion of the dragView pushing other views, we have to employ
2172 a slightly more subtle notion of the direction vector. The question is what two points is
2173 the vector between? The center of the dragView and its desired destination? Not quite, as
2174 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2175 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2176 or right, which helps make pushing feel right.
2177 */
2178 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2179 int spanY, View dragView, int[] resultDirection) {
2180 int[] targetDestination = new int[2];
2181
2182 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2183 Rect dragRect = new Rect();
2184 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2185 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2186
2187 Rect dropRegionRect = new Rect();
2188 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2189 dragView, dropRegionRect, mIntersectingViews);
2190
2191 int dropRegionSpanX = dropRegionRect.width();
2192 int dropRegionSpanY = dropRegionRect.height();
2193
2194 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2195 dropRegionRect.height(), dropRegionRect);
2196
2197 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2198 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2199
2200 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2201 deltaX = 0;
2202 }
2203 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2204 deltaY = 0;
2205 }
2206
2207 if (deltaX == 0 && deltaY == 0) {
2208 // No idea what to do, give a random direction.
2209 resultDirection[0] = 1;
2210 resultDirection[1] = 0;
2211 } else {
2212 computeDirectionVector(deltaX, deltaY, resultDirection);
2213 }
2214 }
2215
2216 // For a given cell and span, fetch the set of views intersecting the region.
2217 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2218 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2219 if (boundingRect != null) {
2220 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2221 }
2222 intersectingViews.clear();
2223 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2224 Rect r1 = new Rect();
2225 final int count = mShortcutsAndWidgets.getChildCount();
2226 for (int i = 0; i < count; i++) {
2227 View child = mShortcutsAndWidgets.getChildAt(i);
2228 if (child == dragView) continue;
2229 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2230 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2231 if (Rect.intersects(r0, r1)) {
2232 mIntersectingViews.add(child);
2233 if (boundingRect != null) {
2234 boundingRect.union(r1);
2235 }
2236 }
2237 }
2238 }
2239
2240 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2241 View dragView, int[] result) {
2242 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2243 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2244 mIntersectingViews);
2245 return !mIntersectingViews.isEmpty();
2246 }
2247
2248 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002249 completeAndClearReorderPreviewAnimations();
2250 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2251 final int count = mShortcutsAndWidgets.getChildCount();
2252 for (int i = 0; i < count; i++) {
2253 View child = mShortcutsAndWidgets.getChildAt(i);
2254 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2255 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2256 lp.tmpCellX = lp.cellX;
2257 lp.tmpCellY = lp.cellY;
2258 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2259 0, false, false);
2260 }
Adam Cohen19f37922012-03-21 11:59:11 -07002261 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002262 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002263 }
Adam Cohen19f37922012-03-21 11:59:11 -07002264 }
2265
Adam Cohenbebf0422012-04-11 18:06:28 -07002266 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2267 View dragView, int[] direction, boolean commit) {
2268 int[] pixelXY = new int[2];
2269 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2270
2271 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002272 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002273 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2274
2275 setUseTempCoords(true);
2276 if (swapSolution != null && swapSolution.isSolution) {
2277 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2278 // committing anything or animating anything as we just want to determine if a solution
2279 // exists
2280 copySolutionToTempState(swapSolution, dragView);
2281 setItemPlacementDirty(true);
2282 animateItemsToSolution(swapSolution, dragView, commit);
2283
2284 if (commit) {
2285 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002286 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002287 setItemPlacementDirty(false);
2288 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002289 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2290 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002291 }
2292 mShortcutsAndWidgets.requestLayout();
2293 }
2294 return swapSolution.isSolution;
2295 }
2296
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002297 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002298 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002299 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002300 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002301
2302 if (resultSpan == null) {
2303 resultSpan = new int[2];
2304 }
2305
Adam Cohen19f37922012-03-21 11:59:11 -07002306 // When we are checking drop validity or actually dropping, we don't recompute the
2307 // direction vector, since we want the solution to match the preview, and it's possible
2308 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002309 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2310 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002311 mDirectionVector[0] = mPreviousReorderDirection[0];
2312 mDirectionVector[1] = mPreviousReorderDirection[1];
2313 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002314 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2315 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2316 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002317 }
2318 } else {
2319 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2320 mPreviousReorderDirection[0] = mDirectionVector[0];
2321 mPreviousReorderDirection[1] = mDirectionVector[1];
2322 }
2323
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002324 // Find a solution involving pushing / displacing any items in the way
2325 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002326 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2327
2328 // We attempt the approach which doesn't shuffle views at all
2329 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2330 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2331
2332 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002333
2334 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2335 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002336 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2337 finalSolution = swapSolution;
2338 } else if (noShuffleSolution.isSolution) {
2339 finalSolution = noShuffleSolution;
2340 }
2341
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002342 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002343 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002344 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2345 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002346 result[0] = finalSolution.cellX;
2347 result[1] = finalSolution.cellY;
2348 resultSpan[0] = finalSolution.spanX;
2349 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002350 } else {
2351 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2352 }
2353 return result;
2354 }
2355
Adam Cohen482ed822012-03-02 14:15:13 -08002356 boolean foundSolution = true;
2357 if (!DESTRUCTIVE_REORDER) {
2358 setUseTempCoords(true);
2359 }
2360
2361 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002362 result[0] = finalSolution.cellX;
2363 result[1] = finalSolution.cellY;
2364 resultSpan[0] = finalSolution.spanX;
2365 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002366
2367 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2368 // committing anything or animating anything as we just want to determine if a solution
2369 // exists
2370 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2371 if (!DESTRUCTIVE_REORDER) {
2372 copySolutionToTempState(finalSolution, dragView);
2373 }
2374 setItemPlacementDirty(true);
2375 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2376
Adam Cohen19f37922012-03-21 11:59:11 -07002377 if (!DESTRUCTIVE_REORDER &&
2378 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002379 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002380 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002381 setItemPlacementDirty(false);
2382 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002383 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2384 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002385 }
2386 }
2387 } else {
2388 foundSolution = false;
2389 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2390 }
2391
2392 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2393 setUseTempCoords(false);
2394 }
Adam Cohen482ed822012-03-02 14:15:13 -08002395
Michael Jurkaa52570f2012-03-20 03:18:20 -07002396 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002397 return result;
2398 }
2399
Adam Cohen19f37922012-03-21 11:59:11 -07002400 void setItemPlacementDirty(boolean dirty) {
2401 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002402 }
Adam Cohen19f37922012-03-21 11:59:11 -07002403 boolean isItemPlacementDirty() {
2404 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002405 }
2406
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002407 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002408 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002409 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2410 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002411 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002412 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002413
Adam Cohenf3900c22012-11-16 18:28:11 -08002414 void save() {
2415 // Copy current state into savedMap
2416 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002417 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002418 }
2419 }
2420
2421 void restore() {
2422 // Restore current state from savedMap
2423 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002424 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002425 }
2426 }
2427
2428 void add(View v, CellAndSpan cs) {
2429 map.put(v, cs);
2430 savedMap.put(v, new CellAndSpan());
2431 sortedViews.add(v);
2432 }
2433
Adam Cohen482ed822012-03-02 14:15:13 -08002434 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002435 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002436 }
2437
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002438 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2439 boolean first = true;
2440 for (View v: views) {
2441 CellAndSpan c = map.get(v);
2442 if (first) {
2443 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2444 first = false;
2445 } else {
2446 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2447 }
2448 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002449 }
Adam Cohen482ed822012-03-02 14:15:13 -08002450 }
2451
Adam Cohendf035382011-04-11 17:22:04 -07002452 /**
Adam Cohendf035382011-04-11 17:22:04 -07002453 * Find a starting cell position that will fit the given bounds nearest the requested
2454 * cell location. Uses Euclidean distance to score multiple vacant areas.
2455 *
2456 * @param pixelX The X location at which you want to search for a vacant area.
2457 * @param pixelY The Y location at which you want to search for a vacant area.
2458 * @param spanX Horizontal span of the object.
2459 * @param spanY Vertical span of the object.
2460 * @param ignoreView Considers space occupied by this view as unoccupied
2461 * @param result Previously returned value to possibly recycle.
2462 * @return The X, Y cell of a vacant area that can contain this object,
2463 * nearest the requested location.
2464 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002465 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002466 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002467 }
2468
Michael Jurka0280c3b2010-09-17 15:00:07 -07002469 boolean existsEmptyCell() {
2470 return findCellForSpan(null, 1, 1);
2471 }
2472
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002473 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002474 * Finds the upper-left coordinate of the first rectangle in the grid that can
2475 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2476 * then this method will only return coordinates for rectangles that contain the cell
2477 * (intersectX, intersectY)
2478 *
2479 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2480 * can be found.
2481 * @param spanX The horizontal span of the cell we want to find.
2482 * @param spanY The vertical span of the cell we want to find.
2483 *
2484 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002485 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002486 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002487 if (cellXY == null) {
2488 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002489 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002490 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002491 }
2492
2493 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002494 * A drag event has begun over this layout.
2495 * It may have begun over this layout (in which case onDragChild is called first),
2496 * or it may have begun on another layout.
2497 */
2498 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002499 mDragging = true;
2500 }
2501
2502 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002503 * Called when drag has left this CellLayout or has been completed (successfully or not)
2504 */
2505 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002506 // This can actually be called when we aren't in a drag, e.g. when adding a new
2507 // item to this layout via the customize drawer.
2508 // Guard against that case.
2509 if (mDragging) {
2510 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002511 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002512
2513 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002514 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002515 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2516 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002517 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002518 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002519 }
2520
2521 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002522 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002523 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002524 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 *
2526 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002528 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002529 if (child != null) {
2530 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002531 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002532 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002533 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002534 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 }
2536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002538 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002539 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 * @param cellX X coordinate of upper left corner expressed as a cell position
2541 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002542 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002544 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002546 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547 final int cellWidth = mCellWidth;
2548 final int cellHeight = mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002549
Winson Chung4b825dcd2011-06-19 12:41:22 -07002550 final int hStartPadding = getPaddingLeft();
2551 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002552
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302553 int width = cellHSpan * cellWidth;
2554 int height = cellVSpan * cellHeight;
2555 int x = hStartPadding + cellX * cellWidth;
2556 int y = vStartPadding + cellY * cellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -07002557
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002558 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002559 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002560
Adam Cohend4844c32011-02-18 19:25:06 -08002561 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002562 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002563 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002564 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002565 }
2566
Adam Cohend4844c32011-02-18 19:25:06 -08002567 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002568 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002569 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002570 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002571 }
2572
Adam Cohen2801caf2011-05-13 20:57:39 -07002573 public int getDesiredWidth() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302574 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth);
Adam Cohen2801caf2011-05-13 20:57:39 -07002575 }
2576
2577 public int getDesiredHeight() {
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302578 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight);
Adam Cohen2801caf2011-05-13 20:57:39 -07002579 }
2580
Michael Jurka66d72172011-04-12 16:29:25 -07002581 public boolean isOccupied(int x, int y) {
2582 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002583 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002584 } else {
2585 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2586 }
2587 }
2588
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 @Override
2590 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2591 return new CellLayout.LayoutParams(getContext(), attrs);
2592 }
2593
2594 @Override
2595 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2596 return p instanceof CellLayout.LayoutParams;
2597 }
2598
2599 @Override
2600 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2601 return new CellLayout.LayoutParams(p);
2602 }
2603
2604 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2605 /**
2606 * Horizontal location of the item in the grid.
2607 */
2608 @ViewDebug.ExportedProperty
2609 public int cellX;
2610
2611 /**
2612 * Vertical location of the item in the grid.
2613 */
2614 @ViewDebug.ExportedProperty
2615 public int cellY;
2616
2617 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002618 * Temporary horizontal location of the item in the grid during reorder
2619 */
2620 public int tmpCellX;
2621
2622 /**
2623 * Temporary vertical location of the item in the grid during reorder
2624 */
2625 public int tmpCellY;
2626
2627 /**
2628 * Indicates that the temporary coordinates should be used to layout the items
2629 */
2630 public boolean useTmpCoords;
2631
2632 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002633 * Number of cells spanned horizontally by the item.
2634 */
2635 @ViewDebug.ExportedProperty
2636 public int cellHSpan;
2637
2638 /**
2639 * Number of cells spanned vertically by the item.
2640 */
2641 @ViewDebug.ExportedProperty
2642 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002643
Adam Cohen1b607ed2011-03-03 17:26:50 -08002644 /**
2645 * Indicates whether the item will set its x, y, width and height parameters freely,
2646 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2647 */
Adam Cohend4844c32011-02-18 19:25:06 -08002648 public boolean isLockedToGrid = true;
2649
Adam Cohen482ed822012-03-02 14:15:13 -08002650 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002651 * Indicates that this item should use the full extents of its parent.
2652 */
2653 public boolean isFullscreen = false;
2654
2655 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002656 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002657 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002658 */
2659 public boolean canReorder = true;
2660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002661 // X coordinate of the view in the layout.
2662 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002663 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002664 // Y coordinate of the view in the layout.
2665 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002666 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002667
Romain Guy84f296c2009-11-04 15:00:44 -08002668 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002669
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002670 public LayoutParams(Context c, AttributeSet attrs) {
2671 super(c, attrs);
2672 cellHSpan = 1;
2673 cellVSpan = 1;
2674 }
2675
2676 public LayoutParams(ViewGroup.LayoutParams source) {
2677 super(source);
2678 cellHSpan = 1;
2679 cellVSpan = 1;
2680 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002681
2682 public LayoutParams(LayoutParams source) {
2683 super(source);
2684 this.cellX = source.cellX;
2685 this.cellY = source.cellY;
2686 this.cellHSpan = source.cellHSpan;
2687 this.cellVSpan = source.cellVSpan;
2688 }
2689
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002690 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002691 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692 this.cellX = cellX;
2693 this.cellY = cellY;
2694 this.cellHSpan = cellHSpan;
2695 this.cellVSpan = cellVSpan;
2696 }
2697
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302698 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) {
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002699 setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f);
2700 }
2701
2702 /**
2703 * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs
2704 * to be scaled.
2705 *
2706 * ie. In multi-window mode, we setup widgets so that they are measured and laid out
2707 * using their full/invariant device profile sizes.
2708 */
2709 public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount,
2710 float cellScaleX, float cellScaleY) {
Adam Cohend4844c32011-02-18 19:25:06 -08002711 if (isLockedToGrid) {
2712 final int myCellHSpan = cellHSpan;
2713 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002714 int myCellX = useTmpCoords ? tmpCellX : cellX;
2715 int myCellY = useTmpCoords ? tmpCellY : cellY;
2716
2717 if (invertHorizontally) {
2718 myCellX = colCount - myCellX - cellHSpan;
2719 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002720
Jon Miranda7ae64ff2016-11-21 16:18:46 -08002721 width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin);
2722 height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin);
Sunny Goyalaa8a8712016-11-20 15:26:01 +05302723 x = (myCellX * cellWidth + leftMargin);
2724 y = (myCellY * cellHeight + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002725 }
2726 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002727
Winson Chungaafa03c2010-06-11 17:34:16 -07002728 public String toString() {
2729 return "(" + this.cellX + ", " + this.cellY + ")";
2730 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002731
2732 public void setWidth(int width) {
2733 this.width = width;
2734 }
2735
2736 public int getWidth() {
2737 return width;
2738 }
2739
2740 public void setHeight(int height) {
2741 this.height = height;
2742 }
2743
2744 public int getHeight() {
2745 return height;
2746 }
2747
2748 public void setX(int x) {
2749 this.x = x;
2750 }
2751
2752 public int getX() {
2753 return x;
2754 }
2755
2756 public void setY(int y) {
2757 this.y = y;
2758 }
2759
2760 public int getY() {
2761 return y;
2762 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763 }
2764
Michael Jurka0280c3b2010-09-17 15:00:07 -07002765 // This class stores info for two purposes:
2766 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2767 // its spanX, spanY, and the screen it is on
2768 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2769 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2770 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002771 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002772 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002773 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002774 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002775
Sunny Goyal83a8f042015-05-19 12:52:12 -07002776 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002777 cellX = info.cellX;
2778 cellY = info.cellY;
2779 spanX = info.spanX;
2780 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002781 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002782 screenId = info.screenId;
2783 container = info.container;
2784 }
2785
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002786 @Override
2787 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002788 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2789 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002790 }
2791 }
Michael Jurkad771c962011-08-09 15:00:48 -07002792
Tony Wickham86930612015-09-09 13:50:40 -07002793 /**
2794 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2795 * if necessary).
2796 */
2797 public boolean hasReorderSolution(ItemInfo itemInfo) {
2798 int[] cellPoint = new int[2];
2799 // Check for a solution starting at every cell.
2800 for (int cellX = 0; cellX < getCountX(); cellX++) {
2801 for (int cellY = 0; cellY < getCountY(); cellY++) {
2802 cellToPoint(cellX, cellY, cellPoint);
2803 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2804 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2805 true, new ItemConfiguration()).isSolution) {
2806 return true;
2807 }
2808 }
2809 }
2810 return false;
2811 }
2812
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002813 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002814 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002815 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002816}