blob: baccfd15786ab52fb8babe40a025364bb5406ee3 [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;
Joe Onorato4be866d2010-10-10 11:26:02 -070027import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080029import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070031import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080033import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070034import android.graphics.drawable.Drawable;
Sunny Goyal2805e632015-05-20 15:35:32 -070035import android.graphics.drawable.TransitionDrawable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080036import android.os.Build;
Adam Cohen1462de32012-07-24 22:34:36 -070037import android.os.Parcelable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080038import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070041import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.MotionEvent;
43import android.view.View;
44import android.view.ViewDebug;
45import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080046import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070047import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Sunny Goyal4b6eb262015-05-14 19:24:40 -070049import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070050import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070051import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
52import com.android.launcher3.accessibility.FolderAccessibilityHelper;
53import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Tony Wickhame0c33232016-02-08 11:37:04 -080054import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070055import com.android.launcher3.config.ProviderConfig;
Sunny Goyal26119432016-02-18 22:09:23 +000056import com.android.launcher3.folder.FolderIcon;
Sunny Goyal06e21a22016-08-11 16:02:02 -070057import com.android.launcher3.graphics.DragPreviewProvider;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070058import com.android.launcher3.util.CellAndSpan;
59import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070060import com.android.launcher3.util.ParcelableSparseArray;
Adam Cohen091440a2015-03-18 14:16:05 -070061import com.android.launcher3.util.Thunk;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070062
Adam Cohen69ce2e52011-07-03 19:25:21 -070063import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070064import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080065import java.util.Collections;
66import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070067import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080068import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070069
Sunny Goyal4b6eb262015-05-14 19:24:40 -070070public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070071 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
72 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
73
Tony Wickhama0628cc2015-10-14 15:23:04 -070074 private static final String TAG = "CellLayout";
75 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070076
Adam Cohen2acce882012-03-28 19:03:19 -070077 private Launcher mLauncher;
Sunny Goyal4ffec482016-02-09 11:28:52 -080078 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070079 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080080 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070081 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070082 private int mFixedCellWidth;
83 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070084
Sunny Goyal4ffec482016-02-09 11:28:52 -080085 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070086 private int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -080087 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070088 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Adam Cohen234c4cd2011-07-17 21:03:04 -070090 private int mOriginalWidthGap;
91 private int mOriginalHeightGap;
Sunny Goyal4ffec482016-02-09 11:28:52 -080092 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070093 @Thunk int mWidthGap;
Sunny Goyal4ffec482016-02-09 11:28:52 -080094 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070095 @Thunk int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070096 private int mMaxGap;
Adam Cohen917e3882013-10-31 15:03:35 -070097 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070098 private boolean mIsDragTarget = true;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070099 private boolean mJailContent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700101 // These are temporary variables to prevent having to allocate a new object just to
102 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -0700103 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -0700104 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700105
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700106 private GridOccupancy mOccupied;
107 private GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108
Michael Jurkadee05892010-07-27 10:01:56 -0700109 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -0700110 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700111
Adam Cohenefca0272016-02-24 19:19:06 -0800112 private ArrayList<FolderIcon.PreviewBackground> mFolderBackgrounds = new ArrayList<FolderIcon.PreviewBackground>();
113 FolderIcon.PreviewBackground mFolderLeaveBehind = new FolderIcon.PreviewBackground();
114 Paint mFolderBgPaint = new Paint();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700115
Michael Jurka5f1c5092010-09-03 14:15:02 -0700116 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700117
Tony Wickham33326072016-04-04 15:05:49 -0700118 private static final int BACKGROUND_ACTIVATE_DURATION =
119 FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 120 : 0;
Sunny Goyal2805e632015-05-20 15:35:32 -0700120 private final TransitionDrawable mBackground;
121
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700122 // These values allow a fixed measurement to be set on the CellLayout.
123 private int mFixedWidth = -1;
124 private int mFixedHeight = -1;
125
Michael Jurka33945b22010-12-21 18:19:38 -0800126 // If we're actively dragging something over this screen, mIsDragOverlapping is true
127 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700128
Winson Chung150fbab2010-09-29 17:14:26 -0700129 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700130 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700131 @Thunk Rect[] mDragOutlines = new Rect[4];
132 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700133 private InterruptibleInOutAnimator[] mDragOutlineAnims =
134 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700135
136 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700137 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700138 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700139
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700140 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800141
Sunny Goyal316490e2015-06-02 09:38:28 -0700142 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
143 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700144
145 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700146
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700147 // When a drag operation is in progress, holds the nearest cell to the touch point
148 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Joe Onorato4be866d2010-10-10 11:26:02 -0700150 private boolean mDragging = false;
151
Patrick Dubroyce34a972010-10-19 10:34:32 -0700152 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700153 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700154
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800155 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700156 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800157
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800158 public static final int MODE_SHOW_REORDER_HINT = 0;
159 public static final int MODE_DRAG_OVER = 1;
160 public static final int MODE_ON_DROP = 2;
161 public static final int MODE_ON_DROP_EXTERNAL = 3;
162 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700163 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800164 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
165
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800166 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700167 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohen091440a2015-03-18 14:16:05 -0700168 @Thunk float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700169
Adam Cohen482ed822012-03-02 14:15:13 -0800170 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
171 private Rect mOccupiedRect = new Rect();
172 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700173 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700174 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800175
Sunny Goyal2805e632015-05-20 15:35:32 -0700176 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700177
Michael Jurkaca993832012-06-29 15:17:04 -0700178 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700179
Adam Cohenc9735cf2015-01-23 16:11:55 -0800180 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700181 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800182 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 public CellLayout(Context context) {
185 this(context, null);
186 }
187
188 public CellLayout(Context context, AttributeSet attrs) {
189 this(context, attrs, 0);
190 }
191
192 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
193 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700194
195 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
196 // the user where a dragged item will land when dropped.
197 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800198 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700199 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700200
Adam Cohen2e6da152015-05-06 11:42:25 -0700201 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Winson Chung11a1a532013-09-13 11:14:45 -0700203 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800204 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700205 mWidthGap = mOriginalWidthGap = 0;
206 mHeightGap = mOriginalHeightGap = 0;
207 mMaxGap = Integer.MAX_VALUE;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700208
209 mCountX = grid.inv.numColumns;
210 mCountY = grid.inv.numRows;
211 mOccupied = new GridOccupancy(mCountX, mCountY);
212 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
213
Adam Cohen5b53f292012-03-29 14:30:35 -0700214 mPreviousReorderDirection[0] = INVALID_DIRECTION;
215 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Adam Cohenefca0272016-02-24 19:19:06 -0800217 mFolderLeaveBehind.delegateCellX = -1;
218 mFolderLeaveBehind.delegateCellY = -1;
219
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700222 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700223
Tony Wickhame0c33232016-02-08 11:37:04 -0800224 mBackground = (TransitionDrawable) res.getDrawable(
225 FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? R.drawable.bg_screenpanel
226 : R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700227 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700228 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800229
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800230 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700231 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700232
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700233 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700234 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700235 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700236 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800237 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700238 }
239
240 // When dragging things around the home screens, we show a green outline of
241 // where the item will land. The outlines gradually fade out, leaving a trail
242 // behind the drag path.
243 // Set up all the animations that are used to implement this fading.
244 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700245 final float fromAlphaValue = 0;
246 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700247
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700248 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700249
250 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700251 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100252 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700253 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700254 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700255 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700256 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700257 final Bitmap outline = (Bitmap)anim.getTag();
258
259 // If an animation is started and then stopped very quickly, we can still
260 // get spurious updates we've cleared the tag. Guard against this.
261 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700262 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700263 Object val = animation.getAnimatedValue();
264 Log.d(TAG, "anim " + thisIndex + " update: " + val +
265 ", isStopped " + anim.isStopped());
266 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700267 // Try to prevent it from continuing to run
268 animation.cancel();
269 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700270 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800271 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700272 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700273 }
274 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700275 // The animation holds a reference to the drag outline bitmap as long is it's
276 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700277 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700278 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700279 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700280 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700281 anim.setTag(null);
282 }
283 }
284 });
285 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700286 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700287
Michael Jurkaa52570f2012-03-20 03:18:20 -0700288 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700289 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700290 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700291
Mady Mellorbb835202015-07-15 16:34:34 -0700292 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700293
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700294 mTouchFeedbackView = new ClickShadowView(context);
295 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700296 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700297 }
298
Adam Cohenc9735cf2015-01-23 16:11:55 -0800299 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700300 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800301 mUseTouchHelper = enable;
302 if (!enable) {
303 ViewCompat.setAccessibilityDelegate(this, null);
304 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
305 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
306 setOnClickListener(mLauncher);
307 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700308 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
309 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
310 mTouchHelper = new WorkspaceAccessibilityHelper(this);
311 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
312 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
313 mTouchHelper = new FolderAccessibilityHelper(this);
314 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800315 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
316 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
317 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
318 setOnClickListener(mTouchHelper);
319 }
320
321 // Invalidate the accessibility hierarchy
322 if (getParent() != null) {
323 getParent().notifySubtreeAccessibilityStateChanged(
324 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
325 }
326 }
327
328 @Override
329 public boolean dispatchHoverEvent(MotionEvent event) {
330 // Always attempt to dispatch hover events to accessibility first.
331 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
332 return true;
333 }
334 return super.dispatchHoverEvent(event);
335 }
336
337 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800338 public boolean onInterceptTouchEvent(MotionEvent ev) {
339 if (mUseTouchHelper ||
340 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
341 return true;
342 }
343 return false;
344 }
345
Mady Melloref044dd2015-06-02 15:35:07 -0700346 @Override
347 public boolean onTouchEvent(MotionEvent ev) {
348 boolean handled = super.onTouchEvent(ev);
349 // Stylus button press on a home screen should not switch between overview mode and
350 // the home screen mode, however, once in overview mode stylus button press should be
351 // enabled to allow rearranging the different home screens. So check what mode
352 // the workspace is in, and only perform stylus button presses while in overview mode.
353 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700354 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700355 return true;
356 }
357 return handled;
358 }
359
Chris Craik01f2d7f2013-10-01 14:41:56 -0700360 public void enableHardwareLayer(boolean hasLayer) {
361 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700362 }
363
364 public void buildHardwareLayer() {
365 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700366 }
367
Adam Cohen307fe232012-08-16 17:55:58 -0700368 public float getChildrenScale() {
369 return mIsHotseat ? mHotseatScale : 1.0f;
370 }
371
Winson Chung5f8afe62013-08-12 16:19:28 -0700372 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700373 mFixedCellWidth = mCellWidth = width;
374 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700375 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
376 mCountX, mCountY);
377 }
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();
Adam Cohen2374abf2013-04-16 14:56:57 -0700385 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700386 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700387 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700388 }
389
Adam Cohen2374abf2013-04-16 14:56:57 -0700390 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
391 public void setInvertIfRtl(boolean invert) {
392 mShortcutsAndWidgets.setInvertIfRtl(invert);
393 }
394
Adam Cohen917e3882013-10-31 15:03:35 -0700395 public void setDropPending(boolean pending) {
396 mDropPending = pending;
397 }
398
399 public boolean isDropPending() {
400 return mDropPending;
401 }
402
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700403 @Override
404 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700405 if (icon == null || background == null) {
406 mTouchFeedbackView.setBitmap(null);
407 mTouchFeedbackView.animate().cancel();
408 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700409 if (mTouchFeedbackView.setBitmap(background)) {
Winsonbe9798b2016-07-20 12:55:49 -0700410 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
411 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700412 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700413 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800414 }
415 }
416
Adam Cohenc50438c2014-08-19 17:43:05 -0700417 void disableDragTarget() {
418 mIsDragTarget = false;
419 }
420
Tony Wickham0f97b782015-12-02 17:55:07 -0800421 public boolean isDragTarget() {
422 return mIsDragTarget;
423 }
424
Adam Cohenc50438c2014-08-19 17:43:05 -0700425 void setIsDragOverlapping(boolean isDragOverlapping) {
426 if (mIsDragOverlapping != isDragOverlapping) {
427 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700428 if (mIsDragOverlapping) {
429 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
430 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700431 if (mBackgroundAlpha > 0f) {
432 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
433 } else {
434 mBackground.resetTransition();
435 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700436 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700437 invalidate();
438 }
439 }
440
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700441 public void disableJailContent() {
442 mJailContent = false;
443 }
444
445 @Override
446 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
447 if (mJailContent) {
448 ParcelableSparseArray jail = getJailedArray(container);
449 super.dispatchSaveInstanceState(jail);
450 container.put(R.id.cell_layout_jail_id, jail);
451 } else {
452 super.dispatchSaveInstanceState(container);
453 }
454 }
455
456 @Override
457 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
458 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
459 }
460
461 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
462 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
463 return parcelable instanceof ParcelableSparseArray ?
464 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
465 }
466
Tony Wickham0f97b782015-12-02 17:55:07 -0800467 public boolean getIsDragOverlapping() {
468 return mIsDragOverlapping;
469 }
470
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700471 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700472 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700473 if (!mIsDragTarget) {
474 return;
475 }
476
Michael Jurka3e7c7632010-10-02 16:01:03 -0700477 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
478 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
479 // When we're small, we are either drawn normally or in the "accepts drops" state (during
480 // a drag). However, we also drag the mini hover background *over* one of those two
481 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700482 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700483 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700484 }
Romain Guya6abce82009-11-10 02:54:41 -0800485
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700486 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700487 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700488 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700489 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700490 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700491 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700492 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700493 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700494 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800495
Adam Cohen482ed822012-03-02 14:15:13 -0800496 if (DEBUG_VISUALIZE_OCCUPIED) {
497 int[] pt = new int[2];
498 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700499 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800500 for (int i = 0; i < mCountX; i++) {
501 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700502 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800503 cellToPoint(i, j, pt);
504 canvas.save();
505 canvas.translate(pt[0], pt[1]);
506 cd.draw(canvas);
507 canvas.restore();
508 }
509 }
510 }
511 }
512
Adam Cohenefca0272016-02-24 19:19:06 -0800513 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
514 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
515 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
516 canvas.save();
517 canvas.translate(mTempLocation[0], mTempLocation[1]);
518 bg.drawBackground(canvas, mFolderBgPaint);
Adam Cohenf172b742016-03-30 19:28:34 -0700519 if (!bg.isClipping) {
520 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
521 }
Adam Cohenefca0272016-02-24 19:19:06 -0800522 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700523 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700524
Adam Cohenefca0272016-02-24 19:19:06 -0800525 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
526 cellToPoint(mFolderLeaveBehind.delegateCellX,
527 mFolderLeaveBehind.delegateCellY, mTempLocation);
528 canvas.save();
529 canvas.translate(mTempLocation[0], mTempLocation[1]);
530 mFolderLeaveBehind.drawLeaveBehind(canvas, mFolderBgPaint);
531 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700532 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700533 }
534
Adam Cohenefca0272016-02-24 19:19:06 -0800535 @Override
536 protected void dispatchDraw(Canvas canvas) {
537 super.dispatchDraw(canvas);
538
539 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
540 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700541 if (bg.isClipping) {
542 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
543 canvas.save();
544 canvas.translate(mTempLocation[0], mTempLocation[1]);
545 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
546 canvas.restore();
547 }
Adam Cohenefca0272016-02-24 19:19:06 -0800548 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700549 }
550
Adam Cohenefca0272016-02-24 19:19:06 -0800551 public void addFolderBackground(FolderIcon.PreviewBackground bg) {
552 mFolderBackgrounds.add(bg);
553 }
554 public void removeFolderBackground(FolderIcon.PreviewBackground bg) {
555 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700556 }
557
Adam Cohenc51934b2011-07-26 21:07:43 -0700558 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800559
560 DeviceProfile grid = mLauncher.getDeviceProfile();
561 View child = getChildAt(x, y);
562
563 mFolderLeaveBehind.setup(getResources().getDisplayMetrics(), grid, null,
564 child.getMeasuredWidth(), child.getPaddingTop());
565
566 mFolderLeaveBehind.delegateCellX = x;
567 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700568 invalidate();
569 }
570
571 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800572 mFolderLeaveBehind.delegateCellX = -1;
573 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700574 invalidate();
575 }
576
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700577 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700578 public boolean shouldDelayChildPressedState() {
579 return false;
580 }
581
Adam Cohen1462de32012-07-24 22:34:36 -0700582 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700583 try {
584 dispatchRestoreInstanceState(states);
585 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700586 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700587 throw ex;
588 }
589 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
590 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
591 }
Adam Cohen1462de32012-07-24 22:34:36 -0700592 }
593
Michael Jurkae6235dd2011-10-04 15:02:05 -0700594 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700595 public void cancelLongPress() {
596 super.cancelLongPress();
597
598 // Cancel long press for all children
599 final int count = getChildCount();
600 for (int i = 0; i < count; i++) {
601 final View child = getChildAt(i);
602 child.cancelLongPress();
603 }
604 }
605
Michael Jurkadee05892010-07-27 10:01:56 -0700606 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
607 mInterceptTouchListener = listener;
608 }
609
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800610 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700611 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 }
613
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800614 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700615 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 }
617
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800618 public void setIsHotseat(boolean isHotseat) {
619 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700620 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800621 }
622
Sunny Goyale9b651e2015-04-24 11:44:51 -0700623 public boolean isHotseat() {
624 return mIsHotseat;
625 }
626
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800627 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700628 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700629 final LayoutParams lp = params;
630
Andrew Flynnde38e422012-05-08 11:22:15 -0700631 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800632 if (child instanceof BubbleTextView) {
633 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700634 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800635 }
636
Adam Cohen307fe232012-08-16 17:55:58 -0700637 child.setScaleX(getChildrenScale());
638 child.setScaleY(getChildrenScale());
639
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 // Generate an id for each view, this assumes we have at most 256x256 cells
641 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700642 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 // If the horizontal or vertical span is set to -1, it is taken to
644 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700645 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
646 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647
Winson Chungaafa03c2010-06-11 17:34:16 -0700648 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700649 if (LOGD) {
650 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
651 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700652 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700653
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700654 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700655
Winson Chungaafa03c2010-06-11 17:34:16 -0700656 return true;
657 }
658 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700663 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700664 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700665 }
666
667 @Override
668 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700669 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700670 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700671 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700672 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700673 }
674
675 @Override
676 public void removeView(View view) {
677 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700678 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700679 }
680
681 @Override
682 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
684 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700685 }
686
687 @Override
688 public void removeViewInLayout(View view) {
689 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700690 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700691 }
692
693 @Override
694 public void removeViews(int start, int count) {
695 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700696 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700697 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700698 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700699 }
700
701 @Override
702 public void removeViewsInLayout(int start, int count) {
703 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700704 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700705 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700706 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800707 }
708
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700709 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700710 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 * @param x X coordinate of the point
712 * @param y Y coordinate of the point
713 * @param result Array of 2 ints to hold the x and y coordinate of the cell
714 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700715 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700716 final int hStartPadding = getPaddingLeft();
717 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718
719 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
720 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
721
Adam Cohend22015c2010-07-26 22:02:18 -0700722 final int xAxis = mCountX;
723 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724
725 if (result[0] < 0) result[0] = 0;
726 if (result[0] >= xAxis) result[0] = xAxis - 1;
727 if (result[1] < 0) result[1] = 0;
728 if (result[1] >= yAxis) result[1] = yAxis - 1;
729 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 /**
732 * Given a point, return the cell that most closely encloses that point
733 * @param x X coordinate of the point
734 * @param y Y coordinate of the point
735 * @param result Array of 2 ints to hold the x and y coordinate of the cell
736 */
737 void pointToCellRounded(int x, int y, int[] result) {
738 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
739 }
740
741 /**
742 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700743 *
744 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700746 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 * @param result Array of 2 ints to hold the x and y coordinate of the point
748 */
749 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700750 final int hStartPadding = getPaddingLeft();
751 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752
753 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
754 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
755 }
756
Adam Cohene3e27a82011-04-15 12:07:39 -0700757 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800758 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700759 *
760 * @param cellX X coordinate of the cell
761 * @param cellY Y coordinate of the cell
762 *
763 * @param result Array of 2 ints to hold the x and y coordinate of the point
764 */
765 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700766 regionToCenterPoint(cellX, cellY, 1, 1, result);
767 }
768
769 /**
770 * Given a cell coordinate and span return the point that represents the center of the regio
771 *
772 * @param cellX X coordinate of the cell
773 * @param cellY Y coordinate of the cell
774 *
775 * @param result Array of 2 ints to hold the x and y coordinate of the point
776 */
777 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700778 final int hStartPadding = getPaddingLeft();
779 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700780 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
781 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
782 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
783 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700784 }
785
Adam Cohen19f37922012-03-21 11:59:11 -0700786 /**
787 * Given a cell coordinate and span fills out a corresponding pixel rect
788 *
789 * @param cellX X coordinate of the cell
790 * @param cellY Y coordinate of the cell
791 * @param result Rect in which to write the result
792 */
793 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
794 final int hStartPadding = getPaddingLeft();
795 final int vStartPadding = getPaddingTop();
796 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
797 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
798 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
799 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
800 }
801
Adam Cohen482ed822012-03-02 14:15:13 -0800802 public float getDistanceFromCell(float x, float y, int[] cell) {
803 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700804 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800805 }
806
Adam Cohenf9c184a2016-01-15 16:47:43 -0800807 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800808 return mCellWidth;
809 }
810
811 int getCellHeight() {
812 return mCellHeight;
813 }
814
Adam Cohend4844c32011-02-18 19:25:06 -0800815 int getWidthGap() {
816 return mWidthGap;
817 }
818
819 int getHeightGap() {
820 return mHeightGap;
821 }
822
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700823 public void setFixedSize(int width, int height) {
824 mFixedWidth = width;
825 mFixedHeight = height;
826 }
827
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 @Override
829 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700832 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
833 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700834 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
835 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700836 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700837 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
838 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700839 if (cw != mCellWidth || ch != mCellHeight) {
840 mCellWidth = cw;
841 mCellHeight = ch;
842 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
843 mHeightGap, mCountX, mCountY);
844 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700845 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700846
Winson Chung2d75f122013-09-23 16:53:31 -0700847 int newWidth = childWidthSize;
848 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700849 if (mFixedWidth > 0 && mFixedHeight > 0) {
850 newWidth = mFixedWidth;
851 newHeight = mFixedHeight;
852 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
854 }
855
Adam Cohend22015c2010-07-26 22:02:18 -0700856 int numWidthGaps = mCountX - 1;
857 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858
Adam Cohen234c4cd2011-07-17 21:03:04 -0700859 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700860 int hSpace = childWidthSize;
861 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700862 int hFreeSpace = hSpace - (mCountX * mCellWidth);
863 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700864 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
865 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700866 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
867 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700868 } else {
869 mWidthGap = mOriginalWidthGap;
870 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700871 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700872
873 // Make the feedback view large enough to hold the blur bitmap.
874 mTouchFeedbackView.measure(
875 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
876 MeasureSpec.EXACTLY),
877 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
878 MeasureSpec.EXACTLY));
879
880 mShortcutsAndWidgets.measure(
881 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
882 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
883
884 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
885 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700886 if (mFixedWidth > 0 && mFixedHeight > 0) {
887 setMeasuredDimension(maxWidth, maxHeight);
888 } else {
889 setMeasuredDimension(widthSize, heightSize);
890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 }
892
893 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700894 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800895 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
896 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
897 int left = getPaddingLeft();
898 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800899 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800900 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700901 int right = r - l - getPaddingRight();
902 if (!isFullscreen) {
903 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
904 }
905
Winson Chung38848ca2013-10-08 12:03:44 -0700906 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700907 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700908
909 mTouchFeedbackView.layout(left, top,
910 left + mTouchFeedbackView.getMeasuredWidth(),
911 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700912 mShortcutsAndWidgets.layout(left, top, right, bottom);
913
914 // Expand the background drawing bounds by the padding baked into the background drawable
915 mBackground.getPadding(mTempRect);
916 mBackground.setBounds(
917 left - mTempRect.left,
918 top - mTempRect.top,
919 right + mTempRect.right,
920 bottom + mTempRect.bottom);
921 }
922
Tony Wickhama501d492015-11-03 18:05:01 -0800923 /**
924 * Returns the amount of space left over after subtracting padding and cells. This space will be
925 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
926 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
927 */
928 public int getUnusedHorizontalSpace() {
929 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
930 }
931
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 @Override
933 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700934 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 }
936
937 @Override
938 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700939 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
Michael Jurka5f1c5092010-09-03 14:15:02 -0700942 public float getBackgroundAlpha() {
943 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700944 }
945
Michael Jurka5f1c5092010-09-03 14:15:02 -0700946 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800947 if (mBackgroundAlpha != alpha) {
948 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700949 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800950 }
Michael Jurkadee05892010-07-27 10:01:56 -0700951 }
952
Sunny Goyal2805e632015-05-20 15:35:32 -0700953 @Override
954 protected boolean verifyDrawable(Drawable who) {
955 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
956 }
957
Michael Jurkaa52570f2012-03-20 03:18:20 -0700958 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700959 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700960 }
961
Michael Jurkaa52570f2012-03-20 03:18:20 -0700962 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700963 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700964 }
965
Patrick Dubroy440c3602010-07-13 17:50:32 -0700966 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700967 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700968 }
969
Adam Cohen76fc0852011-06-17 13:26:23 -0700970 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800971 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700972 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800973
Adam Cohen19f37922012-03-21 11:59:11 -0700974 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700975 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
976 final ItemInfo info = (ItemInfo) child.getTag();
977
978 // We cancel any existing animations
979 if (mReorderAnimators.containsKey(lp)) {
980 mReorderAnimators.get(lp).cancel();
981 mReorderAnimators.remove(lp);
982 }
983
Adam Cohen482ed822012-03-02 14:15:13 -0800984 final int oldX = lp.x;
985 final int oldY = lp.y;
986 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700987 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
988 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
989 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800990 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700991 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800992 if (permanent) {
993 lp.cellX = info.cellX = cellX;
994 lp.cellY = info.cellY = cellY;
995 } else {
996 lp.tmpCellX = cellX;
997 lp.tmpCellY = cellY;
998 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700999 clc.setupLp(lp);
1000 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001001 final int newX = lp.x;
1002 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001003
Adam Cohen76fc0852011-06-17 13:26:23 -07001004 lp.x = oldX;
1005 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001006
Adam Cohen482ed822012-03-02 14:15:13 -08001007 // Exit early if we're not actually moving the view
1008 if (oldX == newX && oldY == newY) {
1009 lp.isLockedToGrid = true;
1010 return true;
1011 }
1012
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001013 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001014 va.setDuration(duration);
1015 mReorderAnimators.put(lp, va);
1016
1017 va.addUpdateListener(new AnimatorUpdateListener() {
1018 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001019 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001020 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001021 lp.x = (int) ((1 - r) * oldX + r * newX);
1022 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001023 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001024 }
1025 });
Adam Cohen482ed822012-03-02 14:15:13 -08001026 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001027 boolean cancelled = false;
1028 public void onAnimationEnd(Animator animation) {
1029 // If the animation was cancelled, it means that another animation
1030 // has interrupted this one, and we don't want to lock the item into
1031 // place just yet.
1032 if (!cancelled) {
1033 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001034 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001035 }
1036 if (mReorderAnimators.containsKey(lp)) {
1037 mReorderAnimators.remove(lp);
1038 }
1039 }
1040 public void onAnimationCancel(Animator animation) {
1041 cancelled = true;
1042 }
1043 });
Adam Cohen482ed822012-03-02 14:15:13 -08001044 va.setStartDelay(delay);
1045 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001046 return true;
1047 }
1048 return false;
1049 }
1050
Sunny Goyal06e21a22016-08-11 16:02:02 -07001051 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
1052 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001053 final int oldDragCellX = mDragCell[0];
1054 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001055
Sunny Goyal06e21a22016-08-11 16:02:02 -07001056 if (outlineProvider == null || outlineProvider.gerenatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001057 return;
1058 }
1059
Sunny Goyal06e21a22016-08-11 16:02:02 -07001060 Bitmap dragOutline = outlineProvider.gerenatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -08001061 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001062 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1063 Rect dragRegion = dragObject.dragView.getDragRegion();
1064
Adam Cohen482ed822012-03-02 14:15:13 -08001065 mDragCell[0] = cellX;
1066 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001067
Joe Onorato4be866d2010-10-10 11:26:02 -07001068 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001069 mDragOutlineAnims[oldIndex].animateOut();
1070 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001071 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001072
Adam Cohend41fbf52012-02-16 23:53:59 -08001073 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001074 cellToRect(cellX, cellY, spanX, spanY, r);
Sunny Goyal106bf642015-07-16 12:18:06 -07001075 } else {
1076 // Find the top left corner of the rect the object will occupy
1077 final int[] topLeft = mTmpPoint;
1078 cellToPoint(cellX, cellY, topLeft);
1079
1080 int left = topLeft[0];
1081 int top = topLeft[1];
1082
1083 if (v != null && dragOffset == null) {
1084 // When drawing the drag outline, it did not account for margin offsets
1085 // added by the view's parent.
1086 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1087 left += lp.leftMargin;
1088 top += lp.topMargin;
1089
1090 // Offsets due to the size difference between the View and the dragOutline.
1091 // There is a size difference to account for the outer blur, which may lie
1092 // outside the bounds of the view.
1093 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1094 // We center about the x axis
1095 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1096 - dragOutline.getWidth()) / 2;
1097 } else {
1098 if (dragOffset != null && dragRegion != null) {
1099 // Center the drag region *horizontally* in the cell and apply a drag
1100 // outline offset
1101 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1102 - dragRegion.width()) / 2;
1103 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1104 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1105 top += dragOffset.y + cellPaddingY;
1106 } else {
1107 // Center the drag outline in the cell
1108 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1109 - dragOutline.getWidth()) / 2;
1110 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1111 - dragOutline.getHeight()) / 2;
1112 }
1113 }
1114 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001115 }
Winson Chung150fbab2010-09-29 17:14:26 -07001116
Sunny Goyal106bf642015-07-16 12:18:06 -07001117 Utilities.scaleRectAboutCenter(r, getChildrenScale());
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001118 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1119 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001120
1121 if (dragObject.stateAnnouncer != null) {
1122 String msg;
1123 if (isHotseat()) {
1124 msg = getContext().getString(R.string.move_to_hotseat_position,
1125 Math.max(cellX, cellY) + 1);
1126 } else {
1127 msg = getContext().getString(R.string.move_to_empty_cell,
1128 cellY + 1, cellX + 1);
1129 }
1130 dragObject.stateAnnouncer.announce(msg);
1131 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001132 }
1133 }
1134
Adam Cohene0310962011-04-18 16:15:31 -07001135 public void clearDragOutlines() {
1136 final int oldIndex = mDragOutlineCurrent;
1137 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001138 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001139 }
1140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001142 * Find a vacant area that will fit the given bounds nearest the requested
1143 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001144 *
Romain Guy51afc022009-05-04 18:03:43 -07001145 * @param pixelX The X location at which you want to search for a vacant area.
1146 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001147 * @param minSpanX The minimum horizontal span required
1148 * @param minSpanY The minimum vertical span required
1149 * @param spanX Horizontal span of the object.
1150 * @param spanY Vertical span of the object.
1151 * @param result Array in which to place the result, or null (in which case a new array will
1152 * be allocated)
1153 * @return The X, Y cell of a vacant area that can contain this object,
1154 * nearest the requested location.
1155 */
1156 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1157 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001158 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001159 result, resultSpan);
1160 }
1161
Adam Cohend41fbf52012-02-16 23:53:59 -08001162 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1163 private void lazyInitTempRectStack() {
1164 if (mTempRectStack.isEmpty()) {
1165 for (int i = 0; i < mCountX * mCountY; i++) {
1166 mTempRectStack.push(new Rect());
1167 }
1168 }
1169 }
Adam Cohen482ed822012-03-02 14:15:13 -08001170
Adam Cohend41fbf52012-02-16 23:53:59 -08001171 private void recycleTempRects(Stack<Rect> used) {
1172 while (!used.isEmpty()) {
1173 mTempRectStack.push(used.pop());
1174 }
1175 }
1176
1177 /**
1178 * Find a vacant area that will fit the given bounds nearest the requested
1179 * cell location. Uses Euclidean distance to score multiple vacant areas.
1180 *
1181 * @param pixelX The X location at which you want to search for a vacant area.
1182 * @param pixelY The Y location at which you want to search for a vacant area.
1183 * @param minSpanX The minimum horizontal span required
1184 * @param minSpanY The minimum vertical span required
1185 * @param spanX Horizontal span of the object.
1186 * @param spanY Vertical span of the object.
1187 * @param ignoreOccupied If true, the result can be an occupied cell
1188 * @param result Array in which to place the result, or null (in which case a new array will
1189 * be allocated)
1190 * @return The X, Y cell of a vacant area that can contain this object,
1191 * nearest the requested location.
1192 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001193 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1194 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001195 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001196
Adam Cohene3e27a82011-04-15 12:07:39 -07001197 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1198 // to the center of the item, but we are searching based on the top-left cell, so
1199 // we translate the point over to correspond to the top-left.
1200 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1201 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1202
Jeff Sharkey70864282009-04-07 21:08:40 -07001203 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001204 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001205 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001206 final Rect bestRect = new Rect(-1, -1, -1, -1);
1207 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001208
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001209 final int countX = mCountX;
1210 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001211
Adam Cohend41fbf52012-02-16 23:53:59 -08001212 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1213 spanX < minSpanX || spanY < minSpanY) {
1214 return bestXY;
1215 }
1216
1217 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001218 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001219 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1220 int ySize = -1;
1221 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001222 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001223 // First, let's see if this thing fits anywhere
1224 for (int i = 0; i < minSpanX; i++) {
1225 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001226 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001227 continue inner;
1228 }
Michael Jurkac28de512010-08-13 11:27:44 -07001229 }
1230 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001231 xSize = minSpanX;
1232 ySize = minSpanY;
1233
1234 // We know that the item will fit at _some_ acceptable size, now let's see
1235 // how big we can make it. We'll alternate between incrementing x and y spans
1236 // until we hit a limit.
1237 boolean incX = true;
1238 boolean hitMaxX = xSize >= spanX;
1239 boolean hitMaxY = ySize >= spanY;
1240 while (!(hitMaxX && hitMaxY)) {
1241 if (incX && !hitMaxX) {
1242 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001243 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001244 // We can't move out horizontally
1245 hitMaxX = true;
1246 }
1247 }
1248 if (!hitMaxX) {
1249 xSize++;
1250 }
1251 } else if (!hitMaxY) {
1252 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001253 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001254 // We can't move out vertically
1255 hitMaxY = true;
1256 }
1257 }
1258 if (!hitMaxY) {
1259 ySize++;
1260 }
1261 }
1262 hitMaxX |= xSize >= spanX;
1263 hitMaxY |= ySize >= spanY;
1264 incX = !incX;
1265 }
1266 incX = true;
1267 hitMaxX = xSize >= spanX;
1268 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001269 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001270 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001271 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272
Adam Cohend41fbf52012-02-16 23:53:59 -08001273 // We verify that the current rect is not a sub-rect of any of our previous
1274 // candidates. In this case, the current rect is disqualified in favour of the
1275 // containing rect.
1276 Rect currentRect = mTempRectStack.pop();
1277 currentRect.set(x, y, x + xSize, y + ySize);
1278 boolean contained = false;
1279 for (Rect r : validRegions) {
1280 if (r.contains(currentRect)) {
1281 contained = true;
1282 break;
1283 }
1284 }
1285 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001286 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001287
Adam Cohend41fbf52012-02-16 23:53:59 -08001288 if ((distance <= bestDistance && !contained) ||
1289 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001290 bestDistance = distance;
1291 bestXY[0] = x;
1292 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001293 if (resultSpan != null) {
1294 resultSpan[0] = xSize;
1295 resultSpan[1] = ySize;
1296 }
1297 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001298 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 }
1300 }
1301
Adam Cohenc0dcf592011-06-01 15:30:43 -07001302 // Return -1, -1 if no suitable location found
1303 if (bestDistance == Double.MAX_VALUE) {
1304 bestXY[0] = -1;
1305 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001306 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001307 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001308 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001310
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001311 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001312 * Find a vacant area that will fit the given bounds nearest the requested
1313 * cell location, and will also weigh in a suggested direction vector of the
1314 * desired location. This method computers distance based on unit grid distances,
1315 * not pixel distances.
1316 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001317 * @param cellX The X cell nearest to which you want to search for a vacant area.
1318 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001319 * @param spanX Horizontal span of the object.
1320 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001321 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001322 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001323 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001324 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001325 * @param result Array in which to place the result, or null (in which case a new array will
1326 * be allocated)
1327 * @return The X, Y cell of a vacant area that can contain this object,
1328 * nearest the requested location.
1329 */
1330 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001331 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001332 // Keep track of best-scoring drop area
1333 final int[] bestXY = result != null ? result : new int[2];
1334 float bestDistance = Float.MAX_VALUE;
1335 int bestDirectionScore = Integer.MIN_VALUE;
1336
1337 final int countX = mCountX;
1338 final int countY = mCountY;
1339
1340 for (int y = 0; y < countY - (spanY - 1); y++) {
1341 inner:
1342 for (int x = 0; x < countX - (spanX - 1); x++) {
1343 // First, let's see if this thing fits anywhere
1344 for (int i = 0; i < spanX; i++) {
1345 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001346 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001347 continue inner;
1348 }
1349 }
1350 }
1351
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001352 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001353 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001354 computeDirectionVector(x - cellX, y - cellY, curDirection);
1355 // The direction score is just the dot product of the two candidate direction
1356 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001357 int curDirectionScore = direction[0] * curDirection[0] +
1358 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001359 if (Float.compare(distance, bestDistance) < 0 ||
1360 (Float.compare(distance, bestDistance) == 0
1361 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001362 bestDistance = distance;
1363 bestDirectionScore = curDirectionScore;
1364 bestXY[0] = x;
1365 bestXY[1] = y;
1366 }
1367 }
1368 }
1369
1370 // Return -1, -1 if no suitable location found
1371 if (bestDistance == Float.MAX_VALUE) {
1372 bestXY[0] = -1;
1373 bestXY[1] = -1;
1374 }
1375 return bestXY;
1376 }
1377
1378 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001379 int[] direction, ItemConfiguration currentState) {
1380 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001381 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001382 mTmpOccupied.markCells(c, false);
1383 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001384
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001385 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1386 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001387
1388 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001389 c.cellX = mTempLocation[0];
1390 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001391 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001392 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001393 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001394 return success;
1395 }
1396
Adam Cohenf3900c22012-11-16 18:28:11 -08001397 /**
1398 * This helper class defines a cluster of views. It helps with defining complex edges
1399 * of the cluster and determining how those edges interact with other views. The edges
1400 * essentially define a fine-grained boundary around the cluster of views -- like a more
1401 * precise version of a bounding box.
1402 */
1403 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001404 final static int LEFT = 1 << 0;
1405 final static int TOP = 1 << 1;
1406 final static int RIGHT = 1 << 2;
1407 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001408
Adam Cohenf3900c22012-11-16 18:28:11 -08001409 ArrayList<View> views;
1410 ItemConfiguration config;
1411 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001412
Adam Cohenf3900c22012-11-16 18:28:11 -08001413 int[] leftEdge = new int[mCountY];
1414 int[] rightEdge = new int[mCountY];
1415 int[] topEdge = new int[mCountX];
1416 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001417 int dirtyEdges;
1418 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001419
1420 @SuppressWarnings("unchecked")
1421 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1422 this.views = (ArrayList<View>) views.clone();
1423 this.config = config;
1424 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001425 }
1426
Adam Cohenf3900c22012-11-16 18:28:11 -08001427 void resetEdges() {
1428 for (int i = 0; i < mCountX; i++) {
1429 topEdge[i] = -1;
1430 bottomEdge[i] = -1;
1431 }
1432 for (int i = 0; i < mCountY; i++) {
1433 leftEdge[i] = -1;
1434 rightEdge[i] = -1;
1435 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001436 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001437 boundingRectDirty = true;
1438 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001439
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001440 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001441 int count = views.size();
1442 for (int i = 0; i < count; i++) {
1443 CellAndSpan cs = config.map.get(views.get(i));
1444 switch (which) {
1445 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001446 int left = cs.cellX;
1447 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001448 if (left < leftEdge[j] || leftEdge[j] < 0) {
1449 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001450 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001451 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001452 break;
1453 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001454 int right = cs.cellX + cs.spanX;
1455 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001456 if (right > rightEdge[j]) {
1457 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001458 }
1459 }
1460 break;
1461 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001462 int top = cs.cellY;
1463 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001464 if (top < topEdge[j] || topEdge[j] < 0) {
1465 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001466 }
1467 }
1468 break;
1469 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001470 int bottom = cs.cellY + cs.spanY;
1471 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001472 if (bottom > bottomEdge[j]) {
1473 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001474 }
1475 }
1476 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001477 }
1478 }
1479 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001480
1481 boolean isViewTouchingEdge(View v, int whichEdge) {
1482 CellAndSpan cs = config.map.get(v);
1483
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001484 if ((dirtyEdges & whichEdge) == whichEdge) {
1485 computeEdge(whichEdge);
1486 dirtyEdges &= ~whichEdge;
1487 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001488
1489 switch (whichEdge) {
1490 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001491 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1492 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001493 return true;
1494 }
1495 }
1496 break;
1497 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001498 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1499 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001500 return true;
1501 }
1502 }
1503 break;
1504 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001505 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1506 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001507 return true;
1508 }
1509 }
1510 break;
1511 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001512 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1513 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001514 return true;
1515 }
1516 }
1517 break;
1518 }
1519 return false;
1520 }
1521
1522 void shift(int whichEdge, int delta) {
1523 for (View v: views) {
1524 CellAndSpan c = config.map.get(v);
1525 switch (whichEdge) {
1526 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001527 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001528 break;
1529 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001530 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001531 break;
1532 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001533 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001534 break;
1535 case BOTTOM:
1536 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001537 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001538 break;
1539 }
1540 }
1541 resetEdges();
1542 }
1543
1544 public void addView(View v) {
1545 views.add(v);
1546 resetEdges();
1547 }
1548
1549 public Rect getBoundingRect() {
1550 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001551 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001552 }
1553 return boundingRect;
1554 }
1555
Adam Cohenf3900c22012-11-16 18:28:11 -08001556 PositionComparator comparator = new PositionComparator();
1557 class PositionComparator implements Comparator<View> {
1558 int whichEdge = 0;
1559 public int compare(View left, View right) {
1560 CellAndSpan l = config.map.get(left);
1561 CellAndSpan r = config.map.get(right);
1562 switch (whichEdge) {
1563 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001564 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001565 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001566 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001567 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001568 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001569 case BOTTOM:
1570 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001571 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001572 }
1573 }
1574 }
1575
1576 public void sortConfigurationForEdgePush(int edge) {
1577 comparator.whichEdge = edge;
1578 Collections.sort(config.sortedViews, comparator);
1579 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001580 }
1581
Adam Cohenf3900c22012-11-16 18:28:11 -08001582 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1583 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001584
Adam Cohenf3900c22012-11-16 18:28:11 -08001585 ViewCluster cluster = new ViewCluster(views, currentState);
1586 Rect clusterRect = cluster.getBoundingRect();
1587 int whichEdge;
1588 int pushDistance;
1589 boolean fail = false;
1590
1591 // Determine the edge of the cluster that will be leading the push and how far
1592 // the cluster must be shifted.
1593 if (direction[0] < 0) {
1594 whichEdge = ViewCluster.LEFT;
1595 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001596 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001597 whichEdge = ViewCluster.RIGHT;
1598 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1599 } else if (direction[1] < 0) {
1600 whichEdge = ViewCluster.TOP;
1601 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1602 } else {
1603 whichEdge = ViewCluster.BOTTOM;
1604 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001605 }
1606
Adam Cohenf3900c22012-11-16 18:28:11 -08001607 // Break early for invalid push distance.
1608 if (pushDistance <= 0) {
1609 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001610 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001611
1612 // Mark the occupied state as false for the group of views we want to move.
1613 for (View v: views) {
1614 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001615 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001616 }
1617
1618 // We save the current configuration -- if we fail to find a solution we will revert
1619 // to the initial state. The process of finding a solution modifies the configuration
1620 // in place, hence the need for revert in the failure case.
1621 currentState.save();
1622
1623 // The pushing algorithm is simplified by considering the views in the order in which
1624 // they would be pushed by the cluster. For example, if the cluster is leading with its
1625 // left edge, we consider sort the views by their right edge, from right to left.
1626 cluster.sortConfigurationForEdgePush(whichEdge);
1627
1628 while (pushDistance > 0 && !fail) {
1629 for (View v: currentState.sortedViews) {
1630 // For each view that isn't in the cluster, we see if the leading edge of the
1631 // cluster is contacting the edge of that view. If so, we add that view to the
1632 // cluster.
1633 if (!cluster.views.contains(v) && v != dragView) {
1634 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1635 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1636 if (!lp.canReorder) {
1637 // The push solution includes the all apps button, this is not viable.
1638 fail = true;
1639 break;
1640 }
1641 cluster.addView(v);
1642 CellAndSpan c = currentState.map.get(v);
1643
1644 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001645 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001646 }
1647 }
1648 }
1649 pushDistance--;
1650
1651 // The cluster has been completed, now we move the whole thing over in the appropriate
1652 // direction.
1653 cluster.shift(whichEdge, 1);
1654 }
1655
1656 boolean foundSolution = false;
1657 clusterRect = cluster.getBoundingRect();
1658
1659 // Due to the nature of the algorithm, the only check required to verify a valid solution
1660 // is to ensure that completed shifted cluster lies completely within the cell layout.
1661 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1662 clusterRect.bottom <= mCountY) {
1663 foundSolution = true;
1664 } else {
1665 currentState.restore();
1666 }
1667
1668 // In either case, we set the occupied array as marked for the location of the views
1669 for (View v: cluster.views) {
1670 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001671 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001672 }
1673
1674 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001675 }
1676
Adam Cohen482ed822012-03-02 14:15:13 -08001677 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001678 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001679 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001680
Adam Cohen8baab352012-03-20 17:39:21 -07001681 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001682 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001683 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001684 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001685
Adam Cohen8baab352012-03-20 17:39:21 -07001686 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001687 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001688 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001689 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001690 }
1691
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001692 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001693 int top = boundingRect.top;
1694 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001695 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001696 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001697 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001698 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001699 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001700 }
1701
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001702 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001703
Adam Cohenf3900c22012-11-16 18:28:11 -08001704 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001705 boundingRect.height(), direction,
1706 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001707
Adam Cohen8baab352012-03-20 17:39:21 -07001708 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001709 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001710 int deltaX = mTempLocation[0] - boundingRect.left;
1711 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001712 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001713 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001714 c.cellX += deltaX;
1715 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001716 }
1717 success = true;
1718 }
Adam Cohen8baab352012-03-20 17:39:21 -07001719
1720 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001721 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001722 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001723 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001724 }
1725 return success;
1726 }
1727
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001728 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1729 // to push items in each of the cardinal directions, in an order based on the direction vector
1730 // passed.
1731 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1732 int[] direction, View ignoreView, ItemConfiguration solution) {
1733 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001734 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001735 // separately in each of the components.
1736 int temp = direction[1];
1737 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001738
1739 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001740 ignoreView, solution)) {
1741 return true;
1742 }
1743 direction[1] = temp;
1744 temp = direction[0];
1745 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001746
1747 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001748 ignoreView, solution)) {
1749 return true;
1750 }
1751 // Revert the direction
1752 direction[0] = temp;
1753
1754 // Now we try pushing in each component of the opposite direction
1755 direction[0] *= -1;
1756 direction[1] *= -1;
1757 temp = direction[1];
1758 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001759 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001760 ignoreView, solution)) {
1761 return true;
1762 }
1763
1764 direction[1] = temp;
1765 temp = direction[0];
1766 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001767 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001768 ignoreView, solution)) {
1769 return true;
1770 }
1771 // revert the direction
1772 direction[0] = temp;
1773 direction[0] *= -1;
1774 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001775
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001776 } else {
1777 // If the direction vector has a single non-zero component, we push first in the
1778 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001779 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001780 ignoreView, solution)) {
1781 return true;
1782 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001783 // Then we try the opposite direction
1784 direction[0] *= -1;
1785 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001786 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001787 ignoreView, solution)) {
1788 return true;
1789 }
1790 // Switch the direction back
1791 direction[0] *= -1;
1792 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001793
1794 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001795 // to find a solution by pushing along the perpendicular axis.
1796
1797 // Swap the components
1798 int temp = direction[1];
1799 direction[1] = direction[0];
1800 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001801 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001802 ignoreView, solution)) {
1803 return true;
1804 }
1805
1806 // Then we try the opposite direction
1807 direction[0] *= -1;
1808 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001809 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001810 ignoreView, solution)) {
1811 return true;
1812 }
1813 // Switch the direction back
1814 direction[0] *= -1;
1815 direction[1] *= -1;
1816
1817 // Swap the components back
1818 temp = direction[1];
1819 direction[1] = direction[0];
1820 direction[0] = temp;
1821 }
1822 return false;
1823 }
1824
Adam Cohen482ed822012-03-02 14:15:13 -08001825 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001826 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001827 // Return early if get invalid cell positions
1828 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001829
Adam Cohen8baab352012-03-20 17:39:21 -07001830 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001831 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001832
Adam Cohen8baab352012-03-20 17:39:21 -07001833 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001834 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001835 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001836 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001837 c.cellX = cellX;
1838 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001839 }
Adam Cohen482ed822012-03-02 14:15:13 -08001840 }
Adam Cohen482ed822012-03-02 14:15:13 -08001841 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1842 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001843 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001844 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001845 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001846 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001847 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001848 if (Rect.intersects(r0, r1)) {
1849 if (!lp.canReorder) {
1850 return false;
1851 }
1852 mIntersectingViews.add(child);
1853 }
1854 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001855
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001856 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1857
Winson Chung5f8afe62013-08-12 16:19:28 -07001858 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001859 // we try to find a solution such that no displaced item travels through another item
1860 // without also displacing that item.
1861 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001862 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001863 return true;
1864 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001865
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001866 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001867 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001868 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001869 return true;
1870 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001871
Adam Cohen482ed822012-03-02 14:15:13 -08001872 // Ok, they couldn't move as a block, let's move them individually
1873 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001874 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001875 return false;
1876 }
1877 }
1878 return true;
1879 }
1880
1881 /*
1882 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1883 * the provided point and the provided cell
1884 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001885 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001886 double angle = Math.atan(((float) deltaY) / deltaX);
1887
1888 result[0] = 0;
1889 result[1] = 0;
1890 if (Math.abs(Math.cos(angle)) > 0.5f) {
1891 result[0] = (int) Math.signum(deltaX);
1892 }
1893 if (Math.abs(Math.sin(angle)) > 0.5f) {
1894 result[1] = (int) Math.signum(deltaY);
1895 }
1896 }
1897
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001898 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001899 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1900 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001901 // Copy the current state into the solution. This solution will be manipulated as necessary.
1902 copyCurrentStateToSolution(solution, false);
1903 // Copy the current occupied array into the temporary occupied array. This array will be
1904 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001905 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001906
1907 // We find the nearest cell into which we would place the dragged item, assuming there's
1908 // nothing in its way.
1909 int result[] = new int[2];
1910 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1911
1912 boolean success = false;
1913 // First we try the exact nearest position of the item being dragged,
1914 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001915 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1916 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001917
1918 if (!success) {
1919 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1920 // x, then 1 in y etc.
1921 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001922 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1923 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001924 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001925 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1926 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001927 }
1928 solution.isSolution = false;
1929 } else {
1930 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001931 solution.cellX = result[0];
1932 solution.cellY = result[1];
1933 solution.spanX = spanX;
1934 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001935 }
1936 return solution;
1937 }
1938
1939 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001940 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001941 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001942 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001943 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001944 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001945 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001946 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001947 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001948 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001949 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001950 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001951 }
1952 }
1953
1954 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001955 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001956
Michael Jurkaa52570f2012-03-20 03:18:20 -07001957 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001958 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001959 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001960 if (child == dragView) continue;
1961 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001962 CellAndSpan c = solution.map.get(child);
1963 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001964 lp.tmpCellX = c.cellX;
1965 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001966 lp.cellHSpan = c.spanX;
1967 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001968 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001969 }
1970 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001971 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001972 }
1973
1974 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1975 commitDragView) {
1976
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001977 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1978 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001979
Michael Jurkaa52570f2012-03-20 03:18:20 -07001980 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001981 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001982 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001983 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001984 CellAndSpan c = solution.map.get(child);
1985 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001986 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001987 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001988 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001989 }
1990 }
1991 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001992 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001993 }
1994 }
1995
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001996
1997 // This method starts or changes the reorder preview animations
1998 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1999 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002000 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002001 for (int i = 0; i < childCount; i++) {
2002 View child = mShortcutsAndWidgets.getChildAt(i);
2003 if (child == dragView) continue;
2004 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002005 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2006 != null && !solution.intersectingViews.contains(child);
2007
Adam Cohen19f37922012-03-21 11:59:11 -07002008 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002009 if (c != null && !skip) {
2010 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002011 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002012 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002013 }
2014 }
2015 }
2016
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002017 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002018 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002019 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002020 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002021 float finalDeltaX;
2022 float finalDeltaY;
2023 float initDeltaX;
2024 float initDeltaY;
2025 float finalScale;
2026 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002027 int mode;
2028 boolean repeating = false;
2029 private static final int PREVIEW_DURATION = 300;
2030 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2031
2032 public static final int MODE_HINT = 0;
2033 public static final int MODE_PREVIEW = 1;
2034
Adam Cohene7587d22012-05-24 18:50:02 -07002035 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002036
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002037 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2038 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002039 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2040 final int x0 = mTmpPoint[0];
2041 final int y0 = mTmpPoint[1];
2042 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2043 final int x1 = mTmpPoint[0];
2044 final int y1 = mTmpPoint[1];
2045 final int dX = x1 - x0;
2046 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002047 finalDeltaX = 0;
2048 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002049 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002050 if (dX == dY && dX == 0) {
2051 } else {
2052 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002053 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002054 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002055 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002056 } else {
2057 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002058 finalDeltaX = (int) (- dir * Math.signum(dX) *
2059 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2060 finalDeltaY = (int) (- dir * Math.signum(dY) *
2061 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002062 }
2063 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002064 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002065 initDeltaX = child.getTranslationX();
2066 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002067 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002068 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002069 this.child = child;
2070 }
2071
Adam Cohend024f982012-05-23 18:26:45 -07002072 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002073 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002074 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002075 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002076 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002077 if (finalDeltaX == 0 && finalDeltaY == 0) {
2078 completeAnimationImmediately();
2079 return;
2080 }
Adam Cohen19f37922012-03-21 11:59:11 -07002081 }
Adam Cohend024f982012-05-23 18:26:45 -07002082 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002083 return;
2084 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002085 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002086 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002087
2088 // Animations are disabled in power save mode, causing the repeated animation to jump
2089 // spastically between beginning and end states. Since this looks bad, we don't repeat
2090 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002091 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002092 va.setRepeatMode(ValueAnimator.REVERSE);
2093 va.setRepeatCount(ValueAnimator.INFINITE);
2094 }
2095
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002096 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002097 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002098 va.addUpdateListener(new AnimatorUpdateListener() {
2099 @Override
2100 public void onAnimationUpdate(ValueAnimator animation) {
2101 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002102 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2103 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2104 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002105 child.setTranslationX(x);
2106 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002107 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002108 child.setScaleX(s);
2109 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002110 }
2111 });
2112 va.addListener(new AnimatorListenerAdapter() {
2113 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002114 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002115 initDeltaX = 0;
2116 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002117 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002118 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002119 }
2120 });
Adam Cohen19f37922012-03-21 11:59:11 -07002121 mShakeAnimators.put(child, this);
2122 va.start();
2123 }
2124
Adam Cohend024f982012-05-23 18:26:45 -07002125 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002126 if (a != null) {
2127 a.cancel();
2128 }
Adam Cohen19f37922012-03-21 11:59:11 -07002129 }
Adam Cohene7587d22012-05-24 18:50:02 -07002130
Adam Cohen091440a2015-03-18 14:16:05 -07002131 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002132 if (a != null) {
2133 a.cancel();
2134 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002135
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002136 a = new LauncherViewPropertyAnimator(child)
2137 .scaleX(getChildrenScale())
2138 .scaleY(getChildrenScale())
2139 .translationX(0)
2140 .translationY(0)
2141 .setDuration(REORDER_ANIMATION_DURATION);
2142 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2143 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002144 }
Adam Cohen19f37922012-03-21 11:59:11 -07002145 }
2146
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002147 private void completeAndClearReorderPreviewAnimations() {
2148 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002149 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002150 }
2151 mShakeAnimators.clear();
2152 }
2153
Adam Cohen482ed822012-03-02 14:15:13 -08002154 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002155 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002156
2157 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2158 int container = Favorites.CONTAINER_DESKTOP;
2159
2160 if (mLauncher.isHotseatLayout(this)) {
2161 screenId = -1;
2162 container = Favorites.CONTAINER_HOTSEAT;
2163 }
2164
Michael Jurkaa52570f2012-03-20 03:18:20 -07002165 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002166 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002167 View child = mShortcutsAndWidgets.getChildAt(i);
2168 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2169 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002170 // We do a null check here because the item info can be null in the case of the
2171 // AllApps button in the hotseat.
2172 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002173 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2174 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2175 || info.spanY != lp.cellVSpan);
2176
Adam Cohen2acce882012-03-28 19:03:19 -07002177 info.cellX = lp.cellX = lp.tmpCellX;
2178 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002179 info.spanX = lp.cellHSpan;
2180 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002181
2182 if (requiresDbUpdate) {
2183 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId,
2184 info.cellX, info.cellY, info.spanX, info.spanY);
2185 }
Adam Cohen2acce882012-03-28 19:03:19 -07002186 }
Adam Cohen482ed822012-03-02 14:15:13 -08002187 }
2188 }
2189
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002190 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002191 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002192 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002193 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002194 lp.useTmpCoords = useTempCoords;
2195 }
2196 }
2197
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002198 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002199 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2200 int[] result = new int[2];
2201 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002202 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002203 resultSpan);
2204 if (result[0] >= 0 && result[1] >= 0) {
2205 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002206 solution.cellX = result[0];
2207 solution.cellY = result[1];
2208 solution.spanX = resultSpan[0];
2209 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002210 solution.isSolution = true;
2211 } else {
2212 solution.isSolution = false;
2213 }
2214 return solution;
2215 }
2216
2217 public void prepareChildForDrag(View child) {
2218 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002219 }
2220
Adam Cohen19f37922012-03-21 11:59:11 -07002221 /* This seems like it should be obvious and straight-forward, but when the direction vector
2222 needs to match with the notion of the dragView pushing other views, we have to employ
2223 a slightly more subtle notion of the direction vector. The question is what two points is
2224 the vector between? The center of the dragView and its desired destination? Not quite, as
2225 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2226 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2227 or right, which helps make pushing feel right.
2228 */
2229 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2230 int spanY, View dragView, int[] resultDirection) {
2231 int[] targetDestination = new int[2];
2232
2233 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2234 Rect dragRect = new Rect();
2235 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2236 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2237
2238 Rect dropRegionRect = new Rect();
2239 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2240 dragView, dropRegionRect, mIntersectingViews);
2241
2242 int dropRegionSpanX = dropRegionRect.width();
2243 int dropRegionSpanY = dropRegionRect.height();
2244
2245 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2246 dropRegionRect.height(), dropRegionRect);
2247
2248 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2249 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2250
2251 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2252 deltaX = 0;
2253 }
2254 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2255 deltaY = 0;
2256 }
2257
2258 if (deltaX == 0 && deltaY == 0) {
2259 // No idea what to do, give a random direction.
2260 resultDirection[0] = 1;
2261 resultDirection[1] = 0;
2262 } else {
2263 computeDirectionVector(deltaX, deltaY, resultDirection);
2264 }
2265 }
2266
2267 // For a given cell and span, fetch the set of views intersecting the region.
2268 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2269 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2270 if (boundingRect != null) {
2271 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2272 }
2273 intersectingViews.clear();
2274 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2275 Rect r1 = new Rect();
2276 final int count = mShortcutsAndWidgets.getChildCount();
2277 for (int i = 0; i < count; i++) {
2278 View child = mShortcutsAndWidgets.getChildAt(i);
2279 if (child == dragView) continue;
2280 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2281 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2282 if (Rect.intersects(r0, r1)) {
2283 mIntersectingViews.add(child);
2284 if (boundingRect != null) {
2285 boundingRect.union(r1);
2286 }
2287 }
2288 }
2289 }
2290
2291 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2292 View dragView, int[] result) {
2293 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2294 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2295 mIntersectingViews);
2296 return !mIntersectingViews.isEmpty();
2297 }
2298
2299 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002300 completeAndClearReorderPreviewAnimations();
2301 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2302 final int count = mShortcutsAndWidgets.getChildCount();
2303 for (int i = 0; i < count; i++) {
2304 View child = mShortcutsAndWidgets.getChildAt(i);
2305 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2306 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2307 lp.tmpCellX = lp.cellX;
2308 lp.tmpCellY = lp.cellY;
2309 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2310 0, false, false);
2311 }
Adam Cohen19f37922012-03-21 11:59:11 -07002312 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002313 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002314 }
Adam Cohen19f37922012-03-21 11:59:11 -07002315 }
2316
Adam Cohenbebf0422012-04-11 18:06:28 -07002317 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2318 View dragView, int[] direction, boolean commit) {
2319 int[] pixelXY = new int[2];
2320 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2321
2322 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002323 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002324 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2325
2326 setUseTempCoords(true);
2327 if (swapSolution != null && swapSolution.isSolution) {
2328 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2329 // committing anything or animating anything as we just want to determine if a solution
2330 // exists
2331 copySolutionToTempState(swapSolution, dragView);
2332 setItemPlacementDirty(true);
2333 animateItemsToSolution(swapSolution, dragView, commit);
2334
2335 if (commit) {
2336 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002337 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002338 setItemPlacementDirty(false);
2339 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002340 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2341 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002342 }
2343 mShortcutsAndWidgets.requestLayout();
2344 }
2345 return swapSolution.isSolution;
2346 }
2347
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002348 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002349 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002350 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002351 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002352
2353 if (resultSpan == null) {
2354 resultSpan = new int[2];
2355 }
2356
Adam Cohen19f37922012-03-21 11:59:11 -07002357 // When we are checking drop validity or actually dropping, we don't recompute the
2358 // direction vector, since we want the solution to match the preview, and it's possible
2359 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002360 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2361 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002362 mDirectionVector[0] = mPreviousReorderDirection[0];
2363 mDirectionVector[1] = mPreviousReorderDirection[1];
2364 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002365 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2366 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2367 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002368 }
2369 } else {
2370 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2371 mPreviousReorderDirection[0] = mDirectionVector[0];
2372 mPreviousReorderDirection[1] = mDirectionVector[1];
2373 }
2374
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002375 // Find a solution involving pushing / displacing any items in the way
2376 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002377 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2378
2379 // We attempt the approach which doesn't shuffle views at all
2380 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2381 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2382
2383 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002384
2385 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2386 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002387 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2388 finalSolution = swapSolution;
2389 } else if (noShuffleSolution.isSolution) {
2390 finalSolution = noShuffleSolution;
2391 }
2392
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002393 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002394 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002395 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2396 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002397 result[0] = finalSolution.cellX;
2398 result[1] = finalSolution.cellY;
2399 resultSpan[0] = finalSolution.spanX;
2400 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002401 } else {
2402 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2403 }
2404 return result;
2405 }
2406
Adam Cohen482ed822012-03-02 14:15:13 -08002407 boolean foundSolution = true;
2408 if (!DESTRUCTIVE_REORDER) {
2409 setUseTempCoords(true);
2410 }
2411
2412 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002413 result[0] = finalSolution.cellX;
2414 result[1] = finalSolution.cellY;
2415 resultSpan[0] = finalSolution.spanX;
2416 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002417
2418 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2419 // committing anything or animating anything as we just want to determine if a solution
2420 // exists
2421 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2422 if (!DESTRUCTIVE_REORDER) {
2423 copySolutionToTempState(finalSolution, dragView);
2424 }
2425 setItemPlacementDirty(true);
2426 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2427
Adam Cohen19f37922012-03-21 11:59:11 -07002428 if (!DESTRUCTIVE_REORDER &&
2429 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002430 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002431 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002432 setItemPlacementDirty(false);
2433 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002434 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2435 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002436 }
2437 }
2438 } else {
2439 foundSolution = false;
2440 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2441 }
2442
2443 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2444 setUseTempCoords(false);
2445 }
Adam Cohen482ed822012-03-02 14:15:13 -08002446
Michael Jurkaa52570f2012-03-20 03:18:20 -07002447 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002448 return result;
2449 }
2450
Adam Cohen19f37922012-03-21 11:59:11 -07002451 void setItemPlacementDirty(boolean dirty) {
2452 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002453 }
Adam Cohen19f37922012-03-21 11:59:11 -07002454 boolean isItemPlacementDirty() {
2455 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002456 }
2457
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002458 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002459 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002460 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2461 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002462 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002463 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002464
Adam Cohenf3900c22012-11-16 18:28:11 -08002465 void save() {
2466 // Copy current state into savedMap
2467 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002468 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002469 }
2470 }
2471
2472 void restore() {
2473 // Restore current state from savedMap
2474 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002475 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002476 }
2477 }
2478
2479 void add(View v, CellAndSpan cs) {
2480 map.put(v, cs);
2481 savedMap.put(v, new CellAndSpan());
2482 sortedViews.add(v);
2483 }
2484
Adam Cohen482ed822012-03-02 14:15:13 -08002485 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002486 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002487 }
2488
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002489 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2490 boolean first = true;
2491 for (View v: views) {
2492 CellAndSpan c = map.get(v);
2493 if (first) {
2494 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2495 first = false;
2496 } else {
2497 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2498 }
2499 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002500 }
Adam Cohen482ed822012-03-02 14:15:13 -08002501 }
2502
Adam Cohendf035382011-04-11 17:22:04 -07002503 /**
Adam Cohendf035382011-04-11 17:22:04 -07002504 * Find a starting cell position that will fit the given bounds nearest the requested
2505 * cell location. Uses Euclidean distance to score multiple vacant areas.
2506 *
2507 * @param pixelX The X location at which you want to search for a vacant area.
2508 * @param pixelY The Y location at which you want to search for a vacant area.
2509 * @param spanX Horizontal span of the object.
2510 * @param spanY Vertical span of the object.
2511 * @param ignoreView Considers space occupied by this view as unoccupied
2512 * @param result Previously returned value to possibly recycle.
2513 * @return The X, Y cell of a vacant area that can contain this object,
2514 * nearest the requested location.
2515 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002516 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002517 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002518 }
2519
Michael Jurka0280c3b2010-09-17 15:00:07 -07002520 boolean existsEmptyCell() {
2521 return findCellForSpan(null, 1, 1);
2522 }
2523
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002524 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002525 * Finds the upper-left coordinate of the first rectangle in the grid that can
2526 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2527 * then this method will only return coordinates for rectangles that contain the cell
2528 * (intersectX, intersectY)
2529 *
2530 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2531 * can be found.
2532 * @param spanX The horizontal span of the cell we want to find.
2533 * @param spanY The vertical span of the cell we want to find.
2534 *
2535 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002536 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002537 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002538 if (cellXY == null) {
2539 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002540 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002541 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002542 }
2543
2544 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002545 * A drag event has begun over this layout.
2546 * It may have begun over this layout (in which case onDragChild is called first),
2547 * or it may have begun on another layout.
2548 */
2549 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002550 mDragging = true;
2551 }
2552
2553 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002554 * Called when drag has left this CellLayout or has been completed (successfully or not)
2555 */
2556 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002557 // This can actually be called when we aren't in a drag, e.g. when adding a new
2558 // item to this layout via the customize drawer.
2559 // Guard against that case.
2560 if (mDragging) {
2561 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002562 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002563
2564 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002565 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002566 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2567 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002568 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002569 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002570 }
2571
2572 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002573 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002574 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002575 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 *
2577 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002578 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002579 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002580 if (child != null) {
2581 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002582 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002583 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002584 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002585 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002586 }
2587
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002588 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002590 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591 * @param cellX X coordinate of upper left corner expressed as a cell position
2592 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002593 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002594 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002595 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002596 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002597 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002598 final int cellWidth = mCellWidth;
2599 final int cellHeight = mCellHeight;
2600 final int widthGap = mWidthGap;
2601 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002602
Winson Chung4b825dcd2011-06-19 12:41:22 -07002603 final int hStartPadding = getPaddingLeft();
2604 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002605
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002606 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2607 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2608
2609 int x = hStartPadding + cellX * (cellWidth + widthGap);
2610 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002611
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002612 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002613 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002614
Adam Cohend4844c32011-02-18 19:25:06 -08002615 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002616 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002617 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002618 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002619 }
2620
Adam Cohend4844c32011-02-18 19:25:06 -08002621 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002622 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002623 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002624 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002625 }
2626
Adam Cohen2801caf2011-05-13 20:57:39 -07002627 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002628 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002629 (Math.max((mCountX - 1), 0) * mWidthGap);
2630 }
2631
2632 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002633 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002634 (Math.max((mCountY - 1), 0) * mHeightGap);
2635 }
2636
Michael Jurka66d72172011-04-12 16:29:25 -07002637 public boolean isOccupied(int x, int y) {
2638 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002639 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002640 } else {
2641 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2642 }
2643 }
2644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002645 @Override
2646 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2647 return new CellLayout.LayoutParams(getContext(), attrs);
2648 }
2649
2650 @Override
2651 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2652 return p instanceof CellLayout.LayoutParams;
2653 }
2654
2655 @Override
2656 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2657 return new CellLayout.LayoutParams(p);
2658 }
2659
2660 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2661 /**
2662 * Horizontal location of the item in the grid.
2663 */
2664 @ViewDebug.ExportedProperty
2665 public int cellX;
2666
2667 /**
2668 * Vertical location of the item in the grid.
2669 */
2670 @ViewDebug.ExportedProperty
2671 public int cellY;
2672
2673 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002674 * Temporary horizontal location of the item in the grid during reorder
2675 */
2676 public int tmpCellX;
2677
2678 /**
2679 * Temporary vertical location of the item in the grid during reorder
2680 */
2681 public int tmpCellY;
2682
2683 /**
2684 * Indicates that the temporary coordinates should be used to layout the items
2685 */
2686 public boolean useTmpCoords;
2687
2688 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002689 * Number of cells spanned horizontally by the item.
2690 */
2691 @ViewDebug.ExportedProperty
2692 public int cellHSpan;
2693
2694 /**
2695 * Number of cells spanned vertically by the item.
2696 */
2697 @ViewDebug.ExportedProperty
2698 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002699
Adam Cohen1b607ed2011-03-03 17:26:50 -08002700 /**
2701 * Indicates whether the item will set its x, y, width and height parameters freely,
2702 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2703 */
Adam Cohend4844c32011-02-18 19:25:06 -08002704 public boolean isLockedToGrid = true;
2705
Adam Cohen482ed822012-03-02 14:15:13 -08002706 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002707 * Indicates that this item should use the full extents of its parent.
2708 */
2709 public boolean isFullscreen = false;
2710
2711 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002712 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002713 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002714 */
2715 public boolean canReorder = true;
2716
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002717 // X coordinate of the view in the layout.
2718 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002719 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720 // Y coordinate of the view in the layout.
2721 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002722 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002723
Romain Guy84f296c2009-11-04 15:00:44 -08002724 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002725
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002726 public LayoutParams(Context c, AttributeSet attrs) {
2727 super(c, attrs);
2728 cellHSpan = 1;
2729 cellVSpan = 1;
2730 }
2731
2732 public LayoutParams(ViewGroup.LayoutParams source) {
2733 super(source);
2734 cellHSpan = 1;
2735 cellVSpan = 1;
2736 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002737
2738 public LayoutParams(LayoutParams source) {
2739 super(source);
2740 this.cellX = source.cellX;
2741 this.cellY = source.cellY;
2742 this.cellHSpan = source.cellHSpan;
2743 this.cellVSpan = source.cellVSpan;
2744 }
2745
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002746 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002747 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002748 this.cellX = cellX;
2749 this.cellY = cellY;
2750 this.cellHSpan = cellHSpan;
2751 this.cellVSpan = cellVSpan;
2752 }
2753
Adam Cohen2374abf2013-04-16 14:56:57 -07002754 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2755 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002756 if (isLockedToGrid) {
2757 final int myCellHSpan = cellHSpan;
2758 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002759 int myCellX = useTmpCoords ? tmpCellX : cellX;
2760 int myCellY = useTmpCoords ? tmpCellY : cellY;
2761
2762 if (invertHorizontally) {
2763 myCellX = colCount - myCellX - cellHSpan;
2764 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002765
Adam Cohend4844c32011-02-18 19:25:06 -08002766 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2767 leftMargin - rightMargin;
2768 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2769 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002770 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2771 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002772 }
2773 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002774
Winson Chungaafa03c2010-06-11 17:34:16 -07002775 public String toString() {
2776 return "(" + this.cellX + ", " + this.cellY + ")";
2777 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002778
2779 public void setWidth(int width) {
2780 this.width = width;
2781 }
2782
2783 public int getWidth() {
2784 return width;
2785 }
2786
2787 public void setHeight(int height) {
2788 this.height = height;
2789 }
2790
2791 public int getHeight() {
2792 return height;
2793 }
2794
2795 public void setX(int x) {
2796 this.x = x;
2797 }
2798
2799 public int getX() {
2800 return x;
2801 }
2802
2803 public void setY(int y) {
2804 this.y = y;
2805 }
2806
2807 public int getY() {
2808 return y;
2809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002810 }
2811
Michael Jurka0280c3b2010-09-17 15:00:07 -07002812 // This class stores info for two purposes:
2813 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2814 // its spanX, spanY, and the screen it is on
2815 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2816 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2817 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002818 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002819 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002820 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002821 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002822
Sunny Goyal83a8f042015-05-19 12:52:12 -07002823 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002824 cellX = info.cellX;
2825 cellY = info.cellY;
2826 spanX = info.spanX;
2827 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002828 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002829 screenId = info.screenId;
2830 container = info.container;
2831 }
2832
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002833 @Override
2834 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002835 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2836 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002837 }
2838 }
Michael Jurkad771c962011-08-09 15:00:48 -07002839
Tony Wickham86930612015-09-09 13:50:40 -07002840 /**
2841 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2842 * if necessary).
2843 */
2844 public boolean hasReorderSolution(ItemInfo itemInfo) {
2845 int[] cellPoint = new int[2];
2846 // Check for a solution starting at every cell.
2847 for (int cellX = 0; cellX < getCountX(); cellX++) {
2848 for (int cellY = 0; cellY < getCountY(); cellY++) {
2849 cellToPoint(cellX, cellY, cellPoint);
2850 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2851 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2852 true, new ItemConfiguration()).isSolution) {
2853 return true;
2854 }
2855 }
2856 }
2857 return false;
2858 }
2859
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002860 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002861 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002862 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002863}