blob: 84b504ebcbd8f66f197775fe9eb0a52438e70c89 [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;
Jon Miranda93e1f042016-11-11 14:13:04 -0800621 mShortcutsAndWidgets.setContainerType(isHotseat
622 ? ShortcutAndWidgetContainer.HOTSEAT
623 : ShortcutAndWidgetContainer.DEFAULT);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800624 }
625
Sunny Goyale9b651e2015-04-24 11:44:51 -0700626 public boolean isHotseat() {
627 return mIsHotseat;
628 }
629
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800630 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700631 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700632 final LayoutParams lp = params;
633
Andrew Flynnde38e422012-05-08 11:22:15 -0700634 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800635 if (child instanceof BubbleTextView) {
636 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700637 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800638 }
639
Adam Cohen307fe232012-08-16 17:55:58 -0700640 child.setScaleX(getChildrenScale());
641 child.setScaleY(getChildrenScale());
642
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 // Generate an id for each view, this assumes we have at most 256x256 cells
644 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700645 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700646 // If the horizontal or vertical span is set to -1, it is taken to
647 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700648 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
649 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650
Winson Chungaafa03c2010-06-11 17:34:16 -0700651 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700652 if (LOGD) {
653 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
654 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700655 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700656
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700657 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700658
Winson Chungaafa03c2010-06-11 17:34:16 -0700659 return true;
660 }
661 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700663
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700665 public void removeAllViews() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700666 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700667 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 }
669
670 @Override
671 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700672 if (mShortcutsAndWidgets.getChildCount() > 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700673 mOccupied.clear();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700674 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700675 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 }
677
678 @Override
679 public void removeView(View view) {
680 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700681 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700682 }
683
684 @Override
685 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700686 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
687 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700688 }
689
690 @Override
691 public void removeViewInLayout(View view) {
692 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700693 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700694 }
695
696 @Override
697 public void removeViews(int start, int count) {
698 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700699 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700700 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700701 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700702 }
703
704 @Override
705 public void removeViewsInLayout(int start, int count) {
706 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700707 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700708 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700709 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800710 }
711
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700712 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700713 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 * @param x X coordinate of the point
715 * @param y Y coordinate of the point
716 * @param result Array of 2 ints to hold the x and y coordinate of the cell
717 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700718 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700719 final int hStartPadding = getPaddingLeft();
720 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721
722 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
723 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
724
Adam Cohend22015c2010-07-26 22:02:18 -0700725 final int xAxis = mCountX;
726 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727
728 if (result[0] < 0) result[0] = 0;
729 if (result[0] >= xAxis) result[0] = xAxis - 1;
730 if (result[1] < 0) result[1] = 0;
731 if (result[1] >= yAxis) result[1] = yAxis - 1;
732 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 /**
735 * Given a point, return the cell that most closely encloses that point
736 * @param x X coordinate of the point
737 * @param y Y coordinate of the point
738 * @param result Array of 2 ints to hold the x and y coordinate of the cell
739 */
740 void pointToCellRounded(int x, int y, int[] result) {
741 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
742 }
743
744 /**
745 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700746 *
747 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700749 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 * @param result Array of 2 ints to hold the x and y coordinate of the point
751 */
752 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700753 final int hStartPadding = getPaddingLeft();
754 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755
756 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
757 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
758 }
759
Adam Cohene3e27a82011-04-15 12:07:39 -0700760 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800761 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700762 *
763 * @param cellX X coordinate of the cell
764 * @param cellY Y coordinate of the cell
765 *
766 * @param result Array of 2 ints to hold the x and y coordinate of the point
767 */
768 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700769 regionToCenterPoint(cellX, cellY, 1, 1, result);
770 }
771
772 /**
773 * Given a cell coordinate and span return the point that represents the center of the regio
774 *
775 * @param cellX X coordinate of the cell
776 * @param cellY Y coordinate of the cell
777 *
778 * @param result Array of 2 ints to hold the x and y coordinate of the point
779 */
780 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700781 final int hStartPadding = getPaddingLeft();
782 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700783 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
784 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
785 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
786 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700787 }
788
Adam Cohen19f37922012-03-21 11:59:11 -0700789 /**
790 * Given a cell coordinate and span fills out a corresponding pixel rect
791 *
792 * @param cellX X coordinate of the cell
793 * @param cellY Y coordinate of the cell
794 * @param result Rect in which to write the result
795 */
796 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
797 final int hStartPadding = getPaddingLeft();
798 final int vStartPadding = getPaddingTop();
799 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
800 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
801 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
802 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
803 }
804
Adam Cohen482ed822012-03-02 14:15:13 -0800805 public float getDistanceFromCell(float x, float y, int[] cell) {
806 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700807 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800808 }
809
Adam Cohenf9c184a2016-01-15 16:47:43 -0800810 public int getCellWidth() {
Romain Guy84f296c2009-11-04 15:00:44 -0800811 return mCellWidth;
812 }
813
814 int getCellHeight() {
815 return mCellHeight;
816 }
817
Adam Cohend4844c32011-02-18 19:25:06 -0800818 int getWidthGap() {
819 return mWidthGap;
820 }
821
822 int getHeightGap() {
823 return mHeightGap;
824 }
825
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700826 public void setFixedSize(int width, int height) {
827 mFixedWidth = width;
828 mFixedHeight = height;
829 }
830
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 @Override
832 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700835 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
836 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700837 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
838 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700839 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700840 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
841 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700842 if (cw != mCellWidth || ch != mCellHeight) {
843 mCellWidth = cw;
844 mCellHeight = ch;
845 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
846 mHeightGap, mCountX, mCountY);
847 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700848 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700849
Winson Chung2d75f122013-09-23 16:53:31 -0700850 int newWidth = childWidthSize;
851 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700852 if (mFixedWidth > 0 && mFixedHeight > 0) {
853 newWidth = mFixedWidth;
854 newHeight = mFixedHeight;
855 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
857 }
858
Adam Cohend22015c2010-07-26 22:02:18 -0700859 int numWidthGaps = mCountX - 1;
860 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861
Adam Cohen234c4cd2011-07-17 21:03:04 -0700862 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700863 int hSpace = childWidthSize;
864 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700865 int hFreeSpace = hSpace - (mCountX * mCellWidth);
866 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700867 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
868 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700869 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
870 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700871 } else {
872 mWidthGap = mOriginalWidthGap;
873 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700874 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700875
876 // Make the feedback view large enough to hold the blur bitmap.
877 mTouchFeedbackView.measure(
878 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
879 MeasureSpec.EXACTLY),
880 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
881 MeasureSpec.EXACTLY));
882
883 mShortcutsAndWidgets.measure(
884 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
885 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
886
887 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
888 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700889 if (mFixedWidth > 0 && mFixedHeight > 0) {
890 setMeasuredDimension(maxWidth, maxHeight);
891 } else {
892 setMeasuredDimension(widthSize, heightSize);
893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 }
895
896 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700897 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800898 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
899 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
900 int left = getPaddingLeft();
901 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800902 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800903 }
Sunny Goyal7c786f72016-06-01 14:08:21 -0700904 int right = r - l - getPaddingRight();
905 if (!isFullscreen) {
906 right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
907 }
908
Winson Chung38848ca2013-10-08 12:03:44 -0700909 int top = getPaddingTop();
Sunny Goyal7c786f72016-06-01 14:08:21 -0700910 int bottom = b - t - getPaddingBottom();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700911
912 mTouchFeedbackView.layout(left, top,
913 left + mTouchFeedbackView.getMeasuredWidth(),
914 top + mTouchFeedbackView.getMeasuredHeight());
Sunny Goyal7c786f72016-06-01 14:08:21 -0700915 mShortcutsAndWidgets.layout(left, top, right, bottom);
916
917 // Expand the background drawing bounds by the padding baked into the background drawable
918 mBackground.getPadding(mTempRect);
919 mBackground.setBounds(
920 left - mTempRect.left,
921 top - mTempRect.top,
922 right + mTempRect.right,
923 bottom + mTempRect.bottom);
924 }
925
Tony Wickhama501d492015-11-03 18:05:01 -0800926 /**
927 * Returns the amount of space left over after subtracting padding and cells. This space will be
928 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
929 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
930 */
931 public int getUnusedHorizontalSpace() {
932 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
933 }
934
Michael Jurka5f1c5092010-09-03 14:15:02 -0700935 public float getBackgroundAlpha() {
936 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700937 }
938
Michael Jurka5f1c5092010-09-03 14:15:02 -0700939 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800940 if (mBackgroundAlpha != alpha) {
941 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700942 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800943 }
Michael Jurkadee05892010-07-27 10:01:56 -0700944 }
945
Sunny Goyal2805e632015-05-20 15:35:32 -0700946 @Override
947 protected boolean verifyDrawable(Drawable who) {
948 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
949 }
950
Michael Jurkaa52570f2012-03-20 03:18:20 -0700951 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700952 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700953 }
954
Michael Jurkaa52570f2012-03-20 03:18:20 -0700955 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700956 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700957 }
958
Patrick Dubroy440c3602010-07-13 17:50:32 -0700959 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700960 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700961 }
962
Adam Cohen76fc0852011-06-17 13:26:23 -0700963 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800964 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700965 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800966
Adam Cohen19f37922012-03-21 11:59:11 -0700967 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700968 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
969 final ItemInfo info = (ItemInfo) child.getTag();
970
971 // We cancel any existing animations
972 if (mReorderAnimators.containsKey(lp)) {
973 mReorderAnimators.get(lp).cancel();
974 mReorderAnimators.remove(lp);
975 }
976
Adam Cohen482ed822012-03-02 14:15:13 -0800977 final int oldX = lp.x;
978 final int oldY = lp.y;
979 if (adjustOccupied) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700980 GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
981 occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
982 occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
Adam Cohen482ed822012-03-02 14:15:13 -0800983 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700984 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800985 if (permanent) {
986 lp.cellX = info.cellX = cellX;
987 lp.cellY = info.cellY = cellY;
988 } else {
989 lp.tmpCellX = cellX;
990 lp.tmpCellY = cellY;
991 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700992 clc.setupLp(lp);
993 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800994 final int newX = lp.x;
995 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700996
Adam Cohen76fc0852011-06-17 13:26:23 -0700997 lp.x = oldX;
998 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700999
Adam Cohen482ed822012-03-02 14:15:13 -08001000 // Exit early if we're not actually moving the view
1001 if (oldX == newX && oldY == newY) {
1002 lp.isLockedToGrid = true;
1003 return true;
1004 }
1005
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001006 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001007 va.setDuration(duration);
1008 mReorderAnimators.put(lp, va);
1009
1010 va.addUpdateListener(new AnimatorUpdateListener() {
1011 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001012 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001013 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001014 lp.x = (int) ((1 - r) * oldX + r * newX);
1015 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001016 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001017 }
1018 });
Adam Cohen482ed822012-03-02 14:15:13 -08001019 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001020 boolean cancelled = false;
1021 public void onAnimationEnd(Animator animation) {
1022 // If the animation was cancelled, it means that another animation
1023 // has interrupted this one, and we don't want to lock the item into
1024 // place just yet.
1025 if (!cancelled) {
1026 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001027 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001028 }
1029 if (mReorderAnimators.containsKey(lp)) {
1030 mReorderAnimators.remove(lp);
1031 }
1032 }
1033 public void onAnimationCancel(Animator animation) {
1034 cancelled = true;
1035 }
1036 });
Adam Cohen482ed822012-03-02 14:15:13 -08001037 va.setStartDelay(delay);
1038 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001039 return true;
1040 }
1041 return false;
1042 }
1043
Sunny Goyal06e21a22016-08-11 16:02:02 -07001044 void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY,
1045 int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001046 final int oldDragCellX = mDragCell[0];
1047 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001048
Hyunyoung Song0de01172016-10-05 16:27:48 -07001049 if (outlineProvider == null || outlineProvider.generatedDragOutline == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001050 return;
1051 }
1052
Hyunyoung Song0de01172016-10-05 16:27:48 -07001053 Bitmap dragOutline = outlineProvider.generatedDragOutline;
Adam Cohen482ed822012-03-02 14:15:13 -08001054 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001055 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1056 Rect dragRegion = dragObject.dragView.getDragRegion();
1057
Adam Cohen482ed822012-03-02 14:15:13 -08001058 mDragCell[0] = cellX;
1059 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001060
Joe Onorato4be866d2010-10-10 11:26:02 -07001061 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001062 mDragOutlineAnims[oldIndex].animateOut();
1063 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001064 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001065
Adam Cohend41fbf52012-02-16 23:53:59 -08001066 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001067 cellToRect(cellX, cellY, spanX, spanY, r);
Sunny Goyal106bf642015-07-16 12:18:06 -07001068 } else {
1069 // Find the top left corner of the rect the object will occupy
1070 final int[] topLeft = mTmpPoint;
1071 cellToPoint(cellX, cellY, topLeft);
1072
1073 int left = topLeft[0];
1074 int top = topLeft[1];
1075
1076 if (v != null && dragOffset == null) {
1077 // When drawing the drag outline, it did not account for margin offsets
1078 // added by the view's parent.
1079 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1080 left += lp.leftMargin;
1081 top += lp.topMargin;
1082
1083 // Offsets due to the size difference between the View and the dragOutline.
1084 // There is a size difference to account for the outer blur, which may lie
1085 // outside the bounds of the view.
1086 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1087 // We center about the x axis
1088 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1089 - dragOutline.getWidth()) / 2;
1090 } else {
1091 if (dragOffset != null && dragRegion != null) {
1092 // Center the drag region *horizontally* in the cell and apply a drag
1093 // outline offset
1094 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1095 - dragRegion.width()) / 2;
1096 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1097 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1098 top += dragOffset.y + cellPaddingY;
1099 } else {
1100 // Center the drag outline in the cell
1101 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1102 - dragOutline.getWidth()) / 2;
1103 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1104 - dragOutline.getHeight()) / 2;
1105 }
1106 }
1107 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001108 }
Winson Chung150fbab2010-09-29 17:14:26 -07001109
Sunny Goyal106bf642015-07-16 12:18:06 -07001110 Utilities.scaleRectAboutCenter(r, getChildrenScale());
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001111 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1112 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001113
1114 if (dragObject.stateAnnouncer != null) {
1115 String msg;
1116 if (isHotseat()) {
1117 msg = getContext().getString(R.string.move_to_hotseat_position,
1118 Math.max(cellX, cellY) + 1);
1119 } else {
1120 msg = getContext().getString(R.string.move_to_empty_cell,
1121 cellY + 1, cellX + 1);
1122 }
1123 dragObject.stateAnnouncer.announce(msg);
1124 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001125 }
1126 }
1127
Adam Cohene0310962011-04-18 16:15:31 -07001128 public void clearDragOutlines() {
1129 final int oldIndex = mDragOutlineCurrent;
1130 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001131 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001132 }
1133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001135 * Find a vacant area that will fit the given bounds nearest the requested
1136 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001137 *
Romain Guy51afc022009-05-04 18:03:43 -07001138 * @param pixelX The X location at which you want to search for a vacant area.
1139 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001140 * @param minSpanX The minimum horizontal span required
1141 * @param minSpanY The minimum vertical span required
1142 * @param spanX Horizontal span of the object.
1143 * @param spanY Vertical span of the object.
1144 * @param result Array in which to place the result, or null (in which case a new array will
1145 * be allocated)
1146 * @return The X, Y cell of a vacant area that can contain this object,
1147 * nearest the requested location.
1148 */
1149 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1150 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001151 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001152 result, resultSpan);
1153 }
1154
Adam Cohend41fbf52012-02-16 23:53:59 -08001155 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1156 private void lazyInitTempRectStack() {
1157 if (mTempRectStack.isEmpty()) {
1158 for (int i = 0; i < mCountX * mCountY; i++) {
1159 mTempRectStack.push(new Rect());
1160 }
1161 }
1162 }
Adam Cohen482ed822012-03-02 14:15:13 -08001163
Adam Cohend41fbf52012-02-16 23:53:59 -08001164 private void recycleTempRects(Stack<Rect> used) {
1165 while (!used.isEmpty()) {
1166 mTempRectStack.push(used.pop());
1167 }
1168 }
1169
1170 /**
1171 * Find a vacant area that will fit the given bounds nearest the requested
1172 * cell location. Uses Euclidean distance to score multiple vacant areas.
1173 *
1174 * @param pixelX The X location at which you want to search for a vacant area.
1175 * @param pixelY The Y location at which you want to search for a vacant area.
1176 * @param minSpanX The minimum horizontal span required
1177 * @param minSpanY The minimum vertical span required
1178 * @param spanX Horizontal span of the object.
1179 * @param spanY Vertical span of the object.
1180 * @param ignoreOccupied If true, the result can be an occupied cell
1181 * @param result Array in which to place the result, or null (in which case a new array will
1182 * be allocated)
1183 * @return The X, Y cell of a vacant area that can contain this object,
1184 * nearest the requested location.
1185 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001186 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1187 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001188 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001189
Adam Cohene3e27a82011-04-15 12:07:39 -07001190 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1191 // to the center of the item, but we are searching based on the top-left cell, so
1192 // we translate the point over to correspond to the top-left.
1193 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1194 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1195
Jeff Sharkey70864282009-04-07 21:08:40 -07001196 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001197 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001198 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001199 final Rect bestRect = new Rect(-1, -1, -1, -1);
1200 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001201
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001202 final int countX = mCountX;
1203 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001204
Adam Cohend41fbf52012-02-16 23:53:59 -08001205 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1206 spanX < minSpanX || spanY < minSpanY) {
1207 return bestXY;
1208 }
1209
1210 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001211 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001212 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1213 int ySize = -1;
1214 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001215 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001216 // First, let's see if this thing fits anywhere
1217 for (int i = 0; i < minSpanX; i++) {
1218 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001219 if (mOccupied.cells[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001220 continue inner;
1221 }
Michael Jurkac28de512010-08-13 11:27:44 -07001222 }
1223 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001224 xSize = minSpanX;
1225 ySize = minSpanY;
1226
1227 // We know that the item will fit at _some_ acceptable size, now let's see
1228 // how big we can make it. We'll alternate between incrementing x and y spans
1229 // until we hit a limit.
1230 boolean incX = true;
1231 boolean hitMaxX = xSize >= spanX;
1232 boolean hitMaxY = ySize >= spanY;
1233 while (!(hitMaxX && hitMaxY)) {
1234 if (incX && !hitMaxX) {
1235 for (int j = 0; j < ySize; j++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001236 if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001237 // We can't move out horizontally
1238 hitMaxX = true;
1239 }
1240 }
1241 if (!hitMaxX) {
1242 xSize++;
1243 }
1244 } else if (!hitMaxY) {
1245 for (int i = 0; i < xSize; i++) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001246 if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001247 // We can't move out vertically
1248 hitMaxY = true;
1249 }
1250 }
1251 if (!hitMaxY) {
1252 ySize++;
1253 }
1254 }
1255 hitMaxX |= xSize >= spanX;
1256 hitMaxY |= ySize >= spanY;
1257 incX = !incX;
1258 }
1259 incX = true;
1260 hitMaxX = xSize >= spanX;
1261 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001262 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001263 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001264 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265
Adam Cohend41fbf52012-02-16 23:53:59 -08001266 // We verify that the current rect is not a sub-rect of any of our previous
1267 // candidates. In this case, the current rect is disqualified in favour of the
1268 // containing rect.
1269 Rect currentRect = mTempRectStack.pop();
1270 currentRect.set(x, y, x + xSize, y + ySize);
1271 boolean contained = false;
1272 for (Rect r : validRegions) {
1273 if (r.contains(currentRect)) {
1274 contained = true;
1275 break;
1276 }
1277 }
1278 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001279 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001280
Adam Cohend41fbf52012-02-16 23:53:59 -08001281 if ((distance <= bestDistance && !contained) ||
1282 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001283 bestDistance = distance;
1284 bestXY[0] = x;
1285 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001286 if (resultSpan != null) {
1287 resultSpan[0] = xSize;
1288 resultSpan[1] = ySize;
1289 }
1290 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001291 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 }
1293 }
1294
Adam Cohenc0dcf592011-06-01 15:30:43 -07001295 // Return -1, -1 if no suitable location found
1296 if (bestDistance == Double.MAX_VALUE) {
1297 bestXY[0] = -1;
1298 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001299 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001300 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001301 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001303
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001304 /**
Adam Cohen482ed822012-03-02 14:15:13 -08001305 * Find a vacant area that will fit the given bounds nearest the requested
1306 * cell location, and will also weigh in a suggested direction vector of the
1307 * desired location. This method computers distance based on unit grid distances,
1308 * not pixel distances.
1309 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001310 * @param cellX The X cell nearest to which you want to search for a vacant area.
1311 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001312 * @param spanX Horizontal span of the object.
1313 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001314 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001315 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001316 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001317 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001318 * @param result Array in which to place the result, or null (in which case a new array will
1319 * be allocated)
1320 * @return The X, Y cell of a vacant area that can contain this object,
1321 * nearest the requested location.
1322 */
1323 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001324 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001325 // Keep track of best-scoring drop area
1326 final int[] bestXY = result != null ? result : new int[2];
1327 float bestDistance = Float.MAX_VALUE;
1328 int bestDirectionScore = Integer.MIN_VALUE;
1329
1330 final int countX = mCountX;
1331 final int countY = mCountY;
1332
1333 for (int y = 0; y < countY - (spanY - 1); y++) {
1334 inner:
1335 for (int x = 0; x < countX - (spanX - 1); x++) {
1336 // First, let's see if this thing fits anywhere
1337 for (int i = 0; i < spanX; i++) {
1338 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001339 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001340 continue inner;
1341 }
1342 }
1343 }
1344
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001345 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001346 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001347 computeDirectionVector(x - cellX, y - cellY, curDirection);
1348 // The direction score is just the dot product of the two candidate direction
1349 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001350 int curDirectionScore = direction[0] * curDirection[0] +
1351 direction[1] * curDirection[1];
Sunny Goyal8f90dcf2016-08-18 15:01:11 -07001352 if (Float.compare(distance, bestDistance) < 0 ||
1353 (Float.compare(distance, bestDistance) == 0
1354 && curDirectionScore > bestDirectionScore)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001355 bestDistance = distance;
1356 bestDirectionScore = curDirectionScore;
1357 bestXY[0] = x;
1358 bestXY[1] = y;
1359 }
1360 }
1361 }
1362
1363 // Return -1, -1 if no suitable location found
1364 if (bestDistance == Float.MAX_VALUE) {
1365 bestXY[0] = -1;
1366 bestXY[1] = -1;
1367 }
1368 return bestXY;
1369 }
1370
1371 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001372 int[] direction, ItemConfiguration currentState) {
1373 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001374 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001375 mTmpOccupied.markCells(c, false);
1376 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001377
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001378 findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction,
1379 mTmpOccupied.cells, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001380
1381 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001382 c.cellX = mTempLocation[0];
1383 c.cellY = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001384 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001385 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001386 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001387 return success;
1388 }
1389
Adam Cohenf3900c22012-11-16 18:28:11 -08001390 /**
1391 * This helper class defines a cluster of views. It helps with defining complex edges
1392 * of the cluster and determining how those edges interact with other views. The edges
1393 * essentially define a fine-grained boundary around the cluster of views -- like a more
1394 * precise version of a bounding box.
1395 */
1396 private class ViewCluster {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001397 final static int LEFT = 1 << 0;
1398 final static int TOP = 1 << 1;
1399 final static int RIGHT = 1 << 2;
1400 final static int BOTTOM = 1 << 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001401
Adam Cohenf3900c22012-11-16 18:28:11 -08001402 ArrayList<View> views;
1403 ItemConfiguration config;
1404 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001405
Adam Cohenf3900c22012-11-16 18:28:11 -08001406 int[] leftEdge = new int[mCountY];
1407 int[] rightEdge = new int[mCountY];
1408 int[] topEdge = new int[mCountX];
1409 int[] bottomEdge = new int[mCountX];
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001410 int dirtyEdges;
1411 boolean boundingRectDirty;
Adam Cohenf3900c22012-11-16 18:28:11 -08001412
1413 @SuppressWarnings("unchecked")
1414 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1415 this.views = (ArrayList<View>) views.clone();
1416 this.config = config;
1417 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001418 }
1419
Adam Cohenf3900c22012-11-16 18:28:11 -08001420 void resetEdges() {
1421 for (int i = 0; i < mCountX; i++) {
1422 topEdge[i] = -1;
1423 bottomEdge[i] = -1;
1424 }
1425 for (int i = 0; i < mCountY; i++) {
1426 leftEdge[i] = -1;
1427 rightEdge[i] = -1;
1428 }
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001429 dirtyEdges = LEFT | TOP | RIGHT | BOTTOM;
Adam Cohenf3900c22012-11-16 18:28:11 -08001430 boundingRectDirty = true;
1431 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001432
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001433 void computeEdge(int which) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001434 int count = views.size();
1435 for (int i = 0; i < count; i++) {
1436 CellAndSpan cs = config.map.get(views.get(i));
1437 switch (which) {
1438 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001439 int left = cs.cellX;
1440 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001441 if (left < leftEdge[j] || leftEdge[j] < 0) {
1442 leftEdge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001443 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001444 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001445 break;
1446 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001447 int right = cs.cellX + cs.spanX;
1448 for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001449 if (right > rightEdge[j]) {
1450 rightEdge[j] = right;
Adam Cohenf3900c22012-11-16 18:28:11 -08001451 }
1452 }
1453 break;
1454 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001455 int top = cs.cellY;
1456 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001457 if (top < topEdge[j] || topEdge[j] < 0) {
1458 topEdge[j] = top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001459 }
1460 }
1461 break;
1462 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001463 int bottom = cs.cellY + cs.spanY;
1464 for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) {
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001465 if (bottom > bottomEdge[j]) {
1466 bottomEdge[j] = bottom;
Adam Cohenf3900c22012-11-16 18:28:11 -08001467 }
1468 }
1469 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001470 }
1471 }
1472 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001473
1474 boolean isViewTouchingEdge(View v, int whichEdge) {
1475 CellAndSpan cs = config.map.get(v);
1476
Sunny Goyal6dc98b92016-04-03 15:25:29 -07001477 if ((dirtyEdges & whichEdge) == whichEdge) {
1478 computeEdge(whichEdge);
1479 dirtyEdges &= ~whichEdge;
1480 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001481
1482 switch (whichEdge) {
1483 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001484 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1485 if (leftEdge[i] == cs.cellX + cs.spanX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001486 return true;
1487 }
1488 }
1489 break;
1490 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001491 for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) {
1492 if (rightEdge[i] == cs.cellX) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001493 return true;
1494 }
1495 }
1496 break;
1497 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001498 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1499 if (topEdge[i] == cs.cellY + cs.spanY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001500 return true;
1501 }
1502 }
1503 break;
1504 case BOTTOM:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001505 for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) {
1506 if (bottomEdge[i] == cs.cellY) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001507 return true;
1508 }
1509 }
1510 break;
1511 }
1512 return false;
1513 }
1514
1515 void shift(int whichEdge, int delta) {
1516 for (View v: views) {
1517 CellAndSpan c = config.map.get(v);
1518 switch (whichEdge) {
1519 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001520 c.cellX -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001521 break;
1522 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001523 c.cellX += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001524 break;
1525 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001526 c.cellY -= delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001527 break;
1528 case BOTTOM:
1529 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001530 c.cellY += delta;
Adam Cohenf3900c22012-11-16 18:28:11 -08001531 break;
1532 }
1533 }
1534 resetEdges();
1535 }
1536
1537 public void addView(View v) {
1538 views.add(v);
1539 resetEdges();
1540 }
1541
1542 public Rect getBoundingRect() {
1543 if (boundingRectDirty) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001544 config.getBoundingRectForViews(views, boundingRect);
Adam Cohenf3900c22012-11-16 18:28:11 -08001545 }
1546 return boundingRect;
1547 }
1548
Adam Cohenf3900c22012-11-16 18:28:11 -08001549 PositionComparator comparator = new PositionComparator();
1550 class PositionComparator implements Comparator<View> {
1551 int whichEdge = 0;
1552 public int compare(View left, View right) {
1553 CellAndSpan l = config.map.get(left);
1554 CellAndSpan r = config.map.get(right);
1555 switch (whichEdge) {
1556 case LEFT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001557 return (r.cellX + r.spanX) - (l.cellX + l.spanX);
Adam Cohenf3900c22012-11-16 18:28:11 -08001558 case RIGHT:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001559 return l.cellX - r.cellX;
Adam Cohenf3900c22012-11-16 18:28:11 -08001560 case TOP:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001561 return (r.cellY + r.spanY) - (l.cellY + l.spanY);
Adam Cohenf3900c22012-11-16 18:28:11 -08001562 case BOTTOM:
1563 default:
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001564 return l.cellY - r.cellY;
Adam Cohenf3900c22012-11-16 18:28:11 -08001565 }
1566 }
1567 }
1568
1569 public void sortConfigurationForEdgePush(int edge) {
1570 comparator.whichEdge = edge;
1571 Collections.sort(config.sortedViews, comparator);
1572 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001573 }
1574
Adam Cohenf3900c22012-11-16 18:28:11 -08001575 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1576 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001577
Adam Cohenf3900c22012-11-16 18:28:11 -08001578 ViewCluster cluster = new ViewCluster(views, currentState);
1579 Rect clusterRect = cluster.getBoundingRect();
1580 int whichEdge;
1581 int pushDistance;
1582 boolean fail = false;
1583
1584 // Determine the edge of the cluster that will be leading the push and how far
1585 // the cluster must be shifted.
1586 if (direction[0] < 0) {
1587 whichEdge = ViewCluster.LEFT;
1588 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001589 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001590 whichEdge = ViewCluster.RIGHT;
1591 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1592 } else if (direction[1] < 0) {
1593 whichEdge = ViewCluster.TOP;
1594 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1595 } else {
1596 whichEdge = ViewCluster.BOTTOM;
1597 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001598 }
1599
Adam Cohenf3900c22012-11-16 18:28:11 -08001600 // Break early for invalid push distance.
1601 if (pushDistance <= 0) {
1602 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001603 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001604
1605 // Mark the occupied state as false for the group of views we want to move.
1606 for (View v: views) {
1607 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001608 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001609 }
1610
1611 // We save the current configuration -- if we fail to find a solution we will revert
1612 // to the initial state. The process of finding a solution modifies the configuration
1613 // in place, hence the need for revert in the failure case.
1614 currentState.save();
1615
1616 // The pushing algorithm is simplified by considering the views in the order in which
1617 // they would be pushed by the cluster. For example, if the cluster is leading with its
1618 // left edge, we consider sort the views by their right edge, from right to left.
1619 cluster.sortConfigurationForEdgePush(whichEdge);
1620
1621 while (pushDistance > 0 && !fail) {
1622 for (View v: currentState.sortedViews) {
1623 // For each view that isn't in the cluster, we see if the leading edge of the
1624 // cluster is contacting the edge of that view. If so, we add that view to the
1625 // cluster.
1626 if (!cluster.views.contains(v) && v != dragView) {
1627 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1628 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1629 if (!lp.canReorder) {
1630 // The push solution includes the all apps button, this is not viable.
1631 fail = true;
1632 break;
1633 }
1634 cluster.addView(v);
1635 CellAndSpan c = currentState.map.get(v);
1636
1637 // Adding view to cluster, mark it as not occupied.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001638 mTmpOccupied.markCells(c, false);
Adam Cohenf3900c22012-11-16 18:28:11 -08001639 }
1640 }
1641 }
1642 pushDistance--;
1643
1644 // The cluster has been completed, now we move the whole thing over in the appropriate
1645 // direction.
1646 cluster.shift(whichEdge, 1);
1647 }
1648
1649 boolean foundSolution = false;
1650 clusterRect = cluster.getBoundingRect();
1651
1652 // Due to the nature of the algorithm, the only check required to verify a valid solution
1653 // is to ensure that completed shifted cluster lies completely within the cell layout.
1654 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1655 clusterRect.bottom <= mCountY) {
1656 foundSolution = true;
1657 } else {
1658 currentState.restore();
1659 }
1660
1661 // In either case, we set the occupied array as marked for the location of the views
1662 for (View v: cluster.views) {
1663 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001664 mTmpOccupied.markCells(c, true);
Adam Cohenf3900c22012-11-16 18:28:11 -08001665 }
1666
1667 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001668 }
1669
Adam Cohen482ed822012-03-02 14:15:13 -08001670 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001671 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001672 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001673
Adam Cohen8baab352012-03-20 17:39:21 -07001674 boolean success = false;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001675 Rect boundingRect = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001676 // We construct a rect which represents the entire group of views passed in
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001677 currentState.getBoundingRectForViews(views, boundingRect);
Adam Cohen8baab352012-03-20 17:39:21 -07001678
Adam Cohen8baab352012-03-20 17:39:21 -07001679 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001680 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001681 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001682 mTmpOccupied.markCells(c, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001683 }
1684
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001685 GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
Adam Cohen47a876d2012-03-19 13:21:41 -07001686 int top = boundingRect.top;
1687 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001688 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001689 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001690 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001691 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001692 blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001693 }
1694
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001695 mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001696
Adam Cohenf3900c22012-11-16 18:28:11 -08001697 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001698 boundingRect.height(), direction,
1699 mTmpOccupied.cells, blockOccupied.cells, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001700
Adam Cohen8baab352012-03-20 17:39:21 -07001701 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001702 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001703 int deltaX = mTempLocation[0] - boundingRect.left;
1704 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001705 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001706 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001707 c.cellX += deltaX;
1708 c.cellY += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001709 }
1710 success = true;
1711 }
Adam Cohen8baab352012-03-20 17:39:21 -07001712
1713 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001714 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001715 CellAndSpan c = currentState.map.get(v);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001716 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001717 }
1718 return success;
1719 }
1720
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001721 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1722 // to push items in each of the cardinal directions, in an order based on the direction vector
1723 // passed.
1724 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1725 int[] direction, View ignoreView, ItemConfiguration solution) {
1726 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001727 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001728 // separately in each of the components.
1729 int temp = direction[1];
1730 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001731
1732 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001733 ignoreView, solution)) {
1734 return true;
1735 }
1736 direction[1] = temp;
1737 temp = direction[0];
1738 direction[0] = 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 // Revert the direction
1745 direction[0] = temp;
1746
1747 // Now we try pushing in each component of the opposite direction
1748 direction[0] *= -1;
1749 direction[1] *= -1;
1750 temp = direction[1];
1751 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001752 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001753 ignoreView, solution)) {
1754 return true;
1755 }
1756
1757 direction[1] = temp;
1758 temp = direction[0];
1759 direction[0] = 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 // revert the direction
1765 direction[0] = temp;
1766 direction[0] *= -1;
1767 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001768
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001769 } else {
1770 // If the direction vector has a single non-zero component, we push first in the
1771 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001772 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001773 ignoreView, solution)) {
1774 return true;
1775 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001776 // Then we try the opposite direction
1777 direction[0] *= -1;
1778 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001779 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001780 ignoreView, solution)) {
1781 return true;
1782 }
1783 // Switch the direction back
1784 direction[0] *= -1;
1785 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001786
1787 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001788 // to find a solution by pushing along the perpendicular axis.
1789
1790 // Swap the components
1791 int temp = direction[1];
1792 direction[1] = direction[0];
1793 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001794 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001795 ignoreView, solution)) {
1796 return true;
1797 }
1798
1799 // Then we try the opposite direction
1800 direction[0] *= -1;
1801 direction[1] *= -1;
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 // Switch the direction back
1807 direction[0] *= -1;
1808 direction[1] *= -1;
1809
1810 // Swap the components back
1811 temp = direction[1];
1812 direction[1] = direction[0];
1813 direction[0] = temp;
1814 }
1815 return false;
1816 }
1817
Adam Cohen482ed822012-03-02 14:15:13 -08001818 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001819 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001820 // Return early if get invalid cell positions
1821 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001822
Adam Cohen8baab352012-03-20 17:39:21 -07001823 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001824 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001825
Adam Cohen8baab352012-03-20 17:39:21 -07001826 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001827 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001828 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001829 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001830 c.cellX = cellX;
1831 c.cellY = cellY;
Adam Cohen19f37922012-03-21 11:59:11 -07001832 }
Adam Cohen482ed822012-03-02 14:15:13 -08001833 }
Adam Cohen482ed822012-03-02 14:15:13 -08001834 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1835 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001836 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001837 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001838 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001839 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001840 r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001841 if (Rect.intersects(r0, r1)) {
1842 if (!lp.canReorder) {
1843 return false;
1844 }
1845 mIntersectingViews.add(child);
1846 }
1847 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001848
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001849 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1850
Winson Chung5f8afe62013-08-12 16:19:28 -07001851 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001852 // we try to find a solution such that no displaced item travels through another item
1853 // without also displacing that item.
1854 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001855 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001856 return true;
1857 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001858
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001859 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001860 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001861 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001862 return true;
1863 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001864
Adam Cohen482ed822012-03-02 14:15:13 -08001865 // Ok, they couldn't move as a block, let's move them individually
1866 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001867 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001868 return false;
1869 }
1870 }
1871 return true;
1872 }
1873
1874 /*
1875 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1876 * the provided point and the provided cell
1877 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001878 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001879 double angle = Math.atan(((float) deltaY) / deltaX);
1880
1881 result[0] = 0;
1882 result[1] = 0;
1883 if (Math.abs(Math.cos(angle)) > 0.5f) {
1884 result[0] = (int) Math.signum(deltaX);
1885 }
1886 if (Math.abs(Math.sin(angle)) > 0.5f) {
1887 result[1] = (int) Math.signum(deltaY);
1888 }
1889 }
1890
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001891 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001892 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1893 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001894 // Copy the current state into the solution. This solution will be manipulated as necessary.
1895 copyCurrentStateToSolution(solution, false);
1896 // Copy the current occupied array into the temporary occupied array. This array will be
1897 // manipulated as necessary to find a solution.
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001898 mOccupied.copyTo(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001899
1900 // We find the nearest cell into which we would place the dragged item, assuming there's
1901 // nothing in its way.
1902 int result[] = new int[2];
1903 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1904
1905 boolean success = false;
1906 // First we try the exact nearest position of the item being dragged,
1907 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001908 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1909 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001910
1911 if (!success) {
1912 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1913 // x, then 1 in y etc.
1914 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001915 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1916 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001917 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001918 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1919 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001920 }
1921 solution.isSolution = false;
1922 } else {
1923 solution.isSolution = true;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001924 solution.cellX = result[0];
1925 solution.cellY = result[1];
1926 solution.spanX = spanX;
1927 solution.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001928 }
1929 return solution;
1930 }
1931
1932 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001933 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001934 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001935 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001936 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001937 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001938 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001939 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001940 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001941 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001942 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001943 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001944 }
1945 }
1946
1947 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001948 mTmpOccupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001949
Michael Jurkaa52570f2012-03-20 03:18:20 -07001950 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001951 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001952 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001953 if (child == dragView) continue;
1954 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001955 CellAndSpan c = solution.map.get(child);
1956 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001957 lp.tmpCellX = c.cellX;
1958 lp.tmpCellY = c.cellY;
Adam Cohen8baab352012-03-20 17:39:21 -07001959 lp.cellHSpan = c.spanX;
1960 lp.cellVSpan = c.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001961 mTmpOccupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001962 }
1963 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001964 mTmpOccupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001965 }
1966
1967 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1968 commitDragView) {
1969
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001970 GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1971 occupied.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001972
Michael Jurkaa52570f2012-03-20 03:18:20 -07001973 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001974 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001975 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001976 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001977 CellAndSpan c = solution.map.get(child);
1978 if (c != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001979 animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0,
Adam Cohen19f37922012-03-21 11:59:11 -07001980 DESTRUCTIVE_REORDER, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001981 occupied.markCells(c, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001982 }
1983 }
1984 if (commitDragView) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001985 occupied.markCells(solution, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001986 }
1987 }
1988
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001989
1990 // This method starts or changes the reorder preview animations
1991 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
1992 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07001993 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001994 for (int i = 0; i < childCount; i++) {
1995 View child = mShortcutsAndWidgets.getChildAt(i);
1996 if (child == dragView) continue;
1997 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001998 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
1999 != null && !solution.intersectingViews.contains(child);
2000
Adam Cohen19f37922012-03-21 11:59:11 -07002001 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002002 if (c != null && !skip) {
2003 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002004 lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002005 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002006 }
2007 }
2008 }
2009
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002010 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002011 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002012 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002013 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002014 float finalDeltaX;
2015 float finalDeltaY;
2016 float initDeltaX;
2017 float initDeltaY;
2018 float finalScale;
2019 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002020 int mode;
2021 boolean repeating = false;
2022 private static final int PREVIEW_DURATION = 300;
2023 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2024
2025 public static final int MODE_HINT = 0;
2026 public static final int MODE_PREVIEW = 1;
2027
Adam Cohene7587d22012-05-24 18:50:02 -07002028 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002029
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002030 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2031 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002032 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2033 final int x0 = mTmpPoint[0];
2034 final int y0 = mTmpPoint[1];
2035 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2036 final int x1 = mTmpPoint[0];
2037 final int y1 = mTmpPoint[1];
2038 final int dX = x1 - x0;
2039 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002040 finalDeltaX = 0;
2041 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002042 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002043 if (dX == dY && dX == 0) {
2044 } else {
2045 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002046 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002047 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002048 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002049 } else {
2050 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002051 finalDeltaX = (int) (- dir * Math.signum(dX) *
2052 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2053 finalDeltaY = (int) (- dir * Math.signum(dY) *
2054 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002055 }
2056 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002057 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002058 initDeltaX = child.getTranslationX();
2059 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002060 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002061 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002062 this.child = child;
2063 }
2064
Adam Cohend024f982012-05-23 18:26:45 -07002065 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002066 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002067 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002068 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002069 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002070 if (finalDeltaX == 0 && finalDeltaY == 0) {
2071 completeAnimationImmediately();
2072 return;
2073 }
Adam Cohen19f37922012-03-21 11:59:11 -07002074 }
Adam Cohend024f982012-05-23 18:26:45 -07002075 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002076 return;
2077 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002078 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002079 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002080
2081 // Animations are disabled in power save mode, causing the repeated animation to jump
2082 // spastically between beginning and end states. Since this looks bad, we don't repeat
2083 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002084 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002085 va.setRepeatMode(ValueAnimator.REVERSE);
2086 va.setRepeatCount(ValueAnimator.INFINITE);
2087 }
2088
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002089 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002090 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002091 va.addUpdateListener(new AnimatorUpdateListener() {
2092 @Override
2093 public void onAnimationUpdate(ValueAnimator animation) {
2094 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002095 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2096 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2097 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002098 child.setTranslationX(x);
2099 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002100 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002101 child.setScaleX(s);
2102 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002103 }
2104 });
2105 va.addListener(new AnimatorListenerAdapter() {
2106 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002107 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002108 initDeltaX = 0;
2109 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002110 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002111 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002112 }
2113 });
Adam Cohen19f37922012-03-21 11:59:11 -07002114 mShakeAnimators.put(child, this);
2115 va.start();
2116 }
2117
Adam Cohend024f982012-05-23 18:26:45 -07002118 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002119 if (a != null) {
2120 a.cancel();
2121 }
Adam Cohen19f37922012-03-21 11:59:11 -07002122 }
Adam Cohene7587d22012-05-24 18:50:02 -07002123
Adam Cohen091440a2015-03-18 14:16:05 -07002124 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002125 if (a != null) {
2126 a.cancel();
2127 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002128
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002129 a = new LauncherViewPropertyAnimator(child)
2130 .scaleX(getChildrenScale())
2131 .scaleY(getChildrenScale())
2132 .translationX(0)
2133 .translationY(0)
2134 .setDuration(REORDER_ANIMATION_DURATION);
2135 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2136 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002137 }
Adam Cohen19f37922012-03-21 11:59:11 -07002138 }
2139
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002140 private void completeAndClearReorderPreviewAnimations() {
2141 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002142 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002143 }
2144 mShakeAnimators.clear();
2145 }
2146
Adam Cohen482ed822012-03-02 14:15:13 -08002147 private void commitTempPlacement() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002148 mTmpOccupied.copyTo(mOccupied);
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002149
2150 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2151 int container = Favorites.CONTAINER_DESKTOP;
2152
2153 if (mLauncher.isHotseatLayout(this)) {
2154 screenId = -1;
2155 container = Favorites.CONTAINER_HOTSEAT;
2156 }
2157
Michael Jurkaa52570f2012-03-20 03:18:20 -07002158 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002159 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002160 View child = mShortcutsAndWidgets.getChildAt(i);
2161 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2162 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002163 // We do a null check here because the item info can be null in the case of the
2164 // AllApps button in the hotseat.
2165 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002166 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2167 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2168 || info.spanY != lp.cellVSpan);
2169
Adam Cohen2acce882012-03-28 19:03:19 -07002170 info.cellX = lp.cellX = lp.tmpCellX;
2171 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002172 info.spanX = lp.cellHSpan;
2173 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002174
2175 if (requiresDbUpdate) {
2176 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId,
2177 info.cellX, info.cellY, info.spanX, info.spanY);
2178 }
Adam Cohen2acce882012-03-28 19:03:19 -07002179 }
Adam Cohen482ed822012-03-02 14:15:13 -08002180 }
2181 }
2182
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002183 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002184 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002185 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002186 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002187 lp.useTmpCoords = useTempCoords;
2188 }
2189 }
2190
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002191 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002192 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2193 int[] result = new int[2];
2194 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002195 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002196 resultSpan);
2197 if (result[0] >= 0 && result[1] >= 0) {
2198 copyCurrentStateToSolution(solution, false);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002199 solution.cellX = result[0];
2200 solution.cellY = result[1];
2201 solution.spanX = resultSpan[0];
2202 solution.spanY = resultSpan[1];
Adam Cohen482ed822012-03-02 14:15:13 -08002203 solution.isSolution = true;
2204 } else {
2205 solution.isSolution = false;
2206 }
2207 return solution;
2208 }
2209
Adam Cohen19f37922012-03-21 11:59:11 -07002210 /* This seems like it should be obvious and straight-forward, but when the direction vector
2211 needs to match with the notion of the dragView pushing other views, we have to employ
2212 a slightly more subtle notion of the direction vector. The question is what two points is
2213 the vector between? The center of the dragView and its desired destination? Not quite, as
2214 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2215 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2216 or right, which helps make pushing feel right.
2217 */
2218 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2219 int spanY, View dragView, int[] resultDirection) {
2220 int[] targetDestination = new int[2];
2221
2222 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2223 Rect dragRect = new Rect();
2224 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2225 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2226
2227 Rect dropRegionRect = new Rect();
2228 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2229 dragView, dropRegionRect, mIntersectingViews);
2230
2231 int dropRegionSpanX = dropRegionRect.width();
2232 int dropRegionSpanY = dropRegionRect.height();
2233
2234 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2235 dropRegionRect.height(), dropRegionRect);
2236
2237 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2238 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2239
2240 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2241 deltaX = 0;
2242 }
2243 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2244 deltaY = 0;
2245 }
2246
2247 if (deltaX == 0 && deltaY == 0) {
2248 // No idea what to do, give a random direction.
2249 resultDirection[0] = 1;
2250 resultDirection[1] = 0;
2251 } else {
2252 computeDirectionVector(deltaX, deltaY, resultDirection);
2253 }
2254 }
2255
2256 // For a given cell and span, fetch the set of views intersecting the region.
2257 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2258 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2259 if (boundingRect != null) {
2260 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2261 }
2262 intersectingViews.clear();
2263 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2264 Rect r1 = new Rect();
2265 final int count = mShortcutsAndWidgets.getChildCount();
2266 for (int i = 0; i < count; i++) {
2267 View child = mShortcutsAndWidgets.getChildAt(i);
2268 if (child == dragView) continue;
2269 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2270 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2271 if (Rect.intersects(r0, r1)) {
2272 mIntersectingViews.add(child);
2273 if (boundingRect != null) {
2274 boundingRect.union(r1);
2275 }
2276 }
2277 }
2278 }
2279
2280 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2281 View dragView, int[] result) {
2282 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2283 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2284 mIntersectingViews);
2285 return !mIntersectingViews.isEmpty();
2286 }
2287
2288 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002289 completeAndClearReorderPreviewAnimations();
2290 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2291 final int count = mShortcutsAndWidgets.getChildCount();
2292 for (int i = 0; i < count; i++) {
2293 View child = mShortcutsAndWidgets.getChildAt(i);
2294 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2295 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2296 lp.tmpCellX = lp.cellX;
2297 lp.tmpCellY = lp.cellY;
2298 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2299 0, false, false);
2300 }
Adam Cohen19f37922012-03-21 11:59:11 -07002301 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002302 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002303 }
Adam Cohen19f37922012-03-21 11:59:11 -07002304 }
2305
Adam Cohenbebf0422012-04-11 18:06:28 -07002306 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2307 View dragView, int[] direction, boolean commit) {
2308 int[] pixelXY = new int[2];
2309 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2310
2311 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002312 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002313 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2314
2315 setUseTempCoords(true);
2316 if (swapSolution != null && swapSolution.isSolution) {
2317 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2318 // committing anything or animating anything as we just want to determine if a solution
2319 // exists
2320 copySolutionToTempState(swapSolution, dragView);
2321 setItemPlacementDirty(true);
2322 animateItemsToSolution(swapSolution, dragView, commit);
2323
2324 if (commit) {
2325 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002326 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002327 setItemPlacementDirty(false);
2328 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002329 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2330 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002331 }
2332 mShortcutsAndWidgets.requestLayout();
2333 }
2334 return swapSolution.isSolution;
2335 }
2336
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002337 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002338 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002339 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002340 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002341
2342 if (resultSpan == null) {
2343 resultSpan = new int[2];
2344 }
2345
Adam Cohen19f37922012-03-21 11:59:11 -07002346 // When we are checking drop validity or actually dropping, we don't recompute the
2347 // direction vector, since we want the solution to match the preview, and it's possible
2348 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002349 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2350 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002351 mDirectionVector[0] = mPreviousReorderDirection[0];
2352 mDirectionVector[1] = mPreviousReorderDirection[1];
2353 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002354 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2355 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2356 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002357 }
2358 } else {
2359 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2360 mPreviousReorderDirection[0] = mDirectionVector[0];
2361 mPreviousReorderDirection[1] = mDirectionVector[1];
2362 }
2363
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002364 // Find a solution involving pushing / displacing any items in the way
2365 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002366 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2367
2368 // We attempt the approach which doesn't shuffle views at all
2369 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2370 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2371
2372 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002373
2374 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2375 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002376 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2377 finalSolution = swapSolution;
2378 } else if (noShuffleSolution.isSolution) {
2379 finalSolution = noShuffleSolution;
2380 }
2381
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002382 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002383 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002384 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2385 ReorderPreviewAnimation.MODE_HINT);
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002386 result[0] = finalSolution.cellX;
2387 result[1] = finalSolution.cellY;
2388 resultSpan[0] = finalSolution.spanX;
2389 resultSpan[1] = finalSolution.spanY;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002390 } else {
2391 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2392 }
2393 return result;
2394 }
2395
Adam Cohen482ed822012-03-02 14:15:13 -08002396 boolean foundSolution = true;
2397 if (!DESTRUCTIVE_REORDER) {
2398 setUseTempCoords(true);
2399 }
2400
2401 if (finalSolution != null) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002402 result[0] = finalSolution.cellX;
2403 result[1] = finalSolution.cellY;
2404 resultSpan[0] = finalSolution.spanX;
2405 resultSpan[1] = finalSolution.spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002406
2407 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2408 // committing anything or animating anything as we just want to determine if a solution
2409 // exists
2410 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2411 if (!DESTRUCTIVE_REORDER) {
2412 copySolutionToTempState(finalSolution, dragView);
2413 }
2414 setItemPlacementDirty(true);
2415 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2416
Adam Cohen19f37922012-03-21 11:59:11 -07002417 if (!DESTRUCTIVE_REORDER &&
2418 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002419 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002420 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002421 setItemPlacementDirty(false);
2422 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002423 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2424 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002425 }
2426 }
2427 } else {
2428 foundSolution = false;
2429 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2430 }
2431
2432 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2433 setUseTempCoords(false);
2434 }
Adam Cohen482ed822012-03-02 14:15:13 -08002435
Michael Jurkaa52570f2012-03-20 03:18:20 -07002436 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002437 return result;
2438 }
2439
Adam Cohen19f37922012-03-21 11:59:11 -07002440 void setItemPlacementDirty(boolean dirty) {
2441 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002442 }
Adam Cohen19f37922012-03-21 11:59:11 -07002443 boolean isItemPlacementDirty() {
2444 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002445 }
2446
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002447 private static class ItemConfiguration extends CellAndSpan {
Adam Cohen8baab352012-03-20 17:39:21 -07002448 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002449 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2450 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002451 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002452 boolean isSolution = false;
Adam Cohen482ed822012-03-02 14:15:13 -08002453
Adam Cohenf3900c22012-11-16 18:28:11 -08002454 void save() {
2455 // Copy current state into savedMap
2456 for (View v: map.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002457 savedMap.get(v).copyFrom(map.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002458 }
2459 }
2460
2461 void restore() {
2462 // Restore current state from savedMap
2463 for (View v: savedMap.keySet()) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002464 map.get(v).copyFrom(savedMap.get(v));
Adam Cohenf3900c22012-11-16 18:28:11 -08002465 }
2466 }
2467
2468 void add(View v, CellAndSpan cs) {
2469 map.put(v, cs);
2470 savedMap.put(v, new CellAndSpan());
2471 sortedViews.add(v);
2472 }
2473
Adam Cohen482ed822012-03-02 14:15:13 -08002474 int area() {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002475 return spanX * spanY;
Adam Cohenf3900c22012-11-16 18:28:11 -08002476 }
2477
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002478 void getBoundingRectForViews(ArrayList<View> views, Rect outRect) {
2479 boolean first = true;
2480 for (View v: views) {
2481 CellAndSpan c = map.get(v);
2482 if (first) {
2483 outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2484 first = false;
2485 } else {
2486 outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY);
2487 }
2488 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002489 }
Adam Cohen482ed822012-03-02 14:15:13 -08002490 }
2491
Adam Cohendf035382011-04-11 17:22:04 -07002492 /**
Adam Cohendf035382011-04-11 17:22:04 -07002493 * Find a starting cell position that will fit the given bounds nearest the requested
2494 * cell location. Uses Euclidean distance to score multiple vacant areas.
2495 *
2496 * @param pixelX The X location at which you want to search for a vacant area.
2497 * @param pixelY The Y location at which you want to search for a vacant area.
2498 * @param spanX Horizontal span of the object.
2499 * @param spanY Vertical span of the object.
2500 * @param ignoreView Considers space occupied by this view as unoccupied
2501 * @param result Previously returned value to possibly recycle.
2502 * @return The X, Y cell of a vacant area that can contain this object,
2503 * nearest the requested location.
2504 */
Adam Cohenf9c184a2016-01-15 16:47:43 -08002505 public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002506 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002507 }
2508
Michael Jurka0280c3b2010-09-17 15:00:07 -07002509 boolean existsEmptyCell() {
2510 return findCellForSpan(null, 1, 1);
2511 }
2512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002514 * Finds the upper-left coordinate of the first rectangle in the grid that can
2515 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2516 * then this method will only return coordinates for rectangles that contain the cell
2517 * (intersectX, intersectY)
2518 *
2519 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2520 * can be found.
2521 * @param spanX The horizontal span of the cell we want to find.
2522 * @param spanY The vertical span of the cell we want to find.
2523 *
2524 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002525 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002526 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002527 if (cellXY == null) {
2528 cellXY = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07002529 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002530 return mOccupied.findVacantCell(cellXY, spanX, spanY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002531 }
2532
2533 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002534 * A drag event has begun over this layout.
2535 * It may have begun over this layout (in which case onDragChild is called first),
2536 * or it may have begun on another layout.
2537 */
2538 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002539 mDragging = true;
2540 }
2541
2542 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002543 * Called when drag has left this CellLayout or has been completed (successfully or not)
2544 */
2545 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002546 // This can actually be called when we aren't in a drag, e.g. when adding a new
2547 // item to this layout via the customize drawer.
2548 // Guard against that case.
2549 if (mDragging) {
2550 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002551 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002552
2553 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002554 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002555 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2556 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002557 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002558 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002559 }
2560
2561 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002562 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002563 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002564 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002565 *
2566 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002567 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002568 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002569 if (child != null) {
2570 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002571 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002572 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002573 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002574 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002575 }
2576
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002578 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002579 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 * @param cellX X coordinate of upper left corner expressed as a cell position
2581 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002582 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002583 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002584 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002585 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002586 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 final int cellWidth = mCellWidth;
2588 final int cellHeight = mCellHeight;
2589 final int widthGap = mWidthGap;
2590 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002591
Winson Chung4b825dcd2011-06-19 12:41:22 -07002592 final int hStartPadding = getPaddingLeft();
2593 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002594
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2596 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2597
2598 int x = hStartPadding + cellX * (cellWidth + widthGap);
2599 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002600
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002601 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002603
Adam Cohend4844c32011-02-18 19:25:06 -08002604 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002605 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002606 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002607 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002608 }
2609
Adam Cohend4844c32011-02-18 19:25:06 -08002610 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002611 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002612 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002613 mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002614 }
2615
Adam Cohen2801caf2011-05-13 20:57:39 -07002616 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002617 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002618 (Math.max((mCountX - 1), 0) * mWidthGap);
2619 }
2620
2621 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002622 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002623 (Math.max((mCountY - 1), 0) * mHeightGap);
2624 }
2625
Michael Jurka66d72172011-04-12 16:29:25 -07002626 public boolean isOccupied(int x, int y) {
2627 if (x < mCountX && y < mCountY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002628 return mOccupied.cells[x][y];
Michael Jurka66d72172011-04-12 16:29:25 -07002629 } else {
2630 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2631 }
2632 }
2633
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 @Override
2635 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2636 return new CellLayout.LayoutParams(getContext(), attrs);
2637 }
2638
2639 @Override
2640 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2641 return p instanceof CellLayout.LayoutParams;
2642 }
2643
2644 @Override
2645 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2646 return new CellLayout.LayoutParams(p);
2647 }
2648
2649 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2650 /**
2651 * Horizontal location of the item in the grid.
2652 */
2653 @ViewDebug.ExportedProperty
2654 public int cellX;
2655
2656 /**
2657 * Vertical location of the item in the grid.
2658 */
2659 @ViewDebug.ExportedProperty
2660 public int cellY;
2661
2662 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002663 * Temporary horizontal location of the item in the grid during reorder
2664 */
2665 public int tmpCellX;
2666
2667 /**
2668 * Temporary vertical location of the item in the grid during reorder
2669 */
2670 public int tmpCellY;
2671
2672 /**
2673 * Indicates that the temporary coordinates should be used to layout the items
2674 */
2675 public boolean useTmpCoords;
2676
2677 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002678 * Number of cells spanned horizontally by the item.
2679 */
2680 @ViewDebug.ExportedProperty
2681 public int cellHSpan;
2682
2683 /**
2684 * Number of cells spanned vertically by the item.
2685 */
2686 @ViewDebug.ExportedProperty
2687 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002688
Adam Cohen1b607ed2011-03-03 17:26:50 -08002689 /**
2690 * Indicates whether the item will set its x, y, width and height parameters freely,
2691 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2692 */
Adam Cohend4844c32011-02-18 19:25:06 -08002693 public boolean isLockedToGrid = true;
2694
Adam Cohen482ed822012-03-02 14:15:13 -08002695 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002696 * Indicates that this item should use the full extents of its parent.
2697 */
2698 public boolean isFullscreen = false;
2699
2700 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002701 * Indicates whether this item can be reordered. Always true except in the case of the
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002702 * the AllApps button and QSB place holder.
Adam Cohen482ed822012-03-02 14:15:13 -08002703 */
2704 public boolean canReorder = true;
2705
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002706 // X coordinate of the view in the layout.
2707 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002708 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002709 // Y coordinate of the view in the layout.
2710 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002711 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002712
Romain Guy84f296c2009-11-04 15:00:44 -08002713 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002714
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002715 public LayoutParams(Context c, AttributeSet attrs) {
2716 super(c, attrs);
2717 cellHSpan = 1;
2718 cellVSpan = 1;
2719 }
2720
2721 public LayoutParams(ViewGroup.LayoutParams source) {
2722 super(source);
2723 cellHSpan = 1;
2724 cellVSpan = 1;
2725 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002726
2727 public LayoutParams(LayoutParams source) {
2728 super(source);
2729 this.cellX = source.cellX;
2730 this.cellY = source.cellY;
2731 this.cellHSpan = source.cellHSpan;
2732 this.cellVSpan = source.cellVSpan;
2733 }
2734
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002735 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002736 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002737 this.cellX = cellX;
2738 this.cellY = cellY;
2739 this.cellHSpan = cellHSpan;
2740 this.cellVSpan = cellVSpan;
2741 }
2742
Adam Cohen2374abf2013-04-16 14:56:57 -07002743 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2744 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002745 if (isLockedToGrid) {
2746 final int myCellHSpan = cellHSpan;
2747 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002748 int myCellX = useTmpCoords ? tmpCellX : cellX;
2749 int myCellY = useTmpCoords ? tmpCellY : cellY;
2750
2751 if (invertHorizontally) {
2752 myCellX = colCount - myCellX - cellHSpan;
2753 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002754
Adam Cohend4844c32011-02-18 19:25:06 -08002755 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2756 leftMargin - rightMargin;
2757 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2758 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002759 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2760 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002761 }
2762 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002763
Winson Chungaafa03c2010-06-11 17:34:16 -07002764 public String toString() {
2765 return "(" + this.cellX + ", " + this.cellY + ")";
2766 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002767
2768 public void setWidth(int width) {
2769 this.width = width;
2770 }
2771
2772 public int getWidth() {
2773 return width;
2774 }
2775
2776 public void setHeight(int height) {
2777 this.height = height;
2778 }
2779
2780 public int getHeight() {
2781 return height;
2782 }
2783
2784 public void setX(int x) {
2785 this.x = x;
2786 }
2787
2788 public int getX() {
2789 return x;
2790 }
2791
2792 public void setY(int y) {
2793 this.y = y;
2794 }
2795
2796 public int getY() {
2797 return y;
2798 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002799 }
2800
Michael Jurka0280c3b2010-09-17 15:00:07 -07002801 // This class stores info for two purposes:
2802 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2803 // its spanX, spanY, and the screen it is on
2804 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2805 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2806 // the CellLayout that was long clicked
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002807 public static final class CellInfo extends CellAndSpan {
Adam Cohenf9c184a2016-01-15 16:47:43 -08002808 public View cell;
Adam Cohendcd297f2013-06-18 13:13:40 -07002809 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002810 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002811
Sunny Goyal83a8f042015-05-19 12:52:12 -07002812 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002813 cellX = info.cellX;
2814 cellY = info.cellY;
2815 spanX = info.spanX;
2816 spanY = info.spanY;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002817 cell = v;
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002818 screenId = info.screenId;
2819 container = info.container;
2820 }
2821
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002822 @Override
2823 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002824 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2825 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002826 }
2827 }
Michael Jurkad771c962011-08-09 15:00:48 -07002828
Tony Wickham86930612015-09-09 13:50:40 -07002829 /**
2830 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2831 * if necessary).
2832 */
2833 public boolean hasReorderSolution(ItemInfo itemInfo) {
2834 int[] cellPoint = new int[2];
2835 // Check for a solution starting at every cell.
2836 for (int cellX = 0; cellX < getCountX(); cellX++) {
2837 for (int cellY = 0; cellY < getCountY(); cellY++) {
2838 cellToPoint(cellX, cellY, cellPoint);
2839 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2840 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2841 true, new ItemConfiguration()).isSolution) {
2842 return true;
2843 }
2844 }
2845 }
2846 return false;
2847 }
2848
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002849 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07002850 return mOccupied.isRegionVacant(x, y, spanX, spanY);
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002851 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002852}