blob: 979c9508bf590780301ca7e96adf6cf63ecb8dc9 [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);
Tony2fd02082016-10-07 12:50:01 -0700199 mLauncher = Launcher.getLauncher(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 }
Sunny Goyalf28e6af2016-08-31 16:02:40 -0700239 mDragOutlinePaint.setColor(getResources().getColor(R.color.outline_color));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700240
241 // When dragging things around the home screens, we show a green outline of
242 // where the item will land. The outlines gradually fade out, leaving a trail
243 // behind the drag path.
244 // Set up all the animations that are used to implement this fading.
245 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700246 final float fromAlphaValue = 0;
247 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700248
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700249 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700250
251 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700252 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100253 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700254 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700255 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700256 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700257 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700258 final Bitmap outline = (Bitmap)anim.getTag();
259
260 // If an animation is started and then stopped very quickly, we can still
261 // get spurious updates we've cleared the tag. Guard against this.
262 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700263 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700264 Object val = animation.getAnimatedValue();
265 Log.d(TAG, "anim " + thisIndex + " update: " + val +
266 ", isStopped " + anim.isStopped());
267 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 // Try to prevent it from continuing to run
269 animation.cancel();
270 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700271 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800272 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700274 }
275 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700276 // The animation holds a reference to the drag outline bitmap as long is it's
277 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700278 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700279 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700280 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700281 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700282 anim.setTag(null);
283 }
284 }
285 });
286 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700287 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700288
Michael Jurkaa52570f2012-03-20 03:18:20 -0700289 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700290 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700291 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700292
Mady Mellorbb835202015-07-15 16:34:34 -0700293 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700294
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700295 mTouchFeedbackView = new ClickShadowView(context);
296 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700297 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700298 }
299
Adam Cohenc9735cf2015-01-23 16:11:55 -0800300 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700301 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800302 mUseTouchHelper = enable;
303 if (!enable) {
304 ViewCompat.setAccessibilityDelegate(this, null);
305 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
306 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
307 setOnClickListener(mLauncher);
308 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700309 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
310 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
311 mTouchHelper = new WorkspaceAccessibilityHelper(this);
312 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
313 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
314 mTouchHelper = new FolderAccessibilityHelper(this);
315 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800316 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
317 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
318 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
319 setOnClickListener(mTouchHelper);
320 }
321
322 // Invalidate the accessibility hierarchy
323 if (getParent() != null) {
324 getParent().notifySubtreeAccessibilityStateChanged(
325 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
326 }
327 }
328
329 @Override
330 public boolean dispatchHoverEvent(MotionEvent event) {
331 // Always attempt to dispatch hover events to accessibility first.
332 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
333 return true;
334 }
335 return super.dispatchHoverEvent(event);
336 }
337
338 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800339 public boolean onInterceptTouchEvent(MotionEvent ev) {
340 if (mUseTouchHelper ||
341 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
342 return true;
343 }
344 return false;
345 }
346
Mady Melloref044dd2015-06-02 15:35:07 -0700347 @Override
348 public boolean onTouchEvent(MotionEvent ev) {
349 boolean handled = super.onTouchEvent(ev);
350 // Stylus button press on a home screen should not switch between overview mode and
351 // the home screen mode, however, once in overview mode stylus button press should be
352 // enabled to allow rearranging the different home screens. So check what mode
353 // the workspace is in, and only perform stylus button presses while in overview mode.
354 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700355 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700356 return true;
357 }
358 return handled;
359 }
360
Chris Craik01f2d7f2013-10-01 14:41:56 -0700361 public void enableHardwareLayer(boolean hasLayer) {
362 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700363 }
364
365 public void buildHardwareLayer() {
366 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700367 }
368
Adam Cohen307fe232012-08-16 17:55:58 -0700369 public float getChildrenScale() {
370 return mIsHotseat ? mHotseatScale : 1.0f;
371 }
372
Winson Chung5f8afe62013-08-12 16:19:28 -0700373 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700374 mFixedCellWidth = mCellWidth = width;
375 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700376 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
377 mCountX, mCountY);
378 }
379
Adam Cohen2801caf2011-05-13 20:57:39 -0700380 public void setGridSize(int x, int y) {
381 mCountX = x;
382 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700383 mOccupied = new GridOccupancy(mCountX, mCountY);
384 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700385 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700386 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700387 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700388 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700389 }
390
Adam Cohen2374abf2013-04-16 14:56:57 -0700391 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
392 public void setInvertIfRtl(boolean invert) {
393 mShortcutsAndWidgets.setInvertIfRtl(invert);
394 }
395
Adam Cohen917e3882013-10-31 15:03:35 -0700396 public void setDropPending(boolean pending) {
397 mDropPending = pending;
398 }
399
400 public boolean isDropPending() {
401 return mDropPending;
402 }
403
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700404 @Override
405 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700406 if (icon == null || background == null) {
407 mTouchFeedbackView.setBitmap(null);
408 mTouchFeedbackView.animate().cancel();
409 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700410 if (mTouchFeedbackView.setBitmap(background)) {
Winsonbe9798b2016-07-20 12:55:49 -0700411 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
412 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700413 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700414 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800415 }
416 }
417
Adam Cohenc50438c2014-08-19 17:43:05 -0700418 void disableDragTarget() {
419 mIsDragTarget = false;
420 }
421
Tony Wickham0f97b782015-12-02 17:55:07 -0800422 public boolean isDragTarget() {
423 return mIsDragTarget;
424 }
425
Adam Cohenc50438c2014-08-19 17:43:05 -0700426 void setIsDragOverlapping(boolean isDragOverlapping) {
427 if (mIsDragOverlapping != isDragOverlapping) {
428 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700429 if (mIsDragOverlapping) {
430 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
431 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700432 if (mBackgroundAlpha > 0f) {
433 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
434 } else {
435 mBackground.resetTransition();
436 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700437 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700438 invalidate();
439 }
440 }
441
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700442 public void disableJailContent() {
443 mJailContent = false;
444 }
445
446 @Override
447 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
448 if (mJailContent) {
449 ParcelableSparseArray jail = getJailedArray(container);
450 super.dispatchSaveInstanceState(jail);
451 container.put(R.id.cell_layout_jail_id, jail);
452 } else {
453 super.dispatchSaveInstanceState(container);
454 }
455 }
456
457 @Override
458 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
459 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
460 }
461
462 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
463 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
464 return parcelable instanceof ParcelableSparseArray ?
465 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
466 }
467
Tony Wickham0f97b782015-12-02 17:55:07 -0800468 public boolean getIsDragOverlapping() {
469 return mIsDragOverlapping;
470 }
471
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700472 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700473 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700474 if (!mIsDragTarget) {
475 return;
476 }
477
Michael Jurka3e7c7632010-10-02 16:01:03 -0700478 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
479 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
480 // When we're small, we are either drawn normally or in the "accepts drops" state (during
481 // a drag). However, we also drag the mini hover background *over* one of those two
482 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700483 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700484 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700485 }
Romain Guya6abce82009-11-10 02:54:41 -0800486
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700487 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700488 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700489 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700490 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700491 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700492 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700493 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700494 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700495 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800496
Adam Cohen482ed822012-03-02 14:15:13 -0800497 if (DEBUG_VISUALIZE_OCCUPIED) {
498 int[] pt = new int[2];
499 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700500 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800501 for (int i = 0; i < mCountX; i++) {
502 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700503 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800504 cellToPoint(i, j, pt);
505 canvas.save();
506 canvas.translate(pt[0], pt[1]);
507 cd.draw(canvas);
508 canvas.restore();
509 }
510 }
511 }
512 }
513
Adam Cohenefca0272016-02-24 19:19:06 -0800514 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
515 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
516 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
517 canvas.save();
518 canvas.translate(mTempLocation[0], mTempLocation[1]);
519 bg.drawBackground(canvas, mFolderBgPaint);
Adam Cohenf172b742016-03-30 19:28:34 -0700520 if (!bg.isClipping) {
521 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
522 }
Adam Cohenefca0272016-02-24 19:19:06 -0800523 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700524 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700525
Adam Cohenefca0272016-02-24 19:19:06 -0800526 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
527 cellToPoint(mFolderLeaveBehind.delegateCellX,
528 mFolderLeaveBehind.delegateCellY, mTempLocation);
529 canvas.save();
530 canvas.translate(mTempLocation[0], mTempLocation[1]);
531 mFolderLeaveBehind.drawLeaveBehind(canvas, mFolderBgPaint);
532 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700533 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700534 }
535
Adam Cohenefca0272016-02-24 19:19:06 -0800536 @Override
537 protected void dispatchDraw(Canvas canvas) {
538 super.dispatchDraw(canvas);
539
540 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
541 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700542 if (bg.isClipping) {
543 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
544 canvas.save();
545 canvas.translate(mTempLocation[0], mTempLocation[1]);
546 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
547 canvas.restore();
548 }
Adam Cohenefca0272016-02-24 19:19:06 -0800549 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700550 }
551
Adam Cohenefca0272016-02-24 19:19:06 -0800552 public void addFolderBackground(FolderIcon.PreviewBackground bg) {
553 mFolderBackgrounds.add(bg);
554 }
555 public void removeFolderBackground(FolderIcon.PreviewBackground bg) {
556 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700557 }
558
Adam Cohenc51934b2011-07-26 21:07:43 -0700559 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800560
561 DeviceProfile grid = mLauncher.getDeviceProfile();
562 View child = getChildAt(x, y);
563
564 mFolderLeaveBehind.setup(getResources().getDisplayMetrics(), grid, null,
565 child.getMeasuredWidth(), child.getPaddingTop());
566
567 mFolderLeaveBehind.delegateCellX = x;
568 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700569 invalidate();
570 }
571
572 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800573 mFolderLeaveBehind.delegateCellX = -1;
574 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700575 invalidate();
576 }
577
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700578 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700579 public boolean shouldDelayChildPressedState() {
580 return false;
581 }
582
Adam Cohen1462de32012-07-24 22:34:36 -0700583 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700584 try {
585 dispatchRestoreInstanceState(states);
586 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700587 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700588 throw ex;
589 }
590 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
591 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
592 }
Adam Cohen1462de32012-07-24 22:34:36 -0700593 }
594
Michael Jurkae6235dd2011-10-04 15:02:05 -0700595 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700596 public void cancelLongPress() {
597 super.cancelLongPress();
598
599 // Cancel long press for all children
600 final int count = getChildCount();
601 for (int i = 0; i < count; i++) {
602 final View child = getChildAt(i);
603 child.cancelLongPress();
604 }
605 }
606
Michael Jurkadee05892010-07-27 10:01:56 -0700607 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
608 mInterceptTouchListener = listener;
609 }
610
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800611 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700612 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 }
614
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800615 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700616 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 }
618
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800619 public void setIsHotseat(boolean isHotseat) {
620 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700621 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800622 }
623
Sunny Goyale9b651e2015-04-24 11:44:51 -0700624 public boolean isHotseat() {
625 return mIsHotseat;
626 }
627
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800628 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700629 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700630 final LayoutParams lp = params;
631
Andrew Flynnde38e422012-05-08 11:22:15 -0700632 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800633 if (child instanceof BubbleTextView) {
634 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700635 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800636 }
637
Adam Cohen307fe232012-08-16 17:55:58 -0700638 child.setScaleX(getChildrenScale());
639 child.setScaleY(getChildrenScale());
640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 // Generate an id for each view, this assumes we have at most 256x256 cells
642 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700643 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700644 // If the horizontal or vertical span is set to -1, it is taken to
645 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700646 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
647 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648
Winson Chungaafa03c2010-06-11 17:34:16 -0700649 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700650 if (LOGD) {
651 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
652 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700653 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700654
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700655 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656
Winson Chungaafa03c2010-06-11 17:34:16 -0700657 return true;
658 }
659 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700663 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700664 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700665 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 }
667
668 @Override
669 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700670 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700671 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700672 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700673 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 }
675
676 @Override
677 public void removeView(View view) {
678 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700679 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 }
681
682 @Override
683 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700684 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
685 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700686 }
687
688 @Override
689 public void removeViewInLayout(View view) {
690 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700691 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700692 }
693
694 @Override
695 public void removeViews(int start, int count) {
696 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700697 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700698 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700699 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700700 }
701
702 @Override
703 public void removeViewsInLayout(int start, int count) {
704 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700705 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700706 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700707 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800708 }
709
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700710 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700711 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 * @param x X coordinate of the point
713 * @param y Y coordinate of the point
714 * @param result Array of 2 ints to hold the x and y coordinate of the cell
715 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700716 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700717 final int hStartPadding = getPaddingLeft();
718 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719
720 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
721 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
722
Adam Cohend22015c2010-07-26 22:02:18 -0700723 final int xAxis = mCountX;
724 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725
726 if (result[0] < 0) result[0] = 0;
727 if (result[0] >= xAxis) result[0] = xAxis - 1;
728 if (result[1] < 0) result[1] = 0;
729 if (result[1] >= yAxis) result[1] = yAxis - 1;
730 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700731
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 /**
733 * Given a point, return the cell that most closely encloses that point
734 * @param x X coordinate of the point
735 * @param y Y coordinate of the point
736 * @param result Array of 2 ints to hold the x and y coordinate of the cell
737 */
738 void pointToCellRounded(int x, int y, int[] result) {
739 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
740 }
741
742 /**
743 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700744 *
745 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700747 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 * @param result Array of 2 ints to hold the x and y coordinate of the point
749 */
750 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700751 final int hStartPadding = getPaddingLeft();
752 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753
754 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
755 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
756 }
757
Adam Cohene3e27a82011-04-15 12:07:39 -0700758 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800759 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700760 *
761 * @param cellX X coordinate of the cell
762 * @param cellY Y coordinate of the cell
763 *
764 * @param result Array of 2 ints to hold the x and y coordinate of the point
765 */
766 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700767 regionToCenterPoint(cellX, cellY, 1, 1, result);
768 }
769
770 /**
771 * Given a cell coordinate and span return the point that represents the center of the regio
772 *
773 * @param cellX X coordinate of the cell
774 * @param cellY Y coordinate of the cell
775 *
776 * @param result Array of 2 ints to hold the x and y coordinate of the point
777 */
778 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700779 final int hStartPadding = getPaddingLeft();
780 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700781 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
782 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
783 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
784 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700785 }
786
Adam Cohen19f37922012-03-21 11:59:11 -0700787 /**
788 * Given a cell coordinate and span fills out a corresponding pixel rect
789 *
790 * @param cellX X coordinate of the cell
791 * @param cellY Y coordinate of the cell
792 * @param result Rect in which to write the result
793 */
794 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
795 final int hStartPadding = getPaddingLeft();
796 final int vStartPadding = getPaddingTop();
797 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
798 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
799 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
800 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
801 }
802
Adam Cohen482ed822012-03-02 14:15:13 -0800803 public float getDistanceFromCell(float x, float y, int[] cell) {
804 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700805 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800806 }
807
Adam Cohenf9c184a2016-01-15 16:47:43 -0800808 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800809 return mCellWidth;
810 }
811
812 int getCellHeight() {
813 return mCellHeight;
814 }
815
Adam Cohend4844c32011-02-18 19:25:06 -0800816 int getWidthGap() {
817 return mWidthGap;
818 }
819
820 int getHeightGap() {
821 return mHeightGap;
822 }
823
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700824 public void setFixedSize(int width, int height) {
825 mFixedWidth = width;
826 mFixedHeight = height;
827 }
828
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 @Override
830 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700833 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
834 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700835 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
836 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700837 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700838 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
839 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700840 if (cw != mCellWidth || ch != mCellHeight) {
841 mCellWidth = cw;
842 mCellHeight = ch;
843 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
844 mHeightGap, mCountX, mCountY);
845 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700846 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700847
Winson Chung2d75f122013-09-23 16:53:31 -0700848 int newWidth = childWidthSize;
849 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700850 if (mFixedWidth > 0 && mFixedHeight > 0) {
851 newWidth = mFixedWidth;
852 newHeight = mFixedHeight;
853 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
855 }
856
Adam Cohend22015c2010-07-26 22:02:18 -0700857 int numWidthGaps = mCountX - 1;
858 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859
Adam Cohen234c4cd2011-07-17 21:03:04 -0700860 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700861 int hSpace = childWidthSize;
862 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700863 int hFreeSpace = hSpace - (mCountX * mCellWidth);
864 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700865 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
866 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700867 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
868 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700869 } else {
870 mWidthGap = mOriginalWidthGap;
871 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700872 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700873
874 // Make the feedback view large enough to hold the blur bitmap.
875 mTouchFeedbackView.measure(
876 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
877 MeasureSpec.EXACTLY),
878 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
879 MeasureSpec.EXACTLY));
880
881 mShortcutsAndWidgets.measure(
882 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
883 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
884
885 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
886 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700887 if (mFixedWidth > 0 && mFixedHeight > 0) {
888 setMeasuredDimension(maxWidth, maxHeight);
889 } else {
890 setMeasuredDimension(widthSize, heightSize);
891 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 }
893
894 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700895 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800896 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
897 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
898 int left = getPaddingLeft();
899 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800900 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800901 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700902 int right = r - l - getPaddingRight();
903 if (!isFullscreen) {
904 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
905 }
906
Winson Chung38848ca2013-10-08 12:03:44 -0700907 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700908 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700909
910 mTouchFeedbackView.layout(left, top,
911 left + mTouchFeedbackView.getMeasuredWidth(),
912 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700913 mShortcutsAndWidgets.layout(left, top, right, bottom);
914
915 // Expand the background drawing bounds by the padding baked into the background drawable
916 mBackground.getPadding(mTempRect);
917 mBackground.setBounds(
918 left - mTempRect.left,
919 top - mTempRect.top,
920 right + mTempRect.right,
921 bottom + mTempRect.bottom);
922 }
923
Tony Wickhama501d492015-11-03 18:05:01 -0800924 /**
925 * Returns the amount of space left over after subtracting padding and cells. This space will be
926 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
927 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
928 */
929 public int getUnusedHorizontalSpace() {
930 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
931 }
932
Michael Jurka5f1c5092010-09-03 14:15:02 -0700933 public float getBackgroundAlpha() {
934 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700935 }
936
Michael Jurka5f1c5092010-09-03 14:15:02 -0700937 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800938 if (mBackgroundAlpha != alpha) {
939 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700940 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800941 }
Michael Jurkadee05892010-07-27 10:01:56 -0700942 }
943
Sunny Goyal2805e632015-05-20 15:35:32 -0700944 @Override
945 protected boolean verifyDrawable(Drawable who) {
946 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
947 }
948
Michael Jurkaa52570f2012-03-20 03:18:20 -0700949 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700950 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700951 }
952
Michael Jurkaa52570f2012-03-20 03:18:20 -0700953 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700954 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700955 }
956
Patrick Dubroy440c3602010-07-13 17:50:32 -0700957 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700958 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700959 }
960
Adam Cohen76fc0852011-06-17 13:26:23 -0700961 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800962 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700963 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800964
Adam Cohen19f37922012-03-21 11:59:11 -0700965 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700966 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
967 final ItemInfo info = (ItemInfo) child.getTag();
968
969 // We cancel any existing animations
970 if (mReorderAnimators.containsKey(lp)) {
971 mReorderAnimators.get(lp).cancel();
972 mReorderAnimators.remove(lp);
973 }
974
Adam Cohen482ed822012-03-02 14:15:13 -0800975 final int oldX = lp.x;
976 final int oldY = lp.y;
977 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700978 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
979 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
980 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800981 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700982 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800983 if (permanent) {
984 lp.cellX = info.cellX = cellX;
985 lp.cellY = info.cellY = cellY;
986 } else {
987 lp.tmpCellX = cellX;
988 lp.tmpCellY = cellY;
989 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700990 clc.setupLp(lp);
991 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800992 final int newX = lp.x;
993 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700994
Adam Cohen76fc0852011-06-17 13:26:23 -0700995 lp.x = oldX;
996 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700997
Adam Cohen482ed822012-03-02 14:15:13 -0800998 // Exit early if we're not actually moving the view
999 if (oldX == newX && oldY == newY) {
1000 lp.isLockedToGrid = true;
1001 return true;
1002 }
1003
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001004 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001005 va.setDuration(duration);
1006 mReorderAnimators.put(lp, va);
1007
1008 va.addUpdateListener(new AnimatorUpdateListener() {
1009 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001010 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001011 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001012 lp.x = (int) ((1 - r) * oldX + r * newX);
1013 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001014 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001015 }
1016 });
Adam Cohen482ed822012-03-02 14:15:13 -08001017 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001018 boolean cancelled = false;
1019 public void onAnimationEnd(Animator animation) {
1020 // If the animation was cancelled, it means that another animation
1021 // has interrupted this one, and we don't want to lock the item into
1022 // place just yet.
1023 if (!cancelled) {
1024 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001025 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001026 }
1027 if (mReorderAnimators.containsKey(lp)) {
1028 mReorderAnimators.remove(lp);
1029 }
1030 }
1031 public void onAnimationCancel(Animator animation) {
1032 cancelled = true;
1033 }
1034 });
Adam Cohen482ed822012-03-02 14:15:13 -08001035 va.setStartDelay(delay);
1036 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001037 return true;
1038 }
1039 return false;
1040 }
1041
Sunny Goyal06e21a22016-08-11 16:02:02 -07001042 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
1043 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001044 final int oldDragCellX = mDragCell[0];
1045 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001046
Hyunyoung Song0de01172016-10-05 16:27:48 -07001047 if (outlineProvider == null || outlineProvider.generatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001048 return;
1049 }
1050
Hyunyoung Song0de01172016-10-05 16:27:48 -07001051 Bitmap dragOutline = outlineProvider.generatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -08001052 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001053 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1054 Rect dragRegion = dragObject.dragView.getDragRegion();
1055
Adam Cohen482ed822012-03-02 14:15:13 -08001056 mDragCell[0] = cellX;
1057 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001058
Joe Onorato4be866d2010-10-10 11:26:02 -07001059 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001060 mDragOutlineAnims[oldIndex].animateOut();
1061 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001062 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001063
Adam Cohend41fbf52012-02-16 23:53:59 -08001064 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001065 cellToRect(cellX, cellY, spanX, spanY, r);
Sunny Goyal106bf642015-07-16 12:18:06 -07001066 } else {
1067 // Find the top left corner of the rect the object will occupy
1068 final int[] topLeft = mTmpPoint;
1069 cellToPoint(cellX, cellY, topLeft);
1070
1071 int left = topLeft[0];
1072 int top = topLeft[1];
1073
1074 if (v != null && dragOffset == null) {
1075 // When drawing the drag outline, it did not account for margin offsets
1076 // added by the view's parent.
1077 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1078 left += lp.leftMargin;
1079 top += lp.topMargin;
1080
1081 // Offsets due to the size difference between the View and the dragOutline.
1082 // There is a size difference to account for the outer blur, which may lie
1083 // outside the bounds of the view.
1084 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1085 // We center about the x axis
1086 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1087 - dragOutline.getWidth()) / 2;
1088 } else {
1089 if (dragOffset != null && dragRegion != null) {
1090 // Center the drag region *horizontally* in the cell and apply a drag
1091 // outline offset
1092 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1093 - dragRegion.width()) / 2;
1094 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1095 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1096 top += dragOffset.y + cellPaddingY;
1097 } else {
1098 // Center the drag outline in the cell
1099 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1100 - dragOutline.getWidth()) / 2;
1101 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1102 - dragOutline.getHeight()) / 2;
1103 }
1104 }
1105 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001106 }
Winson Chung150fbab2010-09-29 17:14:26 -07001107
Sunny Goyal106bf642015-07-16 12:18:06 -07001108 Utilities.scaleRectAboutCenter(r, getChildrenScale());
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001109 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1110 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001111
1112 if (dragObject.stateAnnouncer != null) {
1113 String msg;
1114 if (isHotseat()) {
1115 msg = getContext().getString(R.string.move_to_hotseat_position,
1116 Math.max(cellX, cellY) + 1);
1117 } else {
1118 msg = getContext().getString(R.string.move_to_empty_cell,
1119 cellY + 1, cellX + 1);
1120 }
1121 dragObject.stateAnnouncer.announce(msg);
1122 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001123 }
1124 }
1125
Adam Cohene0310962011-04-18 16:15:31 -07001126 public void clearDragOutlines() {
1127 final int oldIndex = mDragOutlineCurrent;
1128 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001129 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001130 }
1131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001133 * Find a vacant area that will fit the given bounds nearest the requested
1134 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001135 *
Romain Guy51afc022009-05-04 18:03:43 -07001136 * @param pixelX The X location at which you want to search for a vacant area.
1137 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001138 * @param minSpanX The minimum horizontal span required
1139 * @param minSpanY The minimum vertical span required
1140 * @param spanX Horizontal span of the object.
1141 * @param spanY Vertical span of the object.
1142 * @param result Array in which to place the result, or null (in which case a new array will
1143 * be allocated)
1144 * @return The X, Y cell of a vacant area that can contain this object,
1145 * nearest the requested location.
1146 */
1147 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1148 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001149 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001150 result, resultSpan);
1151 }
1152
Adam Cohend41fbf52012-02-16 23:53:59 -08001153 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1154 private void lazyInitTempRectStack() {
1155 if (mTempRectStack.isEmpty()) {
1156 for (int i = 0; i < mCountX * mCountY; i++) {
1157 mTempRectStack.push(new Rect());
1158 }
1159 }
1160 }
Adam Cohen482ed822012-03-02 14:15:13 -08001161
Adam Cohend41fbf52012-02-16 23:53:59 -08001162 private void recycleTempRects(Stack<Rect> used) {
1163 while (!used.isEmpty()) {
1164 mTempRectStack.push(used.pop());
1165 }
1166 }
1167
1168 /**
1169 * Find a vacant area that will fit the given bounds nearest the requested
1170 * cell location. Uses Euclidean distance to score multiple vacant areas.
1171 *
1172 * @param pixelX The X location at which you want to search for a vacant area.
1173 * @param pixelY The Y location at which you want to search for a vacant area.
1174 * @param minSpanX The minimum horizontal span required
1175 * @param minSpanY The minimum vertical span required
1176 * @param spanX Horizontal span of the object.
1177 * @param spanY Vertical span of the object.
1178 * @param ignoreOccupied If true, the result can be an occupied cell
1179 * @param result Array in which to place the result, or null (in which case a new array will
1180 * be allocated)
1181 * @return The X, Y cell of a vacant area that can contain this object,
1182 * nearest the requested location.
1183 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001184 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1185 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001186 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001187
Adam Cohene3e27a82011-04-15 12:07:39 -07001188 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1189 // to the center of the item, but we are searching based on the top-left cell, so
1190 // we translate the point over to correspond to the top-left.
1191 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1192 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1193
Jeff Sharkey70864282009-04-07 21:08:40 -07001194 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001195 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001196 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001197 final Rect bestRect = new Rect(-1, -1, -1, -1);
1198 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001199
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001200 final int countX = mCountX;
1201 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001202
Adam Cohend41fbf52012-02-16 23:53:59 -08001203 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1204 spanX < minSpanX || spanY < minSpanY) {
1205 return bestXY;
1206 }
1207
1208 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001209 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001210 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1211 int ySize = -1;
1212 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001213 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001214 // First, let's see if this thing fits anywhere
1215 for (int i = 0; i < minSpanX; i++) {
1216 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001217 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001218 continue inner;
1219 }
Michael Jurkac28de512010-08-13 11:27:44 -07001220 }
1221 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001222 xSize = minSpanX;
1223 ySize = minSpanY;
1224
1225 // We know that the item will fit at _some_ acceptable size, now let's see
1226 // how big we can make it. We'll alternate between incrementing x and y spans
1227 // until we hit a limit.
1228 boolean incX = true;
1229 boolean hitMaxX = xSize >= spanX;
1230 boolean hitMaxY = ySize >= spanY;
1231 while (!(hitMaxX && hitMaxY)) {
1232 if (incX && !hitMaxX) {
1233 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001234 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001235 // We can't move out horizontally
1236 hitMaxX = true;
1237 }
1238 }
1239 if (!hitMaxX) {
1240 xSize++;
1241 }
1242 } else if (!hitMaxY) {
1243 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001244 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001245 // We can't move out vertically
1246 hitMaxY = true;
1247 }
1248 }
1249 if (!hitMaxY) {
1250 ySize++;
1251 }
1252 }
1253 hitMaxX |= xSize >= spanX;
1254 hitMaxY |= ySize >= spanY;
1255 incX = !incX;
1256 }
1257 incX = true;
1258 hitMaxX = xSize >= spanX;
1259 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001260 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001261 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001262 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263
Adam Cohend41fbf52012-02-16 23:53:59 -08001264 // We verify that the current rect is not a sub-rect of any of our previous
1265 // candidates. In this case, the current rect is disqualified in favour of the
1266 // containing rect.
1267 Rect currentRect = mTempRectStack.pop();
1268 currentRect.set(x, y, x + xSize, y + ySize);
1269 boolean contained = false;
1270 for (Rect r : validRegions) {
1271 if (r.contains(currentRect)) {
1272 contained = true;
1273 break;
1274 }
1275 }
1276 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001277 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001278
Adam Cohend41fbf52012-02-16 23:53:59 -08001279 if ((distance <= bestDistance && !contained) ||
1280 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001281 bestDistance = distance;
1282 bestXY[0] = x;
1283 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001284 if (resultSpan != null) {
1285 resultSpan[0] = xSize;
1286 resultSpan[1] = ySize;
1287 }
1288 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001289 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 }
1291 }
1292
Adam Cohenc0dcf592011-06-01 15:30:43 -07001293 // Return -1, -1 if no suitable location found
1294 if (bestDistance == Double.MAX_VALUE) {
1295 bestXY[0] = -1;
1296 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001297 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001298 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001299 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001301
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001302 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001303 * Find a vacant area that will fit the given bounds nearest the requested
1304 * cell location, and will also weigh in a suggested direction vector of the
1305 * desired location. This method computers distance based on unit grid distances,
1306 * not pixel distances.
1307 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001308 * @param cellX The X cell nearest to which you want to search for a vacant area.
1309 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001310 * @param spanX Horizontal span of the object.
1311 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001312 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001313 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001314 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001315 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001316 * @param result Array in which to place the result, or null (in which case a new array will
1317 * be allocated)
1318 * @return The X, Y cell of a vacant area that can contain this object,
1319 * nearest the requested location.
1320 */
1321 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001322 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001323 // Keep track of best-scoring drop area
1324 final int[] bestXY = result != null ? result : new int[2];
1325 float bestDistance = Float.MAX_VALUE;
1326 int bestDirectionScore = Integer.MIN_VALUE;
1327
1328 final int countX = mCountX;
1329 final int countY = mCountY;
1330
1331 for (int y = 0; y < countY - (spanY - 1); y++) {
1332 inner:
1333 for (int x = 0; x < countX - (spanX - 1); x++) {
1334 // First, let's see if this thing fits anywhere
1335 for (int i = 0; i < spanX; i++) {
1336 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001337 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001338 continue inner;
1339 }
1340 }
1341 }
1342
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001343 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001344 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001345 computeDirectionVector(x - cellX, y - cellY, curDirection);
1346 // The direction score is just the dot product of the two candidate direction
1347 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001348 int curDirectionScore = direction[0] * curDirection[0] +
1349 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001350 if (Float.compare(distance, bestDistance) < 0 ||
1351 (Float.compare(distance, bestDistance) == 0
1352 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001353 bestDistance = distance;
1354 bestDirectionScore = curDirectionScore;
1355 bestXY[0] = x;
1356 bestXY[1] = y;
1357 }
1358 }
1359 }
1360
1361 // Return -1, -1 if no suitable location found
1362 if (bestDistance == Float.MAX_VALUE) {
1363 bestXY[0] = -1;
1364 bestXY[1] = -1;
1365 }
1366 return bestXY;
1367 }
1368
1369 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001370 int[] direction, ItemConfiguration currentState) {
1371 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001372 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001373 mTmpOccupied.markCells(c, false);
1374 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001375
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001376 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1377 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001378
1379 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001380 c.cellX = mTempLocation[0];
1381 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001382 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001383 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001384 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001385 return success;
1386 }
1387
Adam Cohenf3900c22012-11-16 18:28:11 -08001388 /**
1389 * This helper class defines a cluster of views. It helps with defining complex edges
1390 * of the cluster and determining how those edges interact with other views. The edges
1391 * essentially define a fine-grained boundary around the cluster of views -- like a more
1392 * precise version of a bounding box.
1393 */
1394 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001395 final static int LEFT = 1 << 0;
1396 final static int TOP = 1 << 1;
1397 final static int RIGHT = 1 << 2;
1398 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001399
Adam Cohenf3900c22012-11-16 18:28:11 -08001400 ArrayList<View> views;
1401 ItemConfiguration config;
1402 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001403
Adam Cohenf3900c22012-11-16 18:28:11 -08001404 int[] leftEdge = new int[mCountY];
1405 int[] rightEdge = new int[mCountY];
1406 int[] topEdge = new int[mCountX];
1407 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001408 int dirtyEdges;
1409 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001410
1411 @SuppressWarnings("unchecked")
1412 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1413 this.views = (ArrayList<View>) views.clone();
1414 this.config = config;
1415 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001416 }
1417
Adam Cohenf3900c22012-11-16 18:28:11 -08001418 void resetEdges() {
1419 for (int i = 0; i < mCountX; i++) {
1420 topEdge[i] = -1;
1421 bottomEdge[i] = -1;
1422 }
1423 for (int i = 0; i < mCountY; i++) {
1424 leftEdge[i] = -1;
1425 rightEdge[i] = -1;
1426 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001427 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001428 boundingRectDirty = true;
1429 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001430
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001431 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001432 int count = views.size();
1433 for (int i = 0; i < count; i++) {
1434 CellAndSpan cs = config.map.get(views.get(i));
1435 switch (which) {
1436 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001437 int left = cs.cellX;
1438 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001439 if (left < leftEdge[j] || leftEdge[j] < 0) {
1440 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001441 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001442 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001443 break;
1444 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001445 int right = cs.cellX + cs.spanX;
1446 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001447 if (right > rightEdge[j]) {
1448 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001449 }
1450 }
1451 break;
1452 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001453 int top = cs.cellY;
1454 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001455 if (top < topEdge[j] || topEdge[j] < 0) {
1456 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001457 }
1458 }
1459 break;
1460 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001461 int bottom = cs.cellY + cs.spanY;
1462 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001463 if (bottom > bottomEdge[j]) {
1464 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001465 }
1466 }
1467 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001468 }
1469 }
1470 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001471
1472 boolean isViewTouchingEdge(View v, int whichEdge) {
1473 CellAndSpan cs = config.map.get(v);
1474
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001475 if ((dirtyEdges & whichEdge) == whichEdge) {
1476 computeEdge(whichEdge);
1477 dirtyEdges &= ~whichEdge;
1478 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001479
1480 switch (whichEdge) {
1481 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001482 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1483 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001484 return true;
1485 }
1486 }
1487 break;
1488 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001489 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1490 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001491 return true;
1492 }
1493 }
1494 break;
1495 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001496 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1497 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001498 return true;
1499 }
1500 }
1501 break;
1502 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001503 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1504 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001505 return true;
1506 }
1507 }
1508 break;
1509 }
1510 return false;
1511 }
1512
1513 void shift(int whichEdge, int delta) {
1514 for (View v: views) {
1515 CellAndSpan c = config.map.get(v);
1516 switch (whichEdge) {
1517 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001518 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001519 break;
1520 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001521 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001522 break;
1523 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001524 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001525 break;
1526 case BOTTOM:
1527 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001528 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001529 break;
1530 }
1531 }
1532 resetEdges();
1533 }
1534
1535 public void addView(View v) {
1536 views.add(v);
1537 resetEdges();
1538 }
1539
1540 public Rect getBoundingRect() {
1541 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001542 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001543 }
1544 return boundingRect;
1545 }
1546
Adam Cohenf3900c22012-11-16 18:28:11 -08001547 PositionComparator comparator = new PositionComparator();
1548 class PositionComparator implements Comparator<View> {
1549 int whichEdge = 0;
1550 public int compare(View left, View right) {
1551 CellAndSpan l = config.map.get(left);
1552 CellAndSpan r = config.map.get(right);
1553 switch (whichEdge) {
1554 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001555 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001556 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001557 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001558 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001559 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001560 case BOTTOM:
1561 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001562 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001563 }
1564 }
1565 }
1566
1567 public void sortConfigurationForEdgePush(int edge) {
1568 comparator.whichEdge = edge;
1569 Collections.sort(config.sortedViews, comparator);
1570 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001571 }
1572
Adam Cohenf3900c22012-11-16 18:28:11 -08001573 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1574 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001575
Adam Cohenf3900c22012-11-16 18:28:11 -08001576 ViewCluster cluster = new ViewCluster(views, currentState);
1577 Rect clusterRect = cluster.getBoundingRect();
1578 int whichEdge;
1579 int pushDistance;
1580 boolean fail = false;
1581
1582 // Determine the edge of the cluster that will be leading the push and how far
1583 // the cluster must be shifted.
1584 if (direction[0] < 0) {
1585 whichEdge = ViewCluster.LEFT;
1586 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001587 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001588 whichEdge = ViewCluster.RIGHT;
1589 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1590 } else if (direction[1] < 0) {
1591 whichEdge = ViewCluster.TOP;
1592 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1593 } else {
1594 whichEdge = ViewCluster.BOTTOM;
1595 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001596 }
1597
Adam Cohenf3900c22012-11-16 18:28:11 -08001598 // Break early for invalid push distance.
1599 if (pushDistance <= 0) {
1600 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001601 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001602
1603 // Mark the occupied state as false for the group of views we want to move.
1604 for (View v: views) {
1605 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001606 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001607 }
1608
1609 // We save the current configuration -- if we fail to find a solution we will revert
1610 // to the initial state. The process of finding a solution modifies the configuration
1611 // in place, hence the need for revert in the failure case.
1612 currentState.save();
1613
1614 // The pushing algorithm is simplified by considering the views in the order in which
1615 // they would be pushed by the cluster. For example, if the cluster is leading with its
1616 // left edge, we consider sort the views by their right edge, from right to left.
1617 cluster.sortConfigurationForEdgePush(whichEdge);
1618
1619 while (pushDistance > 0 && !fail) {
1620 for (View v: currentState.sortedViews) {
1621 // For each view that isn't in the cluster, we see if the leading edge of the
1622 // cluster is contacting the edge of that view. If so, we add that view to the
1623 // cluster.
1624 if (!cluster.views.contains(v) && v != dragView) {
1625 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1626 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1627 if (!lp.canReorder) {
1628 // The push solution includes the all apps button, this is not viable.
1629 fail = true;
1630 break;
1631 }
1632 cluster.addView(v);
1633 CellAndSpan c = currentState.map.get(v);
1634
1635 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001636 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001637 }
1638 }
1639 }
1640 pushDistance--;
1641
1642 // The cluster has been completed, now we move the whole thing over in the appropriate
1643 // direction.
1644 cluster.shift(whichEdge, 1);
1645 }
1646
1647 boolean foundSolution = false;
1648 clusterRect = cluster.getBoundingRect();
1649
1650 // Due to the nature of the algorithm, the only check required to verify a valid solution
1651 // is to ensure that completed shifted cluster lies completely within the cell layout.
1652 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1653 clusterRect.bottom <= mCountY) {
1654 foundSolution = true;
1655 } else {
1656 currentState.restore();
1657 }
1658
1659 // In either case, we set the occupied array as marked for the location of the views
1660 for (View v: cluster.views) {
1661 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001662 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001663 }
1664
1665 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001666 }
1667
Adam Cohen482ed822012-03-02 14:15:13 -08001668 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001669 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001670 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001671
Adam Cohen8baab352012-03-20 17:39:21 -07001672 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001673 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001674 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001675 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001676
Adam Cohen8baab352012-03-20 17:39:21 -07001677 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001678 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001679 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001680 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001681 }
1682
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001683 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001684 int top = boundingRect.top;
1685 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001686 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001687 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001688 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001689 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001690 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001691 }
1692
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001693 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001694
Adam Cohenf3900c22012-11-16 18:28:11 -08001695 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001696 boundingRect.height(), direction,
1697 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001698
Adam Cohen8baab352012-03-20 17:39:21 -07001699 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001700 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001701 int deltaX = mTempLocation[0] - boundingRect.left;
1702 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001703 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001704 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001705 c.cellX += deltaX;
1706 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001707 }
1708 success = true;
1709 }
Adam Cohen8baab352012-03-20 17:39:21 -07001710
1711 // In either case, we set the occupied array as marked for the location of the views
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 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001715 }
1716 return success;
1717 }
1718
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001719 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1720 // to push items in each of the cardinal directions, in an order based on the direction vector
1721 // passed.
1722 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1723 int[] direction, View ignoreView, ItemConfiguration solution) {
1724 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001725 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001726 // separately in each of the components.
1727 int temp = direction[1];
1728 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001729
1730 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001731 ignoreView, solution)) {
1732 return true;
1733 }
1734 direction[1] = temp;
1735 temp = direction[0];
1736 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001737
1738 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001739 ignoreView, solution)) {
1740 return true;
1741 }
1742 // Revert the direction
1743 direction[0] = temp;
1744
1745 // Now we try pushing in each component of the opposite direction
1746 direction[0] *= -1;
1747 direction[1] *= -1;
1748 temp = direction[1];
1749 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001750 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001751 ignoreView, solution)) {
1752 return true;
1753 }
1754
1755 direction[1] = temp;
1756 temp = direction[0];
1757 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001758 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001759 ignoreView, solution)) {
1760 return true;
1761 }
1762 // revert the direction
1763 direction[0] = temp;
1764 direction[0] *= -1;
1765 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001766
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001767 } else {
1768 // If the direction vector has a single non-zero component, we push first in the
1769 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001770 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001771 ignoreView, solution)) {
1772 return true;
1773 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001774 // Then we try the opposite direction
1775 direction[0] *= -1;
1776 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001777 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001778 ignoreView, solution)) {
1779 return true;
1780 }
1781 // Switch the direction back
1782 direction[0] *= -1;
1783 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001784
1785 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001786 // to find a solution by pushing along the perpendicular axis.
1787
1788 // Swap the components
1789 int temp = direction[1];
1790 direction[1] = direction[0];
1791 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001792 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001793 ignoreView, solution)) {
1794 return true;
1795 }
1796
1797 // Then we try the opposite direction
1798 direction[0] *= -1;
1799 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001800 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001801 ignoreView, solution)) {
1802 return true;
1803 }
1804 // Switch the direction back
1805 direction[0] *= -1;
1806 direction[1] *= -1;
1807
1808 // Swap the components back
1809 temp = direction[1];
1810 direction[1] = direction[0];
1811 direction[0] = temp;
1812 }
1813 return false;
1814 }
1815
Adam Cohen482ed822012-03-02 14:15:13 -08001816 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001817 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001818 // Return early if get invalid cell positions
1819 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001820
Adam Cohen8baab352012-03-20 17:39:21 -07001821 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001822 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001823
Adam Cohen8baab352012-03-20 17:39:21 -07001824 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001825 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001826 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001827 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001828 c.cellX = cellX;
1829 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001830 }
Adam Cohen482ed822012-03-02 14:15:13 -08001831 }
Adam Cohen482ed822012-03-02 14:15:13 -08001832 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1833 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001834 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001835 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001836 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001837 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001838 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001839 if (Rect.intersects(r0, r1)) {
1840 if (!lp.canReorder) {
1841 return false;
1842 }
1843 mIntersectingViews.add(child);
1844 }
1845 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001846
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001847 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1848
Winson Chung5f8afe62013-08-12 16:19:28 -07001849 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001850 // we try to find a solution such that no displaced item travels through another item
1851 // without also displacing that item.
1852 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001853 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001854 return true;
1855 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001856
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001857 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001858 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001859 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001860 return true;
1861 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001862
Adam Cohen482ed822012-03-02 14:15:13 -08001863 // Ok, they couldn't move as a block, let's move them individually
1864 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001865 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001866 return false;
1867 }
1868 }
1869 return true;
1870 }
1871
1872 /*
1873 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1874 * the provided point and the provided cell
1875 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001876 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001877 double angle = Math.atan(((float) deltaY) / deltaX);
1878
1879 result[0] = 0;
1880 result[1] = 0;
1881 if (Math.abs(Math.cos(angle)) > 0.5f) {
1882 result[0] = (int) Math.signum(deltaX);
1883 }
1884 if (Math.abs(Math.sin(angle)) > 0.5f) {
1885 result[1] = (int) Math.signum(deltaY);
1886 }
1887 }
1888
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001889 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001890 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1891 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001892 // Copy the current state into the solution. This solution will be manipulated as necessary.
1893 copyCurrentStateToSolution(solution, false);
1894 // Copy the current occupied array into the temporary occupied array. This array will be
1895 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001896 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001897
1898 // We find the nearest cell into which we would place the dragged item, assuming there's
1899 // nothing in its way.
1900 int result[] = new int[2];
1901 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1902
1903 boolean success = false;
1904 // First we try the exact nearest position of the item being dragged,
1905 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001906 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1907 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001908
1909 if (!success) {
1910 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1911 // x, then 1 in y etc.
1912 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001913 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1914 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001915 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001916 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1917 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001918 }
1919 solution.isSolution = false;
1920 } else {
1921 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001922 solution.cellX = result[0];
1923 solution.cellY = result[1];
1924 solution.spanX = spanX;
1925 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001926 }
1927 return solution;
1928 }
1929
1930 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001931 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001932 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001933 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001934 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001935 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001936 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001937 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001938 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001939 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001940 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001941 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001942 }
1943 }
1944
1945 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001946 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001947
Michael Jurkaa52570f2012-03-20 03:18:20 -07001948 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001949 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001950 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001951 if (child == dragView) continue;
1952 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001953 CellAndSpan c = solution.map.get(child);
1954 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001955 lp.tmpCellX = c.cellX;
1956 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001957 lp.cellHSpan = c.spanX;
1958 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001959 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001960 }
1961 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001962 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001963 }
1964
1965 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1966 commitDragView) {
1967
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001968 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1969 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001970
Michael Jurkaa52570f2012-03-20 03:18:20 -07001971 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001972 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001973 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001974 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001975 CellAndSpan c = solution.map.get(child);
1976 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001977 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001978 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001979 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001980 }
1981 }
1982 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001983 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001984 }
1985 }
1986
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001987
1988 // This method starts or changes the reorder preview animations
1989 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1990 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001991 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001992 for (int i = 0; i < childCount; i++) {
1993 View child = mShortcutsAndWidgets.getChildAt(i);
1994 if (child == dragView) continue;
1995 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001996 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1997 != null && !solution.intersectingViews.contains(child);
1998
Adam Cohen19f37922012-03-21 11:59:11 -07001999 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002000 if (c != null && !skip) {
2001 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002002 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002003 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002004 }
2005 }
2006 }
2007
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002008 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002009 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002010 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002011 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002012 float finalDeltaX;
2013 float finalDeltaY;
2014 float initDeltaX;
2015 float initDeltaY;
2016 float finalScale;
2017 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002018 int mode;
2019 boolean repeating = false;
2020 private static final int PREVIEW_DURATION = 300;
2021 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2022
2023 public static final int MODE_HINT = 0;
2024 public static final int MODE_PREVIEW = 1;
2025
Adam Cohene7587d22012-05-24 18:50:02 -07002026 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002027
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002028 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2029 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002030 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2031 final int x0 = mTmpPoint[0];
2032 final int y0 = mTmpPoint[1];
2033 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2034 final int x1 = mTmpPoint[0];
2035 final int y1 = mTmpPoint[1];
2036 final int dX = x1 - x0;
2037 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002038 finalDeltaX = 0;
2039 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002040 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002041 if (dX == dY && dX == 0) {
2042 } else {
2043 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002044 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002045 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002046 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002047 } else {
2048 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002049 finalDeltaX = (int) (- dir * Math.signum(dX) *
2050 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2051 finalDeltaY = (int) (- dir * Math.signum(dY) *
2052 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002053 }
2054 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002055 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002056 initDeltaX = child.getTranslationX();
2057 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002058 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002059 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002060 this.child = child;
2061 }
2062
Adam Cohend024f982012-05-23 18:26:45 -07002063 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002064 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002065 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002066 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002067 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002068 if (finalDeltaX == 0 && finalDeltaY == 0) {
2069 completeAnimationImmediately();
2070 return;
2071 }
Adam Cohen19f37922012-03-21 11:59:11 -07002072 }
Adam Cohend024f982012-05-23 18:26:45 -07002073 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002074 return;
2075 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002076 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002077 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002078
2079 // Animations are disabled in power save mode, causing the repeated animation to jump
2080 // spastically between beginning and end states. Since this looks bad, we don't repeat
2081 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002082 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002083 va.setRepeatMode(ValueAnimator.REVERSE);
2084 va.setRepeatCount(ValueAnimator.INFINITE);
2085 }
2086
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002087 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002088 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002089 va.addUpdateListener(new AnimatorUpdateListener() {
2090 @Override
2091 public void onAnimationUpdate(ValueAnimator animation) {
2092 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002093 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2094 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2095 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002096 child.setTranslationX(x);
2097 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002098 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002099 child.setScaleX(s);
2100 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002101 }
2102 });
2103 va.addListener(new AnimatorListenerAdapter() {
2104 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002105 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002106 initDeltaX = 0;
2107 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002108 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002109 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002110 }
2111 });
Adam Cohen19f37922012-03-21 11:59:11 -07002112 mShakeAnimators.put(child, this);
2113 va.start();
2114 }
2115
Adam Cohend024f982012-05-23 18:26:45 -07002116 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002117 if (a != null) {
2118 a.cancel();
2119 }
Adam Cohen19f37922012-03-21 11:59:11 -07002120 }
Adam Cohene7587d22012-05-24 18:50:02 -07002121
Adam Cohen091440a2015-03-18 14:16:05 -07002122 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002123 if (a != null) {
2124 a.cancel();
2125 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002126
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002127 a = new LauncherViewPropertyAnimator(child)
2128 .scaleX(getChildrenScale())
2129 .scaleY(getChildrenScale())
2130 .translationX(0)
2131 .translationY(0)
2132 .setDuration(REORDER_ANIMATION_DURATION);
2133 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2134 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002135 }
Adam Cohen19f37922012-03-21 11:59:11 -07002136 }
2137
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002138 private void completeAndClearReorderPreviewAnimations() {
2139 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002140 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002141 }
2142 mShakeAnimators.clear();
2143 }
2144
Adam Cohen482ed822012-03-02 14:15:13 -08002145 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002146 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002147
2148 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2149 int container = Favorites.CONTAINER_DESKTOP;
2150
2151 if (mLauncher.isHotseatLayout(this)) {
2152 screenId = -1;
2153 container = Favorites.CONTAINER_HOTSEAT;
2154 }
2155
Michael Jurkaa52570f2012-03-20 03:18:20 -07002156 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002157 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002158 View child = mShortcutsAndWidgets.getChildAt(i);
2159 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2160 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002161 // We do a null check here because the item info can be null in the case of the
2162 // AllApps button in the hotseat.
2163 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002164 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2165 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2166 || info.spanY != lp.cellVSpan);
2167
Adam Cohen2acce882012-03-28 19:03:19 -07002168 info.cellX = lp.cellX = lp.tmpCellX;
2169 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002170 info.spanX = lp.cellHSpan;
2171 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002172
2173 if (requiresDbUpdate) {
2174 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId,
2175 info.cellX, info.cellY, info.spanX, info.spanY);
2176 }
Adam Cohen2acce882012-03-28 19:03:19 -07002177 }
Adam Cohen482ed822012-03-02 14:15:13 -08002178 }
2179 }
2180
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002181 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002182 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002183 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002184 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002185 lp.useTmpCoords = useTempCoords;
2186 }
2187 }
2188
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002189 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002190 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2191 int[] result = new int[2];
2192 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002193 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002194 resultSpan);
2195 if (result[0] >= 0 && result[1] >= 0) {
2196 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002197 solution.cellX = result[0];
2198 solution.cellY = result[1];
2199 solution.spanX = resultSpan[0];
2200 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002201 solution.isSolution = true;
2202 } else {
2203 solution.isSolution = false;
2204 }
2205 return solution;
2206 }
2207
Adam Cohen19f37922012-03-21 11:59:11 -07002208 /* This seems like it should be obvious and straight-forward, but when the direction vector
2209 needs to match with the notion of the dragView pushing other views, we have to employ
2210 a slightly more subtle notion of the direction vector. The question is what two points is
2211 the vector between? The center of the dragView and its desired destination? Not quite, as
2212 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2213 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2214 or right, which helps make pushing feel right.
2215 */
2216 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2217 int spanY, View dragView, int[] resultDirection) {
2218 int[] targetDestination = new int[2];
2219
2220 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2221 Rect dragRect = new Rect();
2222 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2223 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2224
2225 Rect dropRegionRect = new Rect();
2226 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2227 dragView, dropRegionRect, mIntersectingViews);
2228
2229 int dropRegionSpanX = dropRegionRect.width();
2230 int dropRegionSpanY = dropRegionRect.height();
2231
2232 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2233 dropRegionRect.height(), dropRegionRect);
2234
2235 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2236 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2237
2238 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2239 deltaX = 0;
2240 }
2241 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2242 deltaY = 0;
2243 }
2244
2245 if (deltaX == 0 && deltaY == 0) {
2246 // No idea what to do, give a random direction.
2247 resultDirection[0] = 1;
2248 resultDirection[1] = 0;
2249 } else {
2250 computeDirectionVector(deltaX, deltaY, resultDirection);
2251 }
2252 }
2253
2254 // For a given cell and span, fetch the set of views intersecting the region.
2255 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2256 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2257 if (boundingRect != null) {
2258 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2259 }
2260 intersectingViews.clear();
2261 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2262 Rect r1 = new Rect();
2263 final int count = mShortcutsAndWidgets.getChildCount();
2264 for (int i = 0; i < count; i++) {
2265 View child = mShortcutsAndWidgets.getChildAt(i);
2266 if (child == dragView) continue;
2267 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2268 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2269 if (Rect.intersects(r0, r1)) {
2270 mIntersectingViews.add(child);
2271 if (boundingRect != null) {
2272 boundingRect.union(r1);
2273 }
2274 }
2275 }
2276 }
2277
2278 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2279 View dragView, int[] result) {
2280 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2281 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2282 mIntersectingViews);
2283 return !mIntersectingViews.isEmpty();
2284 }
2285
2286 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002287 completeAndClearReorderPreviewAnimations();
2288 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2289 final int count = mShortcutsAndWidgets.getChildCount();
2290 for (int i = 0; i < count; i++) {
2291 View child = mShortcutsAndWidgets.getChildAt(i);
2292 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2293 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2294 lp.tmpCellX = lp.cellX;
2295 lp.tmpCellY = lp.cellY;
2296 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2297 0, false, false);
2298 }
Adam Cohen19f37922012-03-21 11:59:11 -07002299 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002300 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002301 }
Adam Cohen19f37922012-03-21 11:59:11 -07002302 }
2303
Adam Cohenbebf0422012-04-11 18:06:28 -07002304 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2305 View dragView, int[] direction, boolean commit) {
2306 int[] pixelXY = new int[2];
2307 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2308
2309 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002310 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002311 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2312
2313 setUseTempCoords(true);
2314 if (swapSolution != null && swapSolution.isSolution) {
2315 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2316 // committing anything or animating anything as we just want to determine if a solution
2317 // exists
2318 copySolutionToTempState(swapSolution, dragView);
2319 setItemPlacementDirty(true);
2320 animateItemsToSolution(swapSolution, dragView, commit);
2321
2322 if (commit) {
2323 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002324 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002325 setItemPlacementDirty(false);
2326 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002327 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2328 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002329 }
2330 mShortcutsAndWidgets.requestLayout();
2331 }
2332 return swapSolution.isSolution;
2333 }
2334
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002335 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002336 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002337 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002338 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002339
2340 if (resultSpan == null) {
2341 resultSpan = new int[2];
2342 }
2343
Adam Cohen19f37922012-03-21 11:59:11 -07002344 // When we are checking drop validity or actually dropping, we don't recompute the
2345 // direction vector, since we want the solution to match the preview, and it's possible
2346 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002347 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2348 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002349 mDirectionVector[0] = mPreviousReorderDirection[0];
2350 mDirectionVector[1] = mPreviousReorderDirection[1];
2351 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002352 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2353 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2354 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002355 }
2356 } else {
2357 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2358 mPreviousReorderDirection[0] = mDirectionVector[0];
2359 mPreviousReorderDirection[1] = mDirectionVector[1];
2360 }
2361
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002362 // Find a solution involving pushing / displacing any items in the way
2363 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002364 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2365
2366 // We attempt the approach which doesn't shuffle views at all
2367 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2368 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2369
2370 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002371
2372 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2373 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002374 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2375 finalSolution = swapSolution;
2376 } else if (noShuffleSolution.isSolution) {
2377 finalSolution = noShuffleSolution;
2378 }
2379
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002380 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002381 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002382 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2383 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002384 result[0] = finalSolution.cellX;
2385 result[1] = finalSolution.cellY;
2386 resultSpan[0] = finalSolution.spanX;
2387 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002388 } else {
2389 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2390 }
2391 return result;
2392 }
2393
Adam Cohen482ed822012-03-02 14:15:13 -08002394 boolean foundSolution = true;
2395 if (!DESTRUCTIVE_REORDER) {
2396 setUseTempCoords(true);
2397 }
2398
2399 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002400 result[0] = finalSolution.cellX;
2401 result[1] = finalSolution.cellY;
2402 resultSpan[0] = finalSolution.spanX;
2403 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002404
2405 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2406 // committing anything or animating anything as we just want to determine if a solution
2407 // exists
2408 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2409 if (!DESTRUCTIVE_REORDER) {
2410 copySolutionToTempState(finalSolution, dragView);
2411 }
2412 setItemPlacementDirty(true);
2413 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2414
Adam Cohen19f37922012-03-21 11:59:11 -07002415 if (!DESTRUCTIVE_REORDER &&
2416 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002417 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002418 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002419 setItemPlacementDirty(false);
2420 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002421 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2422 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002423 }
2424 }
2425 } else {
2426 foundSolution = false;
2427 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2428 }
2429
2430 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2431 setUseTempCoords(false);
2432 }
Adam Cohen482ed822012-03-02 14:15:13 -08002433
Michael Jurkaa52570f2012-03-20 03:18:20 -07002434 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002435 return result;
2436 }
2437
Adam Cohen19f37922012-03-21 11:59:11 -07002438 void setItemPlacementDirty(boolean dirty) {
2439 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002440 }
Adam Cohen19f37922012-03-21 11:59:11 -07002441 boolean isItemPlacementDirty() {
2442 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002443 }
2444
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002445 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002446 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002447 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2448 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002449 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002450 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002451
Adam Cohenf3900c22012-11-16 18:28:11 -08002452 void save() {
2453 // Copy current state into savedMap
2454 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002455 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002456 }
2457 }
2458
2459 void restore() {
2460 // Restore current state from savedMap
2461 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002462 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002463 }
2464 }
2465
2466 void add(View v, CellAndSpan cs) {
2467 map.put(v, cs);
2468 savedMap.put(v, new CellAndSpan());
2469 sortedViews.add(v);
2470 }
2471
Adam Cohen482ed822012-03-02 14:15:13 -08002472 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002473 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002474 }
2475
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002476 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2477 boolean first = true;
2478 for (View v: views) {
2479 CellAndSpan c = map.get(v);
2480 if (first) {
2481 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2482 first = false;
2483 } else {
2484 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2485 }
2486 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002487 }
Adam Cohen482ed822012-03-02 14:15:13 -08002488 }
2489
Adam Cohendf035382011-04-11 17:22:04 -07002490 /**
Adam Cohendf035382011-04-11 17:22:04 -07002491 * Find a starting cell position that will fit the given bounds nearest the requested
2492 * cell location. Uses Euclidean distance to score multiple vacant areas.
2493 *
2494 * @param pixelX The X location at which you want to search for a vacant area.
2495 * @param pixelY The Y location at which you want to search for a vacant area.
2496 * @param spanX Horizontal span of the object.
2497 * @param spanY Vertical span of the object.
2498 * @param ignoreView Considers space occupied by this view as unoccupied
2499 * @param result Previously returned value to possibly recycle.
2500 * @return The X, Y cell of a vacant area that can contain this object,
2501 * nearest the requested location.
2502 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002503 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002504 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002505 }
2506
Michael Jurka0280c3b2010-09-17 15:00:07 -07002507 boolean existsEmptyCell() {
2508 return findCellForSpan(null, 1, 1);
2509 }
2510
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002512 * Finds the upper-left coordinate of the first rectangle in the grid that can
2513 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2514 * then this method will only return coordinates for rectangles that contain the cell
2515 * (intersectX, intersectY)
2516 *
2517 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2518 * can be found.
2519 * @param spanX The horizontal span of the cell we want to find.
2520 * @param spanY The vertical span of the cell we want to find.
2521 *
2522 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002523 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002524 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002525 if (cellXY == null) {
2526 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002527 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002528 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002529 }
2530
2531 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002532 * A drag event has begun over this layout.
2533 * It may have begun over this layout (in which case onDragChild is called first),
2534 * or it may have begun on another layout.
2535 */
2536 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002537 mDragging = true;
2538 }
2539
2540 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002541 * Called when drag has left this CellLayout or has been completed (successfully or not)
2542 */
2543 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002544 // This can actually be called when we aren't in a drag, e.g. when adding a new
2545 // item to this layout via the customize drawer.
2546 // Guard against that case.
2547 if (mDragging) {
2548 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002549 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002550
2551 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002552 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002553 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2554 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002555 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002556 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002557 }
2558
2559 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002560 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002561 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002562 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 *
2564 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002565 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002566 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002567 if (child != null) {
2568 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002569 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002570 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002571 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002572 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002573 }
2574
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002575 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002577 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002578 * @param cellX X coordinate of upper left corner expressed as a cell position
2579 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002580 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002582 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002583 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002584 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002585 final int cellWidth = mCellWidth;
2586 final int cellHeight = mCellHeight;
2587 final int widthGap = mWidthGap;
2588 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002589
Winson Chung4b825dcd2011-06-19 12:41:22 -07002590 final int hStartPadding = getPaddingLeft();
2591 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002593 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2594 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2595
2596 int x = hStartPadding + cellX * (cellWidth + widthGap);
2597 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002598
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002599 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002600 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002601
Adam Cohend4844c32011-02-18 19:25:06 -08002602 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002603 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002604 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002605 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002606 }
2607
Adam Cohend4844c32011-02-18 19:25:06 -08002608 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002609 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002610 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002611 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002612 }
2613
Adam Cohen2801caf2011-05-13 20:57:39 -07002614 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002615 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002616 (Math.max((mCountX - 1), 0) * mWidthGap);
2617 }
2618
2619 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002620 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002621 (Math.max((mCountY - 1), 0) * mHeightGap);
2622 }
2623
Michael Jurka66d72172011-04-12 16:29:25 -07002624 public boolean isOccupied(int x, int y) {
2625 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002626 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002627 } else {
2628 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2629 }
2630 }
2631
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002632 @Override
2633 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2634 return new CellLayout.LayoutParams(getContext(), attrs);
2635 }
2636
2637 @Override
2638 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2639 return p instanceof CellLayout.LayoutParams;
2640 }
2641
2642 @Override
2643 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2644 return new CellLayout.LayoutParams(p);
2645 }
2646
2647 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2648 /**
2649 * Horizontal location of the item in the grid.
2650 */
2651 @ViewDebug.ExportedProperty
2652 public int cellX;
2653
2654 /**
2655 * Vertical location of the item in the grid.
2656 */
2657 @ViewDebug.ExportedProperty
2658 public int cellY;
2659
2660 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002661 * Temporary horizontal location of the item in the grid during reorder
2662 */
2663 public int tmpCellX;
2664
2665 /**
2666 * Temporary vertical location of the item in the grid during reorder
2667 */
2668 public int tmpCellY;
2669
2670 /**
2671 * Indicates that the temporary coordinates should be used to layout the items
2672 */
2673 public boolean useTmpCoords;
2674
2675 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002676 * Number of cells spanned horizontally by the item.
2677 */
2678 @ViewDebug.ExportedProperty
2679 public int cellHSpan;
2680
2681 /**
2682 * Number of cells spanned vertically by the item.
2683 */
2684 @ViewDebug.ExportedProperty
2685 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002686
Adam Cohen1b607ed2011-03-03 17:26:50 -08002687 /**
2688 * Indicates whether the item will set its x, y, width and height parameters freely,
2689 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2690 */
Adam Cohend4844c32011-02-18 19:25:06 -08002691 public boolean isLockedToGrid = true;
2692
Adam Cohen482ed822012-03-02 14:15:13 -08002693 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002694 * Indicates that this item should use the full extents of its parent.
2695 */
2696 public boolean isFullscreen = false;
2697
2698 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002699 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002700 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002701 */
2702 public boolean canReorder = true;
2703
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002704 // X coordinate of the view in the layout.
2705 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002706 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002707 // Y coordinate of the view in the layout.
2708 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002709 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002710
Romain Guy84f296c2009-11-04 15:00:44 -08002711 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002712
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713 public LayoutParams(Context c, AttributeSet attrs) {
2714 super(c, attrs);
2715 cellHSpan = 1;
2716 cellVSpan = 1;
2717 }
2718
2719 public LayoutParams(ViewGroup.LayoutParams source) {
2720 super(source);
2721 cellHSpan = 1;
2722 cellVSpan = 1;
2723 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002724
2725 public LayoutParams(LayoutParams source) {
2726 super(source);
2727 this.cellX = source.cellX;
2728 this.cellY = source.cellY;
2729 this.cellHSpan = source.cellHSpan;
2730 this.cellVSpan = source.cellVSpan;
2731 }
2732
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002733 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002734 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002735 this.cellX = cellX;
2736 this.cellY = cellY;
2737 this.cellHSpan = cellHSpan;
2738 this.cellVSpan = cellVSpan;
2739 }
2740
Adam Cohen2374abf2013-04-16 14:56:57 -07002741 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2742 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002743 if (isLockedToGrid) {
2744 final int myCellHSpan = cellHSpan;
2745 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002746 int myCellX = useTmpCoords ? tmpCellX : cellX;
2747 int myCellY = useTmpCoords ? tmpCellY : cellY;
2748
2749 if (invertHorizontally) {
2750 myCellX = colCount - myCellX - cellHSpan;
2751 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002752
Adam Cohend4844c32011-02-18 19:25:06 -08002753 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2754 leftMargin - rightMargin;
2755 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2756 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002757 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2758 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002759 }
2760 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002761
Winson Chungaafa03c2010-06-11 17:34:16 -07002762 public String toString() {
2763 return "(" + this.cellX + ", " + this.cellY + ")";
2764 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002765
2766 public void setWidth(int width) {
2767 this.width = width;
2768 }
2769
2770 public int getWidth() {
2771 return width;
2772 }
2773
2774 public void setHeight(int height) {
2775 this.height = height;
2776 }
2777
2778 public int getHeight() {
2779 return height;
2780 }
2781
2782 public void setX(int x) {
2783 this.x = x;
2784 }
2785
2786 public int getX() {
2787 return x;
2788 }
2789
2790 public void setY(int y) {
2791 this.y = y;
2792 }
2793
2794 public int getY() {
2795 return y;
2796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002797 }
2798
Michael Jurka0280c3b2010-09-17 15:00:07 -07002799 // This class stores info for two purposes:
2800 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2801 // its spanX, spanY, and the screen it is on
2802 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2803 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2804 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002805 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002806 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002807 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002808 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002809
Sunny Goyal83a8f042015-05-19 12:52:12 -07002810 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002811 cellX = info.cellX;
2812 cellY = info.cellY;
2813 spanX = info.spanX;
2814 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002815 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002816 screenId = info.screenId;
2817 container = info.container;
2818 }
2819
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002820 @Override
2821 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002822 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2823 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002824 }
2825 }
Michael Jurkad771c962011-08-09 15:00:48 -07002826
Tony Wickham86930612015-09-09 13:50:40 -07002827 /**
2828 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2829 * if necessary).
2830 */
2831 public boolean hasReorderSolution(ItemInfo itemInfo) {
2832 int[] cellPoint = new int[2];
2833 // Check for a solution starting at every cell.
2834 for (int cellX = 0; cellX < getCountX(); cellX++) {
2835 for (int cellY = 0; cellY < getCountY(); cellY++) {
2836 cellToPoint(cellX, cellY, cellPoint);
2837 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2838 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2839 true, new ItemConfiguration()).isSolution) {
2840 return true;
2841 }
2842 }
2843 }
2844 return false;
2845 }
2846
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002847 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002848 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002849 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002850}