blob: 92e4043dffe8cab73108f1cc380f6a0ed4382592 [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;
Tony Wickham9e0702f2015-09-02 14:45:39 -070038import android.os.PowerManager;
Adam Cohenc9735cf2015-01-23 16:11:55 -080039import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070042import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080047import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
Tony Wickham9e0702f2015-09-02 14:45:39 -070049import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Sunny Goyal4b6eb262015-05-14 19:24:40 -070051import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.FolderIcon.FolderRingAnimator;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070053import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070054import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
55import com.android.launcher3.accessibility.FolderAccessibilityHelper;
56import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Sunny Goyal6c56c682015-07-16 14:09:05 -070057import com.android.launcher3.config.ProviderConfig;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070058import com.android.launcher3.util.ParcelableSparseArray;
Adam Cohen091440a2015-03-18 14:16:05 -070059import com.android.launcher3.util.Thunk;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070060
Adam Cohen69ce2e52011-07-03 19:25:21 -070061import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070062import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080063import java.util.Collections;
64import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070065import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080066import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070067
Sunny Goyal4b6eb262015-05-14 19:24:40 -070068public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070069 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
70 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
71
Winson Chungaafa03c2010-06-11 17:34:16 -070072 static final String TAG = "CellLayout";
73
Adam Cohen2acce882012-03-28 19:03:19 -070074 private Launcher mLauncher;
Adam Cohen091440a2015-03-18 14:16:05 -070075 @Thunk int mCellWidth;
76 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070077 private int mFixedCellWidth;
78 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070079
Adam Cohen091440a2015-03-18 14:16:05 -070080 @Thunk int mCountX;
81 @Thunk int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082
Adam Cohen234c4cd2011-07-17 21:03:04 -070083 private int mOriginalWidthGap;
84 private int mOriginalHeightGap;
Adam Cohen091440a2015-03-18 14:16:05 -070085 @Thunk int mWidthGap;
86 @Thunk int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070087 private int mMaxGap;
Adam Cohen917e3882013-10-31 15:03:35 -070088 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070089 private boolean mIsDragTarget = true;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070090 private boolean mJailContent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
Patrick Dubroyde7658b2010-09-27 11:15:43 -070092 // These are temporary variables to prevent having to allocate a new object just to
93 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070094 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070095 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070096
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080098 boolean[][] mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099
Michael Jurkadee05892010-07-27 10:01:56 -0700100 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -0700101 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700102
Adam Cohen69ce2e52011-07-03 19:25:21 -0700103 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -0700104 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -0700105
Michael Jurka5f1c5092010-09-03 14:15:02 -0700106 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700107
Sunny Goyal2805e632015-05-20 15:35:32 -0700108 private static final int BACKGROUND_ACTIVATE_DURATION = 120;
109 private final TransitionDrawable mBackground;
110
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700111 // These values allow a fixed measurement to be set on the CellLayout.
112 private int mFixedWidth = -1;
113 private int mFixedHeight = -1;
114
Michael Jurka33945b22010-12-21 18:19:38 -0800115 // If we're actively dragging something over this screen, mIsDragOverlapping is true
116 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700117
Winson Chung150fbab2010-09-29 17:14:26 -0700118 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700119 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk Rect[] mDragOutlines = new Rect[4];
121 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700122 private InterruptibleInOutAnimator[] mDragOutlineAnims =
123 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700124
125 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700126 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700127 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700128
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700129 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800130
Sunny Goyal316490e2015-06-02 09:38:28 -0700131 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
132 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700133
134 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700135
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700136 // When a drag operation is in progress, holds the nearest cell to the touch point
137 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Joe Onorato4be866d2010-10-10 11:26:02 -0700139 private boolean mDragging = false;
140
Patrick Dubroyce34a972010-10-19 10:34:32 -0700141 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700142 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700143
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800144 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700145 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800146
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800147 public static final int MODE_SHOW_REORDER_HINT = 0;
148 public static final int MODE_DRAG_OVER = 1;
149 public static final int MODE_ON_DROP = 2;
150 public static final int MODE_ON_DROP_EXTERNAL = 3;
151 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700152 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800153 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
154
Adam Cohena897f392012-04-27 18:12:05 -0700155 static final int LANDSCAPE = 0;
156 static final int PORTRAIT = 1;
157
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800158 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700159 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohen091440a2015-03-18 14:16:05 -0700160 @Thunk float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700161
Adam Cohen482ed822012-03-02 14:15:13 -0800162 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
163 private Rect mOccupiedRect = new Rect();
164 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700165 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700166 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800167
Sunny Goyal2805e632015-05-20 15:35:32 -0700168 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700169
Michael Jurkaca993832012-06-29 15:17:04 -0700170 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700171
Adam Cohenc9735cf2015-01-23 16:11:55 -0800172 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700173 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800174 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 public CellLayout(Context context) {
177 this(context, null);
178 }
179
180 public CellLayout(Context context, AttributeSet attrs) {
181 this(context, attrs, 0);
182 }
183
184 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
185 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700186
187 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
188 // the user where a dragged item will land when dropped.
189 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800190 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700191 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700192
Adam Cohen2e6da152015-05-06 11:42:25 -0700193 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Winson Chung11a1a532013-09-13 11:14:45 -0700195 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800196 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700197 mWidthGap = mOriginalWidthGap = 0;
198 mHeightGap = mOriginalHeightGap = 0;
199 mMaxGap = Integer.MAX_VALUE;
Adam Cohen2e6da152015-05-06 11:42:25 -0700200 mCountX = (int) grid.inv.numColumns;
201 mCountY = (int) grid.inv.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700202 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800203 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700204 mPreviousReorderDirection[0] = INVALID_DIRECTION;
205 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700208 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700209 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700210
Sunny Goyal2805e632015-05-20 15:35:32 -0700211 mBackground = (TransitionDrawable) res.getDrawable(R.drawable.bg_screenpanel);
212 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700213 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800214
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800215 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700216 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700217
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700218 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700219 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700220 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700221 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800222 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700223 }
224
225 // When dragging things around the home screens, we show a green outline of
226 // where the item will land. The outlines gradually fade out, leaving a trail
227 // behind the drag path.
228 // Set up all the animations that are used to implement this fading.
229 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700230 final float fromAlphaValue = 0;
231 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700232
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700233 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700234
235 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700236 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100237 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700238 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700239 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700240 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700241 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700242 final Bitmap outline = (Bitmap)anim.getTag();
243
244 // If an animation is started and then stopped very quickly, we can still
245 // get spurious updates we've cleared the tag. Guard against this.
246 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700247 @SuppressWarnings("all") // suppress dead code warning
248 final boolean debug = false;
249 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700250 Object val = animation.getAnimatedValue();
251 Log.d(TAG, "anim " + thisIndex + " update: " + val +
252 ", isStopped " + anim.isStopped());
253 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700254 // Try to prevent it from continuing to run
255 animation.cancel();
256 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700257 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800258 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700259 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700260 }
261 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700262 // The animation holds a reference to the drag outline bitmap as long is it's
263 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700264 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700265 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700266 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700267 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 anim.setTag(null);
269 }
270 }
271 });
272 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700273 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700274
Michael Jurkaa52570f2012-03-20 03:18:20 -0700275 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700276 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700277 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700278
Mady Mellorbb835202015-07-15 16:34:34 -0700279 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700280
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700281 mTouchFeedbackView = new ClickShadowView(context);
282 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700283 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700284 }
285
Adam Cohenc9735cf2015-01-23 16:11:55 -0800286 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700287 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800288 mUseTouchHelper = enable;
289 if (!enable) {
290 ViewCompat.setAccessibilityDelegate(this, null);
291 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
292 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
293 setOnClickListener(mLauncher);
294 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700295 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
296 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
297 mTouchHelper = new WorkspaceAccessibilityHelper(this);
298 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
299 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
300 mTouchHelper = new FolderAccessibilityHelper(this);
301 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800302 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
303 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
304 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
305 setOnClickListener(mTouchHelper);
306 }
307
308 // Invalidate the accessibility hierarchy
309 if (getParent() != null) {
310 getParent().notifySubtreeAccessibilityStateChanged(
311 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
312 }
313 }
314
315 @Override
316 public boolean dispatchHoverEvent(MotionEvent event) {
317 // Always attempt to dispatch hover events to accessibility first.
318 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
319 return true;
320 }
321 return super.dispatchHoverEvent(event);
322 }
323
324 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800325 public boolean onInterceptTouchEvent(MotionEvent ev) {
326 if (mUseTouchHelper ||
327 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
328 return true;
329 }
330 return false;
331 }
332
Mady Melloref044dd2015-06-02 15:35:07 -0700333 @Override
334 public boolean onTouchEvent(MotionEvent ev) {
335 boolean handled = super.onTouchEvent(ev);
336 // Stylus button press on a home screen should not switch between overview mode and
337 // the home screen mode, however, once in overview mode stylus button press should be
338 // enabled to allow rearranging the different home screens. So check what mode
339 // the workspace is in, and only perform stylus button presses while in overview mode.
340 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700341 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700342 return true;
343 }
344 return handled;
345 }
346
Chris Craik01f2d7f2013-10-01 14:41:56 -0700347 public void enableHardwareLayer(boolean hasLayer) {
348 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700349 }
350
351 public void buildHardwareLayer() {
352 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700353 }
354
Adam Cohen307fe232012-08-16 17:55:58 -0700355 public float getChildrenScale() {
356 return mIsHotseat ? mHotseatScale : 1.0f;
357 }
358
Winson Chung5f8afe62013-08-12 16:19:28 -0700359 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700360 mFixedCellWidth = mCellWidth = width;
361 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700362 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
363 mCountX, mCountY);
364 }
365
Adam Cohen2801caf2011-05-13 20:57:39 -0700366 public void setGridSize(int x, int y) {
367 mCountX = x;
368 mCountY = y;
369 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800370 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700371 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700372 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700373 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700374 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700375 }
376
Adam Cohen2374abf2013-04-16 14:56:57 -0700377 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
378 public void setInvertIfRtl(boolean invert) {
379 mShortcutsAndWidgets.setInvertIfRtl(invert);
380 }
381
Adam Cohen917e3882013-10-31 15:03:35 -0700382 public void setDropPending(boolean pending) {
383 mDropPending = pending;
384 }
385
386 public boolean isDropPending() {
387 return mDropPending;
388 }
389
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700390 @Override
391 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700392 if (icon == null || background == null) {
393 mTouchFeedbackView.setBitmap(null);
394 mTouchFeedbackView.animate().cancel();
395 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700396 if (mTouchFeedbackView.setBitmap(background)) {
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700397 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets);
398 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700399 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800400 }
401 }
402
Adam Cohenc50438c2014-08-19 17:43:05 -0700403 void disableDragTarget() {
404 mIsDragTarget = false;
405 }
406
Vadim Tryshevfedca432015-08-19 17:55:02 -0700407 public boolean isDragTarget() {
Adam Cohenc50438c2014-08-19 17:43:05 -0700408 return mIsDragTarget;
409 }
410
411 void setIsDragOverlapping(boolean isDragOverlapping) {
412 if (mIsDragOverlapping != isDragOverlapping) {
413 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700414 if (mIsDragOverlapping) {
415 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
416 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700417 if (mBackgroundAlpha > 0f) {
418 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
419 } else {
420 mBackground.resetTransition();
421 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700422 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700423 invalidate();
424 }
425 }
426
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700427 public void disableJailContent() {
428 mJailContent = false;
429 }
430
431 @Override
432 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
433 if (mJailContent) {
434 ParcelableSparseArray jail = getJailedArray(container);
435 super.dispatchSaveInstanceState(jail);
436 container.put(R.id.cell_layout_jail_id, jail);
437 } else {
438 super.dispatchSaveInstanceState(container);
439 }
440 }
441
442 @Override
443 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
444 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
445 }
446
447 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
448 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
449 return parcelable instanceof ParcelableSparseArray ?
450 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
451 }
452
Vadim Tryshevfedca432015-08-19 17:55:02 -0700453 public boolean getIsDragOverlapping() {
Michael Jurka33945b22010-12-21 18:19:38 -0800454 return mIsDragOverlapping;
455 }
456
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700457 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700458 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700459 if (!mIsDragTarget) {
460 return;
461 }
462
Michael Jurka3e7c7632010-10-02 16:01:03 -0700463 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
464 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
465 // When we're small, we are either drawn normally or in the "accepts drops" state (during
466 // a drag). However, we also drag the mini hover background *over* one of those two
467 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700468 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700469 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700470 }
Romain Guya6abce82009-11-10 02:54:41 -0800471
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700472 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700473 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700474 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700475 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700476 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700477 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700478 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700479 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700480 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800481
Adam Cohen482ed822012-03-02 14:15:13 -0800482 if (DEBUG_VISUALIZE_OCCUPIED) {
483 int[] pt = new int[2];
484 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700485 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800486 for (int i = 0; i < mCountX; i++) {
487 for (int j = 0; j < mCountY; j++) {
488 if (mOccupied[i][j]) {
489 cellToPoint(i, j, pt);
490 canvas.save();
491 canvas.translate(pt[0], pt[1]);
492 cd.draw(canvas);
493 canvas.restore();
494 }
495 }
496 }
497 }
498
Andrew Flynn850d2e72012-04-26 16:51:20 -0700499 int previewOffset = FolderRingAnimator.sPreviewSize;
500
Adam Cohen69ce2e52011-07-03 19:25:21 -0700501 // The folder outer / inner ring image(s)
Adam Cohen2e6da152015-05-06 11:42:25 -0700502 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700503 for (int i = 0; i < mFolderOuterRings.size(); i++) {
504 FolderRingAnimator fra = mFolderOuterRings.get(i);
505
Adam Cohen5108bc02013-09-20 17:04:51 -0700506 Drawable d;
507 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700508 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700509 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700510
Winson Chung89f97052013-09-20 11:32:26 -0700511 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700512 int centerX = mTempLocation[0] + mCellWidth / 2;
513 int centerY = mTempLocation[1] + previewOffset / 2 +
514 child.getPaddingTop() + grid.folderBackgroundOffset;
515
Adam Cohen5108bc02013-09-20 17:04:51 -0700516 // Draw outer ring, if it exists
517 if (FolderIcon.HAS_OUTER_RING) {
518 d = FolderRingAnimator.sSharedOuterRingDrawable;
519 width = (int) (fra.getOuterRingSize() * getChildrenScale());
520 height = width;
521 canvas.save();
522 canvas.translate(centerX - width / 2, centerY - height / 2);
523 d.setBounds(0, 0, width, height);
524 d.draw(canvas);
525 canvas.restore();
526 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700527
Winson Chung89f97052013-09-20 11:32:26 -0700528 // Draw inner ring
529 d = FolderRingAnimator.sSharedInnerRingDrawable;
530 width = (int) (fra.getInnerRingSize() * getChildrenScale());
531 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700532 canvas.save();
533 canvas.translate(centerX - width / 2, centerY - width / 2);
534 d.setBounds(0, 0, width, height);
535 d.draw(canvas);
536 canvas.restore();
537 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700538 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700539
540 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
541 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
542 int width = d.getIntrinsicWidth();
543 int height = d.getIntrinsicHeight();
544
545 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700546 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700547 if (child != null) {
548 int centerX = mTempLocation[0] + mCellWidth / 2;
549 int centerY = mTempLocation[1] + previewOffset / 2 +
550 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700551
Winson Chung89f97052013-09-20 11:32:26 -0700552 canvas.save();
553 canvas.translate(centerX - width / 2, centerY - width / 2);
554 d.setBounds(0, 0, width, height);
555 d.draw(canvas);
556 canvas.restore();
557 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700558 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700559 }
560
561 public void showFolderAccept(FolderRingAnimator fra) {
562 mFolderOuterRings.add(fra);
563 }
564
565 public void hideFolderAccept(FolderRingAnimator fra) {
566 if (mFolderOuterRings.contains(fra)) {
567 mFolderOuterRings.remove(fra);
568 }
569 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700570 }
571
Adam Cohenc51934b2011-07-26 21:07:43 -0700572 public void setFolderLeaveBehindCell(int x, int y) {
573 mFolderLeaveBehindCell[0] = x;
574 mFolderLeaveBehindCell[1] = y;
575 invalidate();
576 }
577
578 public void clearFolderLeaveBehind() {
579 mFolderLeaveBehindCell[0] = -1;
580 mFolderLeaveBehindCell[1] = -1;
581 invalidate();
582 }
583
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700584 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700585 public boolean shouldDelayChildPressedState() {
586 return false;
587 }
588
Adam Cohen1462de32012-07-24 22:34:36 -0700589 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700590 try {
591 dispatchRestoreInstanceState(states);
592 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700593 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700594 throw ex;
595 }
596 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
597 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
598 }
Adam Cohen1462de32012-07-24 22:34:36 -0700599 }
600
Michael Jurkae6235dd2011-10-04 15:02:05 -0700601 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700602 public void cancelLongPress() {
603 super.cancelLongPress();
604
605 // Cancel long press for all children
606 final int count = getChildCount();
607 for (int i = 0; i < count; i++) {
608 final View child = getChildAt(i);
609 child.cancelLongPress();
610 }
611 }
612
Michael Jurkadee05892010-07-27 10:01:56 -0700613 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
614 mInterceptTouchListener = listener;
615 }
616
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800617 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700618 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 }
620
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800621 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700622 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 }
624
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800625 public void setIsHotseat(boolean isHotseat) {
626 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700627 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800628 }
629
Sunny Goyale9b651e2015-04-24 11:44:51 -0700630 public boolean isHotseat() {
631 return mIsHotseat;
632 }
633
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800634 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700635 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700636 final LayoutParams lp = params;
637
Andrew Flynnde38e422012-05-08 11:22:15 -0700638 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800639 if (child instanceof BubbleTextView) {
640 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700641 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800642 }
643
Adam Cohen307fe232012-08-16 17:55:58 -0700644 child.setScaleX(getChildrenScale());
645 child.setScaleY(getChildrenScale());
646
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 // Generate an id for each view, this assumes we have at most 256x256 cells
648 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700649 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700650 // If the horizontal or vertical span is set to -1, it is taken to
651 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700652 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
653 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654
Winson Chungaafa03c2010-06-11 17:34:16 -0700655 child.setId(childId);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700656 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700657
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700658 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700659
Winson Chungaafa03c2010-06-11 17:34:16 -0700660 return true;
661 }
662 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 public void removeAllViews() {
667 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700668 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700669 }
670
671 @Override
672 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700673 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700674 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700675 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700676 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700677 }
678
679 @Override
680 public void removeView(View view) {
681 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700682 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700683 }
684
685 @Override
686 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700687 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
688 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700689 }
690
691 @Override
692 public void removeViewInLayout(View view) {
693 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700694 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700695 }
696
697 @Override
698 public void removeViews(int start, int count) {
699 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700700 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700701 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700702 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700703 }
704
705 @Override
706 public void removeViewsInLayout(int start, int count) {
707 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700708 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700709 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700710 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800711 }
712
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700713 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700714 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 * @param x X coordinate of the point
716 * @param y Y coordinate of the point
717 * @param result Array of 2 ints to hold the x and y coordinate of the cell
718 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700719 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700720 final int hStartPadding = getPaddingLeft();
721 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722
723 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
724 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
725
Adam Cohend22015c2010-07-26 22:02:18 -0700726 final int xAxis = mCountX;
727 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728
729 if (result[0] < 0) result[0] = 0;
730 if (result[0] >= xAxis) result[0] = xAxis - 1;
731 if (result[1] < 0) result[1] = 0;
732 if (result[1] >= yAxis) result[1] = yAxis - 1;
733 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700734
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 /**
736 * Given a point, return the cell that most closely encloses that point
737 * @param x X coordinate of the point
738 * @param y Y coordinate of the point
739 * @param result Array of 2 ints to hold the x and y coordinate of the cell
740 */
741 void pointToCellRounded(int x, int y, int[] result) {
742 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
743 }
744
745 /**
746 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700747 *
748 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700750 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 * @param result Array of 2 ints to hold the x and y coordinate of the point
752 */
753 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700754 final int hStartPadding = getPaddingLeft();
755 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756
757 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
758 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
759 }
760
Adam Cohene3e27a82011-04-15 12:07:39 -0700761 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800762 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700763 *
764 * @param cellX X coordinate of the cell
765 * @param cellY Y coordinate of the cell
766 *
767 * @param result Array of 2 ints to hold the x and y coordinate of the point
768 */
769 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700770 regionToCenterPoint(cellX, cellY, 1, 1, result);
771 }
772
773 /**
774 * Given a cell coordinate and span return the point that represents the center of the regio
775 *
776 * @param cellX X coordinate of the cell
777 * @param cellY Y coordinate of the cell
778 *
779 * @param result Array of 2 ints to hold the x and y coordinate of the point
780 */
781 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700782 final int hStartPadding = getPaddingLeft();
783 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700784 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
785 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
786 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
787 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700788 }
789
Adam Cohen19f37922012-03-21 11:59:11 -0700790 /**
791 * Given a cell coordinate and span fills out a corresponding pixel rect
792 *
793 * @param cellX X coordinate of the cell
794 * @param cellY Y coordinate of the cell
795 * @param result Rect in which to write the result
796 */
797 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
798 final int hStartPadding = getPaddingLeft();
799 final int vStartPadding = getPaddingTop();
800 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
801 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
802 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
803 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
804 }
805
Adam Cohen482ed822012-03-02 14:15:13 -0800806 public float getDistanceFromCell(float x, float y, int[] cell) {
807 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700808 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800809 }
810
Romain Guy84f296c2009-11-04 15:00:44 -0800811 int getCellWidth() {
812 return mCellWidth;
813 }
814
815 int getCellHeight() {
816 return mCellHeight;
817 }
818
Adam Cohend4844c32011-02-18 19:25:06 -0800819 int getWidthGap() {
820 return mWidthGap;
821 }
822
823 int getHeightGap() {
824 return mHeightGap;
825 }
826
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700827 public void setFixedSize(int width, int height) {
828 mFixedWidth = width;
829 mFixedHeight = height;
830 }
831
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 @Override
833 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700836 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
837 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700838 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
839 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700840 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700841 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
842 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700843 if (cw != mCellWidth || ch != mCellHeight) {
844 mCellWidth = cw;
845 mCellHeight = ch;
846 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
847 mHeightGap, mCountX, mCountY);
848 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700849 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700850
Winson Chung2d75f122013-09-23 16:53:31 -0700851 int newWidth = childWidthSize;
852 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700853 if (mFixedWidth > 0 && mFixedHeight > 0) {
854 newWidth = mFixedWidth;
855 newHeight = mFixedHeight;
856 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
858 }
859
Adam Cohend22015c2010-07-26 22:02:18 -0700860 int numWidthGaps = mCountX - 1;
861 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862
Adam Cohen234c4cd2011-07-17 21:03:04 -0700863 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700864 int hSpace = childWidthSize;
865 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700866 int hFreeSpace = hSpace - (mCountX * mCellWidth);
867 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700868 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
869 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700870 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
871 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700872 } else {
873 mWidthGap = mOriginalWidthGap;
874 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700875 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700876
877 // Make the feedback view large enough to hold the blur bitmap.
878 mTouchFeedbackView.measure(
879 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
880 MeasureSpec.EXACTLY),
881 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
882 MeasureSpec.EXACTLY));
883
884 mShortcutsAndWidgets.measure(
885 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
886 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
887
888 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
889 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700890 if (mFixedWidth > 0 && mFixedHeight > 0) {
891 setMeasuredDimension(maxWidth, maxHeight);
892 } else {
893 setMeasuredDimension(widthSize, heightSize);
894 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
896
897 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700898 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -0700899 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
900 (mCountX * mCellWidth);
901 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
902 int top = getPaddingTop();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700903
904 mTouchFeedbackView.layout(left, top,
905 left + mTouchFeedbackView.getMeasuredWidth(),
906 top + mTouchFeedbackView.getMeasuredHeight());
907 mShortcutsAndWidgets.layout(left, top,
908 left + r - l,
909 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 }
911
912 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700913 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
914 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -0700915
916 // Expand the background drawing bounds by the padding baked into the background drawable
Sunny Goyal2805e632015-05-20 15:35:32 -0700917 mBackground.getPadding(mTempRect);
918 mBackground.setBounds(-mTempRect.left, -mTempRect.top,
919 w + mTempRect.right, h + mTempRect.bottom);
Michael Jurkadee05892010-07-27 10:01:56 -0700920 }
921
922 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700924 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 }
926
927 @Override
928 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700929 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 }
931
Michael Jurka5f1c5092010-09-03 14:15:02 -0700932 public float getBackgroundAlpha() {
933 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700934 }
935
Michael Jurka5f1c5092010-09-03 14:15:02 -0700936 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800937 if (mBackgroundAlpha != alpha) {
938 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700939 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800940 }
Michael Jurkadee05892010-07-27 10:01:56 -0700941 }
942
Sunny Goyal2805e632015-05-20 15:35:32 -0700943 @Override
944 protected boolean verifyDrawable(Drawable who) {
945 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
946 }
947
Michael Jurkaa52570f2012-03-20 03:18:20 -0700948 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700949 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700950 }
951
Michael Jurkaa52570f2012-03-20 03:18:20 -0700952 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700953 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700954 }
955
Patrick Dubroy440c3602010-07-13 17:50:32 -0700956 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700957 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700958 }
959
Adam Cohen76fc0852011-06-17 13:26:23 -0700960 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800961 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700962 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800963 boolean[][] occupied = mOccupied;
964 if (!permanent) {
965 occupied = mTmpOccupied;
966 }
967
Adam Cohen19f37922012-03-21 11:59:11 -0700968 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700969 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
970 final ItemInfo info = (ItemInfo) child.getTag();
971
972 // We cancel any existing animations
973 if (mReorderAnimators.containsKey(lp)) {
974 mReorderAnimators.get(lp).cancel();
975 mReorderAnimators.remove(lp);
976 }
977
Adam Cohen482ed822012-03-02 14:15:13 -0800978 final int oldX = lp.x;
979 final int oldY = lp.y;
980 if (adjustOccupied) {
981 occupied[lp.cellX][lp.cellY] = false;
982 occupied[cellX][cellY] = true;
983 }
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
Adam Cohen482ed822012-03-02 14:15:13 -08001044 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
Sunny Goyale78e3d72015-09-24 11:23:31 -07001045 int cellY, 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
Adam Cohen2801caf2011-05-13 20:57:39 -07001049 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001050 return;
1051 }
1052
Adam Cohen482ed822012-03-02 14:15:13 -08001053 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001054 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1055 Rect dragRegion = dragObject.dragView.getDragRegion();
1056
Adam Cohen482ed822012-03-02 14:15:13 -08001057 mDragCell[0] = cellX;
1058 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001059
Joe Onorato4be866d2010-10-10 11:26:02 -07001060 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001061 mDragOutlineAnims[oldIndex].animateOut();
1062 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001063 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001064
Adam Cohend41fbf52012-02-16 23:53:59 -08001065 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001066 cellToRect(cellX, cellY, spanX, spanY, r);
Sunny Goyal106bf642015-07-16 12:18:06 -07001067 } else {
1068 // Find the top left corner of the rect the object will occupy
1069 final int[] topLeft = mTmpPoint;
1070 cellToPoint(cellX, cellY, topLeft);
1071
1072 int left = topLeft[0];
1073 int top = topLeft[1];
1074
1075 if (v != null && dragOffset == null) {
1076 // When drawing the drag outline, it did not account for margin offsets
1077 // added by the view's parent.
1078 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1079 left += lp.leftMargin;
1080 top += lp.topMargin;
1081
1082 // Offsets due to the size difference between the View and the dragOutline.
1083 // There is a size difference to account for the outer blur, which may lie
1084 // outside the bounds of the view.
1085 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1086 // We center about the x axis
1087 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1088 - dragOutline.getWidth()) / 2;
1089 } else {
1090 if (dragOffset != null && dragRegion != null) {
1091 // Center the drag region *horizontally* in the cell and apply a drag
1092 // outline offset
1093 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1094 - dragRegion.width()) / 2;
1095 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1096 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1097 top += dragOffset.y + cellPaddingY;
1098 } else {
1099 // Center the drag outline in the cell
1100 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1101 - dragOutline.getWidth()) / 2;
1102 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1103 - dragOutline.getHeight()) / 2;
1104 }
1105 }
1106 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001107 }
Winson Chung150fbab2010-09-29 17:14:26 -07001108
Sunny Goyal106bf642015-07-16 12:18:06 -07001109 Utilities.scaleRectAboutCenter(r, getChildrenScale());
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001110 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1111 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001112
1113 if (dragObject.stateAnnouncer != null) {
1114 String msg;
1115 if (isHotseat()) {
1116 msg = getContext().getString(R.string.move_to_hotseat_position,
1117 Math.max(cellX, cellY) + 1);
1118 } else {
1119 msg = getContext().getString(R.string.move_to_empty_cell,
1120 cellY + 1, cellX + 1);
1121 }
1122 dragObject.stateAnnouncer.announce(msg);
1123 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001124 }
1125 }
1126
Adam Cohene0310962011-04-18 16:15:31 -07001127 public void clearDragOutlines() {
1128 final int oldIndex = mDragOutlineCurrent;
1129 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001130 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001131 }
1132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001134 * Find a vacant area that will fit the given bounds nearest the requested
1135 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001136 *
Romain Guy51afc022009-05-04 18:03:43 -07001137 * @param pixelX The X location at which you want to search for a vacant area.
1138 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001139 * @param minSpanX The minimum horizontal span required
1140 * @param minSpanY The minimum vertical span required
1141 * @param spanX Horizontal span of the object.
1142 * @param spanY Vertical span of the object.
1143 * @param result Array in which to place the result, or null (in which case a new array will
1144 * be allocated)
1145 * @return The X, Y cell of a vacant area that can contain this object,
1146 * nearest the requested location.
1147 */
1148 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1149 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001150 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001151 result, resultSpan);
1152 }
1153
Adam Cohend41fbf52012-02-16 23:53:59 -08001154 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1155 private void lazyInitTempRectStack() {
1156 if (mTempRectStack.isEmpty()) {
1157 for (int i = 0; i < mCountX * mCountY; i++) {
1158 mTempRectStack.push(new Rect());
1159 }
1160 }
1161 }
Adam Cohen482ed822012-03-02 14:15:13 -08001162
Adam Cohend41fbf52012-02-16 23:53:59 -08001163 private void recycleTempRects(Stack<Rect> used) {
1164 while (!used.isEmpty()) {
1165 mTempRectStack.push(used.pop());
1166 }
1167 }
1168
1169 /**
1170 * Find a vacant area that will fit the given bounds nearest the requested
1171 * cell location. Uses Euclidean distance to score multiple vacant areas.
1172 *
1173 * @param pixelX The X location at which you want to search for a vacant area.
1174 * @param pixelY The Y location at which you want to search for a vacant area.
1175 * @param minSpanX The minimum horizontal span required
1176 * @param minSpanY The minimum vertical span required
1177 * @param spanX Horizontal span of the object.
1178 * @param spanY Vertical span of the object.
1179 * @param ignoreOccupied If true, the result can be an occupied cell
1180 * @param result Array in which to place the result, or null (in which case a new array will
1181 * be allocated)
1182 * @return The X, Y cell of a vacant area that can contain this object,
1183 * nearest the requested location.
1184 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001185 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1186 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001187 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001188
Adam Cohene3e27a82011-04-15 12:07:39 -07001189 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1190 // to the center of the item, but we are searching based on the top-left cell, so
1191 // we translate the point over to correspond to the top-left.
1192 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1193 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1194
Jeff Sharkey70864282009-04-07 21:08:40 -07001195 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001196 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001197 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001198 final Rect bestRect = new Rect(-1, -1, -1, -1);
1199 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001200
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001201 final int countX = mCountX;
1202 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001203
Adam Cohend41fbf52012-02-16 23:53:59 -08001204 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1205 spanX < minSpanX || spanY < minSpanY) {
1206 return bestXY;
1207 }
1208
1209 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001210 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001211 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1212 int ySize = -1;
1213 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001214 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001215 // First, let's see if this thing fits anywhere
1216 for (int i = 0; i < minSpanX; i++) {
1217 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001218 if (mOccupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001219 continue inner;
1220 }
Michael Jurkac28de512010-08-13 11:27:44 -07001221 }
1222 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001223 xSize = minSpanX;
1224 ySize = minSpanY;
1225
1226 // We know that the item will fit at _some_ acceptable size, now let's see
1227 // how big we can make it. We'll alternate between incrementing x and y spans
1228 // until we hit a limit.
1229 boolean incX = true;
1230 boolean hitMaxX = xSize >= spanX;
1231 boolean hitMaxY = ySize >= spanY;
1232 while (!(hitMaxX && hitMaxY)) {
1233 if (incX && !hitMaxX) {
1234 for (int j = 0; j < ySize; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001235 if (x + xSize > countX -1 || mOccupied[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001236 // We can't move out horizontally
1237 hitMaxX = true;
1238 }
1239 }
1240 if (!hitMaxX) {
1241 xSize++;
1242 }
1243 } else if (!hitMaxY) {
1244 for (int i = 0; i < xSize; i++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001245 if (y + ySize > countY - 1 || mOccupied[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001246 // We can't move out vertically
1247 hitMaxY = true;
1248 }
1249 }
1250 if (!hitMaxY) {
1251 ySize++;
1252 }
1253 }
1254 hitMaxX |= xSize >= spanX;
1255 hitMaxY |= ySize >= spanY;
1256 incX = !incX;
1257 }
1258 incX = true;
1259 hitMaxX = xSize >= spanX;
1260 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001261 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001262 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001263 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264
Adam Cohend41fbf52012-02-16 23:53:59 -08001265 // We verify that the current rect is not a sub-rect of any of our previous
1266 // candidates. In this case, the current rect is disqualified in favour of the
1267 // containing rect.
1268 Rect currentRect = mTempRectStack.pop();
1269 currentRect.set(x, y, x + xSize, y + ySize);
1270 boolean contained = false;
1271 for (Rect r : validRegions) {
1272 if (r.contains(currentRect)) {
1273 contained = true;
1274 break;
1275 }
1276 }
1277 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001278 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001279
Adam Cohend41fbf52012-02-16 23:53:59 -08001280 if ((distance <= bestDistance && !contained) ||
1281 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001282 bestDistance = distance;
1283 bestXY[0] = x;
1284 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001285 if (resultSpan != null) {
1286 resultSpan[0] = xSize;
1287 resultSpan[1] = ySize;
1288 }
1289 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001290 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
1292 }
1293
Adam Cohenc0dcf592011-06-01 15:30:43 -07001294 // Return -1, -1 if no suitable location found
1295 if (bestDistance == Double.MAX_VALUE) {
1296 bestXY[0] = -1;
1297 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001298 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001299 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001300 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001302
Adam Cohen482ed822012-03-02 14:15:13 -08001303 /**
1304 * Find a vacant area that will fit the given bounds nearest the requested
1305 * cell location, and will also weigh in a suggested direction vector of the
1306 * desired location. This method computers distance based on unit grid distances,
1307 * not pixel distances.
1308 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001309 * @param cellX The X cell nearest to which you want to search for a vacant area.
1310 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001311 * @param spanX Horizontal span of the object.
1312 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001313 * @param direction The favored direction in which the views should move from x, y
1314 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1315 * matches exactly. Otherwise we find the best matching direction.
1316 * @param occoupied The array which represents which cells in the CellLayout are occupied
1317 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001318 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001319 * @param result Array in which to place the result, or null (in which case a new array will
1320 * be allocated)
1321 * @return The X, Y cell of a vacant area that can contain this object,
1322 * nearest the requested location.
1323 */
1324 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001325 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001326 // Keep track of best-scoring drop area
1327 final int[] bestXY = result != null ? result : new int[2];
1328 float bestDistance = Float.MAX_VALUE;
1329 int bestDirectionScore = Integer.MIN_VALUE;
1330
1331 final int countX = mCountX;
1332 final int countY = mCountY;
1333
1334 for (int y = 0; y < countY - (spanY - 1); y++) {
1335 inner:
1336 for (int x = 0; x < countX - (spanX - 1); x++) {
1337 // First, let's see if this thing fits anywhere
1338 for (int i = 0; i < spanX; i++) {
1339 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001340 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001341 continue inner;
1342 }
1343 }
1344 }
1345
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001346 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001347 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001348 computeDirectionVector(x - cellX, y - cellY, curDirection);
1349 // The direction score is just the dot product of the two candidate direction
1350 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001351 int curDirectionScore = direction[0] * curDirection[0] +
1352 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001353 boolean exactDirectionOnly = false;
1354 boolean directionMatches = direction[0] == curDirection[0] &&
1355 direction[0] == curDirection[0];
1356 if ((directionMatches || !exactDirectionOnly) &&
1357 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001358 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1359 bestDistance = distance;
1360 bestDirectionScore = curDirectionScore;
1361 bestXY[0] = x;
1362 bestXY[1] = y;
1363 }
1364 }
1365 }
1366
1367 // Return -1, -1 if no suitable location found
1368 if (bestDistance == Float.MAX_VALUE) {
1369 bestXY[0] = -1;
1370 bestXY[1] = -1;
1371 }
1372 return bestXY;
1373 }
1374
1375 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001376 int[] direction, ItemConfiguration currentState) {
1377 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001378 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001379 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001380 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1381
Adam Cohen8baab352012-03-20 17:39:21 -07001382 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001383
1384 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001385 c.x = mTempLocation[0];
1386 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001387 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001388 }
Adam Cohen8baab352012-03-20 17:39:21 -07001389 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001390 return success;
1391 }
1392
Adam Cohenf3900c22012-11-16 18:28:11 -08001393 /**
1394 * This helper class defines a cluster of views. It helps with defining complex edges
1395 * of the cluster and determining how those edges interact with other views. The edges
1396 * essentially define a fine-grained boundary around the cluster of views -- like a more
1397 * precise version of a bounding box.
1398 */
1399 private class ViewCluster {
1400 final static int LEFT = 0;
1401 final static int TOP = 1;
1402 final static int RIGHT = 2;
1403 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001404
Adam Cohenf3900c22012-11-16 18:28:11 -08001405 ArrayList<View> views;
1406 ItemConfiguration config;
1407 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001408
Adam Cohenf3900c22012-11-16 18:28:11 -08001409 int[] leftEdge = new int[mCountY];
1410 int[] rightEdge = new int[mCountY];
1411 int[] topEdge = new int[mCountX];
1412 int[] bottomEdge = new int[mCountX];
1413 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1414
1415 @SuppressWarnings("unchecked")
1416 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1417 this.views = (ArrayList<View>) views.clone();
1418 this.config = config;
1419 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001420 }
1421
Adam Cohenf3900c22012-11-16 18:28:11 -08001422 void resetEdges() {
1423 for (int i = 0; i < mCountX; i++) {
1424 topEdge[i] = -1;
1425 bottomEdge[i] = -1;
1426 }
1427 for (int i = 0; i < mCountY; i++) {
1428 leftEdge[i] = -1;
1429 rightEdge[i] = -1;
1430 }
1431 leftEdgeDirty = true;
1432 rightEdgeDirty = true;
1433 bottomEdgeDirty = true;
1434 topEdgeDirty = true;
1435 boundingRectDirty = true;
1436 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001437
Adam Cohenf3900c22012-11-16 18:28:11 -08001438 void computeEdge(int which, int[] edge) {
1439 int count = views.size();
1440 for (int i = 0; i < count; i++) {
1441 CellAndSpan cs = config.map.get(views.get(i));
1442 switch (which) {
1443 case LEFT:
1444 int left = cs.x;
1445 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1446 if (left < edge[j] || edge[j] < 0) {
1447 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001448 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001449 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001450 break;
1451 case RIGHT:
1452 int right = cs.x + cs.spanX;
1453 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1454 if (right > edge[j]) {
1455 edge[j] = right;
1456 }
1457 }
1458 break;
1459 case TOP:
1460 int top = cs.y;
1461 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1462 if (top < edge[j] || edge[j] < 0) {
1463 edge[j] = top;
1464 }
1465 }
1466 break;
1467 case BOTTOM:
1468 int bottom = cs.y + cs.spanY;
1469 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1470 if (bottom > edge[j]) {
1471 edge[j] = bottom;
1472 }
1473 }
1474 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001475 }
1476 }
1477 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001478
1479 boolean isViewTouchingEdge(View v, int whichEdge) {
1480 CellAndSpan cs = config.map.get(v);
1481
1482 int[] edge = getEdge(whichEdge);
1483
1484 switch (whichEdge) {
1485 case LEFT:
1486 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1487 if (edge[i] == cs.x + cs.spanX) {
1488 return true;
1489 }
1490 }
1491 break;
1492 case RIGHT:
1493 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1494 if (edge[i] == cs.x) {
1495 return true;
1496 }
1497 }
1498 break;
1499 case TOP:
1500 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1501 if (edge[i] == cs.y + cs.spanY) {
1502 return true;
1503 }
1504 }
1505 break;
1506 case BOTTOM:
1507 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1508 if (edge[i] == cs.y) {
1509 return true;
1510 }
1511 }
1512 break;
1513 }
1514 return false;
1515 }
1516
1517 void shift(int whichEdge, int delta) {
1518 for (View v: views) {
1519 CellAndSpan c = config.map.get(v);
1520 switch (whichEdge) {
1521 case LEFT:
1522 c.x -= delta;
1523 break;
1524 case RIGHT:
1525 c.x += delta;
1526 break;
1527 case TOP:
1528 c.y -= delta;
1529 break;
1530 case BOTTOM:
1531 default:
1532 c.y += delta;
1533 break;
1534 }
1535 }
1536 resetEdges();
1537 }
1538
1539 public void addView(View v) {
1540 views.add(v);
1541 resetEdges();
1542 }
1543
1544 public Rect getBoundingRect() {
1545 if (boundingRectDirty) {
1546 boolean first = true;
1547 for (View v: views) {
1548 CellAndSpan c = config.map.get(v);
1549 if (first) {
1550 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1551 first = false;
1552 } else {
1553 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1554 }
1555 }
1556 }
1557 return boundingRect;
1558 }
1559
1560 public int[] getEdge(int which) {
1561 switch (which) {
1562 case LEFT:
1563 return getLeftEdge();
1564 case RIGHT:
1565 return getRightEdge();
1566 case TOP:
1567 return getTopEdge();
1568 case BOTTOM:
1569 default:
1570 return getBottomEdge();
1571 }
1572 }
1573
1574 public int[] getLeftEdge() {
1575 if (leftEdgeDirty) {
1576 computeEdge(LEFT, leftEdge);
1577 }
1578 return leftEdge;
1579 }
1580
1581 public int[] getRightEdge() {
1582 if (rightEdgeDirty) {
1583 computeEdge(RIGHT, rightEdge);
1584 }
1585 return rightEdge;
1586 }
1587
1588 public int[] getTopEdge() {
1589 if (topEdgeDirty) {
1590 computeEdge(TOP, topEdge);
1591 }
1592 return topEdge;
1593 }
1594
1595 public int[] getBottomEdge() {
1596 if (bottomEdgeDirty) {
1597 computeEdge(BOTTOM, bottomEdge);
1598 }
1599 return bottomEdge;
1600 }
1601
1602 PositionComparator comparator = new PositionComparator();
1603 class PositionComparator implements Comparator<View> {
1604 int whichEdge = 0;
1605 public int compare(View left, View right) {
1606 CellAndSpan l = config.map.get(left);
1607 CellAndSpan r = config.map.get(right);
1608 switch (whichEdge) {
1609 case LEFT:
1610 return (r.x + r.spanX) - (l.x + l.spanX);
1611 case RIGHT:
1612 return l.x - r.x;
1613 case TOP:
1614 return (r.y + r.spanY) - (l.y + l.spanY);
1615 case BOTTOM:
1616 default:
1617 return l.y - r.y;
1618 }
1619 }
1620 }
1621
1622 public void sortConfigurationForEdgePush(int edge) {
1623 comparator.whichEdge = edge;
1624 Collections.sort(config.sortedViews, comparator);
1625 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001626 }
1627
Adam Cohenf3900c22012-11-16 18:28:11 -08001628 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1629 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001630
Adam Cohenf3900c22012-11-16 18:28:11 -08001631 ViewCluster cluster = new ViewCluster(views, currentState);
1632 Rect clusterRect = cluster.getBoundingRect();
1633 int whichEdge;
1634 int pushDistance;
1635 boolean fail = false;
1636
1637 // Determine the edge of the cluster that will be leading the push and how far
1638 // the cluster must be shifted.
1639 if (direction[0] < 0) {
1640 whichEdge = ViewCluster.LEFT;
1641 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001642 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001643 whichEdge = ViewCluster.RIGHT;
1644 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1645 } else if (direction[1] < 0) {
1646 whichEdge = ViewCluster.TOP;
1647 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1648 } else {
1649 whichEdge = ViewCluster.BOTTOM;
1650 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001651 }
1652
Adam Cohenf3900c22012-11-16 18:28:11 -08001653 // Break early for invalid push distance.
1654 if (pushDistance <= 0) {
1655 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001656 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001657
1658 // Mark the occupied state as false for the group of views we want to move.
1659 for (View v: views) {
1660 CellAndSpan c = currentState.map.get(v);
1661 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1662 }
1663
1664 // We save the current configuration -- if we fail to find a solution we will revert
1665 // to the initial state. The process of finding a solution modifies the configuration
1666 // in place, hence the need for revert in the failure case.
1667 currentState.save();
1668
1669 // The pushing algorithm is simplified by considering the views in the order in which
1670 // they would be pushed by the cluster. For example, if the cluster is leading with its
1671 // left edge, we consider sort the views by their right edge, from right to left.
1672 cluster.sortConfigurationForEdgePush(whichEdge);
1673
1674 while (pushDistance > 0 && !fail) {
1675 for (View v: currentState.sortedViews) {
1676 // For each view that isn't in the cluster, we see if the leading edge of the
1677 // cluster is contacting the edge of that view. If so, we add that view to the
1678 // cluster.
1679 if (!cluster.views.contains(v) && v != dragView) {
1680 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1681 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1682 if (!lp.canReorder) {
1683 // The push solution includes the all apps button, this is not viable.
1684 fail = true;
1685 break;
1686 }
1687 cluster.addView(v);
1688 CellAndSpan c = currentState.map.get(v);
1689
1690 // Adding view to cluster, mark it as not occupied.
1691 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1692 }
1693 }
1694 }
1695 pushDistance--;
1696
1697 // The cluster has been completed, now we move the whole thing over in the appropriate
1698 // direction.
1699 cluster.shift(whichEdge, 1);
1700 }
1701
1702 boolean foundSolution = false;
1703 clusterRect = cluster.getBoundingRect();
1704
1705 // Due to the nature of the algorithm, the only check required to verify a valid solution
1706 // is to ensure that completed shifted cluster lies completely within the cell layout.
1707 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1708 clusterRect.bottom <= mCountY) {
1709 foundSolution = true;
1710 } else {
1711 currentState.restore();
1712 }
1713
1714 // In either case, we set the occupied array as marked for the location of the views
1715 for (View v: cluster.views) {
1716 CellAndSpan c = currentState.map.get(v);
1717 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1718 }
1719
1720 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001721 }
1722
Adam Cohen482ed822012-03-02 14:15:13 -08001723 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001724 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001725 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001726
Adam Cohen8baab352012-03-20 17:39:21 -07001727 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001728 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001729 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001730 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001731 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001732 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001733 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001734 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001735 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001736 }
1737 }
Adam Cohen8baab352012-03-20 17:39:21 -07001738
Adam Cohen8baab352012-03-20 17:39:21 -07001739 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001740 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001741 CellAndSpan c = currentState.map.get(v);
1742 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1743 }
1744
Adam Cohen47a876d2012-03-19 13:21:41 -07001745 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1746 int top = boundingRect.top;
1747 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001748 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001749 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001750 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001751 CellAndSpan c = currentState.map.get(v);
1752 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001753 }
1754
Adam Cohen482ed822012-03-02 14:15:13 -08001755 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1756
Adam Cohenf3900c22012-11-16 18:28:11 -08001757 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1758 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001759
Adam Cohen8baab352012-03-20 17:39:21 -07001760 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001761 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001762 int deltaX = mTempLocation[0] - boundingRect.left;
1763 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001764 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001765 CellAndSpan c = currentState.map.get(v);
1766 c.x += deltaX;
1767 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001768 }
1769 success = true;
1770 }
Adam Cohen8baab352012-03-20 17:39:21 -07001771
1772 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001773 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001774 CellAndSpan c = currentState.map.get(v);
1775 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001776 }
1777 return success;
1778 }
1779
1780 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1781 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1782 }
1783
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001784 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1785 // to push items in each of the cardinal directions, in an order based on the direction vector
1786 // passed.
1787 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1788 int[] direction, View ignoreView, ItemConfiguration solution) {
1789 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001790 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001791 // separately in each of the components.
1792 int temp = direction[1];
1793 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001794
1795 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001796 ignoreView, solution)) {
1797 return true;
1798 }
1799 direction[1] = temp;
1800 temp = direction[0];
1801 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001802
1803 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001804 ignoreView, solution)) {
1805 return true;
1806 }
1807 // Revert the direction
1808 direction[0] = temp;
1809
1810 // Now we try pushing in each component of the opposite direction
1811 direction[0] *= -1;
1812 direction[1] *= -1;
1813 temp = direction[1];
1814 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001815 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001816 ignoreView, solution)) {
1817 return true;
1818 }
1819
1820 direction[1] = temp;
1821 temp = direction[0];
1822 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001823 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001824 ignoreView, solution)) {
1825 return true;
1826 }
1827 // revert the direction
1828 direction[0] = temp;
1829 direction[0] *= -1;
1830 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001831
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001832 } else {
1833 // If the direction vector has a single non-zero component, we push first in the
1834 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001835 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001836 ignoreView, solution)) {
1837 return true;
1838 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001839 // Then we try the opposite direction
1840 direction[0] *= -1;
1841 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001842 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001843 ignoreView, solution)) {
1844 return true;
1845 }
1846 // Switch the direction back
1847 direction[0] *= -1;
1848 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001849
1850 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001851 // to find a solution by pushing along the perpendicular axis.
1852
1853 // Swap the components
1854 int temp = direction[1];
1855 direction[1] = direction[0];
1856 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001857 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001858 ignoreView, solution)) {
1859 return true;
1860 }
1861
1862 // Then we try the opposite direction
1863 direction[0] *= -1;
1864 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001865 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001866 ignoreView, solution)) {
1867 return true;
1868 }
1869 // Switch the direction back
1870 direction[0] *= -1;
1871 direction[1] *= -1;
1872
1873 // Swap the components back
1874 temp = direction[1];
1875 direction[1] = direction[0];
1876 direction[0] = temp;
1877 }
1878 return false;
1879 }
1880
Adam Cohen482ed822012-03-02 14:15:13 -08001881 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001882 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001883 // Return early if get invalid cell positions
1884 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001885
Adam Cohen8baab352012-03-20 17:39:21 -07001886 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001887 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001888
Adam Cohen8baab352012-03-20 17:39:21 -07001889 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001890 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001891 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001892 if (c != null) {
1893 c.x = cellX;
1894 c.y = cellY;
1895 }
Adam Cohen482ed822012-03-02 14:15:13 -08001896 }
Adam Cohen482ed822012-03-02 14:15:13 -08001897 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1898 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001899 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001900 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001901 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001902 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001903 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001904 if (Rect.intersects(r0, r1)) {
1905 if (!lp.canReorder) {
1906 return false;
1907 }
1908 mIntersectingViews.add(child);
1909 }
1910 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001911
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001912 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1913
Winson Chung5f8afe62013-08-12 16:19:28 -07001914 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001915 // we try to find a solution such that no displaced item travels through another item
1916 // without also displacing that item.
1917 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001918 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001919 return true;
1920 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001921
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001922 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001923 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001924 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001925 return true;
1926 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001927
Adam Cohen482ed822012-03-02 14:15:13 -08001928 // Ok, they couldn't move as a block, let's move them individually
1929 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001930 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001931 return false;
1932 }
1933 }
1934 return true;
1935 }
1936
1937 /*
1938 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1939 * the provided point and the provided cell
1940 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001941 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001942 double angle = Math.atan(((float) deltaY) / deltaX);
1943
1944 result[0] = 0;
1945 result[1] = 0;
1946 if (Math.abs(Math.cos(angle)) > 0.5f) {
1947 result[0] = (int) Math.signum(deltaX);
1948 }
1949 if (Math.abs(Math.sin(angle)) > 0.5f) {
1950 result[1] = (int) Math.signum(deltaY);
1951 }
1952 }
1953
Adam Cohen8baab352012-03-20 17:39:21 -07001954 private void copyOccupiedArray(boolean[][] occupied) {
1955 for (int i = 0; i < mCountX; i++) {
1956 for (int j = 0; j < mCountY; j++) {
1957 occupied[i][j] = mOccupied[i][j];
1958 }
1959 }
1960 }
1961
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001962 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001963 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1964 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001965 // Copy the current state into the solution. This solution will be manipulated as necessary.
1966 copyCurrentStateToSolution(solution, false);
1967 // Copy the current occupied array into the temporary occupied array. This array will be
1968 // manipulated as necessary to find a solution.
1969 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001970
1971 // We find the nearest cell into which we would place the dragged item, assuming there's
1972 // nothing in its way.
1973 int result[] = new int[2];
1974 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1975
1976 boolean success = false;
1977 // First we try the exact nearest position of the item being dragged,
1978 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001979 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1980 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001981
1982 if (!success) {
1983 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1984 // x, then 1 in y etc.
1985 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001986 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1987 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001988 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001989 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1990 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001991 }
1992 solution.isSolution = false;
1993 } else {
1994 solution.isSolution = true;
1995 solution.dragViewX = result[0];
1996 solution.dragViewY = result[1];
1997 solution.dragViewSpanX = spanX;
1998 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001999 }
2000 return solution;
2001 }
2002
2003 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002004 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002005 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002006 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002007 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002008 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002009 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002010 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002011 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002012 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002013 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002014 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002015 }
2016 }
2017
2018 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2019 for (int i = 0; i < mCountX; i++) {
2020 for (int j = 0; j < mCountY; j++) {
2021 mTmpOccupied[i][j] = false;
2022 }
2023 }
2024
Michael Jurkaa52570f2012-03-20 03:18:20 -07002025 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002026 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002027 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002028 if (child == dragView) continue;
2029 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002030 CellAndSpan c = solution.map.get(child);
2031 if (c != null) {
2032 lp.tmpCellX = c.x;
2033 lp.tmpCellY = c.y;
2034 lp.cellHSpan = c.spanX;
2035 lp.cellVSpan = c.spanY;
2036 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002037 }
2038 }
2039 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2040 solution.dragViewSpanY, mTmpOccupied, true);
2041 }
2042
2043 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2044 commitDragView) {
2045
2046 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2047 for (int i = 0; i < mCountX; i++) {
2048 for (int j = 0; j < mCountY; j++) {
2049 occupied[i][j] = false;
2050 }
2051 }
2052
Michael Jurkaa52570f2012-03-20 03:18:20 -07002053 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002054 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002055 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002056 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002057 CellAndSpan c = solution.map.get(child);
2058 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002059 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2060 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002061 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002062 }
2063 }
2064 if (commitDragView) {
2065 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2066 solution.dragViewSpanY, occupied, true);
2067 }
2068 }
2069
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002070
2071 // This method starts or changes the reorder preview animations
2072 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2073 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002074 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002075 for (int i = 0; i < childCount; i++) {
2076 View child = mShortcutsAndWidgets.getChildAt(i);
2077 if (child == dragView) continue;
2078 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002079 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2080 != null && !solution.intersectingViews.contains(child);
2081
Adam Cohen19f37922012-03-21 11:59:11 -07002082 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002083 if (c != null && !skip) {
2084 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2085 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002086 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002087 }
2088 }
2089 }
2090
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002091 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002092 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002093 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002094 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002095 float finalDeltaX;
2096 float finalDeltaY;
2097 float initDeltaX;
2098 float initDeltaY;
2099 float finalScale;
2100 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002101 int mode;
2102 boolean repeating = false;
2103 private static final int PREVIEW_DURATION = 300;
2104 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2105
2106 public static final int MODE_HINT = 0;
2107 public static final int MODE_PREVIEW = 1;
2108
Adam Cohene7587d22012-05-24 18:50:02 -07002109 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002110
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002111 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2112 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002113 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2114 final int x0 = mTmpPoint[0];
2115 final int y0 = mTmpPoint[1];
2116 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2117 final int x1 = mTmpPoint[0];
2118 final int y1 = mTmpPoint[1];
2119 final int dX = x1 - x0;
2120 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002121 finalDeltaX = 0;
2122 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002123 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002124 if (dX == dY && dX == 0) {
2125 } else {
2126 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002127 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002128 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002129 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002130 } else {
2131 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002132 finalDeltaX = (int) (- dir * Math.signum(dX) *
2133 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2134 finalDeltaY = (int) (- dir * Math.signum(dY) *
2135 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002136 }
2137 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002138 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002139 initDeltaX = child.getTranslationX();
2140 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002141 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002142 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002143 this.child = child;
2144 }
2145
Adam Cohend024f982012-05-23 18:26:45 -07002146 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002147 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002148 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002149 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002150 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002151 if (finalDeltaX == 0 && finalDeltaY == 0) {
2152 completeAnimationImmediately();
2153 return;
2154 }
Adam Cohen19f37922012-03-21 11:59:11 -07002155 }
Adam Cohend024f982012-05-23 18:26:45 -07002156 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002157 return;
2158 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002159 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002160 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002161
2162 // Animations are disabled in power save mode, causing the repeated animation to jump
2163 // spastically between beginning and end states. Since this looks bad, we don't repeat
2164 // the animation in power save mode.
2165 PowerManager powerManager = (PowerManager) getContext()
2166 .getSystemService(Context.POWER_SERVICE);
2167 boolean powerSaverOn = Utilities.ATLEAST_LOLLIPOP && powerManager.isPowerSaveMode();
2168 if (!powerSaverOn) {
2169 va.setRepeatMode(ValueAnimator.REVERSE);
2170 va.setRepeatCount(ValueAnimator.INFINITE);
2171 }
2172
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002173 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002174 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002175 va.addUpdateListener(new AnimatorUpdateListener() {
2176 @Override
2177 public void onAnimationUpdate(ValueAnimator animation) {
2178 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002179 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2180 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2181 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002182 child.setTranslationX(x);
2183 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002184 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002185 child.setScaleX(s);
2186 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002187 }
2188 });
2189 va.addListener(new AnimatorListenerAdapter() {
2190 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002191 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002192 initDeltaX = 0;
2193 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002194 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002195 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002196 }
2197 });
Adam Cohen19f37922012-03-21 11:59:11 -07002198 mShakeAnimators.put(child, this);
2199 va.start();
2200 }
2201
Adam Cohend024f982012-05-23 18:26:45 -07002202 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002203 if (a != null) {
2204 a.cancel();
2205 }
Adam Cohen19f37922012-03-21 11:59:11 -07002206 }
Adam Cohene7587d22012-05-24 18:50:02 -07002207
Adam Cohen091440a2015-03-18 14:16:05 -07002208 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002209 if (a != null) {
2210 a.cancel();
2211 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002212
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002213 a = new LauncherViewPropertyAnimator(child)
2214 .scaleX(getChildrenScale())
2215 .scaleY(getChildrenScale())
2216 .translationX(0)
2217 .translationY(0)
2218 .setDuration(REORDER_ANIMATION_DURATION);
2219 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2220 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002221 }
Adam Cohen19f37922012-03-21 11:59:11 -07002222 }
2223
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002224 private void completeAndClearReorderPreviewAnimations() {
2225 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002226 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002227 }
2228 mShakeAnimators.clear();
2229 }
2230
Adam Cohen482ed822012-03-02 14:15:13 -08002231 private void commitTempPlacement() {
2232 for (int i = 0; i < mCountX; i++) {
2233 for (int j = 0; j < mCountY; j++) {
2234 mOccupied[i][j] = mTmpOccupied[i][j];
2235 }
2236 }
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002237
2238 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2239 int container = Favorites.CONTAINER_DESKTOP;
2240
2241 if (mLauncher.isHotseatLayout(this)) {
2242 screenId = -1;
2243 container = Favorites.CONTAINER_HOTSEAT;
2244 }
2245
Michael Jurkaa52570f2012-03-20 03:18:20 -07002246 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002247 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002248 View child = mShortcutsAndWidgets.getChildAt(i);
2249 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2250 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002251 // We do a null check here because the item info can be null in the case of the
2252 // AllApps button in the hotseat.
2253 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002254 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2255 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2256 || info.spanY != lp.cellVSpan);
2257
Adam Cohen2acce882012-03-28 19:03:19 -07002258 info.cellX = lp.cellX = lp.tmpCellX;
2259 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002260 info.spanX = lp.cellHSpan;
2261 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002262
2263 if (requiresDbUpdate) {
2264 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId,
2265 info.cellX, info.cellY, info.spanX, info.spanY);
2266 }
Adam Cohen2acce882012-03-28 19:03:19 -07002267 }
Adam Cohen482ed822012-03-02 14:15:13 -08002268 }
2269 }
2270
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002271 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002272 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002273 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002274 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002275 lp.useTmpCoords = useTempCoords;
2276 }
2277 }
2278
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002279 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002280 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2281 int[] result = new int[2];
2282 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002283 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002284 resultSpan);
2285 if (result[0] >= 0 && result[1] >= 0) {
2286 copyCurrentStateToSolution(solution, false);
2287 solution.dragViewX = result[0];
2288 solution.dragViewY = result[1];
2289 solution.dragViewSpanX = resultSpan[0];
2290 solution.dragViewSpanY = resultSpan[1];
2291 solution.isSolution = true;
2292 } else {
2293 solution.isSolution = false;
2294 }
2295 return solution;
2296 }
2297
2298 public void prepareChildForDrag(View child) {
2299 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002300 }
2301
Adam Cohen19f37922012-03-21 11:59:11 -07002302 /* This seems like it should be obvious and straight-forward, but when the direction vector
2303 needs to match with the notion of the dragView pushing other views, we have to employ
2304 a slightly more subtle notion of the direction vector. The question is what two points is
2305 the vector between? The center of the dragView and its desired destination? Not quite, as
2306 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2307 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2308 or right, which helps make pushing feel right.
2309 */
2310 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2311 int spanY, View dragView, int[] resultDirection) {
2312 int[] targetDestination = new int[2];
2313
2314 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2315 Rect dragRect = new Rect();
2316 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2317 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2318
2319 Rect dropRegionRect = new Rect();
2320 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2321 dragView, dropRegionRect, mIntersectingViews);
2322
2323 int dropRegionSpanX = dropRegionRect.width();
2324 int dropRegionSpanY = dropRegionRect.height();
2325
2326 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2327 dropRegionRect.height(), dropRegionRect);
2328
2329 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2330 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2331
2332 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2333 deltaX = 0;
2334 }
2335 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2336 deltaY = 0;
2337 }
2338
2339 if (deltaX == 0 && deltaY == 0) {
2340 // No idea what to do, give a random direction.
2341 resultDirection[0] = 1;
2342 resultDirection[1] = 0;
2343 } else {
2344 computeDirectionVector(deltaX, deltaY, resultDirection);
2345 }
2346 }
2347
2348 // For a given cell and span, fetch the set of views intersecting the region.
2349 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2350 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2351 if (boundingRect != null) {
2352 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2353 }
2354 intersectingViews.clear();
2355 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2356 Rect r1 = new Rect();
2357 final int count = mShortcutsAndWidgets.getChildCount();
2358 for (int i = 0; i < count; i++) {
2359 View child = mShortcutsAndWidgets.getChildAt(i);
2360 if (child == dragView) continue;
2361 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2362 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2363 if (Rect.intersects(r0, r1)) {
2364 mIntersectingViews.add(child);
2365 if (boundingRect != null) {
2366 boundingRect.union(r1);
2367 }
2368 }
2369 }
2370 }
2371
2372 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2373 View dragView, int[] result) {
2374 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2375 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2376 mIntersectingViews);
2377 return !mIntersectingViews.isEmpty();
2378 }
2379
2380 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002381 completeAndClearReorderPreviewAnimations();
2382 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2383 final int count = mShortcutsAndWidgets.getChildCount();
2384 for (int i = 0; i < count; i++) {
2385 View child = mShortcutsAndWidgets.getChildAt(i);
2386 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2387 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2388 lp.tmpCellX = lp.cellX;
2389 lp.tmpCellY = lp.cellY;
2390 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2391 0, false, false);
2392 }
Adam Cohen19f37922012-03-21 11:59:11 -07002393 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002394 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002395 }
Adam Cohen19f37922012-03-21 11:59:11 -07002396 }
2397
Adam Cohenbebf0422012-04-11 18:06:28 -07002398 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2399 View dragView, int[] direction, boolean commit) {
2400 int[] pixelXY = new int[2];
2401 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2402
2403 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002404 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002405 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2406
2407 setUseTempCoords(true);
2408 if (swapSolution != null && swapSolution.isSolution) {
2409 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2410 // committing anything or animating anything as we just want to determine if a solution
2411 // exists
2412 copySolutionToTempState(swapSolution, dragView);
2413 setItemPlacementDirty(true);
2414 animateItemsToSolution(swapSolution, dragView, commit);
2415
2416 if (commit) {
2417 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002418 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002419 setItemPlacementDirty(false);
2420 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002421 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2422 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002423 }
2424 mShortcutsAndWidgets.requestLayout();
2425 }
2426 return swapSolution.isSolution;
2427 }
2428
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002429 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002430 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002431 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002432 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002433
2434 if (resultSpan == null) {
2435 resultSpan = new int[2];
2436 }
2437
Adam Cohen19f37922012-03-21 11:59:11 -07002438 // When we are checking drop validity or actually dropping, we don't recompute the
2439 // direction vector, since we want the solution to match the preview, and it's possible
2440 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002441 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2442 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002443 mDirectionVector[0] = mPreviousReorderDirection[0];
2444 mDirectionVector[1] = mPreviousReorderDirection[1];
2445 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002446 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2447 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2448 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002449 }
2450 } else {
2451 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2452 mPreviousReorderDirection[0] = mDirectionVector[0];
2453 mPreviousReorderDirection[1] = mDirectionVector[1];
2454 }
2455
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002456 // Find a solution involving pushing / displacing any items in the way
2457 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002458 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2459
2460 // We attempt the approach which doesn't shuffle views at all
2461 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2462 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2463
2464 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002465
2466 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2467 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002468 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2469 finalSolution = swapSolution;
2470 } else if (noShuffleSolution.isSolution) {
2471 finalSolution = noShuffleSolution;
2472 }
2473
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002474 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002475 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002476 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2477 ReorderPreviewAnimation.MODE_HINT);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002478 result[0] = finalSolution.dragViewX;
2479 result[1] = finalSolution.dragViewY;
2480 resultSpan[0] = finalSolution.dragViewSpanX;
2481 resultSpan[1] = finalSolution.dragViewSpanY;
2482 } else {
2483 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2484 }
2485 return result;
2486 }
2487
Adam Cohen482ed822012-03-02 14:15:13 -08002488 boolean foundSolution = true;
2489 if (!DESTRUCTIVE_REORDER) {
2490 setUseTempCoords(true);
2491 }
2492
2493 if (finalSolution != null) {
2494 result[0] = finalSolution.dragViewX;
2495 result[1] = finalSolution.dragViewY;
2496 resultSpan[0] = finalSolution.dragViewSpanX;
2497 resultSpan[1] = finalSolution.dragViewSpanY;
2498
2499 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2500 // committing anything or animating anything as we just want to determine if a solution
2501 // exists
2502 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2503 if (!DESTRUCTIVE_REORDER) {
2504 copySolutionToTempState(finalSolution, dragView);
2505 }
2506 setItemPlacementDirty(true);
2507 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2508
Adam Cohen19f37922012-03-21 11:59:11 -07002509 if (!DESTRUCTIVE_REORDER &&
2510 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002511 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002512 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002513 setItemPlacementDirty(false);
2514 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002515 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2516 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002517 }
2518 }
2519 } else {
2520 foundSolution = false;
2521 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2522 }
2523
2524 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2525 setUseTempCoords(false);
2526 }
Adam Cohen482ed822012-03-02 14:15:13 -08002527
Michael Jurkaa52570f2012-03-20 03:18:20 -07002528 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002529 return result;
2530 }
2531
Adam Cohen19f37922012-03-21 11:59:11 -07002532 void setItemPlacementDirty(boolean dirty) {
2533 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002534 }
Adam Cohen19f37922012-03-21 11:59:11 -07002535 boolean isItemPlacementDirty() {
2536 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002537 }
2538
Adam Cohen091440a2015-03-18 14:16:05 -07002539 @Thunk class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002540 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002541 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2542 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002543 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002544 boolean isSolution = false;
2545 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2546
Adam Cohenf3900c22012-11-16 18:28:11 -08002547 void save() {
2548 // Copy current state into savedMap
2549 for (View v: map.keySet()) {
2550 map.get(v).copy(savedMap.get(v));
2551 }
2552 }
2553
2554 void restore() {
2555 // Restore current state from savedMap
2556 for (View v: savedMap.keySet()) {
2557 savedMap.get(v).copy(map.get(v));
2558 }
2559 }
2560
2561 void add(View v, CellAndSpan cs) {
2562 map.put(v, cs);
2563 savedMap.put(v, new CellAndSpan());
2564 sortedViews.add(v);
2565 }
2566
Adam Cohen482ed822012-03-02 14:15:13 -08002567 int area() {
2568 return dragViewSpanX * dragViewSpanY;
2569 }
Adam Cohen8baab352012-03-20 17:39:21 -07002570 }
2571
2572 private class CellAndSpan {
2573 int x, y;
2574 int spanX, spanY;
2575
Adam Cohenf3900c22012-11-16 18:28:11 -08002576 public CellAndSpan() {
2577 }
2578
2579 public void copy(CellAndSpan copy) {
2580 copy.x = x;
2581 copy.y = y;
2582 copy.spanX = spanX;
2583 copy.spanY = spanY;
2584 }
2585
Adam Cohen8baab352012-03-20 17:39:21 -07002586 public CellAndSpan(int x, int y, int spanX, int spanY) {
2587 this.x = x;
2588 this.y = y;
2589 this.spanX = spanX;
2590 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002591 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002592
2593 public String toString() {
2594 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2595 }
2596
Adam Cohen482ed822012-03-02 14:15:13 -08002597 }
2598
Adam Cohendf035382011-04-11 17:22:04 -07002599 /**
Adam Cohendf035382011-04-11 17:22:04 -07002600 * Find a starting cell position that will fit the given bounds nearest the requested
2601 * cell location. Uses Euclidean distance to score multiple vacant areas.
2602 *
2603 * @param pixelX The X location at which you want to search for a vacant area.
2604 * @param pixelY The Y location at which you want to search for a vacant area.
2605 * @param spanX Horizontal span of the object.
2606 * @param spanY Vertical span of the object.
2607 * @param ignoreView Considers space occupied by this view as unoccupied
2608 * @param result Previously returned value to possibly recycle.
2609 * @return The X, Y cell of a vacant area that can contain this object,
2610 * nearest the requested location.
2611 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002612 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2613 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002614 }
2615
Michael Jurka0280c3b2010-09-17 15:00:07 -07002616 boolean existsEmptyCell() {
2617 return findCellForSpan(null, 1, 1);
2618 }
2619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002620 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002621 * Finds the upper-left coordinate of the first rectangle in the grid that can
2622 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2623 * then this method will only return coordinates for rectangles that contain the cell
2624 * (intersectX, intersectY)
2625 *
2626 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2627 * can be found.
2628 * @param spanX The horizontal span of the cell we want to find.
2629 * @param spanY The vertical span of the cell we want to find.
2630 *
2631 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002632 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002633 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Michael Jurka28750fb2010-09-24 17:43:49 -07002634 boolean foundCell = false;
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002635 final int endX = mCountX - (spanX - 1);
2636 final int endY = mCountY - (spanY - 1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002637
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002638 for (int y = 0; y < endY && !foundCell; y++) {
2639 inner:
2640 for (int x = 0; x < endX; x++) {
2641 for (int i = 0; i < spanX; i++) {
2642 for (int j = 0; j < spanY; j++) {
2643 if (mOccupied[x + i][y + j]) {
2644 // small optimization: we can skip to after the column we just found
2645 // an occupied cell
2646 x += i;
2647 continue inner;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002648 }
2649 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002650 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002651 if (cellXY != null) {
2652 cellXY[0] = x;
2653 cellXY[1] = y;
2654 }
2655 foundCell = true;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002656 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002657 }
2658 }
2659
Michael Jurka28750fb2010-09-24 17:43:49 -07002660 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002661 }
2662
2663 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002664 * A drag event has begun over this layout.
2665 * It may have begun over this layout (in which case onDragChild is called first),
2666 * or it may have begun on another layout.
2667 */
2668 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002669 mDragging = true;
2670 }
2671
2672 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002673 * Called when drag has left this CellLayout or has been completed (successfully or not)
2674 */
2675 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002676 // This can actually be called when we aren't in a drag, e.g. when adding a new
2677 // item to this layout via the customize drawer.
2678 // Guard against that case.
2679 if (mDragging) {
2680 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002681 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002682
2683 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002684 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002685 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2686 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002687 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002688 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002689 }
2690
2691 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002692 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002693 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002694 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002695 *
2696 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002698 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002699 if (child != null) {
2700 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002701 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002702 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002703 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002704 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002705 }
2706
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002707 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002708 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002709 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002710 * @param cellX X coordinate of upper left corner expressed as a cell position
2711 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002712 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002714 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002715 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002716 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002717 final int cellWidth = mCellWidth;
2718 final int cellHeight = mCellHeight;
2719 final int widthGap = mWidthGap;
2720 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002721
Winson Chung4b825dcd2011-06-19 12:41:22 -07002722 final int hStartPadding = getPaddingLeft();
2723 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002724
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002725 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2726 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2727
2728 int x = hStartPadding + cellX * (cellWidth + widthGap);
2729 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002730
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002731 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002732 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002733
Michael Jurka0280c3b2010-09-17 15:00:07 -07002734 private void clearOccupiedCells() {
2735 for (int x = 0; x < mCountX; x++) {
2736 for (int y = 0; y < mCountY; y++) {
2737 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002738 }
2739 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002740 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002741
Adam Cohend4844c32011-02-18 19:25:06 -08002742 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002743 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002744 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002745 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002746 }
2747
Adam Cohend4844c32011-02-18 19:25:06 -08002748 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002749 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002750 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002751 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002752 }
2753
Adam Cohen482ed822012-03-02 14:15:13 -08002754 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2755 boolean value) {
2756 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002757 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2758 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002759 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002760 }
2761 }
2762 }
2763
Adam Cohen2801caf2011-05-13 20:57:39 -07002764 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002765 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002766 (Math.max((mCountX - 1), 0) * mWidthGap);
2767 }
2768
2769 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002770 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002771 (Math.max((mCountY - 1), 0) * mHeightGap);
2772 }
2773
Michael Jurka66d72172011-04-12 16:29:25 -07002774 public boolean isOccupied(int x, int y) {
2775 if (x < mCountX && y < mCountY) {
2776 return mOccupied[x][y];
2777 } else {
2778 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2779 }
2780 }
2781
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002782 @Override
2783 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2784 return new CellLayout.LayoutParams(getContext(), attrs);
2785 }
2786
2787 @Override
2788 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2789 return p instanceof CellLayout.LayoutParams;
2790 }
2791
2792 @Override
2793 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2794 return new CellLayout.LayoutParams(p);
2795 }
2796
2797 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2798 /**
2799 * Horizontal location of the item in the grid.
2800 */
2801 @ViewDebug.ExportedProperty
2802 public int cellX;
2803
2804 /**
2805 * Vertical location of the item in the grid.
2806 */
2807 @ViewDebug.ExportedProperty
2808 public int cellY;
2809
2810 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002811 * Temporary horizontal location of the item in the grid during reorder
2812 */
2813 public int tmpCellX;
2814
2815 /**
2816 * Temporary vertical location of the item in the grid during reorder
2817 */
2818 public int tmpCellY;
2819
2820 /**
2821 * Indicates that the temporary coordinates should be used to layout the items
2822 */
2823 public boolean useTmpCoords;
2824
2825 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002826 * Number of cells spanned horizontally by the item.
2827 */
2828 @ViewDebug.ExportedProperty
2829 public int cellHSpan;
2830
2831 /**
2832 * Number of cells spanned vertically by the item.
2833 */
2834 @ViewDebug.ExportedProperty
2835 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002836
Adam Cohen1b607ed2011-03-03 17:26:50 -08002837 /**
2838 * Indicates whether the item will set its x, y, width and height parameters freely,
2839 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2840 */
Adam Cohend4844c32011-02-18 19:25:06 -08002841 public boolean isLockedToGrid = true;
2842
Adam Cohen482ed822012-03-02 14:15:13 -08002843 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002844 * Indicates that this item should use the full extents of its parent.
2845 */
2846 public boolean isFullscreen = false;
2847
2848 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002849 * Indicates whether this item can be reordered. Always true except in the case of the
2850 * the AllApps button.
2851 */
2852 public boolean canReorder = true;
2853
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002854 // X coordinate of the view in the layout.
2855 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002856 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857 // Y coordinate of the view in the layout.
2858 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002859 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860
Romain Guy84f296c2009-11-04 15:00:44 -08002861 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002862
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002863 public LayoutParams(Context c, AttributeSet attrs) {
2864 super(c, attrs);
2865 cellHSpan = 1;
2866 cellVSpan = 1;
2867 }
2868
2869 public LayoutParams(ViewGroup.LayoutParams source) {
2870 super(source);
2871 cellHSpan = 1;
2872 cellVSpan = 1;
2873 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002874
2875 public LayoutParams(LayoutParams source) {
2876 super(source);
2877 this.cellX = source.cellX;
2878 this.cellY = source.cellY;
2879 this.cellHSpan = source.cellHSpan;
2880 this.cellVSpan = source.cellVSpan;
2881 }
2882
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002883 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002884 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002885 this.cellX = cellX;
2886 this.cellY = cellY;
2887 this.cellHSpan = cellHSpan;
2888 this.cellVSpan = cellVSpan;
2889 }
2890
Adam Cohen2374abf2013-04-16 14:56:57 -07002891 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2892 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002893 if (isLockedToGrid) {
2894 final int myCellHSpan = cellHSpan;
2895 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002896 int myCellX = useTmpCoords ? tmpCellX : cellX;
2897 int myCellY = useTmpCoords ? tmpCellY : cellY;
2898
2899 if (invertHorizontally) {
2900 myCellX = colCount - myCellX - cellHSpan;
2901 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002902
Adam Cohend4844c32011-02-18 19:25:06 -08002903 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2904 leftMargin - rightMargin;
2905 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2906 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002907 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2908 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002909 }
2910 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002911
Winson Chungaafa03c2010-06-11 17:34:16 -07002912 public String toString() {
2913 return "(" + this.cellX + ", " + this.cellY + ")";
2914 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002915
2916 public void setWidth(int width) {
2917 this.width = width;
2918 }
2919
2920 public int getWidth() {
2921 return width;
2922 }
2923
2924 public void setHeight(int height) {
2925 this.height = height;
2926 }
2927
2928 public int getHeight() {
2929 return height;
2930 }
2931
2932 public void setX(int x) {
2933 this.x = x;
2934 }
2935
2936 public int getX() {
2937 return x;
2938 }
2939
2940 public void setY(int y) {
2941 this.y = y;
2942 }
2943
2944 public int getY() {
2945 return y;
2946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002947 }
2948
Michael Jurka0280c3b2010-09-17 15:00:07 -07002949 // This class stores info for two purposes:
2950 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2951 // its spanX, spanY, and the screen it is on
2952 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2953 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2954 // the CellLayout that was long clicked
Sunny Goyal83a8f042015-05-19 12:52:12 -07002955 public static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002957 int cellX = -1;
2958 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 int spanX;
2960 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07002961 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002962 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963
Sunny Goyal83a8f042015-05-19 12:52:12 -07002964 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002965 cell = v;
2966 cellX = info.cellX;
2967 cellY = info.cellY;
2968 spanX = info.spanX;
2969 spanY = info.spanY;
2970 screenId = info.screenId;
2971 container = info.container;
2972 }
2973
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 @Override
2975 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002976 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2977 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978 }
2979 }
Michael Jurkad771c962011-08-09 15:00:48 -07002980
Sunny Goyala9116722015-04-29 13:55:58 -07002981 public boolean findVacantCell(int spanX, int spanY, int[] outXY) {
2982 return Utilities.findVacantCell(outXY, spanX, spanY, mCountX, mCountY, mOccupied);
2983 }
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002984
Tony Wickham86930612015-09-09 13:50:40 -07002985 /**
2986 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2987 * if necessary).
2988 */
2989 public boolean hasReorderSolution(ItemInfo itemInfo) {
2990 int[] cellPoint = new int[2];
2991 // Check for a solution starting at every cell.
2992 for (int cellX = 0; cellX < getCountX(); cellX++) {
2993 for (int cellY = 0; cellY < getCountY(); cellY++) {
2994 cellToPoint(cellX, cellY, cellPoint);
2995 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
2996 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
2997 true, new ItemConfiguration()).isSolution) {
2998 return true;
2999 }
3000 }
3001 }
3002 return false;
3003 }
3004
Sunny Goyal9ca9c132015-04-29 14:57:22 -07003005 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
3006 int x2 = x + spanX - 1;
3007 int y2 = y + spanY - 1;
3008 if (x < 0 || y < 0 || x2 >= mCountX || y2 >= mCountY) {
3009 return false;
3010 }
3011 for (int i = x; i <= x2; i++) {
3012 for (int j = y; j <= y2; j++) {
3013 if (mOccupied[i][j]) {
3014 return false;
3015 }
3016 }
3017 }
3018
3019 return true;
3020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003021}