blob: 653ee7ec6a240ea82cbe3f03d0d32192c0c7d13f [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,
1045 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
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) {
1054 mDragCell[0] = cellX;
1055 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001056
Joe Onorato4be866d2010-10-10 11:26:02 -07001057 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001058 mDragOutlineAnims[oldIndex].animateOut();
1059 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001060 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001061
Adam Cohend41fbf52012-02-16 23:53:59 -08001062 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001063 cellToRect(cellX, cellY, spanX, spanY, r);
Sunny Goyal106bf642015-07-16 12:18:06 -07001064 } else {
1065 // Find the top left corner of the rect the object will occupy
1066 final int[] topLeft = mTmpPoint;
1067 cellToPoint(cellX, cellY, topLeft);
1068
1069 int left = topLeft[0];
1070 int top = topLeft[1];
1071
1072 if (v != null && dragOffset == null) {
1073 // When drawing the drag outline, it did not account for margin offsets
1074 // added by the view's parent.
1075 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1076 left += lp.leftMargin;
1077 top += lp.topMargin;
1078
1079 // Offsets due to the size difference between the View and the dragOutline.
1080 // There is a size difference to account for the outer blur, which may lie
1081 // outside the bounds of the view.
1082 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1083 // We center about the x axis
1084 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1085 - dragOutline.getWidth()) / 2;
1086 } else {
1087 if (dragOffset != null && dragRegion != null) {
1088 // Center the drag region *horizontally* in the cell and apply a drag
1089 // outline offset
1090 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1091 - dragRegion.width()) / 2;
1092 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1093 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1094 top += dragOffset.y + cellPaddingY;
1095 } else {
1096 // Center the drag outline in the cell
1097 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1098 - dragOutline.getWidth()) / 2;
1099 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1100 - dragOutline.getHeight()) / 2;
1101 }
1102 }
1103 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001104 }
Winson Chung150fbab2010-09-29 17:14:26 -07001105
Sunny Goyal106bf642015-07-16 12:18:06 -07001106 Utilities.scaleRectAboutCenter(r, getChildrenScale());
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001107 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1108 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001109 }
1110 }
1111
Adam Cohene0310962011-04-18 16:15:31 -07001112 public void clearDragOutlines() {
1113 final int oldIndex = mDragOutlineCurrent;
1114 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001115 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001116 }
1117
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001119 * Find a vacant area that will fit the given bounds nearest the requested
1120 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001121 *
Romain Guy51afc022009-05-04 18:03:43 -07001122 * @param pixelX The X location at which you want to search for a vacant area.
1123 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001124 * @param minSpanX The minimum horizontal span required
1125 * @param minSpanY The minimum vertical span required
1126 * @param spanX Horizontal span of the object.
1127 * @param spanY Vertical span of the object.
1128 * @param result Array in which to place the result, or null (in which case a new array will
1129 * be allocated)
1130 * @return The X, Y cell of a vacant area that can contain this object,
1131 * nearest the requested location.
1132 */
1133 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1134 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001135 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001136 result, resultSpan);
1137 }
1138
Adam Cohend41fbf52012-02-16 23:53:59 -08001139 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1140 private void lazyInitTempRectStack() {
1141 if (mTempRectStack.isEmpty()) {
1142 for (int i = 0; i < mCountX * mCountY; i++) {
1143 mTempRectStack.push(new Rect());
1144 }
1145 }
1146 }
Adam Cohen482ed822012-03-02 14:15:13 -08001147
Adam Cohend41fbf52012-02-16 23:53:59 -08001148 private void recycleTempRects(Stack<Rect> used) {
1149 while (!used.isEmpty()) {
1150 mTempRectStack.push(used.pop());
1151 }
1152 }
1153
1154 /**
1155 * Find a vacant area that will fit the given bounds nearest the requested
1156 * cell location. Uses Euclidean distance to score multiple vacant areas.
1157 *
1158 * @param pixelX The X location at which you want to search for a vacant area.
1159 * @param pixelY The Y location at which you want to search for a vacant area.
1160 * @param minSpanX The minimum horizontal span required
1161 * @param minSpanY The minimum vertical span required
1162 * @param spanX Horizontal span of the object.
1163 * @param spanY Vertical span of the object.
1164 * @param ignoreOccupied If true, the result can be an occupied cell
1165 * @param result Array in which to place the result, or null (in which case a new array will
1166 * be allocated)
1167 * @return The X, Y cell of a vacant area that can contain this object,
1168 * nearest the requested location.
1169 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001170 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1171 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001172 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001173
Adam Cohene3e27a82011-04-15 12:07:39 -07001174 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1175 // to the center of the item, but we are searching based on the top-left cell, so
1176 // we translate the point over to correspond to the top-left.
1177 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1178 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1179
Jeff Sharkey70864282009-04-07 21:08:40 -07001180 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001181 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001182 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001183 final Rect bestRect = new Rect(-1, -1, -1, -1);
1184 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001185
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001186 final int countX = mCountX;
1187 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001188
Adam Cohend41fbf52012-02-16 23:53:59 -08001189 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1190 spanX < minSpanX || spanY < minSpanY) {
1191 return bestXY;
1192 }
1193
1194 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001195 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001196 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1197 int ySize = -1;
1198 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001199 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001200 // First, let's see if this thing fits anywhere
1201 for (int i = 0; i < minSpanX; i++) {
1202 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001203 if (mOccupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001204 continue inner;
1205 }
Michael Jurkac28de512010-08-13 11:27:44 -07001206 }
1207 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001208 xSize = minSpanX;
1209 ySize = minSpanY;
1210
1211 // We know that the item will fit at _some_ acceptable size, now let's see
1212 // how big we can make it. We'll alternate between incrementing x and y spans
1213 // until we hit a limit.
1214 boolean incX = true;
1215 boolean hitMaxX = xSize >= spanX;
1216 boolean hitMaxY = ySize >= spanY;
1217 while (!(hitMaxX && hitMaxY)) {
1218 if (incX && !hitMaxX) {
1219 for (int j = 0; j < ySize; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001220 if (x + xSize > countX -1 || mOccupied[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001221 // We can't move out horizontally
1222 hitMaxX = true;
1223 }
1224 }
1225 if (!hitMaxX) {
1226 xSize++;
1227 }
1228 } else if (!hitMaxY) {
1229 for (int i = 0; i < xSize; i++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001230 if (y + ySize > countY - 1 || mOccupied[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001231 // We can't move out vertically
1232 hitMaxY = true;
1233 }
1234 }
1235 if (!hitMaxY) {
1236 ySize++;
1237 }
1238 }
1239 hitMaxX |= xSize >= spanX;
1240 hitMaxY |= ySize >= spanY;
1241 incX = !incX;
1242 }
1243 incX = true;
1244 hitMaxX = xSize >= spanX;
1245 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001246 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001247 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001248 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249
Adam Cohend41fbf52012-02-16 23:53:59 -08001250 // We verify that the current rect is not a sub-rect of any of our previous
1251 // candidates. In this case, the current rect is disqualified in favour of the
1252 // containing rect.
1253 Rect currentRect = mTempRectStack.pop();
1254 currentRect.set(x, y, x + xSize, y + ySize);
1255 boolean contained = false;
1256 for (Rect r : validRegions) {
1257 if (r.contains(currentRect)) {
1258 contained = true;
1259 break;
1260 }
1261 }
1262 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001263 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001264
Adam Cohend41fbf52012-02-16 23:53:59 -08001265 if ((distance <= bestDistance && !contained) ||
1266 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001267 bestDistance = distance;
1268 bestXY[0] = x;
1269 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001270 if (resultSpan != null) {
1271 resultSpan[0] = xSize;
1272 resultSpan[1] = ySize;
1273 }
1274 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001275 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 }
1277 }
1278
Adam Cohenc0dcf592011-06-01 15:30:43 -07001279 // Return -1, -1 if no suitable location found
1280 if (bestDistance == Double.MAX_VALUE) {
1281 bestXY[0] = -1;
1282 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001283 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001284 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001285 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001287
Adam Cohen482ed822012-03-02 14:15:13 -08001288 /**
1289 * Find a vacant area that will fit the given bounds nearest the requested
1290 * cell location, and will also weigh in a suggested direction vector of the
1291 * desired location. This method computers distance based on unit grid distances,
1292 * not pixel distances.
1293 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001294 * @param cellX The X cell nearest to which you want to search for a vacant area.
1295 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001296 * @param spanX Horizontal span of the object.
1297 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001298 * @param direction The favored direction in which the views should move from x, y
1299 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1300 * matches exactly. Otherwise we find the best matching direction.
1301 * @param occoupied The array which represents which cells in the CellLayout are occupied
1302 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001303 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001304 * @param result Array in which to place the result, or null (in which case a new array will
1305 * be allocated)
1306 * @return The X, Y cell of a vacant area that can contain this object,
1307 * nearest the requested location.
1308 */
1309 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001310 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001311 // Keep track of best-scoring drop area
1312 final int[] bestXY = result != null ? result : new int[2];
1313 float bestDistance = Float.MAX_VALUE;
1314 int bestDirectionScore = Integer.MIN_VALUE;
1315
1316 final int countX = mCountX;
1317 final int countY = mCountY;
1318
1319 for (int y = 0; y < countY - (spanY - 1); y++) {
1320 inner:
1321 for (int x = 0; x < countX - (spanX - 1); x++) {
1322 // First, let's see if this thing fits anywhere
1323 for (int i = 0; i < spanX; i++) {
1324 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001325 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001326 continue inner;
1327 }
1328 }
1329 }
1330
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001331 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001332 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001333 computeDirectionVector(x - cellX, y - cellY, curDirection);
1334 // The direction score is just the dot product of the two candidate direction
1335 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001336 int curDirectionScore = direction[0] * curDirection[0] +
1337 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001338 boolean exactDirectionOnly = false;
1339 boolean directionMatches = direction[0] == curDirection[0] &&
1340 direction[0] == curDirection[0];
1341 if ((directionMatches || !exactDirectionOnly) &&
1342 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001343 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1344 bestDistance = distance;
1345 bestDirectionScore = curDirectionScore;
1346 bestXY[0] = x;
1347 bestXY[1] = y;
1348 }
1349 }
1350 }
1351
1352 // Return -1, -1 if no suitable location found
1353 if (bestDistance == Float.MAX_VALUE) {
1354 bestXY[0] = -1;
1355 bestXY[1] = -1;
1356 }
1357 return bestXY;
1358 }
1359
1360 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001361 int[] direction, ItemConfiguration currentState) {
1362 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001363 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001364 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001365 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1366
Adam Cohen8baab352012-03-20 17:39:21 -07001367 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001368
1369 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001370 c.x = mTempLocation[0];
1371 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001372 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001373 }
Adam Cohen8baab352012-03-20 17:39:21 -07001374 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001375 return success;
1376 }
1377
Adam Cohenf3900c22012-11-16 18:28:11 -08001378 /**
1379 * This helper class defines a cluster of views. It helps with defining complex edges
1380 * of the cluster and determining how those edges interact with other views. The edges
1381 * essentially define a fine-grained boundary around the cluster of views -- like a more
1382 * precise version of a bounding box.
1383 */
1384 private class ViewCluster {
1385 final static int LEFT = 0;
1386 final static int TOP = 1;
1387 final static int RIGHT = 2;
1388 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001389
Adam Cohenf3900c22012-11-16 18:28:11 -08001390 ArrayList<View> views;
1391 ItemConfiguration config;
1392 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001393
Adam Cohenf3900c22012-11-16 18:28:11 -08001394 int[] leftEdge = new int[mCountY];
1395 int[] rightEdge = new int[mCountY];
1396 int[] topEdge = new int[mCountX];
1397 int[] bottomEdge = new int[mCountX];
1398 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1399
1400 @SuppressWarnings("unchecked")
1401 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1402 this.views = (ArrayList<View>) views.clone();
1403 this.config = config;
1404 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001405 }
1406
Adam Cohenf3900c22012-11-16 18:28:11 -08001407 void resetEdges() {
1408 for (int i = 0; i < mCountX; i++) {
1409 topEdge[i] = -1;
1410 bottomEdge[i] = -1;
1411 }
1412 for (int i = 0; i < mCountY; i++) {
1413 leftEdge[i] = -1;
1414 rightEdge[i] = -1;
1415 }
1416 leftEdgeDirty = true;
1417 rightEdgeDirty = true;
1418 bottomEdgeDirty = true;
1419 topEdgeDirty = true;
1420 boundingRectDirty = true;
1421 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001422
Adam Cohenf3900c22012-11-16 18:28:11 -08001423 void computeEdge(int which, int[] edge) {
1424 int count = views.size();
1425 for (int i = 0; i < count; i++) {
1426 CellAndSpan cs = config.map.get(views.get(i));
1427 switch (which) {
1428 case LEFT:
1429 int left = cs.x;
1430 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1431 if (left < edge[j] || edge[j] < 0) {
1432 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001433 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001434 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001435 break;
1436 case RIGHT:
1437 int right = cs.x + cs.spanX;
1438 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1439 if (right > edge[j]) {
1440 edge[j] = right;
1441 }
1442 }
1443 break;
1444 case TOP:
1445 int top = cs.y;
1446 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1447 if (top < edge[j] || edge[j] < 0) {
1448 edge[j] = top;
1449 }
1450 }
1451 break;
1452 case BOTTOM:
1453 int bottom = cs.y + cs.spanY;
1454 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1455 if (bottom > edge[j]) {
1456 edge[j] = bottom;
1457 }
1458 }
1459 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001460 }
1461 }
1462 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001463
1464 boolean isViewTouchingEdge(View v, int whichEdge) {
1465 CellAndSpan cs = config.map.get(v);
1466
1467 int[] edge = getEdge(whichEdge);
1468
1469 switch (whichEdge) {
1470 case LEFT:
1471 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1472 if (edge[i] == cs.x + cs.spanX) {
1473 return true;
1474 }
1475 }
1476 break;
1477 case RIGHT:
1478 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1479 if (edge[i] == cs.x) {
1480 return true;
1481 }
1482 }
1483 break;
1484 case TOP:
1485 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1486 if (edge[i] == cs.y + cs.spanY) {
1487 return true;
1488 }
1489 }
1490 break;
1491 case BOTTOM:
1492 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1493 if (edge[i] == cs.y) {
1494 return true;
1495 }
1496 }
1497 break;
1498 }
1499 return false;
1500 }
1501
1502 void shift(int whichEdge, int delta) {
1503 for (View v: views) {
1504 CellAndSpan c = config.map.get(v);
1505 switch (whichEdge) {
1506 case LEFT:
1507 c.x -= delta;
1508 break;
1509 case RIGHT:
1510 c.x += delta;
1511 break;
1512 case TOP:
1513 c.y -= delta;
1514 break;
1515 case BOTTOM:
1516 default:
1517 c.y += delta;
1518 break;
1519 }
1520 }
1521 resetEdges();
1522 }
1523
1524 public void addView(View v) {
1525 views.add(v);
1526 resetEdges();
1527 }
1528
1529 public Rect getBoundingRect() {
1530 if (boundingRectDirty) {
1531 boolean first = true;
1532 for (View v: views) {
1533 CellAndSpan c = config.map.get(v);
1534 if (first) {
1535 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1536 first = false;
1537 } else {
1538 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1539 }
1540 }
1541 }
1542 return boundingRect;
1543 }
1544
1545 public int[] getEdge(int which) {
1546 switch (which) {
1547 case LEFT:
1548 return getLeftEdge();
1549 case RIGHT:
1550 return getRightEdge();
1551 case TOP:
1552 return getTopEdge();
1553 case BOTTOM:
1554 default:
1555 return getBottomEdge();
1556 }
1557 }
1558
1559 public int[] getLeftEdge() {
1560 if (leftEdgeDirty) {
1561 computeEdge(LEFT, leftEdge);
1562 }
1563 return leftEdge;
1564 }
1565
1566 public int[] getRightEdge() {
1567 if (rightEdgeDirty) {
1568 computeEdge(RIGHT, rightEdge);
1569 }
1570 return rightEdge;
1571 }
1572
1573 public int[] getTopEdge() {
1574 if (topEdgeDirty) {
1575 computeEdge(TOP, topEdge);
1576 }
1577 return topEdge;
1578 }
1579
1580 public int[] getBottomEdge() {
1581 if (bottomEdgeDirty) {
1582 computeEdge(BOTTOM, bottomEdge);
1583 }
1584 return bottomEdge;
1585 }
1586
1587 PositionComparator comparator = new PositionComparator();
1588 class PositionComparator implements Comparator<View> {
1589 int whichEdge = 0;
1590 public int compare(View left, View right) {
1591 CellAndSpan l = config.map.get(left);
1592 CellAndSpan r = config.map.get(right);
1593 switch (whichEdge) {
1594 case LEFT:
1595 return (r.x + r.spanX) - (l.x + l.spanX);
1596 case RIGHT:
1597 return l.x - r.x;
1598 case TOP:
1599 return (r.y + r.spanY) - (l.y + l.spanY);
1600 case BOTTOM:
1601 default:
1602 return l.y - r.y;
1603 }
1604 }
1605 }
1606
1607 public void sortConfigurationForEdgePush(int edge) {
1608 comparator.whichEdge = edge;
1609 Collections.sort(config.sortedViews, comparator);
1610 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001611 }
1612
Adam Cohenf3900c22012-11-16 18:28:11 -08001613 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1614 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001615
Adam Cohenf3900c22012-11-16 18:28:11 -08001616 ViewCluster cluster = new ViewCluster(views, currentState);
1617 Rect clusterRect = cluster.getBoundingRect();
1618 int whichEdge;
1619 int pushDistance;
1620 boolean fail = false;
1621
1622 // Determine the edge of the cluster that will be leading the push and how far
1623 // the cluster must be shifted.
1624 if (direction[0] < 0) {
1625 whichEdge = ViewCluster.LEFT;
1626 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001627 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001628 whichEdge = ViewCluster.RIGHT;
1629 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1630 } else if (direction[1] < 0) {
1631 whichEdge = ViewCluster.TOP;
1632 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1633 } else {
1634 whichEdge = ViewCluster.BOTTOM;
1635 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001636 }
1637
Adam Cohenf3900c22012-11-16 18:28:11 -08001638 // Break early for invalid push distance.
1639 if (pushDistance <= 0) {
1640 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001641 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001642
1643 // Mark the occupied state as false for the group of views we want to move.
1644 for (View v: views) {
1645 CellAndSpan c = currentState.map.get(v);
1646 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1647 }
1648
1649 // We save the current configuration -- if we fail to find a solution we will revert
1650 // to the initial state. The process of finding a solution modifies the configuration
1651 // in place, hence the need for revert in the failure case.
1652 currentState.save();
1653
1654 // The pushing algorithm is simplified by considering the views in the order in which
1655 // they would be pushed by the cluster. For example, if the cluster is leading with its
1656 // left edge, we consider sort the views by their right edge, from right to left.
1657 cluster.sortConfigurationForEdgePush(whichEdge);
1658
1659 while (pushDistance > 0 && !fail) {
1660 for (View v: currentState.sortedViews) {
1661 // For each view that isn't in the cluster, we see if the leading edge of the
1662 // cluster is contacting the edge of that view. If so, we add that view to the
1663 // cluster.
1664 if (!cluster.views.contains(v) && v != dragView) {
1665 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1666 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1667 if (!lp.canReorder) {
1668 // The push solution includes the all apps button, this is not viable.
1669 fail = true;
1670 break;
1671 }
1672 cluster.addView(v);
1673 CellAndSpan c = currentState.map.get(v);
1674
1675 // Adding view to cluster, mark it as not occupied.
1676 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1677 }
1678 }
1679 }
1680 pushDistance--;
1681
1682 // The cluster has been completed, now we move the whole thing over in the appropriate
1683 // direction.
1684 cluster.shift(whichEdge, 1);
1685 }
1686
1687 boolean foundSolution = false;
1688 clusterRect = cluster.getBoundingRect();
1689
1690 // Due to the nature of the algorithm, the only check required to verify a valid solution
1691 // is to ensure that completed shifted cluster lies completely within the cell layout.
1692 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1693 clusterRect.bottom <= mCountY) {
1694 foundSolution = true;
1695 } else {
1696 currentState.restore();
1697 }
1698
1699 // In either case, we set the occupied array as marked for the location of the views
1700 for (View v: cluster.views) {
1701 CellAndSpan c = currentState.map.get(v);
1702 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1703 }
1704
1705 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001706 }
1707
Adam Cohen482ed822012-03-02 14:15:13 -08001708 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001709 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001710 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001711
Adam Cohen8baab352012-03-20 17:39:21 -07001712 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001713 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001714 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001715 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001716 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001717 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001718 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001719 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001720 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001721 }
1722 }
Adam Cohen8baab352012-03-20 17:39:21 -07001723
Adam Cohen8baab352012-03-20 17:39:21 -07001724 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001725 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001726 CellAndSpan c = currentState.map.get(v);
1727 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1728 }
1729
Adam Cohen47a876d2012-03-19 13:21:41 -07001730 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1731 int top = boundingRect.top;
1732 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001733 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001734 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001735 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001736 CellAndSpan c = currentState.map.get(v);
1737 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001738 }
1739
Adam Cohen482ed822012-03-02 14:15:13 -08001740 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1741
Adam Cohenf3900c22012-11-16 18:28:11 -08001742 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1743 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001744
Adam Cohen8baab352012-03-20 17:39:21 -07001745 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001746 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001747 int deltaX = mTempLocation[0] - boundingRect.left;
1748 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001749 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001750 CellAndSpan c = currentState.map.get(v);
1751 c.x += deltaX;
1752 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001753 }
1754 success = true;
1755 }
Adam Cohen8baab352012-03-20 17:39:21 -07001756
1757 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001758 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001759 CellAndSpan c = currentState.map.get(v);
1760 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001761 }
1762 return success;
1763 }
1764
1765 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1766 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1767 }
1768
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001769 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1770 // to push items in each of the cardinal directions, in an order based on the direction vector
1771 // passed.
1772 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1773 int[] direction, View ignoreView, ItemConfiguration solution) {
1774 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001775 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001776 // separately in each of the components.
1777 int temp = direction[1];
1778 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001779
1780 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001781 ignoreView, solution)) {
1782 return true;
1783 }
1784 direction[1] = temp;
1785 temp = direction[0];
1786 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001787
1788 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001789 ignoreView, solution)) {
1790 return true;
1791 }
1792 // Revert the direction
1793 direction[0] = temp;
1794
1795 // Now we try pushing in each component of the opposite direction
1796 direction[0] *= -1;
1797 direction[1] *= -1;
1798 temp = direction[1];
1799 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001800 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001801 ignoreView, solution)) {
1802 return true;
1803 }
1804
1805 direction[1] = temp;
1806 temp = direction[0];
1807 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001808 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001809 ignoreView, solution)) {
1810 return true;
1811 }
1812 // revert the direction
1813 direction[0] = temp;
1814 direction[0] *= -1;
1815 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001816
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001817 } else {
1818 // If the direction vector has a single non-zero component, we push first in the
1819 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001820 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001821 ignoreView, solution)) {
1822 return true;
1823 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001824 // Then we try the opposite direction
1825 direction[0] *= -1;
1826 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001827 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001828 ignoreView, solution)) {
1829 return true;
1830 }
1831 // Switch the direction back
1832 direction[0] *= -1;
1833 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001834
1835 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001836 // to find a solution by pushing along the perpendicular axis.
1837
1838 // Swap the components
1839 int temp = direction[1];
1840 direction[1] = direction[0];
1841 direction[0] = temp;
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
1847 // Then we try the opposite direction
1848 direction[0] *= -1;
1849 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001850 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001851 ignoreView, solution)) {
1852 return true;
1853 }
1854 // Switch the direction back
1855 direction[0] *= -1;
1856 direction[1] *= -1;
1857
1858 // Swap the components back
1859 temp = direction[1];
1860 direction[1] = direction[0];
1861 direction[0] = temp;
1862 }
1863 return false;
1864 }
1865
Adam Cohen482ed822012-03-02 14:15:13 -08001866 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001867 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001868 // Return early if get invalid cell positions
1869 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001870
Adam Cohen8baab352012-03-20 17:39:21 -07001871 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001872 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001873
Adam Cohen8baab352012-03-20 17:39:21 -07001874 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001875 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001876 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001877 if (c != null) {
1878 c.x = cellX;
1879 c.y = cellY;
1880 }
Adam Cohen482ed822012-03-02 14:15:13 -08001881 }
Adam Cohen482ed822012-03-02 14:15:13 -08001882 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1883 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001884 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001885 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001886 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001887 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001888 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001889 if (Rect.intersects(r0, r1)) {
1890 if (!lp.canReorder) {
1891 return false;
1892 }
1893 mIntersectingViews.add(child);
1894 }
1895 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001896
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001897 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1898
Winson Chung5f8afe62013-08-12 16:19:28 -07001899 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001900 // we try to find a solution such that no displaced item travels through another item
1901 // without also displacing that item.
1902 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001903 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001904 return true;
1905 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001906
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001907 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001908 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001909 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001910 return true;
1911 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001912
Adam Cohen482ed822012-03-02 14:15:13 -08001913 // Ok, they couldn't move as a block, let's move them individually
1914 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001915 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001916 return false;
1917 }
1918 }
1919 return true;
1920 }
1921
1922 /*
1923 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1924 * the provided point and the provided cell
1925 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001926 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001927 double angle = Math.atan(((float) deltaY) / deltaX);
1928
1929 result[0] = 0;
1930 result[1] = 0;
1931 if (Math.abs(Math.cos(angle)) > 0.5f) {
1932 result[0] = (int) Math.signum(deltaX);
1933 }
1934 if (Math.abs(Math.sin(angle)) > 0.5f) {
1935 result[1] = (int) Math.signum(deltaY);
1936 }
1937 }
1938
Adam Cohen8baab352012-03-20 17:39:21 -07001939 private void copyOccupiedArray(boolean[][] occupied) {
1940 for (int i = 0; i < mCountX; i++) {
1941 for (int j = 0; j < mCountY; j++) {
1942 occupied[i][j] = mOccupied[i][j];
1943 }
1944 }
1945 }
1946
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001947 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001948 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1949 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001950 // Copy the current state into the solution. This solution will be manipulated as necessary.
1951 copyCurrentStateToSolution(solution, false);
1952 // Copy the current occupied array into the temporary occupied array. This array will be
1953 // manipulated as necessary to find a solution.
1954 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001955
1956 // We find the nearest cell into which we would place the dragged item, assuming there's
1957 // nothing in its way.
1958 int result[] = new int[2];
1959 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1960
1961 boolean success = false;
1962 // First we try the exact nearest position of the item being dragged,
1963 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001964 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1965 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001966
1967 if (!success) {
1968 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1969 // x, then 1 in y etc.
1970 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001971 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1972 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001973 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001974 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1975 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001976 }
1977 solution.isSolution = false;
1978 } else {
1979 solution.isSolution = true;
1980 solution.dragViewX = result[0];
1981 solution.dragViewY = result[1];
1982 solution.dragViewSpanX = spanX;
1983 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001984 }
1985 return solution;
1986 }
1987
1988 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001989 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001990 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001991 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001992 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001993 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001994 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001995 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001996 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001997 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001998 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001999 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002000 }
2001 }
2002
2003 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2004 for (int i = 0; i < mCountX; i++) {
2005 for (int j = 0; j < mCountY; j++) {
2006 mTmpOccupied[i][j] = false;
2007 }
2008 }
2009
Michael Jurkaa52570f2012-03-20 03:18:20 -07002010 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002011 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002012 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002013 if (child == dragView) continue;
2014 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002015 CellAndSpan c = solution.map.get(child);
2016 if (c != null) {
2017 lp.tmpCellX = c.x;
2018 lp.tmpCellY = c.y;
2019 lp.cellHSpan = c.spanX;
2020 lp.cellVSpan = c.spanY;
2021 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002022 }
2023 }
2024 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2025 solution.dragViewSpanY, mTmpOccupied, true);
2026 }
2027
2028 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2029 commitDragView) {
2030
2031 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2032 for (int i = 0; i < mCountX; i++) {
2033 for (int j = 0; j < mCountY; j++) {
2034 occupied[i][j] = false;
2035 }
2036 }
2037
Michael Jurkaa52570f2012-03-20 03:18:20 -07002038 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002039 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002040 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002041 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002042 CellAndSpan c = solution.map.get(child);
2043 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002044 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2045 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002046 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002047 }
2048 }
2049 if (commitDragView) {
2050 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2051 solution.dragViewSpanY, occupied, true);
2052 }
2053 }
2054
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002055
2056 // This method starts or changes the reorder preview animations
2057 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2058 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002059 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002060 for (int i = 0; i < childCount; i++) {
2061 View child = mShortcutsAndWidgets.getChildAt(i);
2062 if (child == dragView) continue;
2063 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002064 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2065 != null && !solution.intersectingViews.contains(child);
2066
Adam Cohen19f37922012-03-21 11:59:11 -07002067 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002068 if (c != null && !skip) {
2069 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2070 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002071 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002072 }
2073 }
2074 }
2075
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002076 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002077 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002078 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002079 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002080 float finalDeltaX;
2081 float finalDeltaY;
2082 float initDeltaX;
2083 float initDeltaY;
2084 float finalScale;
2085 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002086 int mode;
2087 boolean repeating = false;
2088 private static final int PREVIEW_DURATION = 300;
2089 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2090
2091 public static final int MODE_HINT = 0;
2092 public static final int MODE_PREVIEW = 1;
2093
Adam Cohene7587d22012-05-24 18:50:02 -07002094 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002095
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002096 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2097 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002098 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2099 final int x0 = mTmpPoint[0];
2100 final int y0 = mTmpPoint[1];
2101 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2102 final int x1 = mTmpPoint[0];
2103 final int y1 = mTmpPoint[1];
2104 final int dX = x1 - x0;
2105 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002106 finalDeltaX = 0;
2107 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002108 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002109 if (dX == dY && dX == 0) {
2110 } else {
2111 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002112 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002113 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002114 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002115 } else {
2116 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002117 finalDeltaX = (int) (- dir * Math.signum(dX) *
2118 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2119 finalDeltaY = (int) (- dir * Math.signum(dY) *
2120 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002121 }
2122 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002123 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002124 initDeltaX = child.getTranslationX();
2125 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002126 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002127 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002128 this.child = child;
2129 }
2130
Adam Cohend024f982012-05-23 18:26:45 -07002131 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002132 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002133 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002134 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002135 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002136 if (finalDeltaX == 0 && finalDeltaY == 0) {
2137 completeAnimationImmediately();
2138 return;
2139 }
Adam Cohen19f37922012-03-21 11:59:11 -07002140 }
Adam Cohend024f982012-05-23 18:26:45 -07002141 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002142 return;
2143 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002144 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002145 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002146
2147 // Animations are disabled in power save mode, causing the repeated animation to jump
2148 // spastically between beginning and end states. Since this looks bad, we don't repeat
2149 // the animation in power save mode.
2150 PowerManager powerManager = (PowerManager) getContext()
2151 .getSystemService(Context.POWER_SERVICE);
2152 boolean powerSaverOn = Utilities.ATLEAST_LOLLIPOP && powerManager.isPowerSaveMode();
2153 if (!powerSaverOn) {
2154 va.setRepeatMode(ValueAnimator.REVERSE);
2155 va.setRepeatCount(ValueAnimator.INFINITE);
2156 }
2157
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002158 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002159 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002160 va.addUpdateListener(new AnimatorUpdateListener() {
2161 @Override
2162 public void onAnimationUpdate(ValueAnimator animation) {
2163 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002164 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2165 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2166 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002167 child.setTranslationX(x);
2168 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002169 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002170 child.setScaleX(s);
2171 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002172 }
2173 });
2174 va.addListener(new AnimatorListenerAdapter() {
2175 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002176 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002177 initDeltaX = 0;
2178 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002179 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002180 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002181 }
2182 });
Adam Cohen19f37922012-03-21 11:59:11 -07002183 mShakeAnimators.put(child, this);
2184 va.start();
2185 }
2186
Adam Cohend024f982012-05-23 18:26:45 -07002187 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002188 if (a != null) {
2189 a.cancel();
2190 }
Adam Cohen19f37922012-03-21 11:59:11 -07002191 }
Adam Cohene7587d22012-05-24 18:50:02 -07002192
Adam Cohen091440a2015-03-18 14:16:05 -07002193 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002194 if (a != null) {
2195 a.cancel();
2196 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002197
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002198 a = new LauncherViewPropertyAnimator(child)
2199 .scaleX(getChildrenScale())
2200 .scaleY(getChildrenScale())
2201 .translationX(0)
2202 .translationY(0)
2203 .setDuration(REORDER_ANIMATION_DURATION);
2204 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2205 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002206 }
Adam Cohen19f37922012-03-21 11:59:11 -07002207 }
2208
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002209 private void completeAndClearReorderPreviewAnimations() {
2210 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002211 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002212 }
2213 mShakeAnimators.clear();
2214 }
2215
Adam Cohen482ed822012-03-02 14:15:13 -08002216 private void commitTempPlacement() {
2217 for (int i = 0; i < mCountX; i++) {
2218 for (int j = 0; j < mCountY; j++) {
2219 mOccupied[i][j] = mTmpOccupied[i][j];
2220 }
2221 }
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002222
2223 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2224 int container = Favorites.CONTAINER_DESKTOP;
2225
2226 if (mLauncher.isHotseatLayout(this)) {
2227 screenId = -1;
2228 container = Favorites.CONTAINER_HOTSEAT;
2229 }
2230
Michael Jurkaa52570f2012-03-20 03:18:20 -07002231 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002232 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002233 View child = mShortcutsAndWidgets.getChildAt(i);
2234 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2235 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002236 // We do a null check here because the item info can be null in the case of the
2237 // AllApps button in the hotseat.
2238 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002239 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2240 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2241 || info.spanY != lp.cellVSpan);
2242
Adam Cohen2acce882012-03-28 19:03:19 -07002243 info.cellX = lp.cellX = lp.tmpCellX;
2244 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002245 info.spanX = lp.cellHSpan;
2246 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002247
2248 if (requiresDbUpdate) {
2249 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId,
2250 info.cellX, info.cellY, info.spanX, info.spanY);
2251 }
Adam Cohen2acce882012-03-28 19:03:19 -07002252 }
Adam Cohen482ed822012-03-02 14:15:13 -08002253 }
2254 }
2255
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002256 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002257 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002258 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002259 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002260 lp.useTmpCoords = useTempCoords;
2261 }
2262 }
2263
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002264 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002265 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2266 int[] result = new int[2];
2267 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002268 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002269 resultSpan);
2270 if (result[0] >= 0 && result[1] >= 0) {
2271 copyCurrentStateToSolution(solution, false);
2272 solution.dragViewX = result[0];
2273 solution.dragViewY = result[1];
2274 solution.dragViewSpanX = resultSpan[0];
2275 solution.dragViewSpanY = resultSpan[1];
2276 solution.isSolution = true;
2277 } else {
2278 solution.isSolution = false;
2279 }
2280 return solution;
2281 }
2282
2283 public void prepareChildForDrag(View child) {
2284 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002285 }
2286
Adam Cohen19f37922012-03-21 11:59:11 -07002287 /* This seems like it should be obvious and straight-forward, but when the direction vector
2288 needs to match with the notion of the dragView pushing other views, we have to employ
2289 a slightly more subtle notion of the direction vector. The question is what two points is
2290 the vector between? The center of the dragView and its desired destination? Not quite, as
2291 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2292 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2293 or right, which helps make pushing feel right.
2294 */
2295 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2296 int spanY, View dragView, int[] resultDirection) {
2297 int[] targetDestination = new int[2];
2298
2299 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2300 Rect dragRect = new Rect();
2301 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2302 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2303
2304 Rect dropRegionRect = new Rect();
2305 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2306 dragView, dropRegionRect, mIntersectingViews);
2307
2308 int dropRegionSpanX = dropRegionRect.width();
2309 int dropRegionSpanY = dropRegionRect.height();
2310
2311 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2312 dropRegionRect.height(), dropRegionRect);
2313
2314 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2315 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2316
2317 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2318 deltaX = 0;
2319 }
2320 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2321 deltaY = 0;
2322 }
2323
2324 if (deltaX == 0 && deltaY == 0) {
2325 // No idea what to do, give a random direction.
2326 resultDirection[0] = 1;
2327 resultDirection[1] = 0;
2328 } else {
2329 computeDirectionVector(deltaX, deltaY, resultDirection);
2330 }
2331 }
2332
2333 // For a given cell and span, fetch the set of views intersecting the region.
2334 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2335 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2336 if (boundingRect != null) {
2337 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2338 }
2339 intersectingViews.clear();
2340 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2341 Rect r1 = new Rect();
2342 final int count = mShortcutsAndWidgets.getChildCount();
2343 for (int i = 0; i < count; i++) {
2344 View child = mShortcutsAndWidgets.getChildAt(i);
2345 if (child == dragView) continue;
2346 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2347 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2348 if (Rect.intersects(r0, r1)) {
2349 mIntersectingViews.add(child);
2350 if (boundingRect != null) {
2351 boundingRect.union(r1);
2352 }
2353 }
2354 }
2355 }
2356
2357 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2358 View dragView, int[] result) {
2359 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2360 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2361 mIntersectingViews);
2362 return !mIntersectingViews.isEmpty();
2363 }
2364
2365 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002366 completeAndClearReorderPreviewAnimations();
2367 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2368 final int count = mShortcutsAndWidgets.getChildCount();
2369 for (int i = 0; i < count; i++) {
2370 View child = mShortcutsAndWidgets.getChildAt(i);
2371 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2372 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2373 lp.tmpCellX = lp.cellX;
2374 lp.tmpCellY = lp.cellY;
2375 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2376 0, false, false);
2377 }
Adam Cohen19f37922012-03-21 11:59:11 -07002378 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002379 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002380 }
Adam Cohen19f37922012-03-21 11:59:11 -07002381 }
2382
Adam Cohenbebf0422012-04-11 18:06:28 -07002383 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2384 View dragView, int[] direction, boolean commit) {
2385 int[] pixelXY = new int[2];
2386 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2387
2388 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002389 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002390 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2391
2392 setUseTempCoords(true);
2393 if (swapSolution != null && swapSolution.isSolution) {
2394 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2395 // committing anything or animating anything as we just want to determine if a solution
2396 // exists
2397 copySolutionToTempState(swapSolution, dragView);
2398 setItemPlacementDirty(true);
2399 animateItemsToSolution(swapSolution, dragView, commit);
2400
2401 if (commit) {
2402 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002403 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002404 setItemPlacementDirty(false);
2405 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002406 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2407 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002408 }
2409 mShortcutsAndWidgets.requestLayout();
2410 }
2411 return swapSolution.isSolution;
2412 }
2413
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002414 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002415 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002416 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002417 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002418
2419 if (resultSpan == null) {
2420 resultSpan = new int[2];
2421 }
2422
Adam Cohen19f37922012-03-21 11:59:11 -07002423 // When we are checking drop validity or actually dropping, we don't recompute the
2424 // direction vector, since we want the solution to match the preview, and it's possible
2425 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002426 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2427 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002428 mDirectionVector[0] = mPreviousReorderDirection[0];
2429 mDirectionVector[1] = mPreviousReorderDirection[1];
2430 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002431 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2432 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2433 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002434 }
2435 } else {
2436 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2437 mPreviousReorderDirection[0] = mDirectionVector[0];
2438 mPreviousReorderDirection[1] = mDirectionVector[1];
2439 }
2440
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002441 // Find a solution involving pushing / displacing any items in the way
2442 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002443 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2444
2445 // We attempt the approach which doesn't shuffle views at all
2446 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2447 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2448
2449 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002450
2451 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2452 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002453 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2454 finalSolution = swapSolution;
2455 } else if (noShuffleSolution.isSolution) {
2456 finalSolution = noShuffleSolution;
2457 }
2458
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002459 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002460 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002461 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2462 ReorderPreviewAnimation.MODE_HINT);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002463 result[0] = finalSolution.dragViewX;
2464 result[1] = finalSolution.dragViewY;
2465 resultSpan[0] = finalSolution.dragViewSpanX;
2466 resultSpan[1] = finalSolution.dragViewSpanY;
2467 } else {
2468 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2469 }
2470 return result;
2471 }
2472
Adam Cohen482ed822012-03-02 14:15:13 -08002473 boolean foundSolution = true;
2474 if (!DESTRUCTIVE_REORDER) {
2475 setUseTempCoords(true);
2476 }
2477
2478 if (finalSolution != null) {
2479 result[0] = finalSolution.dragViewX;
2480 result[1] = finalSolution.dragViewY;
2481 resultSpan[0] = finalSolution.dragViewSpanX;
2482 resultSpan[1] = finalSolution.dragViewSpanY;
2483
2484 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2485 // committing anything or animating anything as we just want to determine if a solution
2486 // exists
2487 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2488 if (!DESTRUCTIVE_REORDER) {
2489 copySolutionToTempState(finalSolution, dragView);
2490 }
2491 setItemPlacementDirty(true);
2492 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2493
Adam Cohen19f37922012-03-21 11:59:11 -07002494 if (!DESTRUCTIVE_REORDER &&
2495 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002496 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002497 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002498 setItemPlacementDirty(false);
2499 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002500 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2501 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002502 }
2503 }
2504 } else {
2505 foundSolution = false;
2506 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2507 }
2508
2509 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2510 setUseTempCoords(false);
2511 }
Adam Cohen482ed822012-03-02 14:15:13 -08002512
Michael Jurkaa52570f2012-03-20 03:18:20 -07002513 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002514 return result;
2515 }
2516
Adam Cohen19f37922012-03-21 11:59:11 -07002517 void setItemPlacementDirty(boolean dirty) {
2518 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002519 }
Adam Cohen19f37922012-03-21 11:59:11 -07002520 boolean isItemPlacementDirty() {
2521 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002522 }
2523
Adam Cohen091440a2015-03-18 14:16:05 -07002524 @Thunk class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002525 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002526 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2527 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002528 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002529 boolean isSolution = false;
2530 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2531
Adam Cohenf3900c22012-11-16 18:28:11 -08002532 void save() {
2533 // Copy current state into savedMap
2534 for (View v: map.keySet()) {
2535 map.get(v).copy(savedMap.get(v));
2536 }
2537 }
2538
2539 void restore() {
2540 // Restore current state from savedMap
2541 for (View v: savedMap.keySet()) {
2542 savedMap.get(v).copy(map.get(v));
2543 }
2544 }
2545
2546 void add(View v, CellAndSpan cs) {
2547 map.put(v, cs);
2548 savedMap.put(v, new CellAndSpan());
2549 sortedViews.add(v);
2550 }
2551
Adam Cohen482ed822012-03-02 14:15:13 -08002552 int area() {
2553 return dragViewSpanX * dragViewSpanY;
2554 }
Adam Cohen8baab352012-03-20 17:39:21 -07002555 }
2556
2557 private class CellAndSpan {
2558 int x, y;
2559 int spanX, spanY;
2560
Adam Cohenf3900c22012-11-16 18:28:11 -08002561 public CellAndSpan() {
2562 }
2563
2564 public void copy(CellAndSpan copy) {
2565 copy.x = x;
2566 copy.y = y;
2567 copy.spanX = spanX;
2568 copy.spanY = spanY;
2569 }
2570
Adam Cohen8baab352012-03-20 17:39:21 -07002571 public CellAndSpan(int x, int y, int spanX, int spanY) {
2572 this.x = x;
2573 this.y = y;
2574 this.spanX = spanX;
2575 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002576 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002577
2578 public String toString() {
2579 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2580 }
2581
Adam Cohen482ed822012-03-02 14:15:13 -08002582 }
2583
Adam Cohendf035382011-04-11 17:22:04 -07002584 /**
Adam Cohendf035382011-04-11 17:22:04 -07002585 * Find a starting cell position that will fit the given bounds nearest the requested
2586 * cell location. Uses Euclidean distance to score multiple vacant areas.
2587 *
2588 * @param pixelX The X location at which you want to search for a vacant area.
2589 * @param pixelY The Y location at which you want to search for a vacant area.
2590 * @param spanX Horizontal span of the object.
2591 * @param spanY Vertical span of the object.
2592 * @param ignoreView Considers space occupied by this view as unoccupied
2593 * @param result Previously returned value to possibly recycle.
2594 * @return The X, Y cell of a vacant area that can contain this object,
2595 * nearest the requested location.
2596 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002597 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2598 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002599 }
2600
Michael Jurka0280c3b2010-09-17 15:00:07 -07002601 boolean existsEmptyCell() {
2602 return findCellForSpan(null, 1, 1);
2603 }
2604
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002605 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002606 * Finds the upper-left coordinate of the first rectangle in the grid that can
2607 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2608 * then this method will only return coordinates for rectangles that contain the cell
2609 * (intersectX, intersectY)
2610 *
2611 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2612 * can be found.
2613 * @param spanX The horizontal span of the cell we want to find.
2614 * @param spanY The vertical span of the cell we want to find.
2615 *
2616 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002617 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002618 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Michael Jurka28750fb2010-09-24 17:43:49 -07002619 boolean foundCell = false;
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002620 final int endX = mCountX - (spanX - 1);
2621 final int endY = mCountY - (spanY - 1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002622
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002623 for (int y = 0; y < endY && !foundCell; y++) {
2624 inner:
2625 for (int x = 0; x < endX; x++) {
2626 for (int i = 0; i < spanX; i++) {
2627 for (int j = 0; j < spanY; j++) {
2628 if (mOccupied[x + i][y + j]) {
2629 // small optimization: we can skip to after the column we just found
2630 // an occupied cell
2631 x += i;
2632 continue inner;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002633 }
2634 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002635 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002636 if (cellXY != null) {
2637 cellXY[0] = x;
2638 cellXY[1] = y;
2639 }
2640 foundCell = true;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002641 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002642 }
2643 }
2644
Michael Jurka28750fb2010-09-24 17:43:49 -07002645 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002646 }
2647
2648 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002649 * A drag event has begun over this layout.
2650 * It may have begun over this layout (in which case onDragChild is called first),
2651 * or it may have begun on another layout.
2652 */
2653 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002654 mDragging = true;
2655 }
2656
2657 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002658 * Called when drag has left this CellLayout or has been completed (successfully or not)
2659 */
2660 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002661 // This can actually be called when we aren't in a drag, e.g. when adding a new
2662 // item to this layout via the customize drawer.
2663 // Guard against that case.
2664 if (mDragging) {
2665 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002666 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002667
2668 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002669 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002670 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2671 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002672 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002673 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002674 }
2675
2676 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002677 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002678 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002679 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002680 *
2681 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002682 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002683 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002684 if (child != null) {
2685 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002686 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002687 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002688 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002689 }
2690
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002691 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002693 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002694 * @param cellX X coordinate of upper left corner expressed as a cell position
2695 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002696 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002698 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002699 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002700 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002701 final int cellWidth = mCellWidth;
2702 final int cellHeight = mCellHeight;
2703 final int widthGap = mWidthGap;
2704 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002705
Winson Chung4b825dcd2011-06-19 12:41:22 -07002706 final int hStartPadding = getPaddingLeft();
2707 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002708
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002709 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2710 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2711
2712 int x = hStartPadding + cellX * (cellWidth + widthGap);
2713 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002714
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002715 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002716 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002717
Michael Jurka0280c3b2010-09-17 15:00:07 -07002718 private void clearOccupiedCells() {
2719 for (int x = 0; x < mCountX; x++) {
2720 for (int y = 0; y < mCountY; y++) {
2721 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002722 }
2723 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002724 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002725
Adam Cohend4844c32011-02-18 19:25:06 -08002726 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002727 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002728 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002729 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002730 }
2731
Adam Cohend4844c32011-02-18 19:25:06 -08002732 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002733 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002734 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002735 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002736 }
2737
Adam Cohen482ed822012-03-02 14:15:13 -08002738 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2739 boolean value) {
2740 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002741 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2742 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002743 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002744 }
2745 }
2746 }
2747
Adam Cohen2801caf2011-05-13 20:57:39 -07002748 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002749 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002750 (Math.max((mCountX - 1), 0) * mWidthGap);
2751 }
2752
2753 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002754 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002755 (Math.max((mCountY - 1), 0) * mHeightGap);
2756 }
2757
Michael Jurka66d72172011-04-12 16:29:25 -07002758 public boolean isOccupied(int x, int y) {
2759 if (x < mCountX && y < mCountY) {
2760 return mOccupied[x][y];
2761 } else {
2762 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2763 }
2764 }
2765
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766 @Override
2767 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2768 return new CellLayout.LayoutParams(getContext(), attrs);
2769 }
2770
2771 @Override
2772 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2773 return p instanceof CellLayout.LayoutParams;
2774 }
2775
2776 @Override
2777 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2778 return new CellLayout.LayoutParams(p);
2779 }
2780
2781 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2782 /**
2783 * Horizontal location of the item in the grid.
2784 */
2785 @ViewDebug.ExportedProperty
2786 public int cellX;
2787
2788 /**
2789 * Vertical location of the item in the grid.
2790 */
2791 @ViewDebug.ExportedProperty
2792 public int cellY;
2793
2794 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002795 * Temporary horizontal location of the item in the grid during reorder
2796 */
2797 public int tmpCellX;
2798
2799 /**
2800 * Temporary vertical location of the item in the grid during reorder
2801 */
2802 public int tmpCellY;
2803
2804 /**
2805 * Indicates that the temporary coordinates should be used to layout the items
2806 */
2807 public boolean useTmpCoords;
2808
2809 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002810 * Number of cells spanned horizontally by the item.
2811 */
2812 @ViewDebug.ExportedProperty
2813 public int cellHSpan;
2814
2815 /**
2816 * Number of cells spanned vertically by the item.
2817 */
2818 @ViewDebug.ExportedProperty
2819 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002820
Adam Cohen1b607ed2011-03-03 17:26:50 -08002821 /**
2822 * Indicates whether the item will set its x, y, width and height parameters freely,
2823 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2824 */
Adam Cohend4844c32011-02-18 19:25:06 -08002825 public boolean isLockedToGrid = true;
2826
Adam Cohen482ed822012-03-02 14:15:13 -08002827 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002828 * Indicates that this item should use the full extents of its parent.
2829 */
2830 public boolean isFullscreen = false;
2831
2832 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002833 * Indicates whether this item can be reordered. Always true except in the case of the
2834 * the AllApps button.
2835 */
2836 public boolean canReorder = true;
2837
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838 // X coordinate of the view in the layout.
2839 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002840 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002841 // Y coordinate of the view in the layout.
2842 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002843 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002844
Romain Guy84f296c2009-11-04 15:00:44 -08002845 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002846
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002847 public LayoutParams(Context c, AttributeSet attrs) {
2848 super(c, attrs);
2849 cellHSpan = 1;
2850 cellVSpan = 1;
2851 }
2852
2853 public LayoutParams(ViewGroup.LayoutParams source) {
2854 super(source);
2855 cellHSpan = 1;
2856 cellVSpan = 1;
2857 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002858
2859 public LayoutParams(LayoutParams source) {
2860 super(source);
2861 this.cellX = source.cellX;
2862 this.cellY = source.cellY;
2863 this.cellHSpan = source.cellHSpan;
2864 this.cellVSpan = source.cellVSpan;
2865 }
2866
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002868 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002869 this.cellX = cellX;
2870 this.cellY = cellY;
2871 this.cellHSpan = cellHSpan;
2872 this.cellVSpan = cellVSpan;
2873 }
2874
Adam Cohen2374abf2013-04-16 14:56:57 -07002875 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2876 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002877 if (isLockedToGrid) {
2878 final int myCellHSpan = cellHSpan;
2879 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002880 int myCellX = useTmpCoords ? tmpCellX : cellX;
2881 int myCellY = useTmpCoords ? tmpCellY : cellY;
2882
2883 if (invertHorizontally) {
2884 myCellX = colCount - myCellX - cellHSpan;
2885 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002886
Adam Cohend4844c32011-02-18 19:25:06 -08002887 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2888 leftMargin - rightMargin;
2889 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2890 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002891 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2892 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002893 }
2894 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002895
Winson Chungaafa03c2010-06-11 17:34:16 -07002896 public String toString() {
2897 return "(" + this.cellX + ", " + this.cellY + ")";
2898 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002899
2900 public void setWidth(int width) {
2901 this.width = width;
2902 }
2903
2904 public int getWidth() {
2905 return width;
2906 }
2907
2908 public void setHeight(int height) {
2909 this.height = height;
2910 }
2911
2912 public int getHeight() {
2913 return height;
2914 }
2915
2916 public void setX(int x) {
2917 this.x = x;
2918 }
2919
2920 public int getX() {
2921 return x;
2922 }
2923
2924 public void setY(int y) {
2925 this.y = y;
2926 }
2927
2928 public int getY() {
2929 return y;
2930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 }
2932
Michael Jurka0280c3b2010-09-17 15:00:07 -07002933 // This class stores info for two purposes:
2934 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2935 // its spanX, spanY, and the screen it is on
2936 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2937 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2938 // the CellLayout that was long clicked
Sunny Goyal83a8f042015-05-19 12:52:12 -07002939 public static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002940 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002941 int cellX = -1;
2942 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002943 int spanX;
2944 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07002945 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002946 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002947
Sunny Goyal83a8f042015-05-19 12:52:12 -07002948 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002949 cell = v;
2950 cellX = info.cellX;
2951 cellY = info.cellY;
2952 spanX = info.spanX;
2953 spanY = info.spanY;
2954 screenId = info.screenId;
2955 container = info.container;
2956 }
2957
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002958 @Override
2959 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002960 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2961 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 }
2963 }
Michael Jurkad771c962011-08-09 15:00:48 -07002964
Sunny Goyala9116722015-04-29 13:55:58 -07002965 public boolean findVacantCell(int spanX, int spanY, int[] outXY) {
2966 return Utilities.findVacantCell(outXY, spanX, spanY, mCountX, mCountY, mOccupied);
2967 }
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002968
2969 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
2970 int x2 = x + spanX - 1;
2971 int y2 = y + spanY - 1;
2972 if (x < 0 || y < 0 || x2 >= mCountX || y2 >= mCountY) {
2973 return false;
2974 }
2975 for (int i = x; i <= x2; i++) {
2976 for (int j = y; j <= y2; j++) {
2977 if (mOccupied[i][j]) {
2978 return false;
2979 }
2980 }
2981 }
2982
2983 return true;
2984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985}