blob: b09723de5d0443895f223d2faf374102864c6a58 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070021import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
Adam Cohenc9735cf2015-01-23 16:11:55 -080024import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Joe Onorato4be866d2010-10-10 11:26:02 -070027import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080029import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070031import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080033import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070034import android.graphics.drawable.Drawable;
Sunny Goyal2805e632015-05-20 15:35:32 -070035import android.graphics.drawable.TransitionDrawable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080036import android.os.Build;
Adam Cohen1462de32012-07-24 22:34:36 -070037import android.os.Parcelable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080038import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070041import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.MotionEvent;
43import android.view.View;
44import android.view.ViewDebug;
45import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080046import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070047import android.view.animation.DecelerateInterpolator;
Tony Wickham9e0702f2015-09-02 14:45:39 -070048import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
Sunny Goyal4b6eb262015-05-14 19:24:40 -070050import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.FolderIcon.FolderRingAnimator;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070052import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070053import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
54import com.android.launcher3.accessibility.FolderAccessibilityHelper;
55import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Sunny Goyal6c56c682015-07-16 14:09:05 -070056import com.android.launcher3.config.ProviderConfig;
Sunny Goyale2fd14b2015-08-27 17:45:46 -070057import com.android.launcher3.util.ParcelableSparseArray;
Adam Cohen091440a2015-03-18 14:16:05 -070058import com.android.launcher3.util.Thunk;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070059
Adam Cohen69ce2e52011-07-03 19:25:21 -070060import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070061import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080062import java.util.Collections;
63import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070064import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080065import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070066
Sunny Goyal4b6eb262015-05-14 19:24:40 -070067public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070068 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
69 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
70
Tony Wickhama0628cc2015-10-14 15:23:04 -070071 private static final String TAG = "CellLayout";
72 private static final boolean LOGD = false;
Winson Chungaafa03c2010-06-11 17:34:16 -070073
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 Cohenfa3c58f2013-12-06 16:10:55 -0800155 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700156 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohen091440a2015-03-18 14:16:05 -0700157 @Thunk float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700158
Adam Cohen482ed822012-03-02 14:15:13 -0800159 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
160 private Rect mOccupiedRect = new Rect();
161 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700162 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700163 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800164
Sunny Goyal2805e632015-05-20 15:35:32 -0700165 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700166
Michael Jurkaca993832012-06-29 15:17:04 -0700167 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700168
Adam Cohenc9735cf2015-01-23 16:11:55 -0800169 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700170 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800171 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 public CellLayout(Context context) {
174 this(context, null);
175 }
176
177 public CellLayout(Context context, AttributeSet attrs) {
178 this(context, attrs, 0);
179 }
180
181 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
182 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700183
184 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
185 // the user where a dragged item will land when dropped.
186 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800187 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700188 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700189
Adam Cohen2e6da152015-05-06 11:42:25 -0700190 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
Winson Chung11a1a532013-09-13 11:14:45 -0700192 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800193 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700194 mWidthGap = mOriginalWidthGap = 0;
195 mHeightGap = mOriginalHeightGap = 0;
196 mMaxGap = Integer.MAX_VALUE;
Adam Cohen2e6da152015-05-06 11:42:25 -0700197 mCountX = (int) grid.inv.numColumns;
198 mCountY = (int) grid.inv.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700199 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800200 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700201 mPreviousReorderDirection[0] = INVALID_DIRECTION;
202 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 setAlwaysDrawnWithCacheEnabled(false);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700205 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700206 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700207
Sunny Goyal2805e632015-05-20 15:35:32 -0700208 mBackground = (TransitionDrawable) res.getDrawable(R.drawable.bg_screenpanel);
209 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700210 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800211
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800212 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700213 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700214
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700215 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700216 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700217 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700218 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800219 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700220 }
221
222 // When dragging things around the home screens, we show a green outline of
223 // where the item will land. The outlines gradually fade out, leaving a trail
224 // behind the drag path.
225 // Set up all the animations that are used to implement this fading.
226 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700227 final float fromAlphaValue = 0;
228 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700229
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700230 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700231
232 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700233 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100234 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700235 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700236 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700237 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700238 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700239 final Bitmap outline = (Bitmap)anim.getTag();
240
241 // If an animation is started and then stopped very quickly, we can still
242 // get spurious updates we've cleared the tag. Guard against this.
243 if (outline == null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -0700244 if (LOGD) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700245 Object val = animation.getAnimatedValue();
246 Log.d(TAG, "anim " + thisIndex + " update: " + val +
247 ", isStopped " + anim.isStopped());
248 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700249 // Try to prevent it from continuing to run
250 animation.cancel();
251 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700252 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800253 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700254 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700255 }
256 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700257 // The animation holds a reference to the drag outline bitmap as long is it's
258 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700259 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700260 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700261 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700262 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700263 anim.setTag(null);
264 }
265 }
266 });
267 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700268 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700269
Michael Jurkaa52570f2012-03-20 03:18:20 -0700270 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700271 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700272 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700273
Mady Mellorbb835202015-07-15 16:34:34 -0700274 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Mady Melloref044dd2015-06-02 15:35:07 -0700275
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700276 mTouchFeedbackView = new ClickShadowView(context);
277 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700278 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700279 }
280
Adam Cohenc9735cf2015-01-23 16:11:55 -0800281 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700282 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800283 mUseTouchHelper = enable;
284 if (!enable) {
285 ViewCompat.setAccessibilityDelegate(this, null);
286 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
287 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
288 setOnClickListener(mLauncher);
289 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700290 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
291 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
292 mTouchHelper = new WorkspaceAccessibilityHelper(this);
293 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
294 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
295 mTouchHelper = new FolderAccessibilityHelper(this);
296 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800297 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
298 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
299 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
300 setOnClickListener(mTouchHelper);
301 }
302
303 // Invalidate the accessibility hierarchy
304 if (getParent() != null) {
305 getParent().notifySubtreeAccessibilityStateChanged(
306 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
307 }
308 }
309
310 @Override
311 public boolean dispatchHoverEvent(MotionEvent event) {
312 // Always attempt to dispatch hover events to accessibility first.
313 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
314 return true;
315 }
316 return super.dispatchHoverEvent(event);
317 }
318
319 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800320 public boolean onInterceptTouchEvent(MotionEvent ev) {
321 if (mUseTouchHelper ||
322 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
323 return true;
324 }
325 return false;
326 }
327
Mady Melloref044dd2015-06-02 15:35:07 -0700328 @Override
329 public boolean onTouchEvent(MotionEvent ev) {
330 boolean handled = super.onTouchEvent(ev);
331 // Stylus button press on a home screen should not switch between overview mode and
332 // the home screen mode, however, once in overview mode stylus button press should be
333 // enabled to allow rearranging the different home screens. So check what mode
334 // the workspace is in, and only perform stylus button presses while in overview mode.
335 if (mLauncher.mWorkspace.isInOverviewMode()
Mady Mellorbb835202015-07-15 16:34:34 -0700336 && mStylusEventHelper.onMotionEvent(ev)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700337 return true;
338 }
339 return handled;
340 }
341
Chris Craik01f2d7f2013-10-01 14:41:56 -0700342 public void enableHardwareLayer(boolean hasLayer) {
343 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700344 }
345
346 public void buildHardwareLayer() {
347 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700348 }
349
Adam Cohen307fe232012-08-16 17:55:58 -0700350 public float getChildrenScale() {
351 return mIsHotseat ? mHotseatScale : 1.0f;
352 }
353
Winson Chung5f8afe62013-08-12 16:19:28 -0700354 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700355 mFixedCellWidth = mCellWidth = width;
356 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700357 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
358 mCountX, mCountY);
359 }
360
Adam Cohen2801caf2011-05-13 20:57:39 -0700361 public void setGridSize(int x, int y) {
362 mCountX = x;
363 mCountY = y;
364 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800365 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700366 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700367 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700368 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700369 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700370 }
371
Adam Cohen2374abf2013-04-16 14:56:57 -0700372 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
373 public void setInvertIfRtl(boolean invert) {
374 mShortcutsAndWidgets.setInvertIfRtl(invert);
375 }
376
Adam Cohen917e3882013-10-31 15:03:35 -0700377 public void setDropPending(boolean pending) {
378 mDropPending = pending;
379 }
380
381 public boolean isDropPending() {
382 return mDropPending;
383 }
384
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700385 @Override
386 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700387 if (icon == null || background == null) {
388 mTouchFeedbackView.setBitmap(null);
389 mTouchFeedbackView.animate().cancel();
390 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700391 if (mTouchFeedbackView.setBitmap(background)) {
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700392 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets);
393 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700394 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800395 }
396 }
397
Adam Cohenc50438c2014-08-19 17:43:05 -0700398 void disableDragTarget() {
399 mIsDragTarget = false;
400 }
401
Tony Wickham0f97b782015-12-02 17:55:07 -0800402 public boolean isDragTarget() {
403 return mIsDragTarget;
404 }
405
Adam Cohenc50438c2014-08-19 17:43:05 -0700406 void setIsDragOverlapping(boolean isDragOverlapping) {
407 if (mIsDragOverlapping != isDragOverlapping) {
408 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700409 if (mIsDragOverlapping) {
410 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
411 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700412 if (mBackgroundAlpha > 0f) {
413 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
414 } else {
415 mBackground.resetTransition();
416 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700417 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700418 invalidate();
419 }
420 }
421
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700422 public void disableJailContent() {
423 mJailContent = false;
424 }
425
426 @Override
427 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
428 if (mJailContent) {
429 ParcelableSparseArray jail = getJailedArray(container);
430 super.dispatchSaveInstanceState(jail);
431 container.put(R.id.cell_layout_jail_id, jail);
432 } else {
433 super.dispatchSaveInstanceState(container);
434 }
435 }
436
437 @Override
438 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
439 super.dispatchRestoreInstanceState(mJailContent ? getJailedArray(container) : container);
440 }
441
442 private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) {
443 final Parcelable parcelable = container.get(R.id.cell_layout_jail_id);
444 return parcelable instanceof ParcelableSparseArray ?
445 (ParcelableSparseArray) parcelable : new ParcelableSparseArray();
446 }
447
Tony Wickham0f97b782015-12-02 17:55:07 -0800448 public boolean getIsDragOverlapping() {
449 return mIsDragOverlapping;
450 }
451
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700452 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700453 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700454 if (!mIsDragTarget) {
455 return;
456 }
457
Michael Jurka3e7c7632010-10-02 16:01:03 -0700458 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
459 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
460 // When we're small, we are either drawn normally or in the "accepts drops" state (during
461 // a drag). However, we also drag the mini hover background *over* one of those two
462 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700463 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700464 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700465 }
Romain Guya6abce82009-11-10 02:54:41 -0800466
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700467 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700468 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700469 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700470 if (alpha > 0) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700471 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700472 paint.setAlpha((int)(alpha + .5f));
Sunny Goyal106bf642015-07-16 12:18:06 -0700473 canvas.drawBitmap(b, null, mDragOutlines[i], paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700474 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700475 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800476
Adam Cohen482ed822012-03-02 14:15:13 -0800477 if (DEBUG_VISUALIZE_OCCUPIED) {
478 int[] pt = new int[2];
479 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700480 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800481 for (int i = 0; i < mCountX; i++) {
482 for (int j = 0; j < mCountY; j++) {
483 if (mOccupied[i][j]) {
484 cellToPoint(i, j, pt);
485 canvas.save();
486 canvas.translate(pt[0], pt[1]);
487 cd.draw(canvas);
488 canvas.restore();
489 }
490 }
491 }
492 }
493
Andrew Flynn850d2e72012-04-26 16:51:20 -0700494 int previewOffset = FolderRingAnimator.sPreviewSize;
495
Adam Cohen69ce2e52011-07-03 19:25:21 -0700496 // The folder outer / inner ring image(s)
Adam Cohen2e6da152015-05-06 11:42:25 -0700497 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700498 for (int i = 0; i < mFolderOuterRings.size(); i++) {
499 FolderRingAnimator fra = mFolderOuterRings.get(i);
500
Adam Cohen5108bc02013-09-20 17:04:51 -0700501 Drawable d;
502 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700503 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700504 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700505
Winson Chung89f97052013-09-20 11:32:26 -0700506 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700507 int centerX = mTempLocation[0] + mCellWidth / 2;
508 int centerY = mTempLocation[1] + previewOffset / 2 +
509 child.getPaddingTop() + grid.folderBackgroundOffset;
510
Adam Cohen5108bc02013-09-20 17:04:51 -0700511 // Draw outer ring, if it exists
512 if (FolderIcon.HAS_OUTER_RING) {
513 d = FolderRingAnimator.sSharedOuterRingDrawable;
514 width = (int) (fra.getOuterRingSize() * getChildrenScale());
515 height = width;
516 canvas.save();
517 canvas.translate(centerX - width / 2, centerY - height / 2);
518 d.setBounds(0, 0, width, height);
519 d.draw(canvas);
520 canvas.restore();
521 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700522
Winson Chung89f97052013-09-20 11:32:26 -0700523 // Draw inner ring
524 d = FolderRingAnimator.sSharedInnerRingDrawable;
525 width = (int) (fra.getInnerRingSize() * getChildrenScale());
526 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700527 canvas.save();
528 canvas.translate(centerX - width / 2, centerY - width / 2);
529 d.setBounds(0, 0, width, height);
530 d.draw(canvas);
531 canvas.restore();
532 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700533 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700534
535 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
536 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
537 int width = d.getIntrinsicWidth();
538 int height = d.getIntrinsicHeight();
539
540 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700541 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700542 if (child != null) {
543 int centerX = mTempLocation[0] + mCellWidth / 2;
544 int centerY = mTempLocation[1] + previewOffset / 2 +
545 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700546
Winson Chung89f97052013-09-20 11:32:26 -0700547 canvas.save();
548 canvas.translate(centerX - width / 2, centerY - width / 2);
549 d.setBounds(0, 0, width, height);
550 d.draw(canvas);
551 canvas.restore();
552 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700553 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700554 }
555
556 public void showFolderAccept(FolderRingAnimator fra) {
557 mFolderOuterRings.add(fra);
558 }
559
560 public void hideFolderAccept(FolderRingAnimator fra) {
561 if (mFolderOuterRings.contains(fra)) {
562 mFolderOuterRings.remove(fra);
563 }
564 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700565 }
566
Adam Cohenc51934b2011-07-26 21:07:43 -0700567 public void setFolderLeaveBehindCell(int x, int y) {
568 mFolderLeaveBehindCell[0] = x;
569 mFolderLeaveBehindCell[1] = y;
570 invalidate();
571 }
572
573 public void clearFolderLeaveBehind() {
574 mFolderLeaveBehindCell[0] = -1;
575 mFolderLeaveBehindCell[1] = -1;
576 invalidate();
577 }
578
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700579 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700580 public boolean shouldDelayChildPressedState() {
581 return false;
582 }
583
Adam Cohen1462de32012-07-24 22:34:36 -0700584 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700585 try {
586 dispatchRestoreInstanceState(states);
587 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700588 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700589 throw ex;
590 }
591 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
592 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
593 }
Adam Cohen1462de32012-07-24 22:34:36 -0700594 }
595
Michael Jurkae6235dd2011-10-04 15:02:05 -0700596 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700597 public void cancelLongPress() {
598 super.cancelLongPress();
599
600 // Cancel long press for all children
601 final int count = getChildCount();
602 for (int i = 0; i < count; i++) {
603 final View child = getChildAt(i);
604 child.cancelLongPress();
605 }
606 }
607
Michael Jurkadee05892010-07-27 10:01:56 -0700608 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
609 mInterceptTouchListener = listener;
610 }
611
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800612 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700613 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 }
615
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800616 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700617 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 }
619
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800620 public void setIsHotseat(boolean isHotseat) {
621 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700622 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800623 }
624
Sunny Goyale9b651e2015-04-24 11:44:51 -0700625 public boolean isHotseat() {
626 return mIsHotseat;
627 }
628
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800629 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700630 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700631 final LayoutParams lp = params;
632
Andrew Flynnde38e422012-05-08 11:22:15 -0700633 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800634 if (child instanceof BubbleTextView) {
635 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700636 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800637 }
638
Adam Cohen307fe232012-08-16 17:55:58 -0700639 child.setScaleX(getChildrenScale());
640 child.setScaleY(getChildrenScale());
641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 // Generate an id for each view, this assumes we have at most 256x256 cells
643 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700644 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700645 // If the horizontal or vertical span is set to -1, it is taken to
646 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700647 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
648 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649
Winson Chungaafa03c2010-06-11 17:34:16 -0700650 child.setId(childId);
Tony Wickhama0628cc2015-10-14 15:23:04 -0700651 if (LOGD) {
652 Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
653 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700654 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700655
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700656 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700657
Winson Chungaafa03c2010-06-11 17:34:16 -0700658 return true;
659 }
660 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700662
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 public void removeAllViews() {
665 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700666 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700667 }
668
669 @Override
670 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700671 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700672 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700673 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700674 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700675 }
676
677 @Override
678 public void removeView(View view) {
679 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700680 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700681 }
682
683 @Override
684 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700685 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
686 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700687 }
688
689 @Override
690 public void removeViewInLayout(View view) {
691 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700692 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700693 }
694
695 @Override
696 public void removeViews(int start, int count) {
697 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700698 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700699 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700700 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700701 }
702
703 @Override
704 public void removeViewsInLayout(int start, int count) {
705 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700706 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700707 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700708 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800709 }
710
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700711 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700712 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 * @param x X coordinate of the point
714 * @param y Y coordinate of the point
715 * @param result Array of 2 ints to hold the x and y coordinate of the cell
716 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700717 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700718 final int hStartPadding = getPaddingLeft();
719 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720
721 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
722 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
723
Adam Cohend22015c2010-07-26 22:02:18 -0700724 final int xAxis = mCountX;
725 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726
727 if (result[0] < 0) result[0] = 0;
728 if (result[0] >= xAxis) result[0] = xAxis - 1;
729 if (result[1] < 0) result[1] = 0;
730 if (result[1] >= yAxis) result[1] = yAxis - 1;
731 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700732
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 /**
734 * Given a point, return the cell that most closely encloses that point
735 * @param x X coordinate of the point
736 * @param y Y coordinate of the point
737 * @param result Array of 2 ints to hold the x and y coordinate of the cell
738 */
739 void pointToCellRounded(int x, int y, int[] result) {
740 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
741 }
742
743 /**
744 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700745 *
746 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700748 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 * @param result Array of 2 ints to hold the x and y coordinate of the point
750 */
751 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700752 final int hStartPadding = getPaddingLeft();
753 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754
755 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
756 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
757 }
758
Adam Cohene3e27a82011-04-15 12:07:39 -0700759 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800760 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700761 *
762 * @param cellX X coordinate of the cell
763 * @param cellY Y coordinate of the cell
764 *
765 * @param result Array of 2 ints to hold the x and y coordinate of the point
766 */
767 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700768 regionToCenterPoint(cellX, cellY, 1, 1, result);
769 }
770
771 /**
772 * Given a cell coordinate and span return the point that represents the center of the regio
773 *
774 * @param cellX X coordinate of the cell
775 * @param cellY Y coordinate of the cell
776 *
777 * @param result Array of 2 ints to hold the x and y coordinate of the point
778 */
779 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700780 final int hStartPadding = getPaddingLeft();
781 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700782 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
783 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
784 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
785 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700786 }
787
Adam Cohen19f37922012-03-21 11:59:11 -0700788 /**
789 * Given a cell coordinate and span fills out a corresponding pixel rect
790 *
791 * @param cellX X coordinate of the cell
792 * @param cellY Y coordinate of the cell
793 * @param result Rect in which to write the result
794 */
795 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
796 final int hStartPadding = getPaddingLeft();
797 final int vStartPadding = getPaddingTop();
798 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
799 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
800 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
801 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
802 }
803
Adam Cohen482ed822012-03-02 14:15:13 -0800804 public float getDistanceFromCell(float x, float y, int[] cell) {
805 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700806 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800807 }
808
Romain Guy84f296c2009-11-04 15:00:44 -0800809 int getCellWidth() {
810 return mCellWidth;
811 }
812
813 int getCellHeight() {
814 return mCellHeight;
815 }
816
Adam Cohend4844c32011-02-18 19:25:06 -0800817 int getWidthGap() {
818 return mWidthGap;
819 }
820
821 int getHeightGap() {
822 return mHeightGap;
823 }
824
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700825 public void setFixedSize(int width, int height) {
826 mFixedWidth = width;
827 mFixedHeight = height;
828 }
829
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 @Override
831 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700834 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
835 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700836 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
837 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700838 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700839 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
840 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700841 if (cw != mCellWidth || ch != mCellHeight) {
842 mCellWidth = cw;
843 mCellHeight = ch;
844 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
845 mHeightGap, mCountX, mCountY);
846 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700847 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700848
Winson Chung2d75f122013-09-23 16:53:31 -0700849 int newWidth = childWidthSize;
850 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700851 if (mFixedWidth > 0 && mFixedHeight > 0) {
852 newWidth = mFixedWidth;
853 newHeight = mFixedHeight;
854 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
856 }
857
Adam Cohend22015c2010-07-26 22:02:18 -0700858 int numWidthGaps = mCountX - 1;
859 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860
Adam Cohen234c4cd2011-07-17 21:03:04 -0700861 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700862 int hSpace = childWidthSize;
863 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700864 int hFreeSpace = hSpace - (mCountX * mCellWidth);
865 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700866 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
867 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700868 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
869 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700870 } else {
871 mWidthGap = mOriginalWidthGap;
872 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700873 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700874
875 // Make the feedback view large enough to hold the blur bitmap.
876 mTouchFeedbackView.measure(
877 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
878 MeasureSpec.EXACTLY),
879 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
880 MeasureSpec.EXACTLY));
881
882 mShortcutsAndWidgets.measure(
883 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
884 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
885
886 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
887 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700888 if (mFixedWidth > 0 && mFixedHeight > 0) {
889 setMeasuredDimension(maxWidth, maxHeight);
890 } else {
891 setMeasuredDimension(widthSize, heightSize);
892 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 }
894
895 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700896 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Tony Wickham26b01422015-11-10 14:44:32 -0800897 boolean isFullscreen = mShortcutsAndWidgets.getChildCount() > 0 &&
898 ((LayoutParams) mShortcutsAndWidgets.getChildAt(0).getLayoutParams()).isFullscreen;
899 int left = getPaddingLeft();
900 if (!isFullscreen) {
Tony Wickhama501d492015-11-03 18:05:01 -0800901 left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f);
Tony Wickham26b01422015-11-10 14:44:32 -0800902 }
Winson Chung38848ca2013-10-08 12:03:44 -0700903 int top = getPaddingTop();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700904
905 mTouchFeedbackView.layout(left, top,
906 left + mTouchFeedbackView.getMeasuredWidth(),
907 top + mTouchFeedbackView.getMeasuredHeight());
908 mShortcutsAndWidgets.layout(left, top,
909 left + r - l,
910 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 }
912
Tony Wickhama501d492015-11-03 18:05:01 -0800913 /**
914 * Returns the amount of space left over after subtracting padding and cells. This space will be
915 * very small, a few pixels at most, and is a result of rounding down when calculating the cell
916 * width in {@link DeviceProfile#calculateCellWidth(int, int)}.
917 */
918 public int getUnusedHorizontalSpace() {
919 return getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth);
920 }
921
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700923 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
924 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -0700925
926 // Expand the background drawing bounds by the padding baked into the background drawable
Sunny Goyal2805e632015-05-20 15:35:32 -0700927 mBackground.getPadding(mTempRect);
928 mBackground.setBounds(-mTempRect.left, -mTempRect.top,
929 w + mTempRect.right, h + mTempRect.bottom);
Michael Jurkadee05892010-07-27 10:01:56 -0700930 }
931
932 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700934 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 }
936
937 @Override
938 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700939 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
Michael Jurka5f1c5092010-09-03 14:15:02 -0700942 public float getBackgroundAlpha() {
943 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700944 }
945
Michael Jurka5f1c5092010-09-03 14:15:02 -0700946 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800947 if (mBackgroundAlpha != alpha) {
948 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700949 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800950 }
Michael Jurkadee05892010-07-27 10:01:56 -0700951 }
952
Sunny Goyal2805e632015-05-20 15:35:32 -0700953 @Override
954 protected boolean verifyDrawable(Drawable who) {
955 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
956 }
957
Michael Jurkaa52570f2012-03-20 03:18:20 -0700958 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700959 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700960 }
961
Michael Jurkaa52570f2012-03-20 03:18:20 -0700962 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700963 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700964 }
965
Patrick Dubroy440c3602010-07-13 17:50:32 -0700966 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700967 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700968 }
969
Adam Cohen76fc0852011-06-17 13:26:23 -0700970 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800971 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700972 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800973 boolean[][] occupied = mOccupied;
974 if (!permanent) {
975 occupied = mTmpOccupied;
976 }
977
Adam Cohen19f37922012-03-21 11:59:11 -0700978 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700979 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
980 final ItemInfo info = (ItemInfo) child.getTag();
981
982 // We cancel any existing animations
983 if (mReorderAnimators.containsKey(lp)) {
984 mReorderAnimators.get(lp).cancel();
985 mReorderAnimators.remove(lp);
986 }
987
Adam Cohen482ed822012-03-02 14:15:13 -0800988 final int oldX = lp.x;
989 final int oldY = lp.y;
990 if (adjustOccupied) {
991 occupied[lp.cellX][lp.cellY] = false;
992 occupied[cellX][cellY] = true;
993 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700994 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800995 if (permanent) {
996 lp.cellX = info.cellX = cellX;
997 lp.cellY = info.cellY = cellY;
998 } else {
999 lp.tmpCellX = cellX;
1000 lp.tmpCellY = cellY;
1001 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001002 clc.setupLp(lp);
1003 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001004 final int newX = lp.x;
1005 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001006
Adam Cohen76fc0852011-06-17 13:26:23 -07001007 lp.x = oldX;
1008 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001009
Adam Cohen482ed822012-03-02 14:15:13 -08001010 // Exit early if we're not actually moving the view
1011 if (oldX == newX && oldY == newY) {
1012 lp.isLockedToGrid = true;
1013 return true;
1014 }
1015
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001016 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001017 va.setDuration(duration);
1018 mReorderAnimators.put(lp, va);
1019
1020 va.addUpdateListener(new AnimatorUpdateListener() {
1021 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001022 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001023 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001024 lp.x = (int) ((1 - r) * oldX + r * newX);
1025 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001026 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001027 }
1028 });
Adam Cohen482ed822012-03-02 14:15:13 -08001029 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001030 boolean cancelled = false;
1031 public void onAnimationEnd(Animator animation) {
1032 // If the animation was cancelled, it means that another animation
1033 // has interrupted this one, and we don't want to lock the item into
1034 // place just yet.
1035 if (!cancelled) {
1036 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001037 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001038 }
1039 if (mReorderAnimators.containsKey(lp)) {
1040 mReorderAnimators.remove(lp);
1041 }
1042 }
1043 public void onAnimationCancel(Animator animation) {
1044 cancelled = true;
1045 }
1046 });
Adam Cohen482ed822012-03-02 14:15:13 -08001047 va.setStartDelay(delay);
1048 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001049 return true;
1050 }
1051 return false;
1052 }
1053
Tony Wickhama501d492015-11-03 18:05:01 -08001054 void visualizeDropLocation(View v, Bitmap dragOutline, int cellX, int cellY, int spanX,
1055 int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001056 final int oldDragCellX = mDragCell[0];
1057 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001058
Adam Cohen2801caf2011-05-13 20:57:39 -07001059 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001060 return;
1061 }
1062
Adam Cohen482ed822012-03-02 14:15:13 -08001063 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001064 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1065 Rect dragRegion = dragObject.dragView.getDragRegion();
1066
Adam Cohen482ed822012-03-02 14:15:13 -08001067 mDragCell[0] = cellX;
1068 mDragCell[1] = cellY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001069
Joe Onorato4be866d2010-10-10 11:26:02 -07001070 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001071 mDragOutlineAnims[oldIndex].animateOut();
1072 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001073 Rect r = mDragOutlines[mDragOutlineCurrent];
Sunny Goyal106bf642015-07-16 12:18:06 -07001074
Adam Cohend41fbf52012-02-16 23:53:59 -08001075 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001076 cellToRect(cellX, cellY, spanX, spanY, r);
Sunny Goyal106bf642015-07-16 12:18:06 -07001077 } else {
1078 // Find the top left corner of the rect the object will occupy
1079 final int[] topLeft = mTmpPoint;
1080 cellToPoint(cellX, cellY, topLeft);
1081
1082 int left = topLeft[0];
1083 int top = topLeft[1];
1084
1085 if (v != null && dragOffset == null) {
1086 // When drawing the drag outline, it did not account for margin offsets
1087 // added by the view's parent.
1088 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1089 left += lp.leftMargin;
1090 top += lp.topMargin;
1091
1092 // Offsets due to the size difference between the View and the dragOutline.
1093 // There is a size difference to account for the outer blur, which may lie
1094 // outside the bounds of the view.
1095 top += (v.getHeight() - dragOutline.getHeight()) / 2;
1096 // We center about the x axis
1097 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1098 - dragOutline.getWidth()) / 2;
1099 } else {
1100 if (dragOffset != null && dragRegion != null) {
1101 // Center the drag region *horizontally* in the cell and apply a drag
1102 // outline offset
1103 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1104 - dragRegion.width()) / 2;
1105 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1106 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1107 top += dragOffset.y + cellPaddingY;
1108 } else {
1109 // Center the drag outline in the cell
1110 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1111 - dragOutline.getWidth()) / 2;
1112 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1113 - dragOutline.getHeight()) / 2;
1114 }
1115 }
1116 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
Adam Cohend41fbf52012-02-16 23:53:59 -08001117 }
Winson Chung150fbab2010-09-29 17:14:26 -07001118
Sunny Goyal106bf642015-07-16 12:18:06 -07001119 Utilities.scaleRectAboutCenter(r, getChildrenScale());
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001120 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1121 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001122
1123 if (dragObject.stateAnnouncer != null) {
1124 String msg;
1125 if (isHotseat()) {
1126 msg = getContext().getString(R.string.move_to_hotseat_position,
1127 Math.max(cellX, cellY) + 1);
1128 } else {
1129 msg = getContext().getString(R.string.move_to_empty_cell,
1130 cellY + 1, cellX + 1);
1131 }
1132 dragObject.stateAnnouncer.announce(msg);
1133 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001134 }
1135 }
1136
Adam Cohene0310962011-04-18 16:15:31 -07001137 public void clearDragOutlines() {
1138 final int oldIndex = mDragOutlineCurrent;
1139 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001140 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001141 }
1142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001144 * Find a vacant area that will fit the given bounds nearest the requested
1145 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001146 *
Romain Guy51afc022009-05-04 18:03:43 -07001147 * @param pixelX The X location at which you want to search for a vacant area.
1148 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001149 * @param minSpanX The minimum horizontal span required
1150 * @param minSpanY The minimum vertical span required
1151 * @param spanX Horizontal span of the object.
1152 * @param spanY Vertical span of the object.
1153 * @param result Array in which to place the result, or null (in which case a new array will
1154 * be allocated)
1155 * @return The X, Y cell of a vacant area that can contain this object,
1156 * nearest the requested location.
1157 */
1158 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1159 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001160 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001161 result, resultSpan);
1162 }
1163
Adam Cohend41fbf52012-02-16 23:53:59 -08001164 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1165 private void lazyInitTempRectStack() {
1166 if (mTempRectStack.isEmpty()) {
1167 for (int i = 0; i < mCountX * mCountY; i++) {
1168 mTempRectStack.push(new Rect());
1169 }
1170 }
1171 }
Adam Cohen482ed822012-03-02 14:15:13 -08001172
Adam Cohend41fbf52012-02-16 23:53:59 -08001173 private void recycleTempRects(Stack<Rect> used) {
1174 while (!used.isEmpty()) {
1175 mTempRectStack.push(used.pop());
1176 }
1177 }
1178
1179 /**
1180 * Find a vacant area that will fit the given bounds nearest the requested
1181 * cell location. Uses Euclidean distance to score multiple vacant areas.
1182 *
1183 * @param pixelX The X location at which you want to search for a vacant area.
1184 * @param pixelY The Y location at which you want to search for a vacant area.
1185 * @param minSpanX The minimum horizontal span required
1186 * @param minSpanY The minimum vertical span required
1187 * @param spanX Horizontal span of the object.
1188 * @param spanY Vertical span of the object.
1189 * @param ignoreOccupied If true, the result can be an occupied cell
1190 * @param result Array in which to place the result, or null (in which case a new array will
1191 * be allocated)
1192 * @return The X, Y cell of a vacant area that can contain this object,
1193 * nearest the requested location.
1194 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001195 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1196 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001197 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001198
Adam Cohene3e27a82011-04-15 12:07:39 -07001199 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1200 // to the center of the item, but we are searching based on the top-left cell, so
1201 // we translate the point over to correspond to the top-left.
1202 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1203 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1204
Jeff Sharkey70864282009-04-07 21:08:40 -07001205 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001206 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001207 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001208 final Rect bestRect = new Rect(-1, -1, -1, -1);
1209 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001210
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001211 final int countX = mCountX;
1212 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001213
Adam Cohend41fbf52012-02-16 23:53:59 -08001214 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1215 spanX < minSpanX || spanY < minSpanY) {
1216 return bestXY;
1217 }
1218
1219 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001220 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001221 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1222 int ySize = -1;
1223 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001224 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001225 // First, let's see if this thing fits anywhere
1226 for (int i = 0; i < minSpanX; i++) {
1227 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001228 if (mOccupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001229 continue inner;
1230 }
Michael Jurkac28de512010-08-13 11:27:44 -07001231 }
1232 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001233 xSize = minSpanX;
1234 ySize = minSpanY;
1235
1236 // We know that the item will fit at _some_ acceptable size, now let's see
1237 // how big we can make it. We'll alternate between incrementing x and y spans
1238 // until we hit a limit.
1239 boolean incX = true;
1240 boolean hitMaxX = xSize >= spanX;
1241 boolean hitMaxY = ySize >= spanY;
1242 while (!(hitMaxX && hitMaxY)) {
1243 if (incX && !hitMaxX) {
1244 for (int j = 0; j < ySize; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001245 if (x + xSize > countX -1 || mOccupied[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001246 // We can't move out horizontally
1247 hitMaxX = true;
1248 }
1249 }
1250 if (!hitMaxX) {
1251 xSize++;
1252 }
1253 } else if (!hitMaxY) {
1254 for (int i = 0; i < xSize; i++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001255 if (y + ySize > countY - 1 || mOccupied[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001256 // We can't move out vertically
1257 hitMaxY = true;
1258 }
1259 }
1260 if (!hitMaxY) {
1261 ySize++;
1262 }
1263 }
1264 hitMaxX |= xSize >= spanX;
1265 hitMaxY |= ySize >= spanY;
1266 incX = !incX;
1267 }
1268 incX = true;
1269 hitMaxX = xSize >= spanX;
1270 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001271 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001272 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001273 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274
Adam Cohend41fbf52012-02-16 23:53:59 -08001275 // We verify that the current rect is not a sub-rect of any of our previous
1276 // candidates. In this case, the current rect is disqualified in favour of the
1277 // containing rect.
1278 Rect currentRect = mTempRectStack.pop();
1279 currentRect.set(x, y, x + xSize, y + ySize);
1280 boolean contained = false;
1281 for (Rect r : validRegions) {
1282 if (r.contains(currentRect)) {
1283 contained = true;
1284 break;
1285 }
1286 }
1287 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001288 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001289
Adam Cohend41fbf52012-02-16 23:53:59 -08001290 if ((distance <= bestDistance && !contained) ||
1291 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001292 bestDistance = distance;
1293 bestXY[0] = x;
1294 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001295 if (resultSpan != null) {
1296 resultSpan[0] = xSize;
1297 resultSpan[1] = ySize;
1298 }
1299 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001300 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302 }
1303
Adam Cohenc0dcf592011-06-01 15:30:43 -07001304 // Return -1, -1 if no suitable location found
1305 if (bestDistance == Double.MAX_VALUE) {
1306 bestXY[0] = -1;
1307 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001308 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001309 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001310 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001312
Adam Cohen482ed822012-03-02 14:15:13 -08001313 /**
1314 * Find a vacant area that will fit the given bounds nearest the requested
1315 * cell location, and will also weigh in a suggested direction vector of the
1316 * desired location. This method computers distance based on unit grid distances,
1317 * not pixel distances.
1318 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001319 * @param cellX The X cell nearest to which you want to search for a vacant area.
1320 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001321 * @param spanX Horizontal span of the object.
1322 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001323 * @param direction The favored direction in which the views should move from x, y
Sunny Goyal9eba1fd2015-10-16 08:58:57 -07001324 * @param occupied The array which represents which cells in the CellLayout are occupied
Adam Cohen47a876d2012-03-19 13:21:41 -07001325 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001326 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001327 * @param result Array in which to place the result, or null (in which case a new array will
1328 * be allocated)
1329 * @return The X, Y cell of a vacant area that can contain this object,
1330 * nearest the requested location.
1331 */
1332 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001333 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001334 // Keep track of best-scoring drop area
1335 final int[] bestXY = result != null ? result : new int[2];
1336 float bestDistance = Float.MAX_VALUE;
1337 int bestDirectionScore = Integer.MIN_VALUE;
1338
1339 final int countX = mCountX;
1340 final int countY = mCountY;
1341
1342 for (int y = 0; y < countY - (spanY - 1); y++) {
1343 inner:
1344 for (int x = 0; x < countX - (spanX - 1); x++) {
1345 // First, let's see if this thing fits anywhere
1346 for (int i = 0; i < spanX; i++) {
1347 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001348 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001349 continue inner;
1350 }
1351 }
1352 }
1353
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001354 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001355 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001356 computeDirectionVector(x - cellX, y - cellY, curDirection);
1357 // The direction score is just the dot product of the two candidate direction
1358 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001359 int curDirectionScore = direction[0] * curDirection[0] +
1360 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001361 boolean exactDirectionOnly = false;
1362 boolean directionMatches = direction[0] == curDirection[0] &&
1363 direction[0] == curDirection[0];
1364 if ((directionMatches || !exactDirectionOnly) &&
1365 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001366 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1367 bestDistance = distance;
1368 bestDirectionScore = curDirectionScore;
1369 bestXY[0] = x;
1370 bestXY[1] = y;
1371 }
1372 }
1373 }
1374
1375 // Return -1, -1 if no suitable location found
1376 if (bestDistance == Float.MAX_VALUE) {
1377 bestXY[0] = -1;
1378 bestXY[1] = -1;
1379 }
1380 return bestXY;
1381 }
1382
1383 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001384 int[] direction, ItemConfiguration currentState) {
1385 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001386 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001387 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001388 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1389
Adam Cohen8baab352012-03-20 17:39:21 -07001390 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001391
1392 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001393 c.x = mTempLocation[0];
1394 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001395 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001396 }
Adam Cohen8baab352012-03-20 17:39:21 -07001397 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001398 return success;
1399 }
1400
Adam Cohenf3900c22012-11-16 18:28:11 -08001401 /**
1402 * This helper class defines a cluster of views. It helps with defining complex edges
1403 * of the cluster and determining how those edges interact with other views. The edges
1404 * essentially define a fine-grained boundary around the cluster of views -- like a more
1405 * precise version of a bounding box.
1406 */
1407 private class ViewCluster {
1408 final static int LEFT = 0;
1409 final static int TOP = 1;
1410 final static int RIGHT = 2;
1411 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001412
Adam Cohenf3900c22012-11-16 18:28:11 -08001413 ArrayList<View> views;
1414 ItemConfiguration config;
1415 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001416
Adam Cohenf3900c22012-11-16 18:28:11 -08001417 int[] leftEdge = new int[mCountY];
1418 int[] rightEdge = new int[mCountY];
1419 int[] topEdge = new int[mCountX];
1420 int[] bottomEdge = new int[mCountX];
1421 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1422
1423 @SuppressWarnings("unchecked")
1424 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1425 this.views = (ArrayList<View>) views.clone();
1426 this.config = config;
1427 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001428 }
1429
Adam Cohenf3900c22012-11-16 18:28:11 -08001430 void resetEdges() {
1431 for (int i = 0; i < mCountX; i++) {
1432 topEdge[i] = -1;
1433 bottomEdge[i] = -1;
1434 }
1435 for (int i = 0; i < mCountY; i++) {
1436 leftEdge[i] = -1;
1437 rightEdge[i] = -1;
1438 }
1439 leftEdgeDirty = true;
1440 rightEdgeDirty = true;
1441 bottomEdgeDirty = true;
1442 topEdgeDirty = true;
1443 boundingRectDirty = true;
1444 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001445
Adam Cohenf3900c22012-11-16 18:28:11 -08001446 void computeEdge(int which, int[] edge) {
1447 int count = views.size();
1448 for (int i = 0; i < count; i++) {
1449 CellAndSpan cs = config.map.get(views.get(i));
1450 switch (which) {
1451 case LEFT:
1452 int left = cs.x;
1453 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1454 if (left < edge[j] || edge[j] < 0) {
1455 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001456 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001457 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001458 break;
1459 case RIGHT:
1460 int right = cs.x + cs.spanX;
1461 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1462 if (right > edge[j]) {
1463 edge[j] = right;
1464 }
1465 }
1466 break;
1467 case TOP:
1468 int top = cs.y;
1469 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1470 if (top < edge[j] || edge[j] < 0) {
1471 edge[j] = top;
1472 }
1473 }
1474 break;
1475 case BOTTOM:
1476 int bottom = cs.y + cs.spanY;
1477 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1478 if (bottom > edge[j]) {
1479 edge[j] = bottom;
1480 }
1481 }
1482 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001483 }
1484 }
1485 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001486
1487 boolean isViewTouchingEdge(View v, int whichEdge) {
1488 CellAndSpan cs = config.map.get(v);
1489
1490 int[] edge = getEdge(whichEdge);
1491
1492 switch (whichEdge) {
1493 case LEFT:
1494 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1495 if (edge[i] == cs.x + cs.spanX) {
1496 return true;
1497 }
1498 }
1499 break;
1500 case RIGHT:
1501 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1502 if (edge[i] == cs.x) {
1503 return true;
1504 }
1505 }
1506 break;
1507 case TOP:
1508 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1509 if (edge[i] == cs.y + cs.spanY) {
1510 return true;
1511 }
1512 }
1513 break;
1514 case BOTTOM:
1515 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1516 if (edge[i] == cs.y) {
1517 return true;
1518 }
1519 }
1520 break;
1521 }
1522 return false;
1523 }
1524
1525 void shift(int whichEdge, int delta) {
1526 for (View v: views) {
1527 CellAndSpan c = config.map.get(v);
1528 switch (whichEdge) {
1529 case LEFT:
1530 c.x -= delta;
1531 break;
1532 case RIGHT:
1533 c.x += delta;
1534 break;
1535 case TOP:
1536 c.y -= delta;
1537 break;
1538 case BOTTOM:
1539 default:
1540 c.y += delta;
1541 break;
1542 }
1543 }
1544 resetEdges();
1545 }
1546
1547 public void addView(View v) {
1548 views.add(v);
1549 resetEdges();
1550 }
1551
1552 public Rect getBoundingRect() {
1553 if (boundingRectDirty) {
1554 boolean first = true;
1555 for (View v: views) {
1556 CellAndSpan c = config.map.get(v);
1557 if (first) {
1558 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1559 first = false;
1560 } else {
1561 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1562 }
1563 }
1564 }
1565 return boundingRect;
1566 }
1567
1568 public int[] getEdge(int which) {
1569 switch (which) {
1570 case LEFT:
1571 return getLeftEdge();
1572 case RIGHT:
1573 return getRightEdge();
1574 case TOP:
1575 return getTopEdge();
1576 case BOTTOM:
1577 default:
1578 return getBottomEdge();
1579 }
1580 }
1581
1582 public int[] getLeftEdge() {
1583 if (leftEdgeDirty) {
1584 computeEdge(LEFT, leftEdge);
1585 }
1586 return leftEdge;
1587 }
1588
1589 public int[] getRightEdge() {
1590 if (rightEdgeDirty) {
1591 computeEdge(RIGHT, rightEdge);
1592 }
1593 return rightEdge;
1594 }
1595
1596 public int[] getTopEdge() {
1597 if (topEdgeDirty) {
1598 computeEdge(TOP, topEdge);
1599 }
1600 return topEdge;
1601 }
1602
1603 public int[] getBottomEdge() {
1604 if (bottomEdgeDirty) {
1605 computeEdge(BOTTOM, bottomEdge);
1606 }
1607 return bottomEdge;
1608 }
1609
1610 PositionComparator comparator = new PositionComparator();
1611 class PositionComparator implements Comparator<View> {
1612 int whichEdge = 0;
1613 public int compare(View left, View right) {
1614 CellAndSpan l = config.map.get(left);
1615 CellAndSpan r = config.map.get(right);
1616 switch (whichEdge) {
1617 case LEFT:
1618 return (r.x + r.spanX) - (l.x + l.spanX);
1619 case RIGHT:
1620 return l.x - r.x;
1621 case TOP:
1622 return (r.y + r.spanY) - (l.y + l.spanY);
1623 case BOTTOM:
1624 default:
1625 return l.y - r.y;
1626 }
1627 }
1628 }
1629
1630 public void sortConfigurationForEdgePush(int edge) {
1631 comparator.whichEdge = edge;
1632 Collections.sort(config.sortedViews, comparator);
1633 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001634 }
1635
Adam Cohenf3900c22012-11-16 18:28:11 -08001636 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1637 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001638
Adam Cohenf3900c22012-11-16 18:28:11 -08001639 ViewCluster cluster = new ViewCluster(views, currentState);
1640 Rect clusterRect = cluster.getBoundingRect();
1641 int whichEdge;
1642 int pushDistance;
1643 boolean fail = false;
1644
1645 // Determine the edge of the cluster that will be leading the push and how far
1646 // the cluster must be shifted.
1647 if (direction[0] < 0) {
1648 whichEdge = ViewCluster.LEFT;
1649 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001650 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001651 whichEdge = ViewCluster.RIGHT;
1652 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1653 } else if (direction[1] < 0) {
1654 whichEdge = ViewCluster.TOP;
1655 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1656 } else {
1657 whichEdge = ViewCluster.BOTTOM;
1658 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001659 }
1660
Adam Cohenf3900c22012-11-16 18:28:11 -08001661 // Break early for invalid push distance.
1662 if (pushDistance <= 0) {
1663 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001664 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001665
1666 // Mark the occupied state as false for the group of views we want to move.
1667 for (View v: views) {
1668 CellAndSpan c = currentState.map.get(v);
1669 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1670 }
1671
1672 // We save the current configuration -- if we fail to find a solution we will revert
1673 // to the initial state. The process of finding a solution modifies the configuration
1674 // in place, hence the need for revert in the failure case.
1675 currentState.save();
1676
1677 // The pushing algorithm is simplified by considering the views in the order in which
1678 // they would be pushed by the cluster. For example, if the cluster is leading with its
1679 // left edge, we consider sort the views by their right edge, from right to left.
1680 cluster.sortConfigurationForEdgePush(whichEdge);
1681
1682 while (pushDistance > 0 && !fail) {
1683 for (View v: currentState.sortedViews) {
1684 // For each view that isn't in the cluster, we see if the leading edge of the
1685 // cluster is contacting the edge of that view. If so, we add that view to the
1686 // cluster.
1687 if (!cluster.views.contains(v) && v != dragView) {
1688 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1689 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1690 if (!lp.canReorder) {
1691 // The push solution includes the all apps button, this is not viable.
1692 fail = true;
1693 break;
1694 }
1695 cluster.addView(v);
1696 CellAndSpan c = currentState.map.get(v);
1697
1698 // Adding view to cluster, mark it as not occupied.
1699 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1700 }
1701 }
1702 }
1703 pushDistance--;
1704
1705 // The cluster has been completed, now we move the whole thing over in the appropriate
1706 // direction.
1707 cluster.shift(whichEdge, 1);
1708 }
1709
1710 boolean foundSolution = false;
1711 clusterRect = cluster.getBoundingRect();
1712
1713 // Due to the nature of the algorithm, the only check required to verify a valid solution
1714 // is to ensure that completed shifted cluster lies completely within the cell layout.
1715 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1716 clusterRect.bottom <= mCountY) {
1717 foundSolution = true;
1718 } else {
1719 currentState.restore();
1720 }
1721
1722 // In either case, we set the occupied array as marked for the location of the views
1723 for (View v: cluster.views) {
1724 CellAndSpan c = currentState.map.get(v);
1725 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1726 }
1727
1728 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001729 }
1730
Adam Cohen482ed822012-03-02 14:15:13 -08001731 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001732 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001733 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001734
Adam Cohen8baab352012-03-20 17:39:21 -07001735 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001736 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001737 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001738 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001739 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001740 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001741 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001742 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001743 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001744 }
1745 }
Adam Cohen8baab352012-03-20 17:39:21 -07001746
Adam Cohen8baab352012-03-20 17:39:21 -07001747 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001748 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001749 CellAndSpan c = currentState.map.get(v);
1750 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1751 }
1752
Adam Cohen47a876d2012-03-19 13:21:41 -07001753 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1754 int top = boundingRect.top;
1755 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001756 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001757 // for interlocking.
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 - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001761 }
1762
Adam Cohen482ed822012-03-02 14:15:13 -08001763 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1764
Adam Cohenf3900c22012-11-16 18:28:11 -08001765 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1766 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001767
Adam Cohen8baab352012-03-20 17:39:21 -07001768 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001769 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001770 int deltaX = mTempLocation[0] - boundingRect.left;
1771 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001772 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001773 CellAndSpan c = currentState.map.get(v);
1774 c.x += deltaX;
1775 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001776 }
1777 success = true;
1778 }
Adam Cohen8baab352012-03-20 17:39:21 -07001779
1780 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001781 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001782 CellAndSpan c = currentState.map.get(v);
1783 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001784 }
1785 return success;
1786 }
1787
1788 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1789 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1790 }
1791
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001792 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1793 // to push items in each of the cardinal directions, in an order based on the direction vector
1794 // passed.
1795 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1796 int[] direction, View ignoreView, ItemConfiguration solution) {
1797 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001798 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001799 // separately in each of the components.
1800 int temp = direction[1];
1801 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001802
1803 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001804 ignoreView, solution)) {
1805 return true;
1806 }
1807 direction[1] = temp;
1808 temp = direction[0];
1809 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001810
1811 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001812 ignoreView, solution)) {
1813 return true;
1814 }
1815 // Revert the direction
1816 direction[0] = temp;
1817
1818 // Now we try pushing in each component of the opposite direction
1819 direction[0] *= -1;
1820 direction[1] *= -1;
1821 temp = direction[1];
1822 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001823 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001824 ignoreView, solution)) {
1825 return true;
1826 }
1827
1828 direction[1] = temp;
1829 temp = direction[0];
1830 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001831 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001832 ignoreView, solution)) {
1833 return true;
1834 }
1835 // revert the direction
1836 direction[0] = temp;
1837 direction[0] *= -1;
1838 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001839
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001840 } else {
1841 // If the direction vector has a single non-zero component, we push first in the
1842 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001843 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001844 ignoreView, solution)) {
1845 return true;
1846 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001847 // 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;
Winson Chung5f8afe62013-08-12 16:19:28 -07001857
1858 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001859 // to find a solution by pushing along the perpendicular axis.
1860
1861 // Swap the components
1862 int temp = direction[1];
1863 direction[1] = direction[0];
1864 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001865 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001866 ignoreView, solution)) {
1867 return true;
1868 }
1869
1870 // Then we try the opposite direction
1871 direction[0] *= -1;
1872 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001873 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001874 ignoreView, solution)) {
1875 return true;
1876 }
1877 // Switch the direction back
1878 direction[0] *= -1;
1879 direction[1] *= -1;
1880
1881 // Swap the components back
1882 temp = direction[1];
1883 direction[1] = direction[0];
1884 direction[0] = temp;
1885 }
1886 return false;
1887 }
1888
Adam Cohen482ed822012-03-02 14:15:13 -08001889 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001890 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001891 // Return early if get invalid cell positions
1892 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001893
Adam Cohen8baab352012-03-20 17:39:21 -07001894 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001895 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001896
Adam Cohen8baab352012-03-20 17:39:21 -07001897 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001898 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001899 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001900 if (c != null) {
1901 c.x = cellX;
1902 c.y = cellY;
1903 }
Adam Cohen482ed822012-03-02 14:15:13 -08001904 }
Adam Cohen482ed822012-03-02 14:15:13 -08001905 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1906 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001907 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001908 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001909 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001910 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001911 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001912 if (Rect.intersects(r0, r1)) {
1913 if (!lp.canReorder) {
1914 return false;
1915 }
1916 mIntersectingViews.add(child);
1917 }
1918 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001919
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001920 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1921
Winson Chung5f8afe62013-08-12 16:19:28 -07001922 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001923 // we try to find a solution such that no displaced item travels through another item
1924 // without also displacing that item.
1925 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001926 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001927 return true;
1928 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001929
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001930 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001931 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001932 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001933 return true;
1934 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001935
Adam Cohen482ed822012-03-02 14:15:13 -08001936 // Ok, they couldn't move as a block, let's move them individually
1937 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001938 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001939 return false;
1940 }
1941 }
1942 return true;
1943 }
1944
1945 /*
1946 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1947 * the provided point and the provided cell
1948 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001949 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001950 double angle = Math.atan(((float) deltaY) / deltaX);
1951
1952 result[0] = 0;
1953 result[1] = 0;
1954 if (Math.abs(Math.cos(angle)) > 0.5f) {
1955 result[0] = (int) Math.signum(deltaX);
1956 }
1957 if (Math.abs(Math.sin(angle)) > 0.5f) {
1958 result[1] = (int) Math.signum(deltaY);
1959 }
1960 }
1961
Adam Cohen8baab352012-03-20 17:39:21 -07001962 private void copyOccupiedArray(boolean[][] occupied) {
1963 for (int i = 0; i < mCountX; i++) {
1964 for (int j = 0; j < mCountY; j++) {
1965 occupied[i][j] = mOccupied[i][j];
1966 }
1967 }
1968 }
1969
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001970 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001971 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1972 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001973 // Copy the current state into the solution. This solution will be manipulated as necessary.
1974 copyCurrentStateToSolution(solution, false);
1975 // Copy the current occupied array into the temporary occupied array. This array will be
1976 // manipulated as necessary to find a solution.
1977 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001978
1979 // We find the nearest cell into which we would place the dragged item, assuming there's
1980 // nothing in its way.
1981 int result[] = new int[2];
1982 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1983
1984 boolean success = false;
1985 // First we try the exact nearest position of the item being dragged,
1986 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001987 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1988 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001989
1990 if (!success) {
1991 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1992 // x, then 1 in y etc.
1993 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001994 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1995 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001996 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001997 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1998 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001999 }
2000 solution.isSolution = false;
2001 } else {
2002 solution.isSolution = true;
2003 solution.dragViewX = result[0];
2004 solution.dragViewY = result[1];
2005 solution.dragViewSpanX = spanX;
2006 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002007 }
2008 return solution;
2009 }
2010
2011 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002012 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002013 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002014 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002015 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002016 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002017 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002018 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002019 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002020 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002021 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002022 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002023 }
2024 }
2025
2026 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2027 for (int i = 0; i < mCountX; i++) {
2028 for (int j = 0; j < mCountY; j++) {
2029 mTmpOccupied[i][j] = false;
2030 }
2031 }
2032
Michael Jurkaa52570f2012-03-20 03:18:20 -07002033 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002034 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002035 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002036 if (child == dragView) continue;
2037 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002038 CellAndSpan c = solution.map.get(child);
2039 if (c != null) {
2040 lp.tmpCellX = c.x;
2041 lp.tmpCellY = c.y;
2042 lp.cellHSpan = c.spanX;
2043 lp.cellVSpan = c.spanY;
2044 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002045 }
2046 }
2047 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2048 solution.dragViewSpanY, mTmpOccupied, true);
2049 }
2050
2051 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2052 commitDragView) {
2053
2054 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2055 for (int i = 0; i < mCountX; i++) {
2056 for (int j = 0; j < mCountY; j++) {
2057 occupied[i][j] = false;
2058 }
2059 }
2060
Michael Jurkaa52570f2012-03-20 03:18:20 -07002061 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002062 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002063 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002064 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002065 CellAndSpan c = solution.map.get(child);
2066 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002067 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2068 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002069 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002070 }
2071 }
2072 if (commitDragView) {
2073 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2074 solution.dragViewSpanY, occupied, true);
2075 }
2076 }
2077
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002078
2079 // This method starts or changes the reorder preview animations
2080 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2081 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002082 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002083 for (int i = 0; i < childCount; i++) {
2084 View child = mShortcutsAndWidgets.getChildAt(i);
2085 if (child == dragView) continue;
2086 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002087 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2088 != null && !solution.intersectingViews.contains(child);
2089
Adam Cohen19f37922012-03-21 11:59:11 -07002090 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002091 if (c != null && !skip) {
2092 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2093 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002094 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002095 }
2096 }
2097 }
2098
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002099 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002100 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002101 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002102 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002103 float finalDeltaX;
2104 float finalDeltaY;
2105 float initDeltaX;
2106 float initDeltaY;
2107 float finalScale;
2108 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002109 int mode;
2110 boolean repeating = false;
2111 private static final int PREVIEW_DURATION = 300;
2112 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2113
2114 public static final int MODE_HINT = 0;
2115 public static final int MODE_PREVIEW = 1;
2116
Adam Cohene7587d22012-05-24 18:50:02 -07002117 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002118
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002119 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2120 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002121 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2122 final int x0 = mTmpPoint[0];
2123 final int y0 = mTmpPoint[1];
2124 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2125 final int x1 = mTmpPoint[0];
2126 final int y1 = mTmpPoint[1];
2127 final int dX = x1 - x0;
2128 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002129 finalDeltaX = 0;
2130 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002131 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002132 if (dX == dY && dX == 0) {
2133 } else {
2134 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002135 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002136 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002137 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002138 } else {
2139 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002140 finalDeltaX = (int) (- dir * Math.signum(dX) *
2141 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2142 finalDeltaY = (int) (- dir * Math.signum(dY) *
2143 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002144 }
2145 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002146 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002147 initDeltaX = child.getTranslationX();
2148 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002149 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002150 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002151 this.child = child;
2152 }
2153
Adam Cohend024f982012-05-23 18:26:45 -07002154 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002155 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002156 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002157 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002158 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002159 if (finalDeltaX == 0 && finalDeltaY == 0) {
2160 completeAnimationImmediately();
2161 return;
2162 }
Adam Cohen19f37922012-03-21 11:59:11 -07002163 }
Adam Cohend024f982012-05-23 18:26:45 -07002164 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002165 return;
2166 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002167 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002168 a = va;
Tony Wickham9e0702f2015-09-02 14:45:39 -07002169
2170 // Animations are disabled in power save mode, causing the repeated animation to jump
2171 // spastically between beginning and end states. Since this looks bad, we don't repeat
2172 // the animation in power save mode.
Tony Wickham112ac952015-11-12 12:31:50 -08002173 if (!Utilities.isPowerSaverOn(getContext())) {
Tony Wickham9e0702f2015-09-02 14:45:39 -07002174 va.setRepeatMode(ValueAnimator.REVERSE);
2175 va.setRepeatCount(ValueAnimator.INFINITE);
2176 }
2177
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002178 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002179 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002180 va.addUpdateListener(new AnimatorUpdateListener() {
2181 @Override
2182 public void onAnimationUpdate(ValueAnimator animation) {
2183 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002184 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2185 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2186 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002187 child.setTranslationX(x);
2188 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002189 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002190 child.setScaleX(s);
2191 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002192 }
2193 });
2194 va.addListener(new AnimatorListenerAdapter() {
2195 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002196 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002197 initDeltaX = 0;
2198 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002199 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002200 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002201 }
2202 });
Adam Cohen19f37922012-03-21 11:59:11 -07002203 mShakeAnimators.put(child, this);
2204 va.start();
2205 }
2206
Adam Cohend024f982012-05-23 18:26:45 -07002207 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002208 if (a != null) {
2209 a.cancel();
2210 }
Adam Cohen19f37922012-03-21 11:59:11 -07002211 }
Adam Cohene7587d22012-05-24 18:50:02 -07002212
Adam Cohen091440a2015-03-18 14:16:05 -07002213 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002214 if (a != null) {
2215 a.cancel();
2216 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002217
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002218 a = new LauncherViewPropertyAnimator(child)
2219 .scaleX(getChildrenScale())
2220 .scaleY(getChildrenScale())
2221 .translationX(0)
2222 .translationY(0)
2223 .setDuration(REORDER_ANIMATION_DURATION);
2224 a.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2225 a.start();
Brandon Keely50e6e562012-05-08 16:28:49 -07002226 }
Adam Cohen19f37922012-03-21 11:59:11 -07002227 }
2228
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002229 private void completeAndClearReorderPreviewAnimations() {
2230 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002231 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002232 }
2233 mShakeAnimators.clear();
2234 }
2235
Adam Cohen482ed822012-03-02 14:15:13 -08002236 private void commitTempPlacement() {
2237 for (int i = 0; i < mCountX; i++) {
2238 for (int j = 0; j < mCountY; j++) {
2239 mOccupied[i][j] = mTmpOccupied[i][j];
2240 }
2241 }
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002242
2243 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2244 int container = Favorites.CONTAINER_DESKTOP;
2245
2246 if (mLauncher.isHotseatLayout(this)) {
2247 screenId = -1;
2248 container = Favorites.CONTAINER_HOTSEAT;
2249 }
2250
Michael Jurkaa52570f2012-03-20 03:18:20 -07002251 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002252 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002253 View child = mShortcutsAndWidgets.getChildAt(i);
2254 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2255 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002256 // We do a null check here because the item info can be null in the case of the
2257 // AllApps button in the hotseat.
2258 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002259 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2260 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2261 || info.spanY != lp.cellVSpan);
2262
Adam Cohen2acce882012-03-28 19:03:19 -07002263 info.cellX = lp.cellX = lp.tmpCellX;
2264 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002265 info.spanX = lp.cellHSpan;
2266 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002267
2268 if (requiresDbUpdate) {
2269 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId,
2270 info.cellX, info.cellY, info.spanX, info.spanY);
2271 }
Adam Cohen2acce882012-03-28 19:03:19 -07002272 }
Adam Cohen482ed822012-03-02 14:15:13 -08002273 }
2274 }
2275
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002276 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002277 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002278 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002279 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002280 lp.useTmpCoords = useTempCoords;
2281 }
2282 }
2283
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002284 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002285 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2286 int[] result = new int[2];
2287 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002288 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002289 resultSpan);
2290 if (result[0] >= 0 && result[1] >= 0) {
2291 copyCurrentStateToSolution(solution, false);
2292 solution.dragViewX = result[0];
2293 solution.dragViewY = result[1];
2294 solution.dragViewSpanX = resultSpan[0];
2295 solution.dragViewSpanY = resultSpan[1];
2296 solution.isSolution = true;
2297 } else {
2298 solution.isSolution = false;
2299 }
2300 return solution;
2301 }
2302
2303 public void prepareChildForDrag(View child) {
2304 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002305 }
2306
Adam Cohen19f37922012-03-21 11:59:11 -07002307 /* This seems like it should be obvious and straight-forward, but when the direction vector
2308 needs to match with the notion of the dragView pushing other views, we have to employ
2309 a slightly more subtle notion of the direction vector. The question is what two points is
2310 the vector between? The center of the dragView and its desired destination? Not quite, as
2311 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2312 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2313 or right, which helps make pushing feel right.
2314 */
2315 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2316 int spanY, View dragView, int[] resultDirection) {
2317 int[] targetDestination = new int[2];
2318
2319 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2320 Rect dragRect = new Rect();
2321 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2322 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2323
2324 Rect dropRegionRect = new Rect();
2325 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2326 dragView, dropRegionRect, mIntersectingViews);
2327
2328 int dropRegionSpanX = dropRegionRect.width();
2329 int dropRegionSpanY = dropRegionRect.height();
2330
2331 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2332 dropRegionRect.height(), dropRegionRect);
2333
2334 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2335 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2336
2337 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2338 deltaX = 0;
2339 }
2340 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2341 deltaY = 0;
2342 }
2343
2344 if (deltaX == 0 && deltaY == 0) {
2345 // No idea what to do, give a random direction.
2346 resultDirection[0] = 1;
2347 resultDirection[1] = 0;
2348 } else {
2349 computeDirectionVector(deltaX, deltaY, resultDirection);
2350 }
2351 }
2352
2353 // For a given cell and span, fetch the set of views intersecting the region.
2354 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2355 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2356 if (boundingRect != null) {
2357 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2358 }
2359 intersectingViews.clear();
2360 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2361 Rect r1 = new Rect();
2362 final int count = mShortcutsAndWidgets.getChildCount();
2363 for (int i = 0; i < count; i++) {
2364 View child = mShortcutsAndWidgets.getChildAt(i);
2365 if (child == dragView) continue;
2366 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2367 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2368 if (Rect.intersects(r0, r1)) {
2369 mIntersectingViews.add(child);
2370 if (boundingRect != null) {
2371 boundingRect.union(r1);
2372 }
2373 }
2374 }
2375 }
2376
2377 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2378 View dragView, int[] result) {
2379 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2380 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2381 mIntersectingViews);
2382 return !mIntersectingViews.isEmpty();
2383 }
2384
2385 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002386 completeAndClearReorderPreviewAnimations();
2387 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2388 final int count = mShortcutsAndWidgets.getChildCount();
2389 for (int i = 0; i < count; i++) {
2390 View child = mShortcutsAndWidgets.getChildAt(i);
2391 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2392 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2393 lp.tmpCellX = lp.cellX;
2394 lp.tmpCellY = lp.cellY;
2395 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2396 0, false, false);
2397 }
Adam Cohen19f37922012-03-21 11:59:11 -07002398 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002399 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002400 }
Adam Cohen19f37922012-03-21 11:59:11 -07002401 }
2402
Adam Cohenbebf0422012-04-11 18:06:28 -07002403 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2404 View dragView, int[] direction, boolean commit) {
2405 int[] pixelXY = new int[2];
2406 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2407
2408 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002409 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002410 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2411
2412 setUseTempCoords(true);
2413 if (swapSolution != null && swapSolution.isSolution) {
2414 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2415 // committing anything or animating anything as we just want to determine if a solution
2416 // exists
2417 copySolutionToTempState(swapSolution, dragView);
2418 setItemPlacementDirty(true);
2419 animateItemsToSolution(swapSolution, dragView, commit);
2420
2421 if (commit) {
2422 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002423 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002424 setItemPlacementDirty(false);
2425 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002426 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2427 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002428 }
2429 mShortcutsAndWidgets.requestLayout();
2430 }
2431 return swapSolution.isSolution;
2432 }
2433
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002434 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002435 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002436 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002437 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002438
2439 if (resultSpan == null) {
2440 resultSpan = new int[2];
2441 }
2442
Adam Cohen19f37922012-03-21 11:59:11 -07002443 // When we are checking drop validity or actually dropping, we don't recompute the
2444 // direction vector, since we want the solution to match the preview, and it's possible
2445 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002446 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2447 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002448 mDirectionVector[0] = mPreviousReorderDirection[0];
2449 mDirectionVector[1] = mPreviousReorderDirection[1];
2450 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002451 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2452 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2453 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002454 }
2455 } else {
2456 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2457 mPreviousReorderDirection[0] = mDirectionVector[0];
2458 mPreviousReorderDirection[1] = mDirectionVector[1];
2459 }
2460
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002461 // Find a solution involving pushing / displacing any items in the way
2462 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002463 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2464
2465 // We attempt the approach which doesn't shuffle views at all
2466 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2467 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2468
2469 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002470
2471 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2472 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002473 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2474 finalSolution = swapSolution;
2475 } else if (noShuffleSolution.isSolution) {
2476 finalSolution = noShuffleSolution;
2477 }
2478
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002479 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002480 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002481 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2482 ReorderPreviewAnimation.MODE_HINT);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002483 result[0] = finalSolution.dragViewX;
2484 result[1] = finalSolution.dragViewY;
2485 resultSpan[0] = finalSolution.dragViewSpanX;
2486 resultSpan[1] = finalSolution.dragViewSpanY;
2487 } else {
2488 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2489 }
2490 return result;
2491 }
2492
Adam Cohen482ed822012-03-02 14:15:13 -08002493 boolean foundSolution = true;
2494 if (!DESTRUCTIVE_REORDER) {
2495 setUseTempCoords(true);
2496 }
2497
2498 if (finalSolution != null) {
2499 result[0] = finalSolution.dragViewX;
2500 result[1] = finalSolution.dragViewY;
2501 resultSpan[0] = finalSolution.dragViewSpanX;
2502 resultSpan[1] = finalSolution.dragViewSpanY;
2503
2504 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2505 // committing anything or animating anything as we just want to determine if a solution
2506 // exists
2507 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2508 if (!DESTRUCTIVE_REORDER) {
2509 copySolutionToTempState(finalSolution, dragView);
2510 }
2511 setItemPlacementDirty(true);
2512 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2513
Adam Cohen19f37922012-03-21 11:59:11 -07002514 if (!DESTRUCTIVE_REORDER &&
2515 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002516 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002517 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002518 setItemPlacementDirty(false);
2519 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002520 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2521 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002522 }
2523 }
2524 } else {
2525 foundSolution = false;
2526 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2527 }
2528
2529 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2530 setUseTempCoords(false);
2531 }
Adam Cohen482ed822012-03-02 14:15:13 -08002532
Michael Jurkaa52570f2012-03-20 03:18:20 -07002533 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002534 return result;
2535 }
2536
Adam Cohen19f37922012-03-21 11:59:11 -07002537 void setItemPlacementDirty(boolean dirty) {
2538 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002539 }
Adam Cohen19f37922012-03-21 11:59:11 -07002540 boolean isItemPlacementDirty() {
2541 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002542 }
2543
Adam Cohen091440a2015-03-18 14:16:05 -07002544 @Thunk class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002545 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002546 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2547 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002548 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002549 boolean isSolution = false;
2550 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2551
Adam Cohenf3900c22012-11-16 18:28:11 -08002552 void save() {
2553 // Copy current state into savedMap
2554 for (View v: map.keySet()) {
2555 map.get(v).copy(savedMap.get(v));
2556 }
2557 }
2558
2559 void restore() {
2560 // Restore current state from savedMap
2561 for (View v: savedMap.keySet()) {
2562 savedMap.get(v).copy(map.get(v));
2563 }
2564 }
2565
2566 void add(View v, CellAndSpan cs) {
2567 map.put(v, cs);
2568 savedMap.put(v, new CellAndSpan());
2569 sortedViews.add(v);
2570 }
2571
Adam Cohen482ed822012-03-02 14:15:13 -08002572 int area() {
2573 return dragViewSpanX * dragViewSpanY;
2574 }
Adam Cohen8baab352012-03-20 17:39:21 -07002575 }
2576
2577 private class CellAndSpan {
2578 int x, y;
2579 int spanX, spanY;
2580
Adam Cohenf3900c22012-11-16 18:28:11 -08002581 public CellAndSpan() {
2582 }
2583
2584 public void copy(CellAndSpan copy) {
2585 copy.x = x;
2586 copy.y = y;
2587 copy.spanX = spanX;
2588 copy.spanY = spanY;
2589 }
2590
Adam Cohen8baab352012-03-20 17:39:21 -07002591 public CellAndSpan(int x, int y, int spanX, int spanY) {
2592 this.x = x;
2593 this.y = y;
2594 this.spanX = spanX;
2595 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002596 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002597
2598 public String toString() {
2599 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2600 }
2601
Adam Cohen482ed822012-03-02 14:15:13 -08002602 }
2603
Adam Cohendf035382011-04-11 17:22:04 -07002604 /**
Adam Cohendf035382011-04-11 17:22:04 -07002605 * Find a starting cell position that will fit the given bounds nearest the requested
2606 * cell location. Uses Euclidean distance to score multiple vacant areas.
2607 *
2608 * @param pixelX The X location at which you want to search for a vacant area.
2609 * @param pixelY The Y location at which you want to search for a vacant area.
2610 * @param spanX Horizontal span of the object.
2611 * @param spanY Vertical span of the object.
2612 * @param ignoreView Considers space occupied by this view as unoccupied
2613 * @param result Previously returned value to possibly recycle.
2614 * @return The X, Y cell of a vacant area that can contain this object,
2615 * nearest the requested location.
2616 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002617 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2618 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002619 }
2620
Michael Jurka0280c3b2010-09-17 15:00:07 -07002621 boolean existsEmptyCell() {
2622 return findCellForSpan(null, 1, 1);
2623 }
2624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002625 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002626 * Finds the upper-left coordinate of the first rectangle in the grid that can
2627 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2628 * then this method will only return coordinates for rectangles that contain the cell
2629 * (intersectX, intersectY)
2630 *
2631 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2632 * can be found.
2633 * @param spanX The horizontal span of the cell we want to find.
2634 * @param spanY The vertical span of the cell we want to find.
2635 *
2636 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002637 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002638 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Michael Jurka28750fb2010-09-24 17:43:49 -07002639 boolean foundCell = false;
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002640 final int endX = mCountX - (spanX - 1);
2641 final int endY = mCountY - (spanY - 1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002642
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002643 for (int y = 0; y < endY && !foundCell; y++) {
2644 inner:
2645 for (int x = 0; x < endX; x++) {
2646 for (int i = 0; i < spanX; i++) {
2647 for (int j = 0; j < spanY; j++) {
2648 if (mOccupied[x + i][y + j]) {
2649 // small optimization: we can skip to after the column we just found
2650 // an occupied cell
2651 x += i;
2652 continue inner;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002653 }
2654 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002655 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002656 if (cellXY != null) {
2657 cellXY[0] = x;
2658 cellXY[1] = y;
2659 }
2660 foundCell = true;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002661 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002662 }
2663 }
2664
Michael Jurka28750fb2010-09-24 17:43:49 -07002665 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002666 }
2667
2668 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002669 * A drag event has begun over this layout.
2670 * It may have begun over this layout (in which case onDragChild is called first),
2671 * or it may have begun on another layout.
2672 */
2673 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002674 mDragging = true;
2675 }
2676
2677 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002678 * Called when drag has left this CellLayout or has been completed (successfully or not)
2679 */
2680 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002681 // This can actually be called when we aren't in a drag, e.g. when adding a new
2682 // item to this layout via the customize drawer.
2683 // Guard against that case.
2684 if (mDragging) {
2685 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002686 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002687
2688 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002689 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002690 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2691 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002692 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002693 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002694 }
2695
2696 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002697 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002698 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002699 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002700 *
2701 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002702 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002703 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002704 if (child != null) {
2705 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002706 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002707 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002708 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002709 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002710 }
2711
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002712 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002714 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002715 * @param cellX X coordinate of upper left corner expressed as a cell position
2716 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002717 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002718 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002719 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002721 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002722 final int cellWidth = mCellWidth;
2723 final int cellHeight = mCellHeight;
2724 final int widthGap = mWidthGap;
2725 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002726
Winson Chung4b825dcd2011-06-19 12:41:22 -07002727 final int hStartPadding = getPaddingLeft();
2728 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002729
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002730 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2731 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2732
2733 int x = hStartPadding + cellX * (cellWidth + widthGap);
2734 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002735
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002736 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002737 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002738
Michael Jurka0280c3b2010-09-17 15:00:07 -07002739 private void clearOccupiedCells() {
2740 for (int x = 0; x < mCountX; x++) {
2741 for (int y = 0; y < mCountY; y++) {
2742 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002743 }
2744 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002745 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002746
Adam Cohend4844c32011-02-18 19:25:06 -08002747 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002748 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002749 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002750 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002751 }
2752
Adam Cohend4844c32011-02-18 19:25:06 -08002753 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002754 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002755 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002756 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002757 }
2758
Adam Cohen482ed822012-03-02 14:15:13 -08002759 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2760 boolean value) {
2761 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002762 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2763 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002764 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002765 }
2766 }
2767 }
2768
Adam Cohen2801caf2011-05-13 20:57:39 -07002769 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002770 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002771 (Math.max((mCountX - 1), 0) * mWidthGap);
2772 }
2773
2774 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002775 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002776 (Math.max((mCountY - 1), 0) * mHeightGap);
2777 }
2778
Michael Jurka66d72172011-04-12 16:29:25 -07002779 public boolean isOccupied(int x, int y) {
2780 if (x < mCountX && y < mCountY) {
2781 return mOccupied[x][y];
2782 } else {
2783 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2784 }
2785 }
2786
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002787 @Override
2788 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2789 return new CellLayout.LayoutParams(getContext(), attrs);
2790 }
2791
2792 @Override
2793 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2794 return p instanceof CellLayout.LayoutParams;
2795 }
2796
2797 @Override
2798 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2799 return new CellLayout.LayoutParams(p);
2800 }
2801
2802 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2803 /**
2804 * Horizontal location of the item in the grid.
2805 */
2806 @ViewDebug.ExportedProperty
2807 public int cellX;
2808
2809 /**
2810 * Vertical location of the item in the grid.
2811 */
2812 @ViewDebug.ExportedProperty
2813 public int cellY;
2814
2815 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002816 * Temporary horizontal location of the item in the grid during reorder
2817 */
2818 public int tmpCellX;
2819
2820 /**
2821 * Temporary vertical location of the item in the grid during reorder
2822 */
2823 public int tmpCellY;
2824
2825 /**
2826 * Indicates that the temporary coordinates should be used to layout the items
2827 */
2828 public boolean useTmpCoords;
2829
2830 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002831 * Number of cells spanned horizontally by the item.
2832 */
2833 @ViewDebug.ExportedProperty
2834 public int cellHSpan;
2835
2836 /**
2837 * Number of cells spanned vertically by the item.
2838 */
2839 @ViewDebug.ExportedProperty
2840 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002841
Adam Cohen1b607ed2011-03-03 17:26:50 -08002842 /**
2843 * Indicates whether the item will set its x, y, width and height parameters freely,
2844 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2845 */
Adam Cohend4844c32011-02-18 19:25:06 -08002846 public boolean isLockedToGrid = true;
2847
Adam Cohen482ed822012-03-02 14:15:13 -08002848 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002849 * Indicates that this item should use the full extents of its parent.
2850 */
2851 public boolean isFullscreen = false;
2852
2853 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002854 * Indicates whether this item can be reordered. Always true except in the case of the
2855 * the AllApps button.
2856 */
2857 public boolean canReorder = true;
2858
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859 // X coordinate of the view in the layout.
2860 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002861 public int x;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002862 // Y coordinate of the view in the layout.
2863 @ViewDebug.ExportedProperty
Vadim Tryshevfedca432015-08-19 17:55:02 -07002864 public int y;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002865
Romain Guy84f296c2009-11-04 15:00:44 -08002866 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002867
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002868 public LayoutParams(Context c, AttributeSet attrs) {
2869 super(c, attrs);
2870 cellHSpan = 1;
2871 cellVSpan = 1;
2872 }
2873
2874 public LayoutParams(ViewGroup.LayoutParams source) {
2875 super(source);
2876 cellHSpan = 1;
2877 cellVSpan = 1;
2878 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002879
2880 public LayoutParams(LayoutParams source) {
2881 super(source);
2882 this.cellX = source.cellX;
2883 this.cellY = source.cellY;
2884 this.cellHSpan = source.cellHSpan;
2885 this.cellVSpan = source.cellVSpan;
2886 }
2887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002888 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002889 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002890 this.cellX = cellX;
2891 this.cellY = cellY;
2892 this.cellHSpan = cellHSpan;
2893 this.cellVSpan = cellVSpan;
2894 }
2895
Adam Cohen2374abf2013-04-16 14:56:57 -07002896 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2897 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002898 if (isLockedToGrid) {
2899 final int myCellHSpan = cellHSpan;
2900 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002901 int myCellX = useTmpCoords ? tmpCellX : cellX;
2902 int myCellY = useTmpCoords ? tmpCellY : cellY;
2903
2904 if (invertHorizontally) {
2905 myCellX = colCount - myCellX - cellHSpan;
2906 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002907
Adam Cohend4844c32011-02-18 19:25:06 -08002908 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2909 leftMargin - rightMargin;
2910 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2911 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002912 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2913 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002914 }
2915 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002916
Winson Chungaafa03c2010-06-11 17:34:16 -07002917 public String toString() {
2918 return "(" + this.cellX + ", " + this.cellY + ")";
2919 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002920
2921 public void setWidth(int width) {
2922 this.width = width;
2923 }
2924
2925 public int getWidth() {
2926 return width;
2927 }
2928
2929 public void setHeight(int height) {
2930 this.height = height;
2931 }
2932
2933 public int getHeight() {
2934 return height;
2935 }
2936
2937 public void setX(int x) {
2938 this.x = x;
2939 }
2940
2941 public int getX() {
2942 return x;
2943 }
2944
2945 public void setY(int y) {
2946 this.y = y;
2947 }
2948
2949 public int getY() {
2950 return y;
2951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002952 }
2953
Michael Jurka0280c3b2010-09-17 15:00:07 -07002954 // This class stores info for two purposes:
2955 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2956 // its spanX, spanY, and the screen it is on
2957 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2958 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2959 // the CellLayout that was long clicked
Sunny Goyal83a8f042015-05-19 12:52:12 -07002960 public static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002961 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002962 int cellX = -1;
2963 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002964 int spanX;
2965 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07002966 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002967 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968
Sunny Goyal83a8f042015-05-19 12:52:12 -07002969 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002970 cell = v;
2971 cellX = info.cellX;
2972 cellY = info.cellY;
2973 spanX = info.spanX;
2974 spanY = info.spanY;
2975 screenId = info.screenId;
2976 container = info.container;
2977 }
2978
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 @Override
2980 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002981 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2982 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002983 }
2984 }
Michael Jurkad771c962011-08-09 15:00:48 -07002985
Sunny Goyala9116722015-04-29 13:55:58 -07002986 public boolean findVacantCell(int spanX, int spanY, int[] outXY) {
2987 return Utilities.findVacantCell(outXY, spanX, spanY, mCountX, mCountY, mOccupied);
2988 }
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002989
Tony Wickham86930612015-09-09 13:50:40 -07002990 /**
2991 * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing
2992 * if necessary).
2993 */
2994 public boolean hasReorderSolution(ItemInfo itemInfo) {
2995 int[] cellPoint = new int[2];
2996 // Check for a solution starting at every cell.
2997 for (int cellX = 0; cellX < getCountX(); cellX++) {
2998 for (int cellY = 0; cellY < getCountY(); cellY++) {
2999 cellToPoint(cellX, cellY, cellPoint);
3000 if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX,
3001 itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null,
3002 true, new ItemConfiguration()).isSolution) {
3003 return true;
3004 }
3005 }
3006 }
3007 return false;
3008 }
3009
Sunny Goyal9ca9c132015-04-29 14:57:22 -07003010 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
3011 int x2 = x + spanX - 1;
3012 int y2 = y + spanY - 1;
3013 if (x < 0 || y < 0 || x2 >= mCountX || y2 >= mCountY) {
3014 return false;
3015 }
3016 for (int i = x; i <= x2; i++) {
3017 for (int j = y; j <= y2; j++) {
3018 if (mOccupied[i][j]) {
3019 return false;
3020 }
3021 }
3022 }
3023
3024 return true;
3025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003026}