blob: ee2131398104694826c08d924c58a95e9ef4010c [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070021import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
Adam Cohenc9735cf2015-01-23 16:11:55 -080024import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Joe Onorato4be866d2010-10-10 11:26:02 -070027import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080029import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070031import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080033import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070034import android.graphics.drawable.Drawable;
Sunny Goyal2805e632015-05-20 15:35:32 -070035import android.graphics.drawable.TransitionDrawable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080036import android.os.Build;
Adam Cohen1462de32012-07-24 22:34:36 -070037import android.os.Parcelable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080038import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070041import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.MotionEvent;
43import android.view.View;
44import android.view.ViewDebug;
45import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080046import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070047import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Sunny Goyal4b6eb262015-05-14 19:24:40 -070049import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070050import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070051import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
52import com.android.launcher3.accessibility.FolderAccessibilityHelper;
53import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Tony Wickhame0c33232016-02-08 11:37:04 -080054import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070055import com.android.launcher3.config.ProviderConfig;
Sunny Goyal26119432016-02-18 22:09:23 +000056import com.android.launcher3.folder.FolderIcon;
Sunny Goyal06e21a22016-08-11 16:02:02 -070057import com.android.launcher3.graphics.DragPreviewProvider;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070058import com.android.launcher3.util.CellAndSpan;
59import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070060import com.android.launcher3.util.ParcelableSparseArray;
Adam Cohen091440a2015-03-18 14:16:05 -070061import com.android.launcher3.util.Thunk;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070062
Adam Cohen69ce2e52011-07-03 19:25:21 -070063import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070064import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080065import java.util.Collections;
66import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070067import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080068import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070069
Sunny Goyal4b6eb262015-05-14 19:24:40 -070070public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070071 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
72 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
73
Tony Wickhama0628cc2015-10-14 15:23:04 -070074 private static final String TAG = "CellLayout";
75 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070076
Adam Cohen2acce882012-03-28 19:03:19 -070077 private Launcher mLauncher;
Sunny Goyal4ffec482016-02-09 11:28:52 -080078 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070079 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080080 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070081 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070082 private int mFixedCellWidth;
83 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070084
Sunny Goyal4ffec482016-02-09 11:28:52 -080085 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070086 private int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -080087 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070088 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Adam Cohen234c4cd2011-07-17 21:03:04 -070090 private int mOriginalWidthGap;
91 private int mOriginalHeightGap;
Sunny Goyal4ffec482016-02-09 11:28:52 -080092 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070093 @Thunk int mWidthGap;
Sunny Goyal4ffec482016-02-09 11:28:52 -080094 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070095 @Thunk int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070096 private int mMaxGap;
Adam Cohen917e3882013-10-31 15:03:35 -070097 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070098 private boolean mIsDragTarget = true;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070099 private boolean mJailContent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700101 // These are temporary variables to prevent having to allocate a new object just to
102 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -0700103 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -0700104 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700105
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700106 private GridOccupancy mOccupied;
107 private GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108
Michael Jurkadee05892010-07-27 10:01:56 -0700109 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -0700110 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700111
Adam Cohenefca0272016-02-24 19:19:06 -0800112 private ArrayList<FolderIcon.PreviewBackground> mFolderBackgrounds = new ArrayList<FolderIcon.PreviewBackground>();
113 FolderIcon.PreviewBackground mFolderLeaveBehind = new FolderIcon.PreviewBackground();
114 Paint mFolderBgPaint = new Paint();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700115
Michael Jurka5f1c5092010-09-03 14:15:02 -0700116 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700117
Tony Wickham33326072016-04-04 15:05:49 -0700118 private static final int BACKGROUND_ACTIVATE_DURATION =
119 FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 120 : 0;
Sunny Goyal2805e632015-05-20 15:35:32 -0700120 private final TransitionDrawable mBackground;
121
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700122 // These values allow a fixed measurement to be set on the CellLayout.
123 private int mFixedWidth = -1;
124 private int mFixedHeight = -1;
125
Michael Jurka33945b22010-12-21 18:19:38 -0800126 // If we're actively dragging something over this screen, mIsDragOverlapping is true
127 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700128
Winson Chung150fbab2010-09-29 17:14:26 -0700129 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700130 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700131 @Thunk Rect[] mDragOutlines = new Rect[4];
132 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700133 private InterruptibleInOutAnimator[] mDragOutlineAnims =
134 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700135
136 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700137 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700138 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700139
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700140 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800141
Sunny Goyal316490e2015-06-02 09:38:28 -0700142 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
143 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700144
145 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700146
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700147 // When a drag operation is in progress, holds the nearest cell to the touch point
148 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Joe Onorato4be866d2010-10-10 11:26:02 -0700150 private boolean mDragging = false;
151
Patrick Dubroyce34a972010-10-19 10:34:32 -0700152 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700153 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700154
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800155 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700156 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800157
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800158 public static final int MODE_SHOW_REORDER_HINT = 0;
159 public static final int MODE_DRAG_OVER = 1;
160 public static final int MODE_ON_DROP = 2;
161 public static final int MODE_ON_DROP_EXTERNAL = 3;
162 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700163 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800164 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
165
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800166 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700167 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohen091440a2015-03-18 14:16:05 -0700168 @Thunk float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700169
Adam Cohen482ed822012-03-02 14:15:13 -0800170 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
171 private Rect mOccupiedRect = new Rect();
172 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700173 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700174 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800175
Sunny Goyal2805e632015-05-20 15:35:32 -0700176 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700177
Michael Jurkaca993832012-06-29 15:17:04 -0700178 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700179
Adam Cohenc9735cf2015-01-23 16:11:55 -0800180 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700181 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800182 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 public CellLayout(Context context) {
185 this(context, null);
186 }
187
188 public CellLayout(Context context, AttributeSet attrs) {
189 this(context, attrs, 0);
190 }
191
192 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
193 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700194
195 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
196 // the user where a dragged item will land when dropped.
197 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800198 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700199 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700200
Adam Cohen2e6da152015-05-06 11:42:25 -0700201 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Winson Chung11a1a532013-09-13 11:14:45 -0700203 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800204 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700205 mWidthGap = mOriginalWidthGap = 0;
206 mHeightGap = mOriginalHeightGap = 0;
207 mMaxGap = Integer.MAX_VALUE;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700208
209 mCountX = grid.inv.numColumns;
210 mCountY = grid.inv.numRows;
211 mOccupied = new GridOccupancy(mCountX, mCountY);
212 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
213
Adam Cohen5b53f292012-03-29 14:30:35 -0700214 mPreviousReorderDirection[0] = INVALID_DIRECTION;
215 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Adam Cohenefca0272016-02-24 19:19:06 -0800217 mFolderLeaveBehind.delegateCellX = -1;
218 mFolderLeaveBehind.delegateCellY = -1;
219
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700222 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700223
Tony Wickhame0c33232016-02-08 11:37:04 -0800224 mBackground = (TransitionDrawable) res.getDrawable(
225 FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? R.drawable.bg_screenpanel
226 : R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700227 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700228 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800229
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800230 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700231 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700232
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700233 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700234 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700235 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700236 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800237 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700238 }
239
240 // When dragging things around the home screens, we show a green outline of
241 // where the item will land. The outlines gradually fade out, leaving a trail
242 // behind the drag path.
243 // Set up all the animations that are used to implement this fading.
244 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700245 final float fromAlphaValue = 0;
246 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700247
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700248 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700249
250 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700251 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100252 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700253 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700254 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700255 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700256 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700257 final Bitmap outline = (Bitmap)anim.getTag();
258
259 // If an animation is started and then stopped very quickly, we can still
260 // get spurious updates we've cleared the tag. Guard against this.
261 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700262 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700263 Object val = animation.getAnimatedValue();
264 Log.d(TAG, "anim " + thisIndex + " update: " + val +
265 ", isStopped " + anim.isStopped());
266 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700267 // Try to prevent it from continuing to run
268 animation.cancel();
269 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700270 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800271 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700272 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700273 }
274 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700275 // The animation holds a reference to the drag outline bitmap as long is it's
276 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700277 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700278 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700279 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700280 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700281 anim.setTag(null);
282 }
283 }
284 });
285 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700286 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700287
Michael Jurkaa52570f2012-03-20 03:18:20 -0700288 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700289 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700290 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700291
Mady Mellorbb835202015-07-15 16:34:34 -0700292 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700293
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700294 mTouchFeedbackView = new ClickShadowView(context);
295 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700296 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700297 }
298
Adam Cohenc9735cf2015-01-23 16:11:55 -0800299 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700300 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800301 mUseTouchHelper = enable;
302 if (!enable) {
303 ViewCompat.setAccessibilityDelegate(this, null);
304 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
305 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
306 setOnClickListener(mLauncher);
307 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700308 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
309 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
310 mTouchHelper = new WorkspaceAccessibilityHelper(this);
311 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
312 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
313 mTouchHelper = new FolderAccessibilityHelper(this);
314 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800315 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
316 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
317 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
318 setOnClickListener(mTouchHelper);
319 }
320
321 // Invalidate the accessibility hierarchy
322 if (getParent() != null) {
323 getParent().notifySubtreeAccessibilityStateChanged(
324 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
325 }
326 }
327
328 @Override
329 public boolean dispatchHoverEvent(MotionEvent event) {
330 // Always attempt to dispatch hover events to accessibility first.
331 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
332 return true;
333 }
334 return super.dispatchHoverEvent(event);
335 }
336
337 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800338 public boolean onInterceptTouchEvent(MotionEvent ev) {
339 if (mUseTouchHelper ||
340 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
341 return true;
342 }
343 return false;
344 }
345
Mady Melloref044dd2015-06-02 15:35:07 -0700346 @Override
347 public boolean onTouchEvent(MotionEvent ev) {
348 boolean handled = super.onTouchEvent(ev);
349 // Stylus button press on a home screen should not switch between overview mode and
350 // the home screen mode, however, once in overview mode stylus button press should be
351 // enabled to allow rearranging the different home screens. So check what mode
352 // the workspace is in, and only perform stylus button presses while in overview mode.
353 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700354 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700355 return true;
356 }
357 return handled;
358 }
359
Chris Craik01f2d7f2013-10-01 14:41:56 -0700360 public void enableHardwareLayer(boolean hasLayer) {
361 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700362 }
363
364 public void buildHardwareLayer() {
365 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700366 }
367
Adam Cohen307fe232012-08-16 17:55:58 -0700368 public float getChildrenScale() {
369 return mIsHotseat ? mHotseatScale : 1.0f;
370 }
371
Winson Chung5f8afe62013-08-12 16:19:28 -0700372 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700373 mFixedCellWidth = mCellWidth = width;
374 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700375 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
376 mCountX, mCountY);
377 }
378
Adam Cohen2801caf2011-05-13 20:57:39 -0700379 public void setGridSize(int x, int y) {
380 mCountX = x;
381 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700382 mOccupied = new GridOccupancy(mCountX, mCountY);
383 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700384 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700385 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700386 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700387 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700388 }
389
Adam Cohen2374abf2013-04-16 14:56:57 -0700390 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
391 public void setInvertIfRtl(boolean invert) {
392 mShortcutsAndWidgets.setInvertIfRtl(invert);
393 }
394
Adam Cohen917e3882013-10-31 15:03:35 -0700395 public void setDropPending(boolean pending) {
396 mDropPending = pending;
397 }
398
399 public boolean isDropPending() {
400 return mDropPending;
401 }
402
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700403 @Override
404 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700405 if (icon == null || background == null) {
406 mTouchFeedbackView.setBitmap(null);
407 mTouchFeedbackView.animate().cancel();
408 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700409 if (mTouchFeedbackView.setBitmap(background)) {
Winsonbe9798b2016-07-20 12:55:49 -0700410 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
411 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700412 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700413 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800414 }
415 }
416
Adam Cohenc50438c2014-08-19 17:43:05 -0700417 void disableDragTarget() {
418 mIsDragTarget = false;
419 }
420
Tony Wickham0f97b782015-12-02 17:55:07 -0800421 public boolean isDragTarget() {
422 return mIsDragTarget;
423 }
424
Adam Cohenc50438c2014-08-19 17:43:05 -0700425 void setIsDragOverlapping(boolean isDragOverlapping) {
426 if (mIsDragOverlapping != isDragOverlapping) {
427 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700428 if (mIsDragOverlapping) {
429 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
430 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700431 if (mBackgroundAlpha > 0f) {
432 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
433 } else {
434 mBackground.resetTransition();
435 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700436 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700437 invalidate();
438 }
439 }
440
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700441 public void disableJailContent() {
442 mJailContent = false;
443 }
444
445 @Override
446 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
447 if (mJailContent) {
448 ParcelableSparseArray jail = getJailedArray(container);
449 super.dispatchSaveInstanceState(jail);
450 container.put(R.id.cell_layout_jail_id, jail);
451 } else {
452 super.dispatchSaveInstanceState(container);
453 }
454 }
455
456 @Override
457 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
458 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
459 }
460
461 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
462 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
463 return parcelable instanceof ParcelableSparseArray ?
464 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
465 }
466
Tony Wickham0f97b782015-12-02 17:55:07 -0800467 public boolean getIsDragOverlapping() {
468 return mIsDragOverlapping;
469 }
470
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700471 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700472 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700473 if (!mIsDragTarget) {
474 return;
475 }
476
Michael Jurka3e7c7632010-10-02 16:01:03 -0700477 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
478 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
479 // When we're small, we are either drawn normally or in the "accepts drops" state (during
480 // a drag). However, we also drag the mini hover background *over* one of those two
481 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700482 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700483 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700484 }
Romain Guya6abce82009-11-10 02:54:41 -0800485
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700486 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700487 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700488 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700489 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700490 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700491 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700492 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700493 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700494 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800495
Adam Cohen482ed822012-03-02 14:15:13 -0800496 if (DEBUG_VISUALIZE_OCCUPIED) {
497 int[] pt = new int[2];
498 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700499 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800500 for (int i = 0; i < mCountX; i++) {
501 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700502 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800503 cellToPoint(i, j, pt);
504 canvas.save();
505 canvas.translate(pt[0], pt[1]);
506 cd.draw(canvas);
507 canvas.restore();
508 }
509 }
510 }
511 }
512
Adam Cohenefca0272016-02-24 19:19:06 -0800513 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
514 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
515 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
516 canvas.save();
517 canvas.translate(mTempLocation[0], mTempLocation[1]);
518 bg.drawBackground(canvas, mFolderBgPaint);
Adam Cohenf172b742016-03-30 19:28:34 -0700519 if (!bg.isClipping) {
520 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
521 }
Adam Cohenefca0272016-02-24 19:19:06 -0800522 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700523 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700524
Adam Cohenefca0272016-02-24 19:19:06 -0800525 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
526 cellToPoint(mFolderLeaveBehind.delegateCellX,
527 mFolderLeaveBehind.delegateCellY, mTempLocation);
528 canvas.save();
529 canvas.translate(mTempLocation[0], mTempLocation[1]);
530 mFolderLeaveBehind.drawLeaveBehind(canvas, mFolderBgPaint);
531 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700532 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700533 }
534
Adam Cohenefca0272016-02-24 19:19:06 -0800535 @Override
536 protected void dispatchDraw(Canvas canvas) {
537 super.dispatchDraw(canvas);
538
539 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
540 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700541 if (bg.isClipping) {
542 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
543 canvas.save();
544 canvas.translate(mTempLocation[0], mTempLocation[1]);
545 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
546 canvas.restore();
547 }
Adam Cohenefca0272016-02-24 19:19:06 -0800548 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700549 }
550
Adam Cohenefca0272016-02-24 19:19:06 -0800551 public void addFolderBackground(FolderIcon.PreviewBackground bg) {
552 mFolderBackgrounds.add(bg);
553 }
554 public void removeFolderBackground(FolderIcon.PreviewBackground bg) {
555 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700556 }
557
Adam Cohenc51934b2011-07-26 21:07:43 -0700558 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800559
560 DeviceProfile grid = mLauncher.getDeviceProfile();
561 View child = getChildAt(x, y);
562
563 mFolderLeaveBehind.setup(getResources().getDisplayMetrics(), grid, null,
564 child.getMeasuredWidth(), child.getPaddingTop());
565
566 mFolderLeaveBehind.delegateCellX = x;
567 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700568 invalidate();
569 }
570
571 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800572 mFolderLeaveBehind.delegateCellX = -1;
573 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700574 invalidate();
575 }
576
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700577 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700578 public boolean shouldDelayChildPressedState() {
579 return false;
580 }
581
Adam Cohen1462de32012-07-24 22:34:36 -0700582 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700583 try {
584 dispatchRestoreInstanceState(states);
585 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700586 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700587 throw ex;
588 }
589 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
590 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
591 }
Adam Cohen1462de32012-07-24 22:34:36 -0700592 }
593
Michael Jurkae6235dd2011-10-04 15:02:05 -0700594 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700595 public void cancelLongPress() {
596 super.cancelLongPress();
597
598 // Cancel long press for all children
599 final int count = getChildCount();
600 for (int i = 0; i < count; i++) {
601 final View child = getChildAt(i);
602 child.cancelLongPress();
603 }
604 }
605
Michael Jurkadee05892010-07-27 10:01:56 -0700606 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
607 mInterceptTouchListener = listener;
608 }
609
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800610 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700611 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 }
613
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800614 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700615 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 }
617
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800618 public void setIsHotseat(boolean isHotseat) {
619 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700620 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800621 }
622
Sunny Goyale9b651e2015-04-24 11:44:51 -0700623 public boolean isHotseat() {
624 return mIsHotseat;
625 }
626
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800627 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700628 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700629 final LayoutParams lp = params;
630
Andrew Flynnde38e422012-05-08 11:22:15 -0700631 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800632 if (child instanceof BubbleTextView) {
633 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700634 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800635 }
636
Adam Cohen307fe232012-08-16 17:55:58 -0700637 child.setScaleX(getChildrenScale());
638 child.setScaleY(getChildrenScale());
639
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 // Generate an id for each view, this assumes we have at most 256x256 cells
641 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700642 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 // If the horizontal or vertical span is set to -1, it is taken to
644 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700645 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
646 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647
Winson Chungaafa03c2010-06-11 17:34:16 -0700648 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700649 if (LOGD) {
650 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
651 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700652 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700653
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700654 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700655
Winson Chungaafa03c2010-06-11 17:34:16 -0700656 return true;
657 }
658 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700663 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700664 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700665 }
666
667 @Override
668 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700669 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700670 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700671 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700672 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700673 }
674
675 @Override
676 public void removeView(View view) {
677 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700678 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700679 }
680
681 @Override
682 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
684 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700685 }
686
687 @Override
688 public void removeViewInLayout(View view) {
689 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700690 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700691 }
692
693 @Override
694 public void removeViews(int start, int count) {
695 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700696 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700697 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700698 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700699 }
700
701 @Override
702 public void removeViewsInLayout(int start, int count) {
703 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700704 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700705 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700706 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800707 }
708
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700709 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700710 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 * @param x X coordinate of the point
712 * @param y Y coordinate of the point
713 * @param result Array of 2 ints to hold the x and y coordinate of the cell
714 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700715 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700716 final int hStartPadding = getPaddingLeft();
717 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718
719 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
720 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
721
Adam Cohend22015c2010-07-26 22:02:18 -0700722 final int xAxis = mCountX;
723 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724
725 if (result[0] < 0) result[0] = 0;
726 if (result[0] >= xAxis) result[0] = xAxis - 1;
727 if (result[1] < 0) result[1] = 0;
728 if (result[1] >= yAxis) result[1] = yAxis - 1;
729 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 /**
732 * Given a point, return the cell that most closely encloses that point
733 * @param x X coordinate of the point
734 * @param y Y coordinate of the point
735 * @param result Array of 2 ints to hold the x and y coordinate of the cell
736 */
737 void pointToCellRounded(int x, int y, int[] result) {
738 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
739 }
740
741 /**
742 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700743 *
744 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700746 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 * @param result Array of 2 ints to hold the x and y coordinate of the point
748 */
749 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700750 final int hStartPadding = getPaddingLeft();
751 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752
753 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
754 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
755 }
756
Adam Cohene3e27a82011-04-15 12:07:39 -0700757 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800758 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700759 *
760 * @param cellX X coordinate of the cell
761 * @param cellY Y coordinate of the cell
762 *
763 * @param result Array of 2 ints to hold the x and y coordinate of the point
764 */
765 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700766 regionToCenterPoint(cellX, cellY, 1, 1, result);
767 }
768
769 /**
770 * Given a cell coordinate and span return the point that represents the center of the regio
771 *
772 * @param cellX X coordinate of the cell
773 * @param cellY Y coordinate of the cell
774 *
775 * @param result Array of 2 ints to hold the x and y coordinate of the point
776 */
777 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700778 final int hStartPadding = getPaddingLeft();
779 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700780 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
781 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
782 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
783 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700784 }
785
Adam Cohen19f37922012-03-21 11:59:11 -0700786 /**
787 * Given a cell coordinate and span fills out a corresponding pixel rect
788 *
789 * @param cellX X coordinate of the cell
790 * @param cellY Y coordinate of the cell
791 * @param result Rect in which to write the result
792 */
793 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
794 final int hStartPadding = getPaddingLeft();
795 final int vStartPadding = getPaddingTop();
796 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
797 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
798 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
799 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
800 }
801
Adam Cohen482ed822012-03-02 14:15:13 -0800802 public float getDistanceFromCell(float x, float y, int[] cell) {
803 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700804 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800805 }
806
Adam Cohenf9c184a2016-01-15 16:47:43 -0800807 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800808 return mCellWidth;
809 }
810
811 int getCellHeight() {
812 return mCellHeight;
813 }
814
Adam Cohend4844c32011-02-18 19:25:06 -0800815 int getWidthGap() {
816 return mWidthGap;
817 }
818
819 int getHeightGap() {
820 return mHeightGap;
821 }
822
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700823 public void setFixedSize(int width, int height) {
824 mFixedWidth = width;
825 mFixedHeight = height;
826 }
827
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 @Override
829 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700832 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
833 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700834 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
835 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700836 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700837 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
838 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700839 if (cw != mCellWidth || ch != mCellHeight) {
840 mCellWidth = cw;
841 mCellHeight = ch;
842 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
843 mHeightGap, mCountX, mCountY);
844 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700845 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700846
Winson Chung2d75f122013-09-23 16:53:31 -0700847 int newWidth = childWidthSize;
848 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700849 if (mFixedWidth > 0 && mFixedHeight > 0) {
850 newWidth = mFixedWidth;
851 newHeight = mFixedHeight;
852 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
854 }
855
Adam Cohend22015c2010-07-26 22:02:18 -0700856 int numWidthGaps = mCountX - 1;
857 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858
Adam Cohen234c4cd2011-07-17 21:03:04 -0700859 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700860 int hSpace = childWidthSize;
861 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700862 int hFreeSpace = hSpace - (mCountX * mCellWidth);
863 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700864 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
865 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700866 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
867 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700868 } else {
869 mWidthGap = mOriginalWidthGap;
870 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700871 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700872
873 // Make the feedback view large enough to hold the blur bitmap.
874 mTouchFeedbackView.measure(
875 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
876 MeasureSpec.EXACTLY),
877 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
878 MeasureSpec.EXACTLY));
879
880 mShortcutsAndWidgets.measure(
881 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
882 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
883
884 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
885 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700886 if (mFixedWidth > 0 && mFixedHeight > 0) {
887 setMeasuredDimension(maxWidth, maxHeight);
888 } else {
889 setMeasuredDimension(widthSize, heightSize);
890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 }
892
893 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700894 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800895 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
896 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
897 int left = getPaddingLeft();
898 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800899 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800900 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700901 int right = r - l - getPaddingRight();
902 if (!isFullscreen) {
903 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
904 }
905
Winson Chung38848ca2013-10-08 12:03:44 -0700906 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700907 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700908
909 mTouchFeedbackView.layout(left, top,
910 left + mTouchFeedbackView.getMeasuredWidth(),
911 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700912 mShortcutsAndWidgets.layout(left, top, right, bottom);
913
914 // Expand the background drawing bounds by the padding baked into the background drawable
915 mBackground.getPadding(mTempRect);
916 mBackground.setBounds(
917 left - mTempRect.left,
918 top - mTempRect.top,
919 right + mTempRect.right,
920 bottom + mTempRect.bottom);
921 }
922
Tony Wickhama501d492015-11-03 18:05:01 -0800923 /**
924 * Returns the amount of space left over after subtracting padding and cells. This space will be
925 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
926 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
927 */
928 public int getUnusedHorizontalSpace() {
929 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
930 }
931
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 @Override
933 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700934 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 }
936
937 @Override
938 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700939 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
Michael Jurka5f1c5092010-09-03 14:15:02 -0700942 public float getBackgroundAlpha() {
943 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700944 }
945
Michael Jurka5f1c5092010-09-03 14:15:02 -0700946 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800947 if (mBackgroundAlpha != alpha) {
948 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700949 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800950 }
Michael Jurkadee05892010-07-27 10:01:56 -0700951 }
952
Sunny Goyal2805e632015-05-20 15:35:32 -0700953 @Override
954 protected boolean verifyDrawable(Drawable who) {
955 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
956 }
957
Michael Jurkaa52570f2012-03-20 03:18:20 -0700958 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700959 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700960 }
961
Michael Jurkaa52570f2012-03-20 03:18:20 -0700962 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700963 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700964 }
965
Patrick Dubroy440c3602010-07-13 17:50:32 -0700966 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700967 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700968 }
969
Adam Cohen76fc0852011-06-17 13:26:23 -0700970 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800971 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700972 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800973
Adam Cohen19f37922012-03-21 11:59:11 -0700974 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700975 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
976 final ItemInfo info = (ItemInfo) child.getTag();
977
978 // We cancel any existing animations
979 if (mReorderAnimators.containsKey(lp)) {
980 mReorderAnimators.get(lp).cancel();
981 mReorderAnimators.remove(lp);
982 }
983
Adam Cohen482ed822012-03-02 14:15:13 -0800984 final int oldX = lp.x;
985 final int oldY = lp.y;
986 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700987 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
988 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
989 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800990 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700991 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800992 if (permanent) {
993 lp.cellX = info.cellX = cellX;
994 lp.cellY = info.cellY = cellY;
995 } else {
996 lp.tmpCellX = cellX;
997 lp.tmpCellY = cellY;
998 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700999 clc.setupLp(lp);
1000 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001001 final int newX = lp.x;
1002 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001003
Adam Cohen76fc0852011-06-17 13:26:23 -07001004 lp.x = oldX;
1005 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001006
Adam Cohen482ed822012-03-02 14:15:13 -08001007 // Exit early if we're not actually moving the view
1008 if (oldX == newX && oldY == newY) {
1009 lp.isLockedToGrid = true;
1010 return true;
1011 }
1012
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001013 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001014 va.setDuration(duration);
1015 mReorderAnimators.put(lp, va);
1016
1017 va.addUpdateListener(new AnimatorUpdateListener() {
1018 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001019 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001020 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001021 lp.x = (int) ((1 - r) * oldX + r * newX);
1022 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001023 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001024 }
1025 });
Adam Cohen482ed822012-03-02 14:15:13 -08001026 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001027 boolean cancelled = false;
1028 public void onAnimationEnd(Animator animation) {
1029 // If the animation was cancelled, it means that another animation
1030 // has interrupted this one, and we don't want to lock the item into
1031 // place just yet.
1032 if (!cancelled) {
1033 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001034 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001035 }
1036 if (mReorderAnimators.containsKey(lp)) {
1037 mReorderAnimators.remove(lp);
1038 }
1039 }
1040 public void onAnimationCancel(Animator animation) {
1041 cancelled = true;
1042 }
1043 });
Adam Cohen482ed822012-03-02 14:15:13 -08001044 va.setStartDelay(delay);
1045 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001046 return true;
1047 }
1048 return false;
1049 }
1050
Sunny Goyal06e21a22016-08-11 16:02:02 -07001051 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
1052 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001053 final int oldDragCellX = mDragCell[0];
1054 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001055
Sunny Goyal06e21a22016-08-11 16:02:02 -07001056 if (outlineProvider == null || outlineProvider.gerenatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001057 return;
1058 }
1059
Sunny Goyal06e21a22016-08-11 16:02:02 -07001060 Bitmap dragOutline = outlineProvider.gerenatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -08001061 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001062 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1063 Rect dragRegion = dragObject.dragView.getDragRegion();
1064
Adam Cohen482ed822012-03-02 14:15:13 -08001065 mDragCell[0] = cellX;
1066 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001067
Joe Onorato4be866d2010-10-10 11:26:02 -07001068 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001069 mDragOutlineAnims[oldIndex].animateOut();
1070 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001071 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001072
Adam Cohend41fbf52012-02-16 23:53:59 -08001073 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001074 cellToRect(cellX, cellY, spanX, spanY, r);
Sunny Goyal106bf642015-07-16 12:18:06 -07001075 } else {
1076 // Find the top left corner of the rect the object will occupy
1077 final int[] topLeft = mTmpPoint;
1078 cellToPoint(cellX, cellY, topLeft);
1079
1080 int left = topLeft[0];
1081 int top = topLeft[1];
1082
1083 if (v != null && dragOffset == null) {
1084 // When drawing the drag outline, it did not account for margin offsets
1085 // added by the view's parent.
1086 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1087 left += lp.leftMargin;
1088 top += lp.topMargin;
1089
1090 // Offsets due to the size difference between the View and the dragOutline.
1091 // There is a size difference to account for the outer blur, which may lie
1092 // outside the bounds of the view.
1093 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1094 // We center about the x axis
1095 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1096 - dragOutline.getWidth()) / 2;
1097 } else {
1098 if (dragOffset != null && dragRegion != null) {
1099 // Center the drag region *horizontally* in the cell and apply a drag
1100 // outline offset
1101 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1102 - dragRegion.width()) / 2;
1103 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1104 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1105 top += dragOffset.y + cellPaddingY;
1106 } else {
1107 // Center the drag outline in the cell
1108 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1109 - dragOutline.getWidth()) / 2;
1110 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1111 - dragOutline.getHeight()) / 2;
1112 }
1113 }
1114 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001115 }
Winson Chung150fbab2010-09-29 17:14:26 -07001116
Sunny Goyal106bf642015-07-16 12:18:06 -07001117 Utilities.scaleRectAboutCenter(r, getChildrenScale());
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001118 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1119 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001120
1121 if (dragObject.stateAnnouncer != null) {
1122 String msg;
1123 if (isHotseat()) {
1124 msg = getContext().getString(R.string.move_to_hotseat_position,
1125 Math.max(cellX, cellY) + 1);
1126 } else {
1127 msg = getContext().getString(R.string.move_to_empty_cell,
1128 cellY + 1, cellX + 1);
1129 }
1130 dragObject.stateAnnouncer.announce(msg);
1131 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001132 }
1133 }
1134
Adam Cohene0310962011-04-18 16:15:31 -07001135 public void clearDragOutlines() {
1136 final int oldIndex = mDragOutlineCurrent;
1137 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001138 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001139 }
1140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001142 * Find a vacant area that will fit the given bounds nearest the requested
1143 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001144 *
Romain Guy51afc022009-05-04 18:03:43 -07001145 * @param pixelX The X location at which you want to search for a vacant area.
1146 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001147 * @param minSpanX The minimum horizontal span required
1148 * @param minSpanY The minimum vertical span required
1149 * @param spanX Horizontal span of the object.
1150 * @param spanY Vertical span of the object.
1151 * @param result Array in which to place the result, or null (in which case a new array will
1152 * be allocated)
1153 * @return The X, Y cell of a vacant area that can contain this object,
1154 * nearest the requested location.
1155 */
1156 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1157 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001158 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001159 result, resultSpan);
1160 }
1161
Adam Cohend41fbf52012-02-16 23:53:59 -08001162 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1163 private void lazyInitTempRectStack() {
1164 if (mTempRectStack.isEmpty()) {
1165 for (int i = 0; i < mCountX * mCountY; i++) {
1166 mTempRectStack.push(new Rect());
1167 }
1168 }
1169 }
Adam Cohen482ed822012-03-02 14:15:13 -08001170
Adam Cohend41fbf52012-02-16 23:53:59 -08001171 private void recycleTempRects(Stack<Rect> used) {
1172 while (!used.isEmpty()) {
1173 mTempRectStack.push(used.pop());
1174 }
1175 }
1176
1177 /**
1178 * Find a vacant area that will fit the given bounds nearest the requested
1179 * cell location. Uses Euclidean distance to score multiple vacant areas.
1180 *
1181 * @param pixelX The X location at which you want to search for a vacant area.
1182 * @param pixelY The Y location at which you want to search for a vacant area.
1183 * @param minSpanX The minimum horizontal span required
1184 * @param minSpanY The minimum vertical span required
1185 * @param spanX Horizontal span of the object.
1186 * @param spanY Vertical span of the object.
1187 * @param ignoreOccupied If true, the result can be an occupied cell
1188 * @param result Array in which to place the result, or null (in which case a new array will
1189 * be allocated)
1190 * @return The X, Y cell of a vacant area that can contain this object,
1191 * nearest the requested location.
1192 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001193 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1194 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001195 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001196
Adam Cohene3e27a82011-04-15 12:07:39 -07001197 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1198 // to the center of the item, but we are searching based on the top-left cell, so
1199 // we translate the point over to correspond to the top-left.
1200 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1201 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1202
Jeff Sharkey70864282009-04-07 21:08:40 -07001203 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001204 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001205 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001206 final Rect bestRect = new Rect(-1, -1, -1, -1);
1207 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001208
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001209 final int countX = mCountX;
1210 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001211
Adam Cohend41fbf52012-02-16 23:53:59 -08001212 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1213 spanX < minSpanX || spanY < minSpanY) {
1214 return bestXY;
1215 }
1216
1217 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001218 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001219 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1220 int ySize = -1;
1221 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001222 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001223 // First, let's see if this thing fits anywhere
1224 for (int i = 0; i < minSpanX; i++) {
1225 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001226 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001227 continue inner;
1228 }
Michael Jurkac28de512010-08-13 11:27:44 -07001229 }
1230 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001231 xSize = minSpanX;
1232 ySize = minSpanY;
1233
1234 // We know that the item will fit at _some_ acceptable size, now let's see
1235 // how big we can make it. We'll alternate between incrementing x and y spans
1236 // until we hit a limit.
1237 boolean incX = true;
1238 boolean hitMaxX = xSize >= spanX;
1239 boolean hitMaxY = ySize >= spanY;
1240 while (!(hitMaxX && hitMaxY)) {
1241 if (incX && !hitMaxX) {
1242 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001243 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001244 // We can't move out horizontally
1245 hitMaxX = true;
1246 }
1247 }
1248 if (!hitMaxX) {
1249 xSize++;
1250 }
1251 } else if (!hitMaxY) {
1252 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001253 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001254 // We can't move out vertically
1255 hitMaxY = true;
1256 }
1257 }
1258 if (!hitMaxY) {
1259 ySize++;
1260 }
1261 }
1262 hitMaxX |= xSize >= spanX;
1263 hitMaxY |= ySize >= spanY;
1264 incX = !incX;
1265 }
1266 incX = true;
1267 hitMaxX = xSize >= spanX;
1268 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001269 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001270 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001271 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272
Adam Cohend41fbf52012-02-16 23:53:59 -08001273 // We verify that the current rect is not a sub-rect of any of our previous
1274 // candidates. In this case, the current rect is disqualified in favour of the
1275 // containing rect.
1276 Rect currentRect = mTempRectStack.pop();
1277 currentRect.set(x, y, x + xSize, y + ySize);
1278 boolean contained = false;
1279 for (Rect r : validRegions) {
1280 if (r.contains(currentRect)) {
1281 contained = true;
1282 break;
1283 }
1284 }
1285 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001286 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001287
Adam Cohend41fbf52012-02-16 23:53:59 -08001288 if ((distance <= bestDistance && !contained) ||
1289 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001290 bestDistance = distance;
1291 bestXY[0] = x;
1292 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001293 if (resultSpan != null) {
1294 resultSpan[0] = xSize;
1295 resultSpan[1] = ySize;
1296 }
1297 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001298 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 }
1300 }
1301
Adam Cohenc0dcf592011-06-01 15:30:43 -07001302 // Return -1, -1 if no suitable location found
1303 if (bestDistance == Double.MAX_VALUE) {
1304 bestXY[0] = -1;
1305 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001306 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001307 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001308 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001310
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001311 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001312 * Find a vacant area that will fit the given bounds nearest the requested
1313 * cell location, and will also weigh in a suggested direction vector of the
1314 * desired location. This method computers distance based on unit grid distances,
1315 * not pixel distances.
1316 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001317 * @param cellX The X cell nearest to which you want to search for a vacant area.
1318 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001319 * @param spanX Horizontal span of the object.
1320 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001321 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001322 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001323 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001324 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001325 * @param result Array in which to place the result, or null (in which case a new array will
1326 * be allocated)
1327 * @return The X, Y cell of a vacant area that can contain this object,
1328 * nearest the requested location.
1329 */
1330 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001331 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001332 // Keep track of best-scoring drop area
1333 final int[] bestXY = result != null ? result : new int[2];
1334 float bestDistance = Float.MAX_VALUE;
1335 int bestDirectionScore = Integer.MIN_VALUE;
1336
1337 final int countX = mCountX;
1338 final int countY = mCountY;
1339
1340 for (int y = 0; y < countY - (spanY - 1); y++) {
1341 inner:
1342 for (int x = 0; x < countX - (spanX - 1); x++) {
1343 // First, let's see if this thing fits anywhere
1344 for (int i = 0; i < spanX; i++) {
1345 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001346 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001347 continue inner;
1348 }
1349 }
1350 }
1351
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001352 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001353 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001354 computeDirectionVector(x - cellX, y - cellY, curDirection);
1355 // The direction score is just the dot product of the two candidate direction
1356 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001357 int curDirectionScore = direction[0] * curDirection[0] +
1358 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001359 boolean exactDirectionOnly = false;
1360 boolean directionMatches = direction[0] == curDirection[0] &&
1361 direction[0] == curDirection[0];
1362 if ((directionMatches || !exactDirectionOnly) &&
1363 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001364 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1365 bestDistance = distance;
1366 bestDirectionScore = curDirectionScore;
1367 bestXY[0] = x;
1368 bestXY[1] = y;
1369 }
1370 }
1371 }
1372
1373 // Return -1, -1 if no suitable location found
1374 if (bestDistance == Float.MAX_VALUE) {
1375 bestXY[0] = -1;
1376 bestXY[1] = -1;
1377 }
1378 return bestXY;
1379 }
1380
1381 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001382 int[] direction, ItemConfiguration currentState) {
1383 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001384 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001385 mTmpOccupied.markCells(c, false);
1386 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001387
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001388 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1389 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001390
1391 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001392 c.cellX = mTempLocation[0];
1393 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001394 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001395 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001396 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001397 return success;
1398 }
1399
Adam Cohenf3900c22012-11-16 18:28:11 -08001400 /**
1401 * This helper class defines a cluster of views. It helps with defining complex edges
1402 * of the cluster and determining how those edges interact with other views. The edges
1403 * essentially define a fine-grained boundary around the cluster of views -- like a more
1404 * precise version of a bounding box.
1405 */
1406 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001407 final static int LEFT = 1 << 0;
1408 final static int TOP = 1 << 1;
1409 final static int RIGHT = 1 << 2;
1410 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001411
Adam Cohenf3900c22012-11-16 18:28:11 -08001412 ArrayList<View> views;
1413 ItemConfiguration config;
1414 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001415
Adam Cohenf3900c22012-11-16 18:28:11 -08001416 int[] leftEdge = new int[mCountY];
1417 int[] rightEdge = new int[mCountY];
1418 int[] topEdge = new int[mCountX];
1419 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001420 int dirtyEdges;
1421 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001422
1423 @SuppressWarnings("unchecked")
1424 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1425 this.views = (ArrayList<View>) views.clone();
1426 this.config = config;
1427 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001428 }
1429
Adam Cohenf3900c22012-11-16 18:28:11 -08001430 void resetEdges() {
1431 for (int i = 0; i < mCountX; i++) {
1432 topEdge[i] = -1;
1433 bottomEdge[i] = -1;
1434 }
1435 for (int i = 0; i < mCountY; i++) {
1436 leftEdge[i] = -1;
1437 rightEdge[i] = -1;
1438 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001439 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001440 boundingRectDirty = true;
1441 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001442
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001443 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001444 int count = views.size();
1445 for (int i = 0; i < count; i++) {
1446 CellAndSpan cs = config.map.get(views.get(i));
1447 switch (which) {
1448 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001449 int left = cs.cellX;
1450 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001451 if (left < leftEdge[j] || leftEdge[j] < 0) {
1452 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001453 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001454 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001455 break;
1456 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001457 int right = cs.cellX + cs.spanX;
1458 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001459 if (right > rightEdge[j]) {
1460 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001461 }
1462 }
1463 break;
1464 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001465 int top = cs.cellY;
1466 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001467 if (top < topEdge[j] || topEdge[j] < 0) {
1468 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001469 }
1470 }
1471 break;
1472 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001473 int bottom = cs.cellY + cs.spanY;
1474 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001475 if (bottom > bottomEdge[j]) {
1476 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001477 }
1478 }
1479 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001480 }
1481 }
1482 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001483
1484 boolean isViewTouchingEdge(View v, int whichEdge) {
1485 CellAndSpan cs = config.map.get(v);
1486
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001487 if ((dirtyEdges & whichEdge) == whichEdge) {
1488 computeEdge(whichEdge);
1489 dirtyEdges &= ~whichEdge;
1490 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001491
1492 switch (whichEdge) {
1493 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001494 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1495 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001496 return true;
1497 }
1498 }
1499 break;
1500 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001501 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1502 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001503 return true;
1504 }
1505 }
1506 break;
1507 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001508 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1509 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001510 return true;
1511 }
1512 }
1513 break;
1514 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001515 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1516 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001517 return true;
1518 }
1519 }
1520 break;
1521 }
1522 return false;
1523 }
1524
1525 void shift(int whichEdge, int delta) {
1526 for (View v: views) {
1527 CellAndSpan c = config.map.get(v);
1528 switch (whichEdge) {
1529 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001530 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001531 break;
1532 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001533 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001534 break;
1535 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001536 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001537 break;
1538 case BOTTOM:
1539 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001540 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001541 break;
1542 }
1543 }
1544 resetEdges();
1545 }
1546
1547 public void addView(View v) {
1548 views.add(v);
1549 resetEdges();
1550 }
1551
1552 public Rect getBoundingRect() {
1553 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001554 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001555 }
1556 return boundingRect;
1557 }
1558
Adam Cohenf3900c22012-11-16 18:28:11 -08001559 PositionComparator comparator = new PositionComparator();
1560 class PositionComparator implements Comparator<View> {
1561 int whichEdge = 0;
1562 public int compare(View left, View right) {
1563 CellAndSpan l = config.map.get(left);
1564 CellAndSpan r = config.map.get(right);
1565 switch (whichEdge) {
1566 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001567 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001568 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001569 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001570 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001571 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001572 case BOTTOM:
1573 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001574 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001575 }
1576 }
1577 }
1578
1579 public void sortConfigurationForEdgePush(int edge) {
1580 comparator.whichEdge = edge;
1581 Collections.sort(config.sortedViews, comparator);
1582 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001583 }
1584
Adam Cohenf3900c22012-11-16 18:28:11 -08001585 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1586 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001587
Adam Cohenf3900c22012-11-16 18:28:11 -08001588 ViewCluster cluster = new ViewCluster(views, currentState);
1589 Rect clusterRect = cluster.getBoundingRect();
1590 int whichEdge;
1591 int pushDistance;
1592 boolean fail = false;
1593
1594 // Determine the edge of the cluster that will be leading the push and how far
1595 // the cluster must be shifted.
1596 if (direction[0] < 0) {
1597 whichEdge = ViewCluster.LEFT;
1598 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001599 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001600 whichEdge = ViewCluster.RIGHT;
1601 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1602 } else if (direction[1] < 0) {
1603 whichEdge = ViewCluster.TOP;
1604 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1605 } else {
1606 whichEdge = ViewCluster.BOTTOM;
1607 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001608 }
1609
Adam Cohenf3900c22012-11-16 18:28:11 -08001610 // Break early for invalid push distance.
1611 if (pushDistance <= 0) {
1612 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001613 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001614
1615 // Mark the occupied state as false for the group of views we want to move.
1616 for (View v: views) {
1617 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001618 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001619 }
1620
1621 // We save the current configuration -- if we fail to find a solution we will revert
1622 // to the initial state. The process of finding a solution modifies the configuration
1623 // in place, hence the need for revert in the failure case.
1624 currentState.save();
1625
1626 // The pushing algorithm is simplified by considering the views in the order in which
1627 // they would be pushed by the cluster. For example, if the cluster is leading with its
1628 // left edge, we consider sort the views by their right edge, from right to left.
1629 cluster.sortConfigurationForEdgePush(whichEdge);
1630
1631 while (pushDistance > 0 && !fail) {
1632 for (View v: currentState.sortedViews) {
1633 // For each view that isn't in the cluster, we see if the leading edge of the
1634 // cluster is contacting the edge of that view. If so, we add that view to the
1635 // cluster.
1636 if (!cluster.views.contains(v) && v != dragView) {
1637 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1638 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1639 if (!lp.canReorder) {
1640 // The push solution includes the all apps button, this is not viable.
1641 fail = true;
1642 break;
1643 }
1644 cluster.addView(v);
1645 CellAndSpan c = currentState.map.get(v);
1646
1647 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001648 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001649 }
1650 }
1651 }
1652 pushDistance--;
1653
1654 // The cluster has been completed, now we move the whole thing over in the appropriate
1655 // direction.
1656 cluster.shift(whichEdge, 1);
1657 }
1658
1659 boolean foundSolution = false;
1660 clusterRect = cluster.getBoundingRect();
1661
1662 // Due to the nature of the algorithm, the only check required to verify a valid solution
1663 // is to ensure that completed shifted cluster lies completely within the cell layout.
1664 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1665 clusterRect.bottom <= mCountY) {
1666 foundSolution = true;
1667 } else {
1668 currentState.restore();
1669 }
1670
1671 // In either case, we set the occupied array as marked for the location of the views
1672 for (View v: cluster.views) {
1673 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001674 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001675 }
1676
1677 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001678 }
1679
Adam Cohen482ed822012-03-02 14:15:13 -08001680 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001681 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001682 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001683
Adam Cohen8baab352012-03-20 17:39:21 -07001684 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001685 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001686 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001687 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001688
Adam Cohen8baab352012-03-20 17:39:21 -07001689 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001690 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001691 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001692 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001693 }
1694
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001695 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001696 int top = boundingRect.top;
1697 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001698 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001699 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001700 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001701 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001702 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001703 }
1704
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001705 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001706
Adam Cohenf3900c22012-11-16 18:28:11 -08001707 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001708 boundingRect.height(), direction,
1709 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001710
Adam Cohen8baab352012-03-20 17:39:21 -07001711 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001712 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001713 int deltaX = mTempLocation[0] - boundingRect.left;
1714 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001715 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001716 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001717 c.cellX += deltaX;
1718 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001719 }
1720 success = true;
1721 }
Adam Cohen8baab352012-03-20 17:39:21 -07001722
1723 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001724 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001725 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001726 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001727 }
1728 return success;
1729 }
1730
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001731 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1732 // to push items in each of the cardinal directions, in an order based on the direction vector
1733 // passed.
1734 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1735 int[] direction, View ignoreView, ItemConfiguration solution) {
1736 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001737 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001738 // separately in each of the components.
1739 int temp = direction[1];
1740 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001741
1742 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001743 ignoreView, solution)) {
1744 return true;
1745 }
1746 direction[1] = temp;
1747 temp = direction[0];
1748 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001749
1750 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001751 ignoreView, solution)) {
1752 return true;
1753 }
1754 // Revert the direction
1755 direction[0] = temp;
1756
1757 // Now we try pushing in each component of the opposite direction
1758 direction[0] *= -1;
1759 direction[1] *= -1;
1760 temp = direction[1];
1761 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001762 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001763 ignoreView, solution)) {
1764 return true;
1765 }
1766
1767 direction[1] = temp;
1768 temp = direction[0];
1769 direction[0] = 0;
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 }
1774 // revert the direction
1775 direction[0] = temp;
1776 direction[0] *= -1;
1777 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001778
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001779 } else {
1780 // If the direction vector has a single non-zero component, we push first in the
1781 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001782 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001783 ignoreView, solution)) {
1784 return true;
1785 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001786 // Then we try the opposite direction
1787 direction[0] *= -1;
1788 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001789 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001790 ignoreView, solution)) {
1791 return true;
1792 }
1793 // Switch the direction back
1794 direction[0] *= -1;
1795 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001796
1797 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001798 // to find a solution by pushing along the perpendicular axis.
1799
1800 // Swap the components
1801 int temp = direction[1];
1802 direction[1] = direction[0];
1803 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001804 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001805 ignoreView, solution)) {
1806 return true;
1807 }
1808
1809 // Then we try the opposite direction
1810 direction[0] *= -1;
1811 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001812 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001813 ignoreView, solution)) {
1814 return true;
1815 }
1816 // Switch the direction back
1817 direction[0] *= -1;
1818 direction[1] *= -1;
1819
1820 // Swap the components back
1821 temp = direction[1];
1822 direction[1] = direction[0];
1823 direction[0] = temp;
1824 }
1825 return false;
1826 }
1827
Adam Cohen482ed822012-03-02 14:15:13 -08001828 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001829 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001830 // Return early if get invalid cell positions
1831 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001832
Adam Cohen8baab352012-03-20 17:39:21 -07001833 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001834 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001835
Adam Cohen8baab352012-03-20 17:39:21 -07001836 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001837 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001838 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001839 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001840 c.cellX = cellX;
1841 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001842 }
Adam Cohen482ed822012-03-02 14:15:13 -08001843 }
Adam Cohen482ed822012-03-02 14:15:13 -08001844 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1845 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001846 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001847 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001848 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001849 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001850 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001851 if (Rect.intersects(r0, r1)) {
1852 if (!lp.canReorder) {
1853 return false;
1854 }
1855 mIntersectingViews.add(child);
1856 }
1857 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001858
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001859 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1860
Winson Chung5f8afe62013-08-12 16:19:28 -07001861 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001862 // we try to find a solution such that no displaced item travels through another item
1863 // without also displacing that item.
1864 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001865 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001866 return true;
1867 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001868
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001869 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001870 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001871 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001872 return true;
1873 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001874
Adam Cohen482ed822012-03-02 14:15:13 -08001875 // Ok, they couldn't move as a block, let's move them individually
1876 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001877 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001878 return false;
1879 }
1880 }
1881 return true;
1882 }
1883
1884 /*
1885 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1886 * the provided point and the provided cell
1887 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001888 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001889 double angle = Math.atan(((float) deltaY) / deltaX);
1890
1891 result[0] = 0;
1892 result[1] = 0;
1893 if (Math.abs(Math.cos(angle)) > 0.5f) {
1894 result[0] = (int) Math.signum(deltaX);
1895 }
1896 if (Math.abs(Math.sin(angle)) > 0.5f) {
1897 result[1] = (int) Math.signum(deltaY);
1898 }
1899 }
1900
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001901 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001902 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1903 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001904 // Copy the current state into the solution. This solution will be manipulated as necessary.
1905 copyCurrentStateToSolution(solution, false);
1906 // Copy the current occupied array into the temporary occupied array. This array will be
1907 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001908 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001909
1910 // We find the nearest cell into which we would place the dragged item, assuming there's
1911 // nothing in its way.
1912 int result[] = new int[2];
1913 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1914
1915 boolean success = false;
1916 // First we try the exact nearest position of the item being dragged,
1917 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001918 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1919 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001920
1921 if (!success) {
1922 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1923 // x, then 1 in y etc.
1924 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001925 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1926 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001927 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001928 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1929 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001930 }
1931 solution.isSolution = false;
1932 } else {
1933 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001934 solution.cellX = result[0];
1935 solution.cellY = result[1];
1936 solution.spanX = spanX;
1937 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001938 }
1939 return solution;
1940 }
1941
1942 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001943 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001944 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001945 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001946 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001947 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001948 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001949 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001950 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001951 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001952 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001953 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001954 }
1955 }
1956
1957 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001958 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001959
Michael Jurkaa52570f2012-03-20 03:18:20 -07001960 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001961 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001962 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001963 if (child == dragView) continue;
1964 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001965 CellAndSpan c = solution.map.get(child);
1966 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001967 lp.tmpCellX = c.cellX;
1968 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001969 lp.cellHSpan = c.spanX;
1970 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001971 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001972 }
1973 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001974 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001975 }
1976
1977 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1978 commitDragView) {
1979
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001980 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1981 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001982
Michael Jurkaa52570f2012-03-20 03:18:20 -07001983 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001984 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001985 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001986 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001987 CellAndSpan c = solution.map.get(child);
1988 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001989 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001990 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001991 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001992 }
1993 }
1994 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001995 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001996 }
1997 }
1998
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001999
2000 // This method starts or changes the reorder preview animations
2001 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2002 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002003 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002004 for (int i = 0; i < childCount; i++) {
2005 View child = mShortcutsAndWidgets.getChildAt(i);
2006 if (child == dragView) continue;
2007 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002008 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2009 != null && !solution.intersectingViews.contains(child);
2010
Adam Cohen19f37922012-03-21 11:59:11 -07002011 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002012 if (c != null && !skip) {
2013 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002014 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002015 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002016 }
2017 }
2018 }
2019
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002020 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002021 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002022 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002023 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002024 float finalDeltaX;
2025 float finalDeltaY;
2026 float initDeltaX;
2027 float initDeltaY;
2028 float finalScale;
2029 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002030 int mode;
2031 boolean repeating = false;
2032 private static final int PREVIEW_DURATION = 300;
2033 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2034
2035 public static final int MODE_HINT = 0;
2036 public static final int MODE_PREVIEW = 1;
2037
Adam Cohene7587d22012-05-24 18:50:02 -07002038 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002039
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002040 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2041 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002042 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2043 final int x0 = mTmpPoint[0];
2044 final int y0 = mTmpPoint[1];
2045 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2046 final int x1 = mTmpPoint[0];
2047 final int y1 = mTmpPoint[1];
2048 final int dX = x1 - x0;
2049 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002050 finalDeltaX = 0;
2051 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002052 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002053 if (dX == dY && dX == 0) {
2054 } else {
2055 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002056 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002057 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002058 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002059 } else {
2060 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002061 finalDeltaX = (int) (- dir * Math.signum(dX) *
2062 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2063 finalDeltaY = (int) (- dir * Math.signum(dY) *
2064 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002065 }
2066 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002067 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002068 initDeltaX = child.getTranslationX();
2069 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002070 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002071 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002072 this.child = child;
2073 }
2074
Adam Cohend024f982012-05-23 18:26:45 -07002075 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002076 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002077 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002078 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002079 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002080 if (finalDeltaX == 0 && finalDeltaY == 0) {
2081 completeAnimationImmediately();
2082 return;
2083 }
Adam Cohen19f37922012-03-21 11:59:11 -07002084 }
Adam Cohend024f982012-05-23 18:26:45 -07002085 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002086 return;
2087 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002088 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002089 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002090
2091 // Animations are disabled in power save mode, causing the repeated animation to jump
2092 // spastically between beginning and end states. Since this looks bad, we don't repeat
2093 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002094 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002095 va.setRepeatMode(ValueAnimator.REVERSE);
2096 va.setRepeatCount(ValueAnimator.INFINITE);
2097 }
2098
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002099 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002100 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002101 va.addUpdateListener(new AnimatorUpdateListener() {
2102 @Override
2103 public void onAnimationUpdate(ValueAnimator animation) {
2104 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002105 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2106 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2107 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002108 child.setTranslationX(x);
2109 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002110 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002111 child.setScaleX(s);
2112 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002113 }
2114 });
2115 va.addListener(new AnimatorListenerAdapter() {
2116 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002117 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002118 initDeltaX = 0;
2119 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002120 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002121 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002122 }
2123 });
Adam Cohen19f37922012-03-21 11:59:11 -07002124 mShakeAnimators.put(child, this);
2125 va.start();
2126 }
2127
Adam Cohend024f982012-05-23 18:26:45 -07002128 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002129 if (a != null) {
2130 a.cancel();
2131 }
Adam Cohen19f37922012-03-21 11:59:11 -07002132 }
Adam Cohene7587d22012-05-24 18:50:02 -07002133
Adam Cohen091440a2015-03-18 14:16:05 -07002134 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002135 if (a != null) {
2136 a.cancel();
2137 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002138
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002139 a = new LauncherViewPropertyAnimator(child)
2140 .scaleX(getChildrenScale())
2141 .scaleY(getChildrenScale())
2142 .translationX(0)
2143 .translationY(0)
2144 .setDuration(REORDER_ANIMATION_DURATION);
2145 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2146 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002147 }
Adam Cohen19f37922012-03-21 11:59:11 -07002148 }
2149
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002150 private void completeAndClearReorderPreviewAnimations() {
2151 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002152 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002153 }
2154 mShakeAnimators.clear();
2155 }
2156
Adam Cohen482ed822012-03-02 14:15:13 -08002157 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002158 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002159
2160 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2161 int container = Favorites.CONTAINER_DESKTOP;
2162
2163 if (mLauncher.isHotseatLayout(this)) {
2164 screenId = -1;
2165 container = Favorites.CONTAINER_HOTSEAT;
2166 }
2167
Michael Jurkaa52570f2012-03-20 03:18:20 -07002168 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002169 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002170 View child = mShortcutsAndWidgets.getChildAt(i);
2171 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2172 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002173 // We do a null check here because the item info can be null in the case of the
2174 // AllApps button in the hotseat.
2175 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002176 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2177 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2178 || info.spanY != lp.cellVSpan);
2179
Adam Cohen2acce882012-03-28 19:03:19 -07002180 info.cellX = lp.cellX = lp.tmpCellX;
2181 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002182 info.spanX = lp.cellHSpan;
2183 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002184
2185 if (requiresDbUpdate) {
2186 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId,
2187 info.cellX, info.cellY, info.spanX, info.spanY);
2188 }
Adam Cohen2acce882012-03-28 19:03:19 -07002189 }
Adam Cohen482ed822012-03-02 14:15:13 -08002190 }
2191 }
2192
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002193 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002194 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002195 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002196 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002197 lp.useTmpCoords = useTempCoords;
2198 }
2199 }
2200
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002201 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002202 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2203 int[] result = new int[2];
2204 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002205 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002206 resultSpan);
2207 if (result[0] >= 0 && result[1] >= 0) {
2208 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002209 solution.cellX = result[0];
2210 solution.cellY = result[1];
2211 solution.spanX = resultSpan[0];
2212 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002213 solution.isSolution = true;
2214 } else {
2215 solution.isSolution = false;
2216 }
2217 return solution;
2218 }
2219
2220 public void prepareChildForDrag(View child) {
2221 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002222 }
2223
Adam Cohen19f37922012-03-21 11:59:11 -07002224 /* This seems like it should be obvious and straight-forward, but when the direction vector
2225 needs to match with the notion of the dragView pushing other views, we have to employ
2226 a slightly more subtle notion of the direction vector. The question is what two points is
2227 the vector between? The center of the dragView and its desired destination? Not quite, as
2228 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2229 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2230 or right, which helps make pushing feel right.
2231 */
2232 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2233 int spanY, View dragView, int[] resultDirection) {
2234 int[] targetDestination = new int[2];
2235
2236 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2237 Rect dragRect = new Rect();
2238 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2239 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2240
2241 Rect dropRegionRect = new Rect();
2242 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2243 dragView, dropRegionRect, mIntersectingViews);
2244
2245 int dropRegionSpanX = dropRegionRect.width();
2246 int dropRegionSpanY = dropRegionRect.height();
2247
2248 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2249 dropRegionRect.height(), dropRegionRect);
2250
2251 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2252 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2253
2254 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2255 deltaX = 0;
2256 }
2257 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2258 deltaY = 0;
2259 }
2260
2261 if (deltaX == 0 && deltaY == 0) {
2262 // No idea what to do, give a random direction.
2263 resultDirection[0] = 1;
2264 resultDirection[1] = 0;
2265 } else {
2266 computeDirectionVector(deltaX, deltaY, resultDirection);
2267 }
2268 }
2269
2270 // For a given cell and span, fetch the set of views intersecting the region.
2271 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2272 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2273 if (boundingRect != null) {
2274 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2275 }
2276 intersectingViews.clear();
2277 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2278 Rect r1 = new Rect();
2279 final int count = mShortcutsAndWidgets.getChildCount();
2280 for (int i = 0; i < count; i++) {
2281 View child = mShortcutsAndWidgets.getChildAt(i);
2282 if (child == dragView) continue;
2283 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2284 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2285 if (Rect.intersects(r0, r1)) {
2286 mIntersectingViews.add(child);
2287 if (boundingRect != null) {
2288 boundingRect.union(r1);
2289 }
2290 }
2291 }
2292 }
2293
2294 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2295 View dragView, int[] result) {
2296 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2297 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2298 mIntersectingViews);
2299 return !mIntersectingViews.isEmpty();
2300 }
2301
2302 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002303 completeAndClearReorderPreviewAnimations();
2304 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2305 final int count = mShortcutsAndWidgets.getChildCount();
2306 for (int i = 0; i < count; i++) {
2307 View child = mShortcutsAndWidgets.getChildAt(i);
2308 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2309 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2310 lp.tmpCellX = lp.cellX;
2311 lp.tmpCellY = lp.cellY;
2312 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2313 0, false, false);
2314 }
Adam Cohen19f37922012-03-21 11:59:11 -07002315 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002316 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002317 }
Adam Cohen19f37922012-03-21 11:59:11 -07002318 }
2319
Adam Cohenbebf0422012-04-11 18:06:28 -07002320 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2321 View dragView, int[] direction, boolean commit) {
2322 int[] pixelXY = new int[2];
2323 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2324
2325 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002326 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002327 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2328
2329 setUseTempCoords(true);
2330 if (swapSolution != null && swapSolution.isSolution) {
2331 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2332 // committing anything or animating anything as we just want to determine if a solution
2333 // exists
2334 copySolutionToTempState(swapSolution, dragView);
2335 setItemPlacementDirty(true);
2336 animateItemsToSolution(swapSolution, dragView, commit);
2337
2338 if (commit) {
2339 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002340 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002341 setItemPlacementDirty(false);
2342 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002343 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2344 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002345 }
2346 mShortcutsAndWidgets.requestLayout();
2347 }
2348 return swapSolution.isSolution;
2349 }
2350
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002351 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002352 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002353 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002354 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002355
2356 if (resultSpan == null) {
2357 resultSpan = new int[2];
2358 }
2359
Adam Cohen19f37922012-03-21 11:59:11 -07002360 // When we are checking drop validity or actually dropping, we don't recompute the
2361 // direction vector, since we want the solution to match the preview, and it's possible
2362 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002363 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2364 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002365 mDirectionVector[0] = mPreviousReorderDirection[0];
2366 mDirectionVector[1] = mPreviousReorderDirection[1];
2367 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002368 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2369 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2370 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002371 }
2372 } else {
2373 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2374 mPreviousReorderDirection[0] = mDirectionVector[0];
2375 mPreviousReorderDirection[1] = mDirectionVector[1];
2376 }
2377
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002378 // Find a solution involving pushing / displacing any items in the way
2379 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002380 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2381
2382 // We attempt the approach which doesn't shuffle views at all
2383 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2384 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2385
2386 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002387
2388 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2389 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002390 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2391 finalSolution = swapSolution;
2392 } else if (noShuffleSolution.isSolution) {
2393 finalSolution = noShuffleSolution;
2394 }
2395
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002396 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002397 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002398 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2399 ReorderPreviewAnimation.MODE_HINT);
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 Cohenfa3c58f2013-12-06 16:10:55 -08002404 } else {
2405 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2406 }
2407 return result;
2408 }
2409
Adam Cohen482ed822012-03-02 14:15:13 -08002410 boolean foundSolution = true;
2411 if (!DESTRUCTIVE_REORDER) {
2412 setUseTempCoords(true);
2413 }
2414
2415 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002416 result[0] = finalSolution.cellX;
2417 result[1] = finalSolution.cellY;
2418 resultSpan[0] = finalSolution.spanX;
2419 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002420
2421 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2422 // committing anything or animating anything as we just want to determine if a solution
2423 // exists
2424 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2425 if (!DESTRUCTIVE_REORDER) {
2426 copySolutionToTempState(finalSolution, dragView);
2427 }
2428 setItemPlacementDirty(true);
2429 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2430
Adam Cohen19f37922012-03-21 11:59:11 -07002431 if (!DESTRUCTIVE_REORDER &&
2432 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002433 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002434 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002435 setItemPlacementDirty(false);
2436 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002437 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2438 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002439 }
2440 }
2441 } else {
2442 foundSolution = false;
2443 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2444 }
2445
2446 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2447 setUseTempCoords(false);
2448 }
Adam Cohen482ed822012-03-02 14:15:13 -08002449
Michael Jurkaa52570f2012-03-20 03:18:20 -07002450 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002451 return result;
2452 }
2453
Adam Cohen19f37922012-03-21 11:59:11 -07002454 void setItemPlacementDirty(boolean dirty) {
2455 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002456 }
Adam Cohen19f37922012-03-21 11:59:11 -07002457 boolean isItemPlacementDirty() {
2458 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002459 }
2460
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002461 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002462 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002463 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2464 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002465 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002466 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002467
Adam Cohenf3900c22012-11-16 18:28:11 -08002468 void save() {
2469 // Copy current state into savedMap
2470 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002471 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002472 }
2473 }
2474
2475 void restore() {
2476 // Restore current state from savedMap
2477 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002478 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002479 }
2480 }
2481
2482 void add(View v, CellAndSpan cs) {
2483 map.put(v, cs);
2484 savedMap.put(v, new CellAndSpan());
2485 sortedViews.add(v);
2486 }
2487
Adam Cohen482ed822012-03-02 14:15:13 -08002488 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002489 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002490 }
2491
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002492 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2493 boolean first = true;
2494 for (View v: views) {
2495 CellAndSpan c = map.get(v);
2496 if (first) {
2497 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2498 first = false;
2499 } else {
2500 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2501 }
2502 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002503 }
Adam Cohen482ed822012-03-02 14:15:13 -08002504 }
2505
Adam Cohendf035382011-04-11 17:22:04 -07002506 /**
Adam Cohendf035382011-04-11 17:22:04 -07002507 * Find a starting cell position that will fit the given bounds nearest the requested
2508 * cell location. Uses Euclidean distance to score multiple vacant areas.
2509 *
2510 * @param pixelX The X location at which you want to search for a vacant area.
2511 * @param pixelY The Y location at which you want to search for a vacant area.
2512 * @param spanX Horizontal span of the object.
2513 * @param spanY Vertical span of the object.
2514 * @param ignoreView Considers space occupied by this view as unoccupied
2515 * @param result Previously returned value to possibly recycle.
2516 * @return The X, Y cell of a vacant area that can contain this object,
2517 * nearest the requested location.
2518 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002519 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002520 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002521 }
2522
Michael Jurka0280c3b2010-09-17 15:00:07 -07002523 boolean existsEmptyCell() {
2524 return findCellForSpan(null, 1, 1);
2525 }
2526
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002528 * Finds the upper-left coordinate of the first rectangle in the grid that can
2529 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2530 * then this method will only return coordinates for rectangles that contain the cell
2531 * (intersectX, intersectY)
2532 *
2533 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2534 * can be found.
2535 * @param spanX The horizontal span of the cell we want to find.
2536 * @param spanY The vertical span of the cell we want to find.
2537 *
2538 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002539 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002540 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002541 if (cellXY == null) {
2542 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002543 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002544 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002545 }
2546
2547 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002548 * A drag event has begun over this layout.
2549 * It may have begun over this layout (in which case onDragChild is called first),
2550 * or it may have begun on another layout.
2551 */
2552 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002553 mDragging = true;
2554 }
2555
2556 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002557 * Called when drag has left this CellLayout or has been completed (successfully or not)
2558 */
2559 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002560 // This can actually be called when we aren't in a drag, e.g. when adding a new
2561 // item to this layout via the customize drawer.
2562 // Guard against that case.
2563 if (mDragging) {
2564 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002565 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002566
2567 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002568 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002569 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2570 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002571 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002572 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002573 }
2574
2575 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002576 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002577 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002578 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 *
2580 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002582 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002583 if (child != null) {
2584 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002585 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002586 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002587 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002588 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 }
2590
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002593 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002594 * @param cellX X coordinate of upper left corner expressed as a cell position
2595 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002596 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002597 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002598 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002599 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002600 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002601 final int cellWidth = mCellWidth;
2602 final int cellHeight = mCellHeight;
2603 final int widthGap = mWidthGap;
2604 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002605
Winson Chung4b825dcd2011-06-19 12:41:22 -07002606 final int hStartPadding = getPaddingLeft();
2607 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002608
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002609 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2610 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2611
2612 int x = hStartPadding + cellX * (cellWidth + widthGap);
2613 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002614
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002615 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002616 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002617
Adam Cohend4844c32011-02-18 19:25:06 -08002618 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002619 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002620 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002621 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002622 }
2623
Adam Cohend4844c32011-02-18 19:25:06 -08002624 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002625 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002626 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002627 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002628 }
2629
Adam Cohen2801caf2011-05-13 20:57:39 -07002630 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002631 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002632 (Math.max((mCountX - 1), 0) * mWidthGap);
2633 }
2634
2635 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002636 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002637 (Math.max((mCountY - 1), 0) * mHeightGap);
2638 }
2639
Michael Jurka66d72172011-04-12 16:29:25 -07002640 public boolean isOccupied(int x, int y) {
2641 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002642 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002643 } else {
2644 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2645 }
2646 }
2647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002648 @Override
2649 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2650 return new CellLayout.LayoutParams(getContext(), attrs);
2651 }
2652
2653 @Override
2654 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2655 return p instanceof CellLayout.LayoutParams;
2656 }
2657
2658 @Override
2659 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2660 return new CellLayout.LayoutParams(p);
2661 }
2662
2663 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2664 /**
2665 * Horizontal location of the item in the grid.
2666 */
2667 @ViewDebug.ExportedProperty
2668 public int cellX;
2669
2670 /**
2671 * Vertical location of the item in the grid.
2672 */
2673 @ViewDebug.ExportedProperty
2674 public int cellY;
2675
2676 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002677 * Temporary horizontal location of the item in the grid during reorder
2678 */
2679 public int tmpCellX;
2680
2681 /**
2682 * Temporary vertical location of the item in the grid during reorder
2683 */
2684 public int tmpCellY;
2685
2686 /**
2687 * Indicates that the temporary coordinates should be used to layout the items
2688 */
2689 public boolean useTmpCoords;
2690
2691 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692 * Number of cells spanned horizontally by the item.
2693 */
2694 @ViewDebug.ExportedProperty
2695 public int cellHSpan;
2696
2697 /**
2698 * Number of cells spanned vertically by the item.
2699 */
2700 @ViewDebug.ExportedProperty
2701 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002702
Adam Cohen1b607ed2011-03-03 17:26:50 -08002703 /**
2704 * Indicates whether the item will set its x, y, width and height parameters freely,
2705 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2706 */
Adam Cohend4844c32011-02-18 19:25:06 -08002707 public boolean isLockedToGrid = true;
2708
Adam Cohen482ed822012-03-02 14:15:13 -08002709 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002710 * Indicates that this item should use the full extents of its parent.
2711 */
2712 public boolean isFullscreen = false;
2713
2714 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002715 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002716 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002717 */
2718 public boolean canReorder = true;
2719
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720 // X coordinate of the view in the layout.
2721 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002722 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002723 // Y coordinate of the view in the layout.
2724 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002725 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002726
Romain Guy84f296c2009-11-04 15:00:44 -08002727 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002728
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002729 public LayoutParams(Context c, AttributeSet attrs) {
2730 super(c, attrs);
2731 cellHSpan = 1;
2732 cellVSpan = 1;
2733 }
2734
2735 public LayoutParams(ViewGroup.LayoutParams source) {
2736 super(source);
2737 cellHSpan = 1;
2738 cellVSpan = 1;
2739 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002740
2741 public LayoutParams(LayoutParams source) {
2742 super(source);
2743 this.cellX = source.cellX;
2744 this.cellY = source.cellY;
2745 this.cellHSpan = source.cellHSpan;
2746 this.cellVSpan = source.cellVSpan;
2747 }
2748
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002749 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002750 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002751 this.cellX = cellX;
2752 this.cellY = cellY;
2753 this.cellHSpan = cellHSpan;
2754 this.cellVSpan = cellVSpan;
2755 }
2756
Adam Cohen2374abf2013-04-16 14:56:57 -07002757 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2758 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002759 if (isLockedToGrid) {
2760 final int myCellHSpan = cellHSpan;
2761 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002762 int myCellX = useTmpCoords ? tmpCellX : cellX;
2763 int myCellY = useTmpCoords ? tmpCellY : cellY;
2764
2765 if (invertHorizontally) {
2766 myCellX = colCount - myCellX - cellHSpan;
2767 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002768
Adam Cohend4844c32011-02-18 19:25:06 -08002769 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2770 leftMargin - rightMargin;
2771 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2772 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002773 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2774 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002775 }
2776 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002777
Winson Chungaafa03c2010-06-11 17:34:16 -07002778 public String toString() {
2779 return "(" + this.cellX + ", " + this.cellY + ")";
2780 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002781
2782 public void setWidth(int width) {
2783 this.width = width;
2784 }
2785
2786 public int getWidth() {
2787 return width;
2788 }
2789
2790 public void setHeight(int height) {
2791 this.height = height;
2792 }
2793
2794 public int getHeight() {
2795 return height;
2796 }
2797
2798 public void setX(int x) {
2799 this.x = x;
2800 }
2801
2802 public int getX() {
2803 return x;
2804 }
2805
2806 public void setY(int y) {
2807 this.y = y;
2808 }
2809
2810 public int getY() {
2811 return y;
2812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002813 }
2814
Michael Jurka0280c3b2010-09-17 15:00:07 -07002815 // This class stores info for two purposes:
2816 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2817 // its spanX, spanY, and the screen it is on
2818 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2819 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2820 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002821 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002822 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002823 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002824 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002825
Sunny Goyal83a8f042015-05-19 12:52:12 -07002826 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002827 cellX = info.cellX;
2828 cellY = info.cellY;
2829 spanX = info.spanX;
2830 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002831 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002832 screenId = info.screenId;
2833 container = info.container;
2834 }
2835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836 @Override
2837 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002838 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2839 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840 }
2841 }
Michael Jurkad771c962011-08-09 15:00:48 -07002842
Tony Wickham86930612015-09-09 13:50:40 -07002843 /**
2844 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2845 * if necessary).
2846 */
2847 public boolean hasReorderSolution(ItemInfo itemInfo) {
2848 int[] cellPoint = new int[2];
2849 // Check for a solution starting at every cell.
2850 for (int cellX = 0; cellX < getCountX(); cellX++) {
2851 for (int cellY = 0; cellY < getCountY(); cellY++) {
2852 cellToPoint(cellX, cellY, cellPoint);
2853 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2854 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2855 true, new ItemConfiguration()).isSolution) {
2856 return true;
2857 }
2858 }
2859 }
2860 return false;
2861 }
2862
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002863 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002864 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002865 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002866}