blob: 57fd0e70a427988023e8ec820338665ecd9ab326 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070021import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
Adam Cohenc9735cf2015-01-23 16:11:55 -080024import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Joe Onorato4be866d2010-10-10 11:26:02 -070027import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080029import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070031import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080033import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070034import android.graphics.drawable.Drawable;
Sunny Goyal2805e632015-05-20 15:35:32 -070035import android.graphics.drawable.TransitionDrawable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080036import android.os.Build;
Adam Cohen1462de32012-07-24 22:34:36 -070037import android.os.Parcelable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080038import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070041import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.MotionEvent;
43import android.view.View;
44import android.view.ViewDebug;
45import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080046import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070047import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Sunny Goyal4b6eb262015-05-14 19:24:40 -070049import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070050import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070051import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
52import com.android.launcher3.accessibility.FolderAccessibilityHelper;
53import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Tony Wickhame0c33232016-02-08 11:37:04 -080054import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070055import com.android.launcher3.config.ProviderConfig;
Sunny Goyal26119432016-02-18 22:09:23 +000056import com.android.launcher3.folder.FolderIcon;
Sunny Goyal06e21a22016-08-11 16:02:02 -070057import com.android.launcher3.graphics.DragPreviewProvider;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070058import com.android.launcher3.util.CellAndSpan;
59import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070060import com.android.launcher3.util.ParcelableSparseArray;
Adam Cohen091440a2015-03-18 14:16:05 -070061import com.android.launcher3.util.Thunk;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070062
Adam Cohen69ce2e52011-07-03 19:25:21 -070063import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070064import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080065import java.util.Collections;
66import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070067import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080068import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070069
Sunny Goyal4b6eb262015-05-14 19:24:40 -070070public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070071 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
72 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
73
Tony Wickhama0628cc2015-10-14 15:23:04 -070074 private static final String TAG = "CellLayout";
75 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070076
Adam Cohen2acce882012-03-28 19:03:19 -070077 private Launcher mLauncher;
Sunny Goyal4ffec482016-02-09 11:28:52 -080078 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070079 @Thunk int mCellWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080080 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070081 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070082 private int mFixedCellWidth;
83 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070084
Sunny Goyal4ffec482016-02-09 11:28:52 -080085 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070086 private int mCountX;
Sunny Goyal4ffec482016-02-09 11:28:52 -080087 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070088 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Adam Cohen234c4cd2011-07-17 21:03:04 -070090 private int mOriginalWidthGap;
91 private int mOriginalHeightGap;
Sunny Goyal4ffec482016-02-09 11:28:52 -080092 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070093 @Thunk int mWidthGap;
Sunny Goyal4ffec482016-02-09 11:28:52 -080094 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen091440a2015-03-18 14:16:05 -070095 @Thunk int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070096 private int mMaxGap;
Adam Cohen917e3882013-10-31 15:03:35 -070097 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070098 private boolean mIsDragTarget = true;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070099 private boolean mJailContent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700101 // These are temporary variables to prevent having to allocate a new object just to
102 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -0700103 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -0700104 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700105
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700106 private GridOccupancy mOccupied;
107 private GridOccupancy mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108
Michael Jurkadee05892010-07-27 10:01:56 -0700109 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -0700110 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700111
Adam Cohenefca0272016-02-24 19:19:06 -0800112 private ArrayList<FolderIcon.PreviewBackground> mFolderBackgrounds = new ArrayList<FolderIcon.PreviewBackground>();
113 FolderIcon.PreviewBackground mFolderLeaveBehind = new FolderIcon.PreviewBackground();
114 Paint mFolderBgPaint = new Paint();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700115
Michael Jurka5f1c5092010-09-03 14:15:02 -0700116 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700117
Tony Wickham33326072016-04-04 15:05:49 -0700118 private static final int BACKGROUND_ACTIVATE_DURATION =
119 FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 120 : 0;
Sunny Goyal2805e632015-05-20 15:35:32 -0700120 private final TransitionDrawable mBackground;
121
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700122 // These values allow a fixed measurement to be set on the CellLayout.
123 private int mFixedWidth = -1;
124 private int mFixedHeight = -1;
125
Michael Jurka33945b22010-12-21 18:19:38 -0800126 // If we're actively dragging something over this screen, mIsDragOverlapping is true
127 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700128
Winson Chung150fbab2010-09-29 17:14:26 -0700129 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700130 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700131 @Thunk Rect[] mDragOutlines = new Rect[4];
132 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700133 private InterruptibleInOutAnimator[] mDragOutlineAnims =
134 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700135
136 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700137 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700138 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700139
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700140 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800141
Sunny Goyal316490e2015-06-02 09:38:28 -0700142 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
143 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700144
145 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700146
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700147 // When a drag operation is in progress, holds the nearest cell to the touch point
148 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Joe Onorato4be866d2010-10-10 11:26:02 -0700150 private boolean mDragging = false;
151
Patrick Dubroyce34a972010-10-19 10:34:32 -0700152 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700153 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700154
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800155 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700156 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800157
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800158 public static final int MODE_SHOW_REORDER_HINT = 0;
159 public static final int MODE_DRAG_OVER = 1;
160 public static final int MODE_ON_DROP = 2;
161 public static final int MODE_ON_DROP_EXTERNAL = 3;
162 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700163 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800164 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
165
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800166 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700167 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohen091440a2015-03-18 14:16:05 -0700168 @Thunk float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700169
Adam Cohen482ed822012-03-02 14:15:13 -0800170 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
171 private Rect mOccupiedRect = new Rect();
172 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700173 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700174 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800175
Sunny Goyal2805e632015-05-20 15:35:32 -0700176 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700177
Michael Jurkaca993832012-06-29 15:17:04 -0700178 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700179
Adam Cohenc9735cf2015-01-23 16:11:55 -0800180 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700181 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800182 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 public CellLayout(Context context) {
185 this(context, null);
186 }
187
188 public CellLayout(Context context, AttributeSet attrs) {
189 this(context, attrs, 0);
190 }
191
192 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
193 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700194
195 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
196 // the user where a dragged item will land when dropped.
197 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800198 setClipToPadding(false);
Tony2fd02082016-10-07 12:50:01 -0700199 mLauncher = Launcher.getLauncher(context);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700200
Adam Cohen2e6da152015-05-06 11:42:25 -0700201 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Winson Chung11a1a532013-09-13 11:14:45 -0700203 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800204 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700205 mWidthGap = mOriginalWidthGap = 0;
206 mHeightGap = mOriginalHeightGap = 0;
207 mMaxGap = Integer.MAX_VALUE;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700208
209 mCountX = grid.inv.numColumns;
210 mCountY = grid.inv.numRows;
211 mOccupied = new GridOccupancy(mCountX, mCountY);
212 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
213
Adam Cohen5b53f292012-03-29 14:30:35 -0700214 mPreviousReorderDirection[0] = INVALID_DIRECTION;
215 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Adam Cohenefca0272016-02-24 19:19:06 -0800217 mFolderLeaveBehind.delegateCellX = -1;
218 mFolderLeaveBehind.delegateCellY = -1;
219
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700222 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700223
Tony Wickhame0c33232016-02-08 11:37:04 -0800224 mBackground = (TransitionDrawable) res.getDrawable(
225 FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? R.drawable.bg_screenpanel
226 : R.drawable.bg_celllayout);
Sunny Goyal2805e632015-05-20 15:35:32 -0700227 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700228 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800229
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800230 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700231 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700232
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700233 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700234 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700235 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700236 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800237 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700238 }
Sunny Goyalf28e6af2016-08-31 16:02:40 -0700239 mDragOutlinePaint.setColor(getResources().getColor(R.color.outline_color));
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700240
241 // When dragging things around the home screens, we show a green outline of
242 // where the item will land. The outlines gradually fade out, leaving a trail
243 // behind the drag path.
244 // Set up all the animations that are used to implement this fading.
245 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700246 final float fromAlphaValue = 0;
247 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700248
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700249 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700250
251 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700252 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100253 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700254 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700255 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700256 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700257 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700258 final Bitmap outline = (Bitmap)anim.getTag();
259
260 // If an animation is started and then stopped very quickly, we can still
261 // get spurious updates we've cleared the tag. Guard against this.
262 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700263 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700264 Object val = animation.getAnimatedValue();
265 Log.d(TAG, "anim " + thisIndex + " update: " + val +
266 ", isStopped " + anim.isStopped());
267 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 // Try to prevent it from continuing to run
269 animation.cancel();
270 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700271 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800272 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700274 }
275 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700276 // The animation holds a reference to the drag outline bitmap as long is it's
277 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700278 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700279 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700280 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700281 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700282 anim.setTag(null);
283 }
284 }
285 });
286 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700287 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700288
Michael Jurkaa52570f2012-03-20 03:18:20 -0700289 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700290 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700291 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700292
Mady Mellorbb835202015-07-15 16:34:34 -0700293 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700294
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700295 mTouchFeedbackView = new ClickShadowView(context);
296 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700297 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700298 }
299
Adam Cohenc9735cf2015-01-23 16:11:55 -0800300 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700301 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800302 mUseTouchHelper = enable;
303 if (!enable) {
304 ViewCompat.setAccessibilityDelegate(this, null);
305 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
306 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
307 setOnClickListener(mLauncher);
308 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700309 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
310 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
311 mTouchHelper = new WorkspaceAccessibilityHelper(this);
312 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
313 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
314 mTouchHelper = new FolderAccessibilityHelper(this);
315 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800316 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
317 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
318 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
319 setOnClickListener(mTouchHelper);
320 }
321
322 // Invalidate the accessibility hierarchy
323 if (getParent() != null) {
324 getParent().notifySubtreeAccessibilityStateChanged(
325 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
326 }
327 }
328
329 @Override
330 public boolean dispatchHoverEvent(MotionEvent event) {
331 // Always attempt to dispatch hover events to accessibility first.
332 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
333 return true;
334 }
335 return super.dispatchHoverEvent(event);
336 }
337
338 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800339 public boolean onInterceptTouchEvent(MotionEvent ev) {
340 if (mUseTouchHelper ||
341 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
342 return true;
343 }
344 return false;
345 }
346
Mady Melloref044dd2015-06-02 15:35:07 -0700347 @Override
348 public boolean onTouchEvent(MotionEvent ev) {
349 boolean handled = super.onTouchEvent(ev);
350 // Stylus button press on a home screen should not switch between overview mode and
351 // the home screen mode, however, once in overview mode stylus button press should be
352 // enabled to allow rearranging the different home screens. So check what mode
353 // the workspace is in, and only perform stylus button presses while in overview mode.
354 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700355 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700356 return true;
357 }
358 return handled;
359 }
360
Chris Craik01f2d7f2013-10-01 14:41:56 -0700361 public void enableHardwareLayer(boolean hasLayer) {
362 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700363 }
364
365 public void buildHardwareLayer() {
366 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700367 }
368
Adam Cohen307fe232012-08-16 17:55:58 -0700369 public float getChildrenScale() {
370 return mIsHotseat ? mHotseatScale : 1.0f;
371 }
372
Winson Chung5f8afe62013-08-12 16:19:28 -0700373 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700374 mFixedCellWidth = mCellWidth = width;
375 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700376 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
377 mCountX, mCountY);
378 }
379
Adam Cohen2801caf2011-05-13 20:57:39 -0700380 public void setGridSize(int x, int y) {
381 mCountX = x;
382 mCountY = y;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700383 mOccupied = new GridOccupancy(mCountX, mCountY);
384 mTmpOccupied = new GridOccupancy(mCountX, mCountY);
Adam Cohen7fbec102012-03-27 12:42:19 -0700385 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700386 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700387 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700388 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700389 }
390
Adam Cohen2374abf2013-04-16 14:56:57 -0700391 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
392 public void setInvertIfRtl(boolean invert) {
393 mShortcutsAndWidgets.setInvertIfRtl(invert);
394 }
395
Adam Cohen917e3882013-10-31 15:03:35 -0700396 public void setDropPending(boolean pending) {
397 mDropPending = pending;
398 }
399
400 public boolean isDropPending() {
401 return mDropPending;
402 }
403
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700404 @Override
405 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700406 if (icon == null || background == null) {
407 mTouchFeedbackView.setBitmap(null);
408 mTouchFeedbackView.animate().cancel();
409 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700410 if (mTouchFeedbackView.setBitmap(background)) {
Winsonbe9798b2016-07-20 12:55:49 -0700411 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets,
412 null /* clipAgainstView */);
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700413 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700414 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800415 }
416 }
417
Adam Cohenc50438c2014-08-19 17:43:05 -0700418 void disableDragTarget() {
419 mIsDragTarget = false;
420 }
421
Tony Wickham0f97b782015-12-02 17:55:07 -0800422 public boolean isDragTarget() {
423 return mIsDragTarget;
424 }
425
Adam Cohenc50438c2014-08-19 17:43:05 -0700426 void setIsDragOverlapping(boolean isDragOverlapping) {
427 if (mIsDragOverlapping != isDragOverlapping) {
428 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700429 if (mIsDragOverlapping) {
430 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
431 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700432 if (mBackgroundAlpha > 0f) {
433 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
434 } else {
435 mBackground.resetTransition();
436 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700437 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700438 invalidate();
439 }
440 }
441
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700442 public void disableJailContent() {
443 mJailContent = false;
444 }
445
446 @Override
447 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
448 if (mJailContent) {
449 ParcelableSparseArray jail = getJailedArray(container);
450 super.dispatchSaveInstanceState(jail);
451 container.put(R.id.cell_layout_jail_id, jail);
452 } else {
453 super.dispatchSaveInstanceState(container);
454 }
455 }
456
457 @Override
458 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
459 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
460 }
461
462 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
463 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
464 return parcelable instanceof ParcelableSparseArray ?
465 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
466 }
467
Tony Wickham0f97b782015-12-02 17:55:07 -0800468 public boolean getIsDragOverlapping() {
469 return mIsDragOverlapping;
470 }
471
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700472 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700473 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700474 if (!mIsDragTarget) {
475 return;
476 }
477
Michael Jurka3e7c7632010-10-02 16:01:03 -0700478 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
479 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
480 // When we're small, we are either drawn normally or in the "accepts drops" state (during
481 // a drag). However, we also drag the mini hover background *over* one of those two
482 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700483 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700484 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700485 }
Romain Guya6abce82009-11-10 02:54:41 -0800486
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700487 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700488 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700489 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700490 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700491 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700492 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700493 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700494 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700495 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800496
Adam Cohen482ed822012-03-02 14:15:13 -0800497 if (DEBUG_VISUALIZE_OCCUPIED) {
498 int[] pt = new int[2];
499 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700500 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800501 for (int i = 0; i < mCountX; i++) {
502 for (int j = 0; j < mCountY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700503 if (mOccupied.cells[i][j]) {
Adam Cohen482ed822012-03-02 14:15:13 -0800504 cellToPoint(i, j, pt);
505 canvas.save();
506 canvas.translate(pt[0], pt[1]);
507 cd.draw(canvas);
508 canvas.restore();
509 }
510 }
511 }
512 }
513
Adam Cohenefca0272016-02-24 19:19:06 -0800514 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
515 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
516 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
517 canvas.save();
518 canvas.translate(mTempLocation[0], mTempLocation[1]);
519 bg.drawBackground(canvas, mFolderBgPaint);
Adam Cohenf172b742016-03-30 19:28:34 -0700520 if (!bg.isClipping) {
521 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
522 }
Adam Cohenefca0272016-02-24 19:19:06 -0800523 canvas.restore();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700524 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700525
Adam Cohenefca0272016-02-24 19:19:06 -0800526 if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
527 cellToPoint(mFolderLeaveBehind.delegateCellX,
528 mFolderLeaveBehind.delegateCellY, mTempLocation);
529 canvas.save();
530 canvas.translate(mTempLocation[0], mTempLocation[1]);
531 mFolderLeaveBehind.drawLeaveBehind(canvas, mFolderBgPaint);
532 canvas.restore();
Adam Cohenc51934b2011-07-26 21:07:43 -0700533 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700534 }
535
Adam Cohenefca0272016-02-24 19:19:06 -0800536 @Override
537 protected void dispatchDraw(Canvas canvas) {
538 super.dispatchDraw(canvas);
539
540 for (int i = 0; i < mFolderBackgrounds.size(); i++) {
541 FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
Adam Cohenf172b742016-03-30 19:28:34 -0700542 if (bg.isClipping) {
543 cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
544 canvas.save();
545 canvas.translate(mTempLocation[0], mTempLocation[1]);
546 bg.drawBackgroundStroke(canvas, mFolderBgPaint);
547 canvas.restore();
548 }
Adam Cohenefca0272016-02-24 19:19:06 -0800549 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700550 }
551
Adam Cohenefca0272016-02-24 19:19:06 -0800552 public void addFolderBackground(FolderIcon.PreviewBackground bg) {
553 mFolderBackgrounds.add(bg);
554 }
555 public void removeFolderBackground(FolderIcon.PreviewBackground bg) {
556 mFolderBackgrounds.remove(bg);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700557 }
558
Adam Cohenc51934b2011-07-26 21:07:43 -0700559 public void setFolderLeaveBehindCell(int x, int y) {
Adam Cohenefca0272016-02-24 19:19:06 -0800560
561 DeviceProfile grid = mLauncher.getDeviceProfile();
562 View child = getChildAt(x, y);
563
564 mFolderLeaveBehind.setup(getResources().getDisplayMetrics(), grid, null,
565 child.getMeasuredWidth(), child.getPaddingTop());
566
567 mFolderLeaveBehind.delegateCellX = x;
568 mFolderLeaveBehind.delegateCellY = y;
Adam Cohenc51934b2011-07-26 21:07:43 -0700569 invalidate();
570 }
571
572 public void clearFolderLeaveBehind() {
Adam Cohenefca0272016-02-24 19:19:06 -0800573 mFolderLeaveBehind.delegateCellX = -1;
574 mFolderLeaveBehind.delegateCellY = -1;
Adam Cohenc51934b2011-07-26 21:07:43 -0700575 invalidate();
576 }
577
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700578 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700579 public boolean shouldDelayChildPressedState() {
580 return false;
581 }
582
Adam Cohen1462de32012-07-24 22:34:36 -0700583 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700584 try {
585 dispatchRestoreInstanceState(states);
586 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700587 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700588 throw ex;
589 }
590 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
591 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
592 }
Adam Cohen1462de32012-07-24 22:34:36 -0700593 }
594
Michael Jurkae6235dd2011-10-04 15:02:05 -0700595 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700596 public void cancelLongPress() {
597 super.cancelLongPress();
598
599 // Cancel long press for all children
600 final int count = getChildCount();
601 for (int i = 0; i < count; i++) {
602 final View child = getChildAt(i);
603 child.cancelLongPress();
604 }
605 }
606
Michael Jurkadee05892010-07-27 10:01:56 -0700607 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
608 mInterceptTouchListener = listener;
609 }
610
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800611 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700612 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 }
614
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800615 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700616 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 }
618
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800619 public void setIsHotseat(boolean isHotseat) {
620 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700621 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800622 }
623
Sunny Goyale9b651e2015-04-24 11:44:51 -0700624 public boolean isHotseat() {
625 return mIsHotseat;
626 }
627
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800628 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700629 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700630 final LayoutParams lp = params;
631
Andrew Flynnde38e422012-05-08 11:22:15 -0700632 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800633 if (child instanceof BubbleTextView) {
634 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700635 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800636 }
637
Adam Cohen307fe232012-08-16 17:55:58 -0700638 child.setScaleX(getChildrenScale());
639 child.setScaleY(getChildrenScale());
640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 // Generate an id for each view, this assumes we have at most 256x256 cells
642 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700643 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700644 // If the horizontal or vertical span is set to -1, it is taken to
645 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700646 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
647 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648
Winson Chungaafa03c2010-06-11 17:34:16 -0700649 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700650 if (LOGD) {
651 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
652 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700653 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700654
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700655 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656
Winson Chungaafa03c2010-06-11 17:34:16 -0700657 return true;
658 }
659 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700663 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700664 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700665 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 }
667
668 @Override
669 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700670 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700671 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700672 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700673 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 }
675
676 @Override
677 public void removeView(View view) {
678 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700679 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 }
681
682 @Override
683 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700684 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
685 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700686 }
687
688 @Override
689 public void removeViewInLayout(View view) {
690 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700691 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700692 }
693
694 @Override
695 public void removeViews(int start, int count) {
696 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700697 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700698 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700699 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700700 }
701
702 @Override
703 public void removeViewsInLayout(int start, int count) {
704 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700705 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700706 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700707 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800708 }
709
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700710 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700711 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 * @param x X coordinate of the point
713 * @param y Y coordinate of the point
714 * @param result Array of 2 ints to hold the x and y coordinate of the cell
715 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700716 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700717 final int hStartPadding = getPaddingLeft();
718 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719
720 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
721 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
722
Adam Cohend22015c2010-07-26 22:02:18 -0700723 final int xAxis = mCountX;
724 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725
726 if (result[0] < 0) result[0] = 0;
727 if (result[0] >= xAxis) result[0] = xAxis - 1;
728 if (result[1] < 0) result[1] = 0;
729 if (result[1] >= yAxis) result[1] = yAxis - 1;
730 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700731
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 /**
733 * Given a point, return the cell that most closely encloses that point
734 * @param x X coordinate of the point
735 * @param y Y coordinate of the point
736 * @param result Array of 2 ints to hold the x and y coordinate of the cell
737 */
738 void pointToCellRounded(int x, int y, int[] result) {
739 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
740 }
741
742 /**
743 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700744 *
745 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700747 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 * @param result Array of 2 ints to hold the x and y coordinate of the point
749 */
750 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700751 final int hStartPadding = getPaddingLeft();
752 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753
754 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
755 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
756 }
757
Adam Cohene3e27a82011-04-15 12:07:39 -0700758 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800759 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700760 *
761 * @param cellX X coordinate of the cell
762 * @param cellY Y coordinate of the cell
763 *
764 * @param result Array of 2 ints to hold the x and y coordinate of the point
765 */
766 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700767 regionToCenterPoint(cellX, cellY, 1, 1, result);
768 }
769
770 /**
771 * Given a cell coordinate and span return the point that represents the center of the regio
772 *
773 * @param cellX X coordinate of the cell
774 * @param cellY Y coordinate of the cell
775 *
776 * @param result Array of 2 ints to hold the x and y coordinate of the point
777 */
778 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700779 final int hStartPadding = getPaddingLeft();
780 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700781 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
782 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
783 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
784 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700785 }
786
Adam Cohen19f37922012-03-21 11:59:11 -0700787 /**
788 * Given a cell coordinate and span fills out a corresponding pixel rect
789 *
790 * @param cellX X coordinate of the cell
791 * @param cellY Y coordinate of the cell
792 * @param result Rect in which to write the result
793 */
794 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
795 final int hStartPadding = getPaddingLeft();
796 final int vStartPadding = getPaddingTop();
797 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
798 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
799 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
800 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
801 }
802
Adam Cohen482ed822012-03-02 14:15:13 -0800803 public float getDistanceFromCell(float x, float y, int[] cell) {
804 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700805 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800806 }
807
Adam Cohenf9c184a2016-01-15 16:47:43 -0800808 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800809 return mCellWidth;
810 }
811
812 int getCellHeight() {
813 return mCellHeight;
814 }
815
Adam Cohend4844c32011-02-18 19:25:06 -0800816 int getWidthGap() {
817 return mWidthGap;
818 }
819
820 int getHeightGap() {
821 return mHeightGap;
822 }
823
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700824 public void setFixedSize(int width, int height) {
825 mFixedWidth = width;
826 mFixedHeight = height;
827 }
828
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 @Override
830 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700833 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
834 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700835 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
836 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700837 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700838 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
839 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700840 if (cw != mCellWidth || ch != mCellHeight) {
841 mCellWidth = cw;
842 mCellHeight = ch;
843 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
844 mHeightGap, mCountX, mCountY);
845 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700846 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700847
Winson Chung2d75f122013-09-23 16:53:31 -0700848 int newWidth = childWidthSize;
849 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700850 if (mFixedWidth > 0 && mFixedHeight > 0) {
851 newWidth = mFixedWidth;
852 newHeight = mFixedHeight;
853 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
855 }
856
Adam Cohend22015c2010-07-26 22:02:18 -0700857 int numWidthGaps = mCountX - 1;
858 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859
Adam Cohen234c4cd2011-07-17 21:03:04 -0700860 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700861 int hSpace = childWidthSize;
862 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700863 int hFreeSpace = hSpace - (mCountX * mCellWidth);
864 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700865 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
866 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700867 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
868 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700869 } else {
870 mWidthGap = mOriginalWidthGap;
871 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700872 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700873
874 // Make the feedback view large enough to hold the blur bitmap.
875 mTouchFeedbackView.measure(
876 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
877 MeasureSpec.EXACTLY),
878 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
879 MeasureSpec.EXACTLY));
880
881 mShortcutsAndWidgets.measure(
882 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
883 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
884
885 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
886 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700887 if (mFixedWidth > 0 && mFixedHeight > 0) {
888 setMeasuredDimension(maxWidth, maxHeight);
889 } else {
890 setMeasuredDimension(widthSize, heightSize);
891 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 }
893
894 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700895 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800896 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
897 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
898 int left = getPaddingLeft();
899 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800900 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800901 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700902 int right = r - l - getPaddingRight();
903 if (!isFullscreen) {
904 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
905 }
906
Winson Chung38848ca2013-10-08 12:03:44 -0700907 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700908 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700909
910 mTouchFeedbackView.layout(left, top,
911 left + mTouchFeedbackView.getMeasuredWidth(),
912 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700913 mShortcutsAndWidgets.layout(left, top, right, bottom);
914
915 // Expand the background drawing bounds by the padding baked into the background drawable
916 mBackground.getPadding(mTempRect);
917 mBackground.setBounds(
918 left - mTempRect.left,
919 top - mTempRect.top,
920 right + mTempRect.right,
921 bottom + mTempRect.bottom);
922 }
923
Tony Wickhama501d492015-11-03 18:05:01 -0800924 /**
925 * Returns the amount of space left over after subtracting padding and cells. This space will be
926 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
927 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
928 */
929 public int getUnusedHorizontalSpace() {
930 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
931 }
932
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 @Override
934 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700935 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
937
938 @Override
939 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700940 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 }
942
Michael Jurka5f1c5092010-09-03 14:15:02 -0700943 public float getBackgroundAlpha() {
944 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700945 }
946
Michael Jurka5f1c5092010-09-03 14:15:02 -0700947 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800948 if (mBackgroundAlpha != alpha) {
949 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700950 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800951 }
Michael Jurkadee05892010-07-27 10:01:56 -0700952 }
953
Sunny Goyal2805e632015-05-20 15:35:32 -0700954 @Override
955 protected boolean verifyDrawable(Drawable who) {
956 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
957 }
958
Michael Jurkaa52570f2012-03-20 03:18:20 -0700959 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700960 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700961 }
962
Michael Jurkaa52570f2012-03-20 03:18:20 -0700963 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700964 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700965 }
966
Patrick Dubroy440c3602010-07-13 17:50:32 -0700967 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700968 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700969 }
970
Adam Cohen76fc0852011-06-17 13:26:23 -0700971 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800972 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700973 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800974
Adam Cohen19f37922012-03-21 11:59:11 -0700975 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700976 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
977 final ItemInfo info = (ItemInfo) child.getTag();
978
979 // We cancel any existing animations
980 if (mReorderAnimators.containsKey(lp)) {
981 mReorderAnimators.get(lp).cancel();
982 mReorderAnimators.remove(lp);
983 }
984
Adam Cohen482ed822012-03-02 14:15:13 -0800985 final int oldX = lp.x;
986 final int oldY = lp.y;
987 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700988 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
989 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
990 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800991 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700992 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800993 if (permanent) {
994 lp.cellX = info.cellX = cellX;
995 lp.cellY = info.cellY = cellY;
996 } else {
997 lp.tmpCellX = cellX;
998 lp.tmpCellY = cellY;
999 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001000 clc.setupLp(lp);
1001 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001002 final int newX = lp.x;
1003 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001004
Adam Cohen76fc0852011-06-17 13:26:23 -07001005 lp.x = oldX;
1006 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001007
Adam Cohen482ed822012-03-02 14:15:13 -08001008 // Exit early if we're not actually moving the view
1009 if (oldX == newX && oldY == newY) {
1010 lp.isLockedToGrid = true;
1011 return true;
1012 }
1013
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001014 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001015 va.setDuration(duration);
1016 mReorderAnimators.put(lp, va);
1017
1018 va.addUpdateListener(new AnimatorUpdateListener() {
1019 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001020 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001021 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001022 lp.x = (int) ((1 - r) * oldX + r * newX);
1023 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001024 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001025 }
1026 });
Adam Cohen482ed822012-03-02 14:15:13 -08001027 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001028 boolean cancelled = false;
1029 public void onAnimationEnd(Animator animation) {
1030 // If the animation was cancelled, it means that another animation
1031 // has interrupted this one, and we don't want to lock the item into
1032 // place just yet.
1033 if (!cancelled) {
1034 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001035 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001036 }
1037 if (mReorderAnimators.containsKey(lp)) {
1038 mReorderAnimators.remove(lp);
1039 }
1040 }
1041 public void onAnimationCancel(Animator animation) {
1042 cancelled = true;
1043 }
1044 });
Adam Cohen482ed822012-03-02 14:15:13 -08001045 va.setStartDelay(delay);
1046 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001047 return true;
1048 }
1049 return false;
1050 }
1051
Sunny Goyal06e21a22016-08-11 16:02:02 -07001052 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
1053 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001054 final int oldDragCellX = mDragCell[0];
1055 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001056
Sunny Goyal06e21a22016-08-11 16:02:02 -07001057 if (outlineProvider == null || outlineProvider.gerenatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001058 return;
1059 }
1060
Sunny Goyal06e21a22016-08-11 16:02:02 -07001061 Bitmap dragOutline = outlineProvider.gerenatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -08001062 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001063 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1064 Rect dragRegion = dragObject.dragView.getDragRegion();
1065
Adam Cohen482ed822012-03-02 14:15:13 -08001066 mDragCell[0] = cellX;
1067 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001068
Joe Onorato4be866d2010-10-10 11:26:02 -07001069 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001070 mDragOutlineAnims[oldIndex].animateOut();
1071 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001072 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001073
Adam Cohend41fbf52012-02-16 23:53:59 -08001074 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001075 cellToRect(cellX, cellY, spanX, spanY, r);
Sunny Goyal106bf642015-07-16 12:18:06 -07001076 } else {
1077 // Find the top left corner of the rect the object will occupy
1078 final int[] topLeft = mTmpPoint;
1079 cellToPoint(cellX, cellY, topLeft);
1080
1081 int left = topLeft[0];
1082 int top = topLeft[1];
1083
1084 if (v != null && dragOffset == null) {
1085 // When drawing the drag outline, it did not account for margin offsets
1086 // added by the view's parent.
1087 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1088 left += lp.leftMargin;
1089 top += lp.topMargin;
1090
1091 // Offsets due to the size difference between the View and the dragOutline.
1092 // There is a size difference to account for the outer blur, which may lie
1093 // outside the bounds of the view.
1094 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1095 // We center about the x axis
1096 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1097 - dragOutline.getWidth()) / 2;
1098 } else {
1099 if (dragOffset != null && dragRegion != null) {
1100 // Center the drag region *horizontally* in the cell and apply a drag
1101 // outline offset
1102 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1103 - dragRegion.width()) / 2;
1104 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1105 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1106 top += dragOffset.y + cellPaddingY;
1107 } else {
1108 // Center the drag outline in the cell
1109 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1110 - dragOutline.getWidth()) / 2;
1111 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1112 - dragOutline.getHeight()) / 2;
1113 }
1114 }
1115 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001116 }
Winson Chung150fbab2010-09-29 17:14:26 -07001117
Sunny Goyal106bf642015-07-16 12:18:06 -07001118 Utilities.scaleRectAboutCenter(r, getChildrenScale());
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001119 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1120 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001121
1122 if (dragObject.stateAnnouncer != null) {
1123 String msg;
1124 if (isHotseat()) {
1125 msg = getContext().getString(R.string.move_to_hotseat_position,
1126 Math.max(cellX, cellY) + 1);
1127 } else {
1128 msg = getContext().getString(R.string.move_to_empty_cell,
1129 cellY + 1, cellX + 1);
1130 }
1131 dragObject.stateAnnouncer.announce(msg);
1132 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001133 }
1134 }
1135
Adam Cohene0310962011-04-18 16:15:31 -07001136 public void clearDragOutlines() {
1137 final int oldIndex = mDragOutlineCurrent;
1138 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001139 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001140 }
1141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001143 * Find a vacant area that will fit the given bounds nearest the requested
1144 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001145 *
Romain Guy51afc022009-05-04 18:03:43 -07001146 * @param pixelX The X location at which you want to search for a vacant area.
1147 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001148 * @param minSpanX The minimum horizontal span required
1149 * @param minSpanY The minimum vertical span required
1150 * @param spanX Horizontal span of the object.
1151 * @param spanY Vertical span of the object.
1152 * @param result Array in which to place the result, or null (in which case a new array will
1153 * be allocated)
1154 * @return The X, Y cell of a vacant area that can contain this object,
1155 * nearest the requested location.
1156 */
1157 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1158 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001159 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001160 result, resultSpan);
1161 }
1162
Adam Cohend41fbf52012-02-16 23:53:59 -08001163 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1164 private void lazyInitTempRectStack() {
1165 if (mTempRectStack.isEmpty()) {
1166 for (int i = 0; i < mCountX * mCountY; i++) {
1167 mTempRectStack.push(new Rect());
1168 }
1169 }
1170 }
Adam Cohen482ed822012-03-02 14:15:13 -08001171
Adam Cohend41fbf52012-02-16 23:53:59 -08001172 private void recycleTempRects(Stack<Rect> used) {
1173 while (!used.isEmpty()) {
1174 mTempRectStack.push(used.pop());
1175 }
1176 }
1177
1178 /**
1179 * Find a vacant area that will fit the given bounds nearest the requested
1180 * cell location. Uses Euclidean distance to score multiple vacant areas.
1181 *
1182 * @param pixelX The X location at which you want to search for a vacant area.
1183 * @param pixelY The Y location at which you want to search for a vacant area.
1184 * @param minSpanX The minimum horizontal span required
1185 * @param minSpanY The minimum vertical span required
1186 * @param spanX Horizontal span of the object.
1187 * @param spanY Vertical span of the object.
1188 * @param ignoreOccupied If true, the result can be an occupied cell
1189 * @param result Array in which to place the result, or null (in which case a new array will
1190 * be allocated)
1191 * @return The X, Y cell of a vacant area that can contain this object,
1192 * nearest the requested location.
1193 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001194 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1195 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001196 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001197
Adam Cohene3e27a82011-04-15 12:07:39 -07001198 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1199 // to the center of the item, but we are searching based on the top-left cell, so
1200 // we translate the point over to correspond to the top-left.
1201 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1202 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1203
Jeff Sharkey70864282009-04-07 21:08:40 -07001204 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001205 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001206 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001207 final Rect bestRect = new Rect(-1, -1, -1, -1);
1208 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001209
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001210 final int countX = mCountX;
1211 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001212
Adam Cohend41fbf52012-02-16 23:53:59 -08001213 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1214 spanX < minSpanX || spanY < minSpanY) {
1215 return bestXY;
1216 }
1217
1218 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001219 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001220 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1221 int ySize = -1;
1222 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001223 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001224 // First, let's see if this thing fits anywhere
1225 for (int i = 0; i < minSpanX; i++) {
1226 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001227 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001228 continue inner;
1229 }
Michael Jurkac28de512010-08-13 11:27:44 -07001230 }
1231 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001232 xSize = minSpanX;
1233 ySize = minSpanY;
1234
1235 // We know that the item will fit at _some_ acceptable size, now let's see
1236 // how big we can make it. We'll alternate between incrementing x and y spans
1237 // until we hit a limit.
1238 boolean incX = true;
1239 boolean hitMaxX = xSize >= spanX;
1240 boolean hitMaxY = ySize >= spanY;
1241 while (!(hitMaxX && hitMaxY)) {
1242 if (incX && !hitMaxX) {
1243 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001244 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001245 // We can't move out horizontally
1246 hitMaxX = true;
1247 }
1248 }
1249 if (!hitMaxX) {
1250 xSize++;
1251 }
1252 } else if (!hitMaxY) {
1253 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001254 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001255 // We can't move out vertically
1256 hitMaxY = true;
1257 }
1258 }
1259 if (!hitMaxY) {
1260 ySize++;
1261 }
1262 }
1263 hitMaxX |= xSize >= spanX;
1264 hitMaxY |= ySize >= spanY;
1265 incX = !incX;
1266 }
1267 incX = true;
1268 hitMaxX = xSize >= spanX;
1269 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001270 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001271 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001272 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273
Adam Cohend41fbf52012-02-16 23:53:59 -08001274 // We verify that the current rect is not a sub-rect of any of our previous
1275 // candidates. In this case, the current rect is disqualified in favour of the
1276 // containing rect.
1277 Rect currentRect = mTempRectStack.pop();
1278 currentRect.set(x, y, x + xSize, y + ySize);
1279 boolean contained = false;
1280 for (Rect r : validRegions) {
1281 if (r.contains(currentRect)) {
1282 contained = true;
1283 break;
1284 }
1285 }
1286 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001287 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001288
Adam Cohend41fbf52012-02-16 23:53:59 -08001289 if ((distance <= bestDistance && !contained) ||
1290 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001291 bestDistance = distance;
1292 bestXY[0] = x;
1293 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001294 if (resultSpan != null) {
1295 resultSpan[0] = xSize;
1296 resultSpan[1] = ySize;
1297 }
1298 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301 }
1302
Adam Cohenc0dcf592011-06-01 15:30:43 -07001303 // Return -1, -1 if no suitable location found
1304 if (bestDistance == Double.MAX_VALUE) {
1305 bestXY[0] = -1;
1306 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001307 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001308 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001309 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001311
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001312 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001313 * Find a vacant area that will fit the given bounds nearest the requested
1314 * cell location, and will also weigh in a suggested direction vector of the
1315 * desired location. This method computers distance based on unit grid distances,
1316 * not pixel distances.
1317 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001318 * @param cellX The X cell nearest to which you want to search for a vacant area.
1319 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001320 * @param spanX Horizontal span of the object.
1321 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001322 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001323 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001324 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001325 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001326 * @param result Array in which to place the result, or null (in which case a new array will
1327 * be allocated)
1328 * @return The X, Y cell of a vacant area that can contain this object,
1329 * nearest the requested location.
1330 */
1331 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001332 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001333 // Keep track of best-scoring drop area
1334 final int[] bestXY = result != null ? result : new int[2];
1335 float bestDistance = Float.MAX_VALUE;
1336 int bestDirectionScore = Integer.MIN_VALUE;
1337
1338 final int countX = mCountX;
1339 final int countY = mCountY;
1340
1341 for (int y = 0; y < countY - (spanY - 1); y++) {
1342 inner:
1343 for (int x = 0; x < countX - (spanX - 1); x++) {
1344 // First, let's see if this thing fits anywhere
1345 for (int i = 0; i < spanX; i++) {
1346 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001347 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001348 continue inner;
1349 }
1350 }
1351 }
1352
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001353 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001354 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001355 computeDirectionVector(x - cellX, y - cellY, curDirection);
1356 // The direction score is just the dot product of the two candidate direction
1357 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001358 int curDirectionScore = direction[0] * curDirection[0] +
1359 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001360 if (Float.compare(distance, bestDistance) < 0 ||
1361 (Float.compare(distance, bestDistance) == 0
1362 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001363 bestDistance = distance;
1364 bestDirectionScore = curDirectionScore;
1365 bestXY[0] = x;
1366 bestXY[1] = y;
1367 }
1368 }
1369 }
1370
1371 // Return -1, -1 if no suitable location found
1372 if (bestDistance == Float.MAX_VALUE) {
1373 bestXY[0] = -1;
1374 bestXY[1] = -1;
1375 }
1376 return bestXY;
1377 }
1378
1379 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001380 int[] direction, ItemConfiguration currentState) {
1381 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001382 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001383 mTmpOccupied.markCells(c, false);
1384 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001385
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001386 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1387 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001388
1389 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001390 c.cellX = mTempLocation[0];
1391 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001392 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001393 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001394 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001395 return success;
1396 }
1397
Adam Cohenf3900c22012-11-16 18:28:11 -08001398 /**
1399 * This helper class defines a cluster of views. It helps with defining complex edges
1400 * of the cluster and determining how those edges interact with other views. The edges
1401 * essentially define a fine-grained boundary around the cluster of views -- like a more
1402 * precise version of a bounding box.
1403 */
1404 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001405 final static int LEFT = 1 << 0;
1406 final static int TOP = 1 << 1;
1407 final static int RIGHT = 1 << 2;
1408 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001409
Adam Cohenf3900c22012-11-16 18:28:11 -08001410 ArrayList<View> views;
1411 ItemConfiguration config;
1412 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001413
Adam Cohenf3900c22012-11-16 18:28:11 -08001414 int[] leftEdge = new int[mCountY];
1415 int[] rightEdge = new int[mCountY];
1416 int[] topEdge = new int[mCountX];
1417 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001418 int dirtyEdges;
1419 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001420
1421 @SuppressWarnings("unchecked")
1422 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1423 this.views = (ArrayList<View>) views.clone();
1424 this.config = config;
1425 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001426 }
1427
Adam Cohenf3900c22012-11-16 18:28:11 -08001428 void resetEdges() {
1429 for (int i = 0; i < mCountX; i++) {
1430 topEdge[i] = -1;
1431 bottomEdge[i] = -1;
1432 }
1433 for (int i = 0; i < mCountY; i++) {
1434 leftEdge[i] = -1;
1435 rightEdge[i] = -1;
1436 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001437 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001438 boundingRectDirty = true;
1439 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001440
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001441 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001442 int count = views.size();
1443 for (int i = 0; i < count; i++) {
1444 CellAndSpan cs = config.map.get(views.get(i));
1445 switch (which) {
1446 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001447 int left = cs.cellX;
1448 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001449 if (left < leftEdge[j] || leftEdge[j] < 0) {
1450 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001451 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001452 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001453 break;
1454 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001455 int right = cs.cellX + cs.spanX;
1456 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001457 if (right > rightEdge[j]) {
1458 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001459 }
1460 }
1461 break;
1462 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001463 int top = cs.cellY;
1464 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001465 if (top < topEdge[j] || topEdge[j] < 0) {
1466 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001467 }
1468 }
1469 break;
1470 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001471 int bottom = cs.cellY + cs.spanY;
1472 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001473 if (bottom > bottomEdge[j]) {
1474 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001475 }
1476 }
1477 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001478 }
1479 }
1480 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001481
1482 boolean isViewTouchingEdge(View v, int whichEdge) {
1483 CellAndSpan cs = config.map.get(v);
1484
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001485 if ((dirtyEdges & whichEdge) == whichEdge) {
1486 computeEdge(whichEdge);
1487 dirtyEdges &= ~whichEdge;
1488 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001489
1490 switch (whichEdge) {
1491 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001492 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1493 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001494 return true;
1495 }
1496 }
1497 break;
1498 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001499 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1500 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001501 return true;
1502 }
1503 }
1504 break;
1505 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001506 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1507 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001508 return true;
1509 }
1510 }
1511 break;
1512 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001513 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1514 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001515 return true;
1516 }
1517 }
1518 break;
1519 }
1520 return false;
1521 }
1522
1523 void shift(int whichEdge, int delta) {
1524 for (View v: views) {
1525 CellAndSpan c = config.map.get(v);
1526 switch (whichEdge) {
1527 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001528 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001529 break;
1530 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001531 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001532 break;
1533 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001534 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001535 break;
1536 case BOTTOM:
1537 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001538 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001539 break;
1540 }
1541 }
1542 resetEdges();
1543 }
1544
1545 public void addView(View v) {
1546 views.add(v);
1547 resetEdges();
1548 }
1549
1550 public Rect getBoundingRect() {
1551 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001552 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001553 }
1554 return boundingRect;
1555 }
1556
Adam Cohenf3900c22012-11-16 18:28:11 -08001557 PositionComparator comparator = new PositionComparator();
1558 class PositionComparator implements Comparator<View> {
1559 int whichEdge = 0;
1560 public int compare(View left, View right) {
1561 CellAndSpan l = config.map.get(left);
1562 CellAndSpan r = config.map.get(right);
1563 switch (whichEdge) {
1564 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001565 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001566 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001567 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001568 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001569 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001570 case BOTTOM:
1571 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001572 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001573 }
1574 }
1575 }
1576
1577 public void sortConfigurationForEdgePush(int edge) {
1578 comparator.whichEdge = edge;
1579 Collections.sort(config.sortedViews, comparator);
1580 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001581 }
1582
Adam Cohenf3900c22012-11-16 18:28:11 -08001583 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1584 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001585
Adam Cohenf3900c22012-11-16 18:28:11 -08001586 ViewCluster cluster = new ViewCluster(views, currentState);
1587 Rect clusterRect = cluster.getBoundingRect();
1588 int whichEdge;
1589 int pushDistance;
1590 boolean fail = false;
1591
1592 // Determine the edge of the cluster that will be leading the push and how far
1593 // the cluster must be shifted.
1594 if (direction[0] < 0) {
1595 whichEdge = ViewCluster.LEFT;
1596 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001597 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001598 whichEdge = ViewCluster.RIGHT;
1599 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1600 } else if (direction[1] < 0) {
1601 whichEdge = ViewCluster.TOP;
1602 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1603 } else {
1604 whichEdge = ViewCluster.BOTTOM;
1605 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001606 }
1607
Adam Cohenf3900c22012-11-16 18:28:11 -08001608 // Break early for invalid push distance.
1609 if (pushDistance <= 0) {
1610 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001611 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001612
1613 // Mark the occupied state as false for the group of views we want to move.
1614 for (View v: views) {
1615 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001616 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001617 }
1618
1619 // We save the current configuration -- if we fail to find a solution we will revert
1620 // to the initial state. The process of finding a solution modifies the configuration
1621 // in place, hence the need for revert in the failure case.
1622 currentState.save();
1623
1624 // The pushing algorithm is simplified by considering the views in the order in which
1625 // they would be pushed by the cluster. For example, if the cluster is leading with its
1626 // left edge, we consider sort the views by their right edge, from right to left.
1627 cluster.sortConfigurationForEdgePush(whichEdge);
1628
1629 while (pushDistance > 0 && !fail) {
1630 for (View v: currentState.sortedViews) {
1631 // For each view that isn't in the cluster, we see if the leading edge of the
1632 // cluster is contacting the edge of that view. If so, we add that view to the
1633 // cluster.
1634 if (!cluster.views.contains(v) && v != dragView) {
1635 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1636 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1637 if (!lp.canReorder) {
1638 // The push solution includes the all apps button, this is not viable.
1639 fail = true;
1640 break;
1641 }
1642 cluster.addView(v);
1643 CellAndSpan c = currentState.map.get(v);
1644
1645 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001646 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001647 }
1648 }
1649 }
1650 pushDistance--;
1651
1652 // The cluster has been completed, now we move the whole thing over in the appropriate
1653 // direction.
1654 cluster.shift(whichEdge, 1);
1655 }
1656
1657 boolean foundSolution = false;
1658 clusterRect = cluster.getBoundingRect();
1659
1660 // Due to the nature of the algorithm, the only check required to verify a valid solution
1661 // is to ensure that completed shifted cluster lies completely within the cell layout.
1662 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1663 clusterRect.bottom <= mCountY) {
1664 foundSolution = true;
1665 } else {
1666 currentState.restore();
1667 }
1668
1669 // In either case, we set the occupied array as marked for the location of the views
1670 for (View v: cluster.views) {
1671 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001672 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001673 }
1674
1675 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001676 }
1677
Adam Cohen482ed822012-03-02 14:15:13 -08001678 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001679 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001680 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001681
Adam Cohen8baab352012-03-20 17:39:21 -07001682 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001683 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001684 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001685 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001686
Adam Cohen8baab352012-03-20 17:39:21 -07001687 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001688 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001689 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001690 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001691 }
1692
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001693 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001694 int top = boundingRect.top;
1695 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001696 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001697 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001698 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001699 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001700 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001701 }
1702
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001703 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001704
Adam Cohenf3900c22012-11-16 18:28:11 -08001705 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001706 boundingRect.height(), direction,
1707 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001708
Adam Cohen8baab352012-03-20 17:39:21 -07001709 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001710 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001711 int deltaX = mTempLocation[0] - boundingRect.left;
1712 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001713 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001714 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001715 c.cellX += deltaX;
1716 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001717 }
1718 success = true;
1719 }
Adam Cohen8baab352012-03-20 17:39:21 -07001720
1721 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001722 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001723 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001724 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001725 }
1726 return success;
1727 }
1728
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001729 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1730 // to push items in each of the cardinal directions, in an order based on the direction vector
1731 // passed.
1732 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1733 int[] direction, View ignoreView, ItemConfiguration solution) {
1734 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001735 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001736 // separately in each of the components.
1737 int temp = direction[1];
1738 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001739
1740 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001741 ignoreView, solution)) {
1742 return true;
1743 }
1744 direction[1] = temp;
1745 temp = direction[0];
1746 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001747
1748 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001749 ignoreView, solution)) {
1750 return true;
1751 }
1752 // Revert the direction
1753 direction[0] = temp;
1754
1755 // Now we try pushing in each component of the opposite direction
1756 direction[0] *= -1;
1757 direction[1] *= -1;
1758 temp = direction[1];
1759 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001760 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001761 ignoreView, solution)) {
1762 return true;
1763 }
1764
1765 direction[1] = temp;
1766 temp = direction[0];
1767 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001768 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001769 ignoreView, solution)) {
1770 return true;
1771 }
1772 // revert the direction
1773 direction[0] = temp;
1774 direction[0] *= -1;
1775 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001776
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001777 } else {
1778 // If the direction vector has a single non-zero component, we push first in the
1779 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001780 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001781 ignoreView, solution)) {
1782 return true;
1783 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001784 // Then we try the opposite direction
1785 direction[0] *= -1;
1786 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001787 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001788 ignoreView, solution)) {
1789 return true;
1790 }
1791 // Switch the direction back
1792 direction[0] *= -1;
1793 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001794
1795 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001796 // to find a solution by pushing along the perpendicular axis.
1797
1798 // Swap the components
1799 int temp = direction[1];
1800 direction[1] = direction[0];
1801 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001802 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001803 ignoreView, solution)) {
1804 return true;
1805 }
1806
1807 // Then we try the opposite direction
1808 direction[0] *= -1;
1809 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001810 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001811 ignoreView, solution)) {
1812 return true;
1813 }
1814 // Switch the direction back
1815 direction[0] *= -1;
1816 direction[1] *= -1;
1817
1818 // Swap the components back
1819 temp = direction[1];
1820 direction[1] = direction[0];
1821 direction[0] = temp;
1822 }
1823 return false;
1824 }
1825
Adam Cohen482ed822012-03-02 14:15:13 -08001826 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001827 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001828 // Return early if get invalid cell positions
1829 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001830
Adam Cohen8baab352012-03-20 17:39:21 -07001831 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001832 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001833
Adam Cohen8baab352012-03-20 17:39:21 -07001834 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001835 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001836 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001837 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001838 c.cellX = cellX;
1839 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001840 }
Adam Cohen482ed822012-03-02 14:15:13 -08001841 }
Adam Cohen482ed822012-03-02 14:15:13 -08001842 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1843 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001844 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001845 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001846 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001847 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001848 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001849 if (Rect.intersects(r0, r1)) {
1850 if (!lp.canReorder) {
1851 return false;
1852 }
1853 mIntersectingViews.add(child);
1854 }
1855 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001856
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001857 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1858
Winson Chung5f8afe62013-08-12 16:19:28 -07001859 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001860 // we try to find a solution such that no displaced item travels through another item
1861 // without also displacing that item.
1862 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001863 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001864 return true;
1865 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001866
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001867 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001868 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001869 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001870 return true;
1871 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001872
Adam Cohen482ed822012-03-02 14:15:13 -08001873 // Ok, they couldn't move as a block, let's move them individually
1874 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001875 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001876 return false;
1877 }
1878 }
1879 return true;
1880 }
1881
1882 /*
1883 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1884 * the provided point and the provided cell
1885 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001886 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001887 double angle = Math.atan(((float) deltaY) / deltaX);
1888
1889 result[0] = 0;
1890 result[1] = 0;
1891 if (Math.abs(Math.cos(angle)) > 0.5f) {
1892 result[0] = (int) Math.signum(deltaX);
1893 }
1894 if (Math.abs(Math.sin(angle)) > 0.5f) {
1895 result[1] = (int) Math.signum(deltaY);
1896 }
1897 }
1898
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001899 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001900 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1901 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001902 // Copy the current state into the solution. This solution will be manipulated as necessary.
1903 copyCurrentStateToSolution(solution, false);
1904 // Copy the current occupied array into the temporary occupied array. This array will be
1905 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001906 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001907
1908 // We find the nearest cell into which we would place the dragged item, assuming there's
1909 // nothing in its way.
1910 int result[] = new int[2];
1911 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1912
1913 boolean success = false;
1914 // First we try the exact nearest position of the item being dragged,
1915 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001916 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1917 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001918
1919 if (!success) {
1920 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1921 // x, then 1 in y etc.
1922 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001923 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1924 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001925 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001926 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1927 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001928 }
1929 solution.isSolution = false;
1930 } else {
1931 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001932 solution.cellX = result[0];
1933 solution.cellY = result[1];
1934 solution.spanX = spanX;
1935 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001936 }
1937 return solution;
1938 }
1939
1940 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001941 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001942 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001943 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001944 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001945 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001946 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001947 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001948 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001949 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001950 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001951 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001952 }
1953 }
1954
1955 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001956 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001957
Michael Jurkaa52570f2012-03-20 03:18:20 -07001958 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001959 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001960 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001961 if (child == dragView) continue;
1962 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001963 CellAndSpan c = solution.map.get(child);
1964 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001965 lp.tmpCellX = c.cellX;
1966 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001967 lp.cellHSpan = c.spanX;
1968 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001969 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001970 }
1971 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001972 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001973 }
1974
1975 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1976 commitDragView) {
1977
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001978 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1979 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001980
Michael Jurkaa52570f2012-03-20 03:18:20 -07001981 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001982 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001983 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001984 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001985 CellAndSpan c = solution.map.get(child);
1986 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001987 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001988 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001989 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001990 }
1991 }
1992 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001993 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001994 }
1995 }
1996
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001997
1998 // This method starts or changes the reorder preview animations
1999 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2000 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002001 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002002 for (int i = 0; i < childCount; i++) {
2003 View child = mShortcutsAndWidgets.getChildAt(i);
2004 if (child == dragView) continue;
2005 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002006 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2007 != null && !solution.intersectingViews.contains(child);
2008
Adam Cohen19f37922012-03-21 11:59:11 -07002009 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002010 if (c != null && !skip) {
2011 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002012 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002013 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002014 }
2015 }
2016 }
2017
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002018 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002019 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002020 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002021 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002022 float finalDeltaX;
2023 float finalDeltaY;
2024 float initDeltaX;
2025 float initDeltaY;
2026 float finalScale;
2027 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002028 int mode;
2029 boolean repeating = false;
2030 private static final int PREVIEW_DURATION = 300;
2031 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2032
2033 public static final int MODE_HINT = 0;
2034 public static final int MODE_PREVIEW = 1;
2035
Adam Cohene7587d22012-05-24 18:50:02 -07002036 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002037
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002038 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2039 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002040 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2041 final int x0 = mTmpPoint[0];
2042 final int y0 = mTmpPoint[1];
2043 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2044 final int x1 = mTmpPoint[0];
2045 final int y1 = mTmpPoint[1];
2046 final int dX = x1 - x0;
2047 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002048 finalDeltaX = 0;
2049 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002050 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002051 if (dX == dY && dX == 0) {
2052 } else {
2053 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002054 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002055 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002056 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002057 } else {
2058 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002059 finalDeltaX = (int) (- dir * Math.signum(dX) *
2060 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2061 finalDeltaY = (int) (- dir * Math.signum(dY) *
2062 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002063 }
2064 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002065 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002066 initDeltaX = child.getTranslationX();
2067 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002068 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002069 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002070 this.child = child;
2071 }
2072
Adam Cohend024f982012-05-23 18:26:45 -07002073 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002074 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002075 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002076 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002077 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002078 if (finalDeltaX == 0 && finalDeltaY == 0) {
2079 completeAnimationImmediately();
2080 return;
2081 }
Adam Cohen19f37922012-03-21 11:59:11 -07002082 }
Adam Cohend024f982012-05-23 18:26:45 -07002083 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002084 return;
2085 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002086 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002087 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002088
2089 // Animations are disabled in power save mode, causing the repeated animation to jump
2090 // spastically between beginning and end states. Since this looks bad, we don't repeat
2091 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002092 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002093 va.setRepeatMode(ValueAnimator.REVERSE);
2094 va.setRepeatCount(ValueAnimator.INFINITE);
2095 }
2096
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002097 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002098 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002099 va.addUpdateListener(new AnimatorUpdateListener() {
2100 @Override
2101 public void onAnimationUpdate(ValueAnimator animation) {
2102 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002103 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2104 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2105 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002106 child.setTranslationX(x);
2107 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002108 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002109 child.setScaleX(s);
2110 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002111 }
2112 });
2113 va.addListener(new AnimatorListenerAdapter() {
2114 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002115 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002116 initDeltaX = 0;
2117 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002118 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002119 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002120 }
2121 });
Adam Cohen19f37922012-03-21 11:59:11 -07002122 mShakeAnimators.put(child, this);
2123 va.start();
2124 }
2125
Adam Cohend024f982012-05-23 18:26:45 -07002126 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002127 if (a != null) {
2128 a.cancel();
2129 }
Adam Cohen19f37922012-03-21 11:59:11 -07002130 }
Adam Cohene7587d22012-05-24 18:50:02 -07002131
Adam Cohen091440a2015-03-18 14:16:05 -07002132 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002133 if (a != null) {
2134 a.cancel();
2135 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002136
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002137 a = new LauncherViewPropertyAnimator(child)
2138 .scaleX(getChildrenScale())
2139 .scaleY(getChildrenScale())
2140 .translationX(0)
2141 .translationY(0)
2142 .setDuration(REORDER_ANIMATION_DURATION);
2143 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2144 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002145 }
Adam Cohen19f37922012-03-21 11:59:11 -07002146 }
2147
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002148 private void completeAndClearReorderPreviewAnimations() {
2149 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002150 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002151 }
2152 mShakeAnimators.clear();
2153 }
2154
Adam Cohen482ed822012-03-02 14:15:13 -08002155 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002156 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002157
2158 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2159 int container = Favorites.CONTAINER_DESKTOP;
2160
2161 if (mLauncher.isHotseatLayout(this)) {
2162 screenId = -1;
2163 container = Favorites.CONTAINER_HOTSEAT;
2164 }
2165
Michael Jurkaa52570f2012-03-20 03:18:20 -07002166 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002167 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002168 View child = mShortcutsAndWidgets.getChildAt(i);
2169 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2170 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002171 // We do a null check here because the item info can be null in the case of the
2172 // AllApps button in the hotseat.
2173 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002174 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2175 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2176 || info.spanY != lp.cellVSpan);
2177
Adam Cohen2acce882012-03-28 19:03:19 -07002178 info.cellX = lp.cellX = lp.tmpCellX;
2179 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002180 info.spanX = lp.cellHSpan;
2181 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002182
2183 if (requiresDbUpdate) {
2184 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId,
2185 info.cellX, info.cellY, info.spanX, info.spanY);
2186 }
Adam Cohen2acce882012-03-28 19:03:19 -07002187 }
Adam Cohen482ed822012-03-02 14:15:13 -08002188 }
2189 }
2190
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002191 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002192 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002193 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002194 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002195 lp.useTmpCoords = useTempCoords;
2196 }
2197 }
2198
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002199 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002200 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2201 int[] result = new int[2];
2202 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002203 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002204 resultSpan);
2205 if (result[0] >= 0 && result[1] >= 0) {
2206 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002207 solution.cellX = result[0];
2208 solution.cellY = result[1];
2209 solution.spanX = resultSpan[0];
2210 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002211 solution.isSolution = true;
2212 } else {
2213 solution.isSolution = false;
2214 }
2215 return solution;
2216 }
2217
2218 public void prepareChildForDrag(View child) {
2219 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002220 }
2221
Adam Cohen19f37922012-03-21 11:59:11 -07002222 /* This seems like it should be obvious and straight-forward, but when the direction vector
2223 needs to match with the notion of the dragView pushing other views, we have to employ
2224 a slightly more subtle notion of the direction vector. The question is what two points is
2225 the vector between? The center of the dragView and its desired destination? Not quite, as
2226 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2227 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2228 or right, which helps make pushing feel right.
2229 */
2230 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2231 int spanY, View dragView, int[] resultDirection) {
2232 int[] targetDestination = new int[2];
2233
2234 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2235 Rect dragRect = new Rect();
2236 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2237 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2238
2239 Rect dropRegionRect = new Rect();
2240 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2241 dragView, dropRegionRect, mIntersectingViews);
2242
2243 int dropRegionSpanX = dropRegionRect.width();
2244 int dropRegionSpanY = dropRegionRect.height();
2245
2246 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2247 dropRegionRect.height(), dropRegionRect);
2248
2249 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2250 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2251
2252 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2253 deltaX = 0;
2254 }
2255 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2256 deltaY = 0;
2257 }
2258
2259 if (deltaX == 0 && deltaY == 0) {
2260 // No idea what to do, give a random direction.
2261 resultDirection[0] = 1;
2262 resultDirection[1] = 0;
2263 } else {
2264 computeDirectionVector(deltaX, deltaY, resultDirection);
2265 }
2266 }
2267
2268 // For a given cell and span, fetch the set of views intersecting the region.
2269 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2270 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2271 if (boundingRect != null) {
2272 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2273 }
2274 intersectingViews.clear();
2275 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2276 Rect r1 = new Rect();
2277 final int count = mShortcutsAndWidgets.getChildCount();
2278 for (int i = 0; i < count; i++) {
2279 View child = mShortcutsAndWidgets.getChildAt(i);
2280 if (child == dragView) continue;
2281 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2282 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2283 if (Rect.intersects(r0, r1)) {
2284 mIntersectingViews.add(child);
2285 if (boundingRect != null) {
2286 boundingRect.union(r1);
2287 }
2288 }
2289 }
2290 }
2291
2292 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2293 View dragView, int[] result) {
2294 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2295 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2296 mIntersectingViews);
2297 return !mIntersectingViews.isEmpty();
2298 }
2299
2300 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002301 completeAndClearReorderPreviewAnimations();
2302 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2303 final int count = mShortcutsAndWidgets.getChildCount();
2304 for (int i = 0; i < count; i++) {
2305 View child = mShortcutsAndWidgets.getChildAt(i);
2306 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2307 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2308 lp.tmpCellX = lp.cellX;
2309 lp.tmpCellY = lp.cellY;
2310 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2311 0, false, false);
2312 }
Adam Cohen19f37922012-03-21 11:59:11 -07002313 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002314 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002315 }
Adam Cohen19f37922012-03-21 11:59:11 -07002316 }
2317
Adam Cohenbebf0422012-04-11 18:06:28 -07002318 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2319 View dragView, int[] direction, boolean commit) {
2320 int[] pixelXY = new int[2];
2321 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2322
2323 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002324 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002325 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2326
2327 setUseTempCoords(true);
2328 if (swapSolution != null && swapSolution.isSolution) {
2329 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2330 // committing anything or animating anything as we just want to determine if a solution
2331 // exists
2332 copySolutionToTempState(swapSolution, dragView);
2333 setItemPlacementDirty(true);
2334 animateItemsToSolution(swapSolution, dragView, commit);
2335
2336 if (commit) {
2337 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002338 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002339 setItemPlacementDirty(false);
2340 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002341 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2342 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002343 }
2344 mShortcutsAndWidgets.requestLayout();
2345 }
2346 return swapSolution.isSolution;
2347 }
2348
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002349 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002350 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002351 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002352 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002353
2354 if (resultSpan == null) {
2355 resultSpan = new int[2];
2356 }
2357
Adam Cohen19f37922012-03-21 11:59:11 -07002358 // When we are checking drop validity or actually dropping, we don't recompute the
2359 // direction vector, since we want the solution to match the preview, and it's possible
2360 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002361 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2362 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002363 mDirectionVector[0] = mPreviousReorderDirection[0];
2364 mDirectionVector[1] = mPreviousReorderDirection[1];
2365 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002366 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2367 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2368 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002369 }
2370 } else {
2371 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2372 mPreviousReorderDirection[0] = mDirectionVector[0];
2373 mPreviousReorderDirection[1] = mDirectionVector[1];
2374 }
2375
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002376 // Find a solution involving pushing / displacing any items in the way
2377 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002378 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2379
2380 // We attempt the approach which doesn't shuffle views at all
2381 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2382 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2383
2384 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002385
2386 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2387 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002388 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2389 finalSolution = swapSolution;
2390 } else if (noShuffleSolution.isSolution) {
2391 finalSolution = noShuffleSolution;
2392 }
2393
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002394 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002395 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002396 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2397 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002398 result[0] = finalSolution.cellX;
2399 result[1] = finalSolution.cellY;
2400 resultSpan[0] = finalSolution.spanX;
2401 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002402 } else {
2403 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2404 }
2405 return result;
2406 }
2407
Adam Cohen482ed822012-03-02 14:15:13 -08002408 boolean foundSolution = true;
2409 if (!DESTRUCTIVE_REORDER) {
2410 setUseTempCoords(true);
2411 }
2412
2413 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002414 result[0] = finalSolution.cellX;
2415 result[1] = finalSolution.cellY;
2416 resultSpan[0] = finalSolution.spanX;
2417 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002418
2419 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2420 // committing anything or animating anything as we just want to determine if a solution
2421 // exists
2422 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2423 if (!DESTRUCTIVE_REORDER) {
2424 copySolutionToTempState(finalSolution, dragView);
2425 }
2426 setItemPlacementDirty(true);
2427 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2428
Adam Cohen19f37922012-03-21 11:59:11 -07002429 if (!DESTRUCTIVE_REORDER &&
2430 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002431 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002432 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002433 setItemPlacementDirty(false);
2434 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002435 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2436 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002437 }
2438 }
2439 } else {
2440 foundSolution = false;
2441 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2442 }
2443
2444 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2445 setUseTempCoords(false);
2446 }
Adam Cohen482ed822012-03-02 14:15:13 -08002447
Michael Jurkaa52570f2012-03-20 03:18:20 -07002448 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002449 return result;
2450 }
2451
Adam Cohen19f37922012-03-21 11:59:11 -07002452 void setItemPlacementDirty(boolean dirty) {
2453 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002454 }
Adam Cohen19f37922012-03-21 11:59:11 -07002455 boolean isItemPlacementDirty() {
2456 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002457 }
2458
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002459 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002460 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002461 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2462 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002463 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002464 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002465
Adam Cohenf3900c22012-11-16 18:28:11 -08002466 void save() {
2467 // Copy current state into savedMap
2468 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002469 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002470 }
2471 }
2472
2473 void restore() {
2474 // Restore current state from savedMap
2475 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002476 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002477 }
2478 }
2479
2480 void add(View v, CellAndSpan cs) {
2481 map.put(v, cs);
2482 savedMap.put(v, new CellAndSpan());
2483 sortedViews.add(v);
2484 }
2485
Adam Cohen482ed822012-03-02 14:15:13 -08002486 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002487 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002488 }
2489
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002490 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2491 boolean first = true;
2492 for (View v: views) {
2493 CellAndSpan c = map.get(v);
2494 if (first) {
2495 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2496 first = false;
2497 } else {
2498 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2499 }
2500 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002501 }
Adam Cohen482ed822012-03-02 14:15:13 -08002502 }
2503
Adam Cohendf035382011-04-11 17:22:04 -07002504 /**
Adam Cohendf035382011-04-11 17:22:04 -07002505 * Find a starting cell position that will fit the given bounds nearest the requested
2506 * cell location. Uses Euclidean distance to score multiple vacant areas.
2507 *
2508 * @param pixelX The X location at which you want to search for a vacant area.
2509 * @param pixelY The Y location at which you want to search for a vacant area.
2510 * @param spanX Horizontal span of the object.
2511 * @param spanY Vertical span of the object.
2512 * @param ignoreView Considers space occupied by this view as unoccupied
2513 * @param result Previously returned value to possibly recycle.
2514 * @return The X, Y cell of a vacant area that can contain this object,
2515 * nearest the requested location.
2516 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002517 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002518 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002519 }
2520
Michael Jurka0280c3b2010-09-17 15:00:07 -07002521 boolean existsEmptyCell() {
2522 return findCellForSpan(null, 1, 1);
2523 }
2524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002526 * Finds the upper-left coordinate of the first rectangle in the grid that can
2527 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2528 * then this method will only return coordinates for rectangles that contain the cell
2529 * (intersectX, intersectY)
2530 *
2531 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2532 * can be found.
2533 * @param spanX The horizontal span of the cell we want to find.
2534 * @param spanY The vertical span of the cell we want to find.
2535 *
2536 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002537 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002538 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002539 if (cellXY == null) {
2540 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002541 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002542 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002543 }
2544
2545 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002546 * A drag event has begun over this layout.
2547 * It may have begun over this layout (in which case onDragChild is called first),
2548 * or it may have begun on another layout.
2549 */
2550 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002551 mDragging = true;
2552 }
2553
2554 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002555 * Called when drag has left this CellLayout or has been completed (successfully or not)
2556 */
2557 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002558 // This can actually be called when we aren't in a drag, e.g. when adding a new
2559 // item to this layout via the customize drawer.
2560 // Guard against that case.
2561 if (mDragging) {
2562 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002563 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002564
2565 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002566 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002567 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2568 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002569 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002570 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002571 }
2572
2573 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002574 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002575 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002576 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 *
2578 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002580 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002581 if (child != null) {
2582 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002583 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002584 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002585 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 }
2588
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002590 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002591 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592 * @param cellX X coordinate of upper left corner expressed as a cell position
2593 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002594 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002596 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002597 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002598 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002599 final int cellWidth = mCellWidth;
2600 final int cellHeight = mCellHeight;
2601 final int widthGap = mWidthGap;
2602 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002603
Winson Chung4b825dcd2011-06-19 12:41:22 -07002604 final int hStartPadding = getPaddingLeft();
2605 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002606
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002607 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2608 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2609
2610 int x = hStartPadding + cellX * (cellWidth + widthGap);
2611 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002612
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002613 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002614 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002615
Adam Cohend4844c32011-02-18 19:25:06 -08002616 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002617 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002618 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002619 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002620 }
2621
Adam Cohend4844c32011-02-18 19:25:06 -08002622 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002623 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002624 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002625 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002626 }
2627
Adam Cohen2801caf2011-05-13 20:57:39 -07002628 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002629 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002630 (Math.max((mCountX - 1), 0) * mWidthGap);
2631 }
2632
2633 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002634 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002635 (Math.max((mCountY - 1), 0) * mHeightGap);
2636 }
2637
Michael Jurka66d72172011-04-12 16:29:25 -07002638 public boolean isOccupied(int x, int y) {
2639 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002640 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002641 } else {
2642 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2643 }
2644 }
2645
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002646 @Override
2647 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2648 return new CellLayout.LayoutParams(getContext(), attrs);
2649 }
2650
2651 @Override
2652 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2653 return p instanceof CellLayout.LayoutParams;
2654 }
2655
2656 @Override
2657 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2658 return new CellLayout.LayoutParams(p);
2659 }
2660
2661 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2662 /**
2663 * Horizontal location of the item in the grid.
2664 */
2665 @ViewDebug.ExportedProperty
2666 public int cellX;
2667
2668 /**
2669 * Vertical location of the item in the grid.
2670 */
2671 @ViewDebug.ExportedProperty
2672 public int cellY;
2673
2674 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002675 * Temporary horizontal location of the item in the grid during reorder
2676 */
2677 public int tmpCellX;
2678
2679 /**
2680 * Temporary vertical location of the item in the grid during reorder
2681 */
2682 public int tmpCellY;
2683
2684 /**
2685 * Indicates that the temporary coordinates should be used to layout the items
2686 */
2687 public boolean useTmpCoords;
2688
2689 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002690 * Number of cells spanned horizontally by the item.
2691 */
2692 @ViewDebug.ExportedProperty
2693 public int cellHSpan;
2694
2695 /**
2696 * Number of cells spanned vertically by the item.
2697 */
2698 @ViewDebug.ExportedProperty
2699 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002700
Adam Cohen1b607ed2011-03-03 17:26:50 -08002701 /**
2702 * Indicates whether the item will set its x, y, width and height parameters freely,
2703 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2704 */
Adam Cohend4844c32011-02-18 19:25:06 -08002705 public boolean isLockedToGrid = true;
2706
Adam Cohen482ed822012-03-02 14:15:13 -08002707 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002708 * Indicates that this item should use the full extents of its parent.
2709 */
2710 public boolean isFullscreen = false;
2711
2712 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002713 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002714 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002715 */
2716 public boolean canReorder = true;
2717
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002718 // X coordinate of the view in the layout.
2719 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002720 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002721 // Y coordinate of the view in the layout.
2722 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002723 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002724
Romain Guy84f296c2009-11-04 15:00:44 -08002725 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002727 public LayoutParams(Context c, AttributeSet attrs) {
2728 super(c, attrs);
2729 cellHSpan = 1;
2730 cellVSpan = 1;
2731 }
2732
2733 public LayoutParams(ViewGroup.LayoutParams source) {
2734 super(source);
2735 cellHSpan = 1;
2736 cellVSpan = 1;
2737 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002738
2739 public LayoutParams(LayoutParams source) {
2740 super(source);
2741 this.cellX = source.cellX;
2742 this.cellY = source.cellY;
2743 this.cellHSpan = source.cellHSpan;
2744 this.cellVSpan = source.cellVSpan;
2745 }
2746
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002747 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002748 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002749 this.cellX = cellX;
2750 this.cellY = cellY;
2751 this.cellHSpan = cellHSpan;
2752 this.cellVSpan = cellVSpan;
2753 }
2754
Adam Cohen2374abf2013-04-16 14:56:57 -07002755 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2756 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002757 if (isLockedToGrid) {
2758 final int myCellHSpan = cellHSpan;
2759 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002760 int myCellX = useTmpCoords ? tmpCellX : cellX;
2761 int myCellY = useTmpCoords ? tmpCellY : cellY;
2762
2763 if (invertHorizontally) {
2764 myCellX = colCount - myCellX - cellHSpan;
2765 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002766
Adam Cohend4844c32011-02-18 19:25:06 -08002767 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2768 leftMargin - rightMargin;
2769 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2770 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002771 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2772 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002773 }
2774 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002775
Winson Chungaafa03c2010-06-11 17:34:16 -07002776 public String toString() {
2777 return "(" + this.cellX + ", " + this.cellY + ")";
2778 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002779
2780 public void setWidth(int width) {
2781 this.width = width;
2782 }
2783
2784 public int getWidth() {
2785 return width;
2786 }
2787
2788 public void setHeight(int height) {
2789 this.height = height;
2790 }
2791
2792 public int getHeight() {
2793 return height;
2794 }
2795
2796 public void setX(int x) {
2797 this.x = x;
2798 }
2799
2800 public int getX() {
2801 return x;
2802 }
2803
2804 public void setY(int y) {
2805 this.y = y;
2806 }
2807
2808 public int getY() {
2809 return y;
2810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002811 }
2812
Michael Jurka0280c3b2010-09-17 15:00:07 -07002813 // This class stores info for two purposes:
2814 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2815 // its spanX, spanY, and the screen it is on
2816 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2817 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2818 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002819 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002820 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002821 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002822 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823
Sunny Goyal83a8f042015-05-19 12:52:12 -07002824 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002825 cellX = info.cellX;
2826 cellY = info.cellY;
2827 spanX = info.spanX;
2828 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002829 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002830 screenId = info.screenId;
2831 container = info.container;
2832 }
2833
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002834 @Override
2835 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002836 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2837 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838 }
2839 }
Michael Jurkad771c962011-08-09 15:00:48 -07002840
Tony Wickham86930612015-09-09 13:50:40 -07002841 /**
2842 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2843 * if necessary).
2844 */
2845 public boolean hasReorderSolution(ItemInfo itemInfo) {
2846 int[] cellPoint = new int[2];
2847 // Check for a solution starting at every cell.
2848 for (int cellX = 0; cellX < getCountX(); cellX++) {
2849 for (int cellY = 0; cellY < getCountY(); cellY++) {
2850 cellToPoint(cellX, cellY, cellPoint);
2851 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2852 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2853 true, new ItemConfiguration()).isSolution) {
2854 return true;
2855 }
2856 }
2857 }
2858 return false;
2859 }
2860
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002861 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002862 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002864}