blob: 5ae7310ffc73a9bb5d3ee2d5c930c21a28918fb1 [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;
Brandon Keely50e6e562012-05-08 16:28:49 -070021import android.animation.AnimatorSet;
Chet Haase00397b12010-10-07 11:13:10 -070022import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070023import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
Adam Cohenc9735cf2015-01-23 16:11:55 -080025import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040027import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070029import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080031import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070033import android.graphics.Point;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080035import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070036import android.graphics.drawable.Drawable;
Sunny Goyal2805e632015-05-20 15:35:32 -070037import android.graphics.drawable.TransitionDrawable;
Adam Cohenc9735cf2015-01-23 16:11:55 -080038import android.os.Build;
Adam Cohen1462de32012-07-24 22:34:36 -070039import android.os.Parcelable;
Tony Wickham489fc562015-09-02 14:45:39 -070040import android.os.PowerManager;
Adam Cohenc9735cf2015-01-23 16:11:55 -080041import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070043import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070044import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.view.MotionEvent;
46import android.view.View;
47import android.view.ViewDebug;
48import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080049import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070050import android.view.animation.DecelerateInterpolator;
Tony Wickham489fc562015-09-02 14:45:39 -070051import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052
Sunny Goyal4b6eb262015-05-14 19:24:40 -070053import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Daniel Sandler325dc232013-06-05 22:57:57 -040054import com.android.launcher3.FolderIcon.FolderRingAnimator;
Sunny Goyale9b651e2015-04-24 11:44:51 -070055import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
56import com.android.launcher3.accessibility.FolderAccessibilityHelper;
57import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
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
Winson Chungaafa03c2010-06-11 17:34:16 -070071 static final String TAG = "CellLayout";
72
Adam Cohen2acce882012-03-28 19:03:19 -070073 private Launcher mLauncher;
Adam Cohen091440a2015-03-18 14:16:05 -070074 @Thunk int mCellWidth;
75 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070076 private int mFixedCellWidth;
77 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070078
Adam Cohen091440a2015-03-18 14:16:05 -070079 @Thunk int mCountX;
80 @Thunk int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081
Adam Cohen234c4cd2011-07-17 21:03:04 -070082 private int mOriginalWidthGap;
83 private int mOriginalHeightGap;
Adam Cohen091440a2015-03-18 14:16:05 -070084 @Thunk int mWidthGap;
85 @Thunk int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070086 private int mMaxGap;
Adam Cohen917e3882013-10-31 15:03:35 -070087 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070088 private boolean mIsDragTarget = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Patrick Dubroyde7658b2010-09-27 11:15:43 -070090 // These are temporary variables to prevent having to allocate a new object just to
91 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070092 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070093 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070094
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080096 boolean[][] mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
Michael Jurkadee05892010-07-27 10:01:56 -070098 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -070099 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700100
Adam Cohen69ce2e52011-07-03 19:25:21 -0700101 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -0700102 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -0700103
Michael Jurka5f1c5092010-09-03 14:15:02 -0700104 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700105
Sunny Goyal2805e632015-05-20 15:35:32 -0700106 private static final int BACKGROUND_ACTIVATE_DURATION = 120;
107 private final TransitionDrawable mBackground;
108
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700109 // These values allow a fixed measurement to be set on the CellLayout.
110 private int mFixedWidth = -1;
111 private int mFixedHeight = -1;
112
Michael Jurka33945b22010-12-21 18:19:38 -0800113 // If we're actively dragging something over this screen, mIsDragOverlapping is true
114 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700115
Winson Chung150fbab2010-09-29 17:14:26 -0700116 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700117 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk Rect[] mDragOutlines = new Rect[4];
119 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700120 private InterruptibleInOutAnimator[] mDragOutlineAnims =
121 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700122
123 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700124 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700125 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700126
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700127 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800128
Sunny Goyal316490e2015-06-02 09:38:28 -0700129 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
130 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700131
132 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700133
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700134 // When a drag operation is in progress, holds the nearest cell to the touch point
135 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Joe Onorato4be866d2010-10-10 11:26:02 -0700137 private boolean mDragging = false;
138
Patrick Dubroyce34a972010-10-19 10:34:32 -0700139 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700140 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700141
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800142 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700143 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800144
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800145 public static final int MODE_SHOW_REORDER_HINT = 0;
146 public static final int MODE_DRAG_OVER = 1;
147 public static final int MODE_ON_DROP = 2;
148 public static final int MODE_ON_DROP_EXTERNAL = 3;
149 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700150 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800151 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
152
Adam Cohena897f392012-04-27 18:12:05 -0700153 static final int LANDSCAPE = 0;
154 static final int PORTRAIT = 1;
155
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800156 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700157 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohen091440a2015-03-18 14:16:05 -0700158 @Thunk float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700159
Adam Cohen482ed822012-03-02 14:15:13 -0800160 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
161 private Rect mOccupiedRect = new Rect();
162 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700163 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700164 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800165
Sunny Goyal2805e632015-05-20 15:35:32 -0700166 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700167
Michael Jurkaca993832012-06-29 15:17:04 -0700168 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700169
Adam Cohenc9735cf2015-01-23 16:11:55 -0800170 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700171 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800172 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 public CellLayout(Context context) {
175 this(context, null);
176 }
177
178 public CellLayout(Context context, AttributeSet attrs) {
179 this(context, attrs, 0);
180 }
181
182 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
183 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700184
185 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
186 // the user where a dragged item will land when dropped.
187 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800188 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700189 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700190
Adam Cohen2e6da152015-05-06 11:42:25 -0700191 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
193
Winson Chung11a1a532013-09-13 11:14:45 -0700194 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800195 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700196 mWidthGap = mOriginalWidthGap = 0;
197 mHeightGap = mOriginalHeightGap = 0;
198 mMaxGap = Integer.MAX_VALUE;
Adam Cohen2e6da152015-05-06 11:42:25 -0700199 mCountX = (int) grid.inv.numColumns;
200 mCountY = (int) grid.inv.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700201 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800202 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700203 mPreviousReorderDirection[0] = INVALID_DIRECTION;
204 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
206 a.recycle();
207
208 setAlwaysDrawnWithCacheEnabled(false);
209
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700210 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700211 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700212
Sunny Goyal2805e632015-05-20 15:35:32 -0700213 mBackground = (TransitionDrawable) res.getDrawable(R.drawable.bg_screenpanel);
214 mBackground.setCallback(this);
Winson Chunge8f1d042015-07-31 12:39:57 -0700215 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurka33945b22010-12-21 18:19:38 -0800216
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800217 mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE *
Winson Chung5f8afe62013-08-12 16:19:28 -0700218 grid.iconSizePx);
Adam Cohen19f37922012-03-21 11:59:11 -0700219
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700220 // Initialize the data structures used for the drag visualization.
Patrick Dubroyce34a972010-10-19 10:34:32 -0700221 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Winson Chungb8c69f32011-10-19 21:36:08 -0700222 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700223 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800224 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700225 }
226
227 // When dragging things around the home screens, we show a green outline of
228 // where the item will land. The outlines gradually fade out, leaving a trail
229 // behind the drag path.
230 // Set up all the animations that are used to implement this fading.
231 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700232 final float fromAlphaValue = 0;
233 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700234
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700235 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700236
237 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700238 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100239 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700240 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700241 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700242 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700243 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700244 final Bitmap outline = (Bitmap)anim.getTag();
245
246 // If an animation is started and then stopped very quickly, we can still
247 // get spurious updates we've cleared the tag. Guard against this.
248 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700249 @SuppressWarnings("all") // suppress dead code warning
250 final boolean debug = false;
251 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700252 Object val = animation.getAnimatedValue();
253 Log.d(TAG, "anim " + thisIndex + " update: " + val +
254 ", isStopped " + anim.isStopped());
255 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700256 // Try to prevent it from continuing to run
257 animation.cancel();
258 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700259 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800260 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700261 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700262 }
263 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700264 // The animation holds a reference to the drag outline bitmap as long is it's
265 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700266 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700267 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700269 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700270 anim.setTag(null);
271 }
272 }
273 });
274 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700275 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700276
Michael Jurkaa52570f2012-03-20 03:18:20 -0700277 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700278 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700279 mCountX, mCountY);
Adam Cohen2374abf2013-04-16 14:56:57 -0700280
Mady Melloref044dd2015-06-02 15:35:07 -0700281 mStylusEventHelper = new StylusEventHelper(this);
282
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700283 mTouchFeedbackView = new ClickShadowView(context);
284 addView(mTouchFeedbackView);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700285 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700286 }
287
Adam Cohenc9735cf2015-01-23 16:11:55 -0800288 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyale9b651e2015-04-24 11:44:51 -0700289 public void enableAccessibleDrag(boolean enable, int dragType) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800290 mUseTouchHelper = enable;
291 if (!enable) {
292 ViewCompat.setAccessibilityDelegate(this, null);
293 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
294 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
295 setOnClickListener(mLauncher);
296 } else {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700297 if (dragType == WORKSPACE_ACCESSIBILITY_DRAG &&
298 !(mTouchHelper instanceof WorkspaceAccessibilityHelper)) {
299 mTouchHelper = new WorkspaceAccessibilityHelper(this);
300 } else if (dragType == FOLDER_ACCESSIBILITY_DRAG &&
301 !(mTouchHelper instanceof FolderAccessibilityHelper)) {
302 mTouchHelper = new FolderAccessibilityHelper(this);
303 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800304 ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
305 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
306 getShortcutsAndWidgets().setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
307 setOnClickListener(mTouchHelper);
308 }
309
310 // Invalidate the accessibility hierarchy
311 if (getParent() != null) {
312 getParent().notifySubtreeAccessibilityStateChanged(
313 this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
314 }
315 }
316
317 @Override
318 public boolean dispatchHoverEvent(MotionEvent event) {
319 // Always attempt to dispatch hover events to accessibility first.
320 if (mUseTouchHelper && mTouchHelper.dispatchHoverEvent(event)) {
321 return true;
322 }
323 return super.dispatchHoverEvent(event);
324 }
325
326 @Override
Adam Cohenc9735cf2015-01-23 16:11:55 -0800327 public boolean onInterceptTouchEvent(MotionEvent ev) {
328 if (mUseTouchHelper ||
329 (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) {
330 return true;
331 }
332 return false;
333 }
334
Mady Melloref044dd2015-06-02 15:35:07 -0700335 @Override
336 public boolean onTouchEvent(MotionEvent ev) {
337 boolean handled = super.onTouchEvent(ev);
338 // Stylus button press on a home screen should not switch between overview mode and
339 // the home screen mode, however, once in overview mode stylus button press should be
340 // enabled to allow rearranging the different home screens. So check what mode
341 // the workspace is in, and only perform stylus button presses while in overview mode.
342 if (mLauncher.mWorkspace.isInOverviewMode()
343 && mStylusEventHelper.checkAndPerformStylusEvent(ev)) {
344 return true;
345 }
346 return handled;
347 }
348
Chris Craik01f2d7f2013-10-01 14:41:56 -0700349 public void enableHardwareLayer(boolean hasLayer) {
350 mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700351 }
352
353 public void buildHardwareLayer() {
354 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700355 }
356
Adam Cohen307fe232012-08-16 17:55:58 -0700357 public float getChildrenScale() {
358 return mIsHotseat ? mHotseatScale : 1.0f;
359 }
360
Winson Chung5f8afe62013-08-12 16:19:28 -0700361 public void setCellDimensions(int width, int height) {
Winson Chung11a1a532013-09-13 11:14:45 -0700362 mFixedCellWidth = mCellWidth = width;
363 mFixedCellHeight = mCellHeight = height;
Winson Chung5f8afe62013-08-12 16:19:28 -0700364 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
365 mCountX, mCountY);
366 }
367
Adam Cohen2801caf2011-05-13 20:57:39 -0700368 public void setGridSize(int x, int y) {
369 mCountX = x;
370 mCountY = y;
371 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800372 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700373 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700374 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -0700375 mCountX, mCountY);
Adam Cohen76fc0852011-06-17 13:26:23 -0700376 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700377 }
378
Adam Cohen2374abf2013-04-16 14:56:57 -0700379 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
380 public void setInvertIfRtl(boolean invert) {
381 mShortcutsAndWidgets.setInvertIfRtl(invert);
382 }
383
Adam Cohen917e3882013-10-31 15:03:35 -0700384 public void setDropPending(boolean pending) {
385 mDropPending = pending;
386 }
387
388 public boolean isDropPending() {
389 return mDropPending;
390 }
391
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700392 @Override
393 public void setPressedIcon(BubbleTextView icon, Bitmap background) {
Sunny Goyal508da152014-08-14 10:53:27 -0700394 if (icon == null || background == null) {
395 mTouchFeedbackView.setBitmap(null);
396 mTouchFeedbackView.animate().cancel();
397 } else {
Sunny Goyal508da152014-08-14 10:53:27 -0700398 if (mTouchFeedbackView.setBitmap(background)) {
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700399 mTouchFeedbackView.alignWithIconView(icon, mShortcutsAndWidgets);
400 mTouchFeedbackView.animateShadow();
Sunny Goyal508da152014-08-14 10:53:27 -0700401 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800402 }
403 }
404
Adam Cohenc50438c2014-08-19 17:43:05 -0700405 void disableDragTarget() {
406 mIsDragTarget = false;
407 }
408
409 boolean isDragTarget() {
410 return mIsDragTarget;
411 }
412
413 void setIsDragOverlapping(boolean isDragOverlapping) {
414 if (mIsDragOverlapping != isDragOverlapping) {
415 mIsDragOverlapping = isDragOverlapping;
Sunny Goyal2805e632015-05-20 15:35:32 -0700416 if (mIsDragOverlapping) {
417 mBackground.startTransition(BACKGROUND_ACTIVATE_DURATION);
418 } else {
Winson Chunge8f1d042015-07-31 12:39:57 -0700419 if (mBackgroundAlpha > 0f) {
420 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
421 } else {
422 mBackground.resetTransition();
423 }
Sunny Goyal2805e632015-05-20 15:35:32 -0700424 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700425 invalidate();
426 }
427 }
428
Michael Jurka33945b22010-12-21 18:19:38 -0800429 boolean getIsDragOverlapping() {
430 return mIsDragOverlapping;
431 }
432
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700433 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700434 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700435 if (!mIsDragTarget) {
436 return;
437 }
438
Michael Jurka3e7c7632010-10-02 16:01:03 -0700439 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
440 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
441 // When we're small, we are either drawn normally or in the "accepts drops" state (during
442 // a drag). However, we also drag the mini hover background *over* one of those two
443 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700444 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700445 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700446 }
Romain Guya6abce82009-11-10 02:54:41 -0800447
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700448 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700449 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700450 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700451 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800452 final Rect r = mDragOutlines[i];
Winson Chung3a6e7f32013-10-09 15:50:52 -0700453 mTempRect.set(r);
454 Utilities.scaleRectAboutCenter(mTempRect, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700455 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700456 paint.setAlpha((int)(alpha + .5f));
Winson Chung3a6e7f32013-10-09 15:50:52 -0700457 canvas.drawBitmap(b, null, mTempRect, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700458 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700459 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800460
Adam Cohen482ed822012-03-02 14:15:13 -0800461 if (DEBUG_VISUALIZE_OCCUPIED) {
462 int[] pt = new int[2];
463 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700464 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800465 for (int i = 0; i < mCountX; i++) {
466 for (int j = 0; j < mCountY; j++) {
467 if (mOccupied[i][j]) {
468 cellToPoint(i, j, pt);
469 canvas.save();
470 canvas.translate(pt[0], pt[1]);
471 cd.draw(canvas);
472 canvas.restore();
473 }
474 }
475 }
476 }
477
Andrew Flynn850d2e72012-04-26 16:51:20 -0700478 int previewOffset = FolderRingAnimator.sPreviewSize;
479
Adam Cohen69ce2e52011-07-03 19:25:21 -0700480 // The folder outer / inner ring image(s)
Adam Cohen2e6da152015-05-06 11:42:25 -0700481 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700482 for (int i = 0; i < mFolderOuterRings.size(); i++) {
483 FolderRingAnimator fra = mFolderOuterRings.get(i);
484
Adam Cohen5108bc02013-09-20 17:04:51 -0700485 Drawable d;
486 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700487 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700488 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700489
Winson Chung89f97052013-09-20 11:32:26 -0700490 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700491 int centerX = mTempLocation[0] + mCellWidth / 2;
492 int centerY = mTempLocation[1] + previewOffset / 2 +
493 child.getPaddingTop() + grid.folderBackgroundOffset;
494
Adam Cohen5108bc02013-09-20 17:04:51 -0700495 // Draw outer ring, if it exists
496 if (FolderIcon.HAS_OUTER_RING) {
497 d = FolderRingAnimator.sSharedOuterRingDrawable;
498 width = (int) (fra.getOuterRingSize() * getChildrenScale());
499 height = width;
500 canvas.save();
501 canvas.translate(centerX - width / 2, centerY - height / 2);
502 d.setBounds(0, 0, width, height);
503 d.draw(canvas);
504 canvas.restore();
505 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700506
Winson Chung89f97052013-09-20 11:32:26 -0700507 // Draw inner ring
508 d = FolderRingAnimator.sSharedInnerRingDrawable;
509 width = (int) (fra.getInnerRingSize() * getChildrenScale());
510 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700511 canvas.save();
512 canvas.translate(centerX - width / 2, centerY - width / 2);
513 d.setBounds(0, 0, width, height);
514 d.draw(canvas);
515 canvas.restore();
516 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700517 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700518
519 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
520 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
521 int width = d.getIntrinsicWidth();
522 int height = d.getIntrinsicHeight();
523
524 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700525 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700526 if (child != null) {
527 int centerX = mTempLocation[0] + mCellWidth / 2;
528 int centerY = mTempLocation[1] + previewOffset / 2 +
529 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700530
Winson Chung89f97052013-09-20 11:32:26 -0700531 canvas.save();
532 canvas.translate(centerX - width / 2, centerY - width / 2);
533 d.setBounds(0, 0, width, height);
534 d.draw(canvas);
535 canvas.restore();
536 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700537 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700538 }
539
540 public void showFolderAccept(FolderRingAnimator fra) {
541 mFolderOuterRings.add(fra);
542 }
543
544 public void hideFolderAccept(FolderRingAnimator fra) {
545 if (mFolderOuterRings.contains(fra)) {
546 mFolderOuterRings.remove(fra);
547 }
548 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700549 }
550
Adam Cohenc51934b2011-07-26 21:07:43 -0700551 public void setFolderLeaveBehindCell(int x, int y) {
552 mFolderLeaveBehindCell[0] = x;
553 mFolderLeaveBehindCell[1] = y;
554 invalidate();
555 }
556
557 public void clearFolderLeaveBehind() {
558 mFolderLeaveBehindCell[0] = -1;
559 mFolderLeaveBehindCell[1] = -1;
560 invalidate();
561 }
562
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700563 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700564 public boolean shouldDelayChildPressedState() {
565 return false;
566 }
567
Adam Cohen1462de32012-07-24 22:34:36 -0700568 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700569 try {
570 dispatchRestoreInstanceState(states);
571 } catch (IllegalArgumentException ex) {
572 if (LauncherAppState.isDogfoodBuild()) {
573 throw ex;
574 }
575 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
576 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
577 }
Adam Cohen1462de32012-07-24 22:34:36 -0700578 }
579
Michael Jurkae6235dd2011-10-04 15:02:05 -0700580 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700581 public void cancelLongPress() {
582 super.cancelLongPress();
583
584 // Cancel long press for all children
585 final int count = getChildCount();
586 for (int i = 0; i < count; i++) {
587 final View child = getChildAt(i);
588 child.cancelLongPress();
589 }
590 }
591
Michael Jurkadee05892010-07-27 10:01:56 -0700592 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
593 mInterceptTouchListener = listener;
594 }
595
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800596 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700597 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
599
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800600 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700601 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 }
603
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800604 public void setIsHotseat(boolean isHotseat) {
605 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700606 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800607 }
608
Sunny Goyale9b651e2015-04-24 11:44:51 -0700609 public boolean isHotseat() {
610 return mIsHotseat;
611 }
612
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800613 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700614 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700615 final LayoutParams lp = params;
616
Andrew Flynnde38e422012-05-08 11:22:15 -0700617 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800618 if (child instanceof BubbleTextView) {
619 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700620 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800621 }
622
Adam Cohen307fe232012-08-16 17:55:58 -0700623 child.setScaleX(getChildrenScale());
624 child.setScaleY(getChildrenScale());
625
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 // Generate an id for each view, this assumes we have at most 256x256 cells
627 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700628 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700629 // If the horizontal or vertical span is set to -1, it is taken to
630 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700631 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
632 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633
Winson Chungaafa03c2010-06-11 17:34:16 -0700634 child.setId(childId);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700635 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700636
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700637 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700638
Winson Chungaafa03c2010-06-11 17:34:16 -0700639 return true;
640 }
641 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700645 public void removeAllViews() {
646 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700647 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700648 }
649
650 @Override
651 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700652 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700653 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700654 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700655 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656 }
657
658 @Override
659 public void removeView(View view) {
660 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700661 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 }
663
664 @Override
665 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700666 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
667 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 }
669
670 @Override
671 public void removeViewInLayout(View view) {
672 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700673 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674 }
675
676 @Override
677 public void removeViews(int start, int count) {
678 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700679 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700681 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700682 }
683
684 @Override
685 public void removeViewsInLayout(int start, int count) {
686 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700687 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700688 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700689 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800690 }
691
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700692 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700693 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 * @param x X coordinate of the point
695 * @param y Y coordinate of the point
696 * @param result Array of 2 ints to hold the x and y coordinate of the cell
697 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700698 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700699 final int hStartPadding = getPaddingLeft();
700 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701
702 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
703 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
704
Adam Cohend22015c2010-07-26 22:02:18 -0700705 final int xAxis = mCountX;
706 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707
708 if (result[0] < 0) result[0] = 0;
709 if (result[0] >= xAxis) result[0] = xAxis - 1;
710 if (result[1] < 0) result[1] = 0;
711 if (result[1] >= yAxis) result[1] = yAxis - 1;
712 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 /**
715 * Given a point, return the cell that most closely encloses that point
716 * @param x X coordinate of the point
717 * @param y Y coordinate of the point
718 * @param result Array of 2 ints to hold the x and y coordinate of the cell
719 */
720 void pointToCellRounded(int x, int y, int[] result) {
721 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
722 }
723
724 /**
725 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700726 *
727 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700729 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 * @param result Array of 2 ints to hold the x and y coordinate of the point
731 */
732 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700733 final int hStartPadding = getPaddingLeft();
734 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735
736 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
737 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
738 }
739
Adam Cohene3e27a82011-04-15 12:07:39 -0700740 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800741 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700742 *
743 * @param cellX X coordinate of the cell
744 * @param cellY Y coordinate of the cell
745 *
746 * @param result Array of 2 ints to hold the x and y coordinate of the point
747 */
748 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700749 regionToCenterPoint(cellX, cellY, 1, 1, result);
750 }
751
752 /**
753 * Given a cell coordinate and span return the point that represents the center of the regio
754 *
755 * @param cellX X coordinate of the cell
756 * @param cellY Y coordinate of the cell
757 *
758 * @param result Array of 2 ints to hold the x and y coordinate of the point
759 */
760 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700761 final int hStartPadding = getPaddingLeft();
762 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700763 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
764 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
765 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
766 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700767 }
768
Adam Cohen19f37922012-03-21 11:59:11 -0700769 /**
770 * Given a cell coordinate and span fills out a corresponding pixel rect
771 *
772 * @param cellX X coordinate of the cell
773 * @param cellY Y coordinate of the cell
774 * @param result Rect in which to write the result
775 */
776 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
777 final int hStartPadding = getPaddingLeft();
778 final int vStartPadding = getPaddingTop();
779 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
780 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
781 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
782 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
783 }
784
Adam Cohen482ed822012-03-02 14:15:13 -0800785 public float getDistanceFromCell(float x, float y, int[] cell) {
786 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700787 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800788 }
789
Romain Guy84f296c2009-11-04 15:00:44 -0800790 int getCellWidth() {
791 return mCellWidth;
792 }
793
794 int getCellHeight() {
795 return mCellHeight;
796 }
797
Adam Cohend4844c32011-02-18 19:25:06 -0800798 int getWidthGap() {
799 return mWidthGap;
800 }
801
802 int getHeightGap() {
803 return mHeightGap;
804 }
805
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700806 public void setFixedSize(int width, int height) {
807 mFixedWidth = width;
808 mFixedHeight = height;
809 }
810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 @Override
812 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700815 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
816 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700817 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
818 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700819 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700820 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
821 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700822 if (cw != mCellWidth || ch != mCellHeight) {
823 mCellWidth = cw;
824 mCellHeight = ch;
825 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
826 mHeightGap, mCountX, mCountY);
827 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700828 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700829
Winson Chung2d75f122013-09-23 16:53:31 -0700830 int newWidth = childWidthSize;
831 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700832 if (mFixedWidth > 0 && mFixedHeight > 0) {
833 newWidth = mFixedWidth;
834 newHeight = mFixedHeight;
835 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
837 }
838
Adam Cohend22015c2010-07-26 22:02:18 -0700839 int numWidthGaps = mCountX - 1;
840 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841
Adam Cohen234c4cd2011-07-17 21:03:04 -0700842 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700843 int hSpace = childWidthSize;
844 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700845 int hFreeSpace = hSpace - (mCountX * mCellWidth);
846 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700847 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
848 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700849 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
850 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700851 } else {
852 mWidthGap = mOriginalWidthGap;
853 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700854 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700855
856 // Make the feedback view large enough to hold the blur bitmap.
857 mTouchFeedbackView.measure(
858 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
859 MeasureSpec.EXACTLY),
860 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
861 MeasureSpec.EXACTLY));
862
863 mShortcutsAndWidgets.measure(
864 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
865 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
866
867 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
868 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700869 if (mFixedWidth > 0 && mFixedHeight > 0) {
870 setMeasuredDimension(maxWidth, maxHeight);
871 } else {
872 setMeasuredDimension(widthSize, heightSize);
873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 }
875
876 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700877 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -0700878 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
879 (mCountX * mCellWidth);
880 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
881 int top = getPaddingTop();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700882
883 mTouchFeedbackView.layout(left, top,
884 left + mTouchFeedbackView.getMeasuredWidth(),
885 top + mTouchFeedbackView.getMeasuredHeight());
886 mShortcutsAndWidgets.layout(left, top,
887 left + r - l,
888 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 }
890
891 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700892 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
893 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -0700894
895 // Expand the background drawing bounds by the padding baked into the background drawable
Sunny Goyal2805e632015-05-20 15:35:32 -0700896 mBackground.getPadding(mTempRect);
897 mBackground.setBounds(-mTempRect.left, -mTempRect.top,
898 w + mTempRect.right, h + mTempRect.bottom);
Michael Jurkadee05892010-07-27 10:01:56 -0700899 }
900
901 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700903 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905
906 @Override
907 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700908 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 }
910
Michael Jurka5f1c5092010-09-03 14:15:02 -0700911 public float getBackgroundAlpha() {
912 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700913 }
914
Michael Jurka5f1c5092010-09-03 14:15:02 -0700915 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800916 if (mBackgroundAlpha != alpha) {
917 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700918 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800919 }
Michael Jurkadee05892010-07-27 10:01:56 -0700920 }
921
Sunny Goyal2805e632015-05-20 15:35:32 -0700922 @Override
923 protected boolean verifyDrawable(Drawable who) {
924 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
925 }
926
Michael Jurkaa52570f2012-03-20 03:18:20 -0700927 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700928 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700929 }
930
Michael Jurkaa52570f2012-03-20 03:18:20 -0700931 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700932 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700933 }
934
Patrick Dubroy440c3602010-07-13 17:50:32 -0700935 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700936 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700937 }
938
Adam Cohen76fc0852011-06-17 13:26:23 -0700939 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800940 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700941 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800942 boolean[][] occupied = mOccupied;
943 if (!permanent) {
944 occupied = mTmpOccupied;
945 }
946
Adam Cohen19f37922012-03-21 11:59:11 -0700947 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700948 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
949 final ItemInfo info = (ItemInfo) child.getTag();
950
951 // We cancel any existing animations
952 if (mReorderAnimators.containsKey(lp)) {
953 mReorderAnimators.get(lp).cancel();
954 mReorderAnimators.remove(lp);
955 }
956
Adam Cohen482ed822012-03-02 14:15:13 -0800957 final int oldX = lp.x;
958 final int oldY = lp.y;
959 if (adjustOccupied) {
960 occupied[lp.cellX][lp.cellY] = false;
961 occupied[cellX][cellY] = true;
962 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700963 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800964 if (permanent) {
965 lp.cellX = info.cellX = cellX;
966 lp.cellY = info.cellY = cellY;
967 } else {
968 lp.tmpCellX = cellX;
969 lp.tmpCellY = cellY;
970 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700971 clc.setupLp(lp);
972 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800973 final int newX = lp.x;
974 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700975
Adam Cohen76fc0852011-06-17 13:26:23 -0700976 lp.x = oldX;
977 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700978
Adam Cohen482ed822012-03-02 14:15:13 -0800979 // Exit early if we're not actually moving the view
980 if (oldX == newX && oldY == newY) {
981 lp.isLockedToGrid = true;
982 return true;
983 }
984
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100985 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800986 va.setDuration(duration);
987 mReorderAnimators.put(lp, va);
988
989 va.addUpdateListener(new AnimatorUpdateListener() {
990 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700991 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -0800992 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700993 lp.x = (int) ((1 - r) * oldX + r * newX);
994 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700995 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700996 }
997 });
Adam Cohen482ed822012-03-02 14:15:13 -0800998 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700999 boolean cancelled = false;
1000 public void onAnimationEnd(Animator animation) {
1001 // If the animation was cancelled, it means that another animation
1002 // has interrupted this one, and we don't want to lock the item into
1003 // place just yet.
1004 if (!cancelled) {
1005 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001006 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001007 }
1008 if (mReorderAnimators.containsKey(lp)) {
1009 mReorderAnimators.remove(lp);
1010 }
1011 }
1012 public void onAnimationCancel(Animator animation) {
1013 cancelled = true;
1014 }
1015 });
Adam Cohen482ed822012-03-02 14:15:13 -08001016 va.setStartDelay(delay);
1017 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001018 return true;
1019 }
1020 return false;
1021 }
1022
Adam Cohen482ed822012-03-02 14:15:13 -08001023 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
Sunny Goyale78e3d72015-09-24 11:23:31 -07001024 int cellY, int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001025 final int oldDragCellX = mDragCell[0];
1026 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001027
Adam Cohen2801caf2011-05-13 20:57:39 -07001028 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001029 return;
1030 }
1031
Adam Cohen482ed822012-03-02 14:15:13 -08001032 if (cellX != oldDragCellX || cellY != oldDragCellY) {
Sunny Goyale78e3d72015-09-24 11:23:31 -07001033 Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
1034 Rect dragRegion = dragObject.dragView.getDragRegion();
1035
Adam Cohen482ed822012-03-02 14:15:13 -08001036 mDragCell[0] = cellX;
1037 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001038 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001039 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001040 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001041
Joe Onorato4be866d2010-10-10 11:26:02 -07001042 int left = topLeft[0];
1043 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001044
Winson Chungb8c69f32011-10-19 21:36:08 -07001045 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001046 // When drawing the drag outline, it did not account for margin offsets
1047 // added by the view's parent.
1048 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1049 left += lp.leftMargin;
1050 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001051
Adam Cohen99e8b402011-03-25 19:23:43 -07001052 // Offsets due to the size difference between the View and the dragOutline.
1053 // There is a size difference to account for the outer blur, which may lie
1054 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001055 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001056 // We center about the x axis
1057 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1058 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001059 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001060 if (dragOffset != null && dragRegion != null) {
1061 // Center the drag region *horizontally* in the cell and apply a drag
1062 // outline offset
1063 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1064 - dragRegion.width()) / 2;
Winson Chung69737c32013-10-08 17:00:19 -07001065 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1066 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1067 top += dragOffset.y + cellPaddingY;
Winson Chungb8c69f32011-10-19 21:36:08 -07001068 } else {
1069 // Center the drag outline in the cell
1070 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1071 - dragOutline.getWidth()) / 2;
1072 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1073 - dragOutline.getHeight()) / 2;
1074 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001075 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001076 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001077 mDragOutlineAnims[oldIndex].animateOut();
1078 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001079 Rect r = mDragOutlines[mDragOutlineCurrent];
1080 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1081 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001082 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001083 }
Winson Chung150fbab2010-09-29 17:14:26 -07001084
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001085 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1086 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Sunny Goyale78e3d72015-09-24 11:23:31 -07001087
1088 if (dragObject.stateAnnouncer != null) {
1089 String msg;
1090 if (isHotseat()) {
1091 msg = getContext().getString(R.string.move_to_hotseat_position,
1092 Math.max(cellX, cellY) + 1);
1093 } else {
1094 msg = getContext().getString(R.string.move_to_empty_cell,
1095 cellY + 1, cellX + 1);
1096 }
1097 dragObject.stateAnnouncer.announce(msg);
1098 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001099 }
1100 }
1101
Adam Cohene0310962011-04-18 16:15:31 -07001102 public void clearDragOutlines() {
1103 final int oldIndex = mDragOutlineCurrent;
1104 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001105 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001106 }
1107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001109 * Find a vacant area that will fit the given bounds nearest the requested
1110 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001111 *
Romain Guy51afc022009-05-04 18:03:43 -07001112 * @param pixelX The X location at which you want to search for a vacant area.
1113 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001114 * @param spanX Horizontal span of the object.
1115 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001116 * @param result Array in which to place the result, or null (in which case a new array will
1117 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001118 * @return The X, Y cell of a vacant area that can contain this object,
1119 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001121 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1122 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, spanX, spanY, result, null);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001123 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001124
Michael Jurka6a1435d2010-09-27 17:35:12 -07001125 /**
1126 * Find a vacant area that will fit the given bounds nearest the requested
1127 * cell location. Uses Euclidean distance to score multiple vacant areas.
1128 *
1129 * @param pixelX The X location at which you want to search for a vacant area.
1130 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001131 * @param minSpanX The minimum horizontal span required
1132 * @param minSpanY The minimum vertical span required
1133 * @param spanX Horizontal span of the object.
1134 * @param spanY Vertical span of the object.
1135 * @param result Array in which to place the result, or null (in which case a new array will
1136 * be allocated)
1137 * @return The X, Y cell of a vacant area that can contain this object,
1138 * nearest the requested location.
1139 */
1140 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1141 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001142 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001143 result, resultSpan);
1144 }
1145
Adam Cohend41fbf52012-02-16 23:53:59 -08001146 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1147 private void lazyInitTempRectStack() {
1148 if (mTempRectStack.isEmpty()) {
1149 for (int i = 0; i < mCountX * mCountY; i++) {
1150 mTempRectStack.push(new Rect());
1151 }
1152 }
1153 }
Adam Cohen482ed822012-03-02 14:15:13 -08001154
Adam Cohend41fbf52012-02-16 23:53:59 -08001155 private void recycleTempRects(Stack<Rect> used) {
1156 while (!used.isEmpty()) {
1157 mTempRectStack.push(used.pop());
1158 }
1159 }
1160
1161 /**
1162 * Find a vacant area that will fit the given bounds nearest the requested
1163 * cell location. Uses Euclidean distance to score multiple vacant areas.
1164 *
1165 * @param pixelX The X location at which you want to search for a vacant area.
1166 * @param pixelY The Y location at which you want to search for a vacant area.
1167 * @param minSpanX The minimum horizontal span required
1168 * @param minSpanY The minimum vertical span required
1169 * @param spanX Horizontal span of the object.
1170 * @param spanY Vertical span of the object.
1171 * @param ignoreOccupied If true, the result can be an occupied cell
1172 * @param result Array in which to place the result, or null (in which case a new array will
1173 * be allocated)
1174 * @return The X, Y cell of a vacant area that can contain this object,
1175 * nearest the requested location.
1176 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001177 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1178 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001179 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001180
Adam Cohene3e27a82011-04-15 12:07:39 -07001181 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1182 // to the center of the item, but we are searching based on the top-left cell, so
1183 // we translate the point over to correspond to the top-left.
1184 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1185 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1186
Jeff Sharkey70864282009-04-07 21:08:40 -07001187 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001188 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001189 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001190 final Rect bestRect = new Rect(-1, -1, -1, -1);
1191 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001192
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001193 final int countX = mCountX;
1194 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001195
Adam Cohend41fbf52012-02-16 23:53:59 -08001196 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1197 spanX < minSpanX || spanY < minSpanY) {
1198 return bestXY;
1199 }
1200
1201 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001202 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001203 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1204 int ySize = -1;
1205 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001206 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001207 // First, let's see if this thing fits anywhere
1208 for (int i = 0; i < minSpanX; i++) {
1209 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001210 if (mOccupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001211 continue inner;
1212 }
Michael Jurkac28de512010-08-13 11:27:44 -07001213 }
1214 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001215 xSize = minSpanX;
1216 ySize = minSpanY;
1217
1218 // We know that the item will fit at _some_ acceptable size, now let's see
1219 // how big we can make it. We'll alternate between incrementing x and y spans
1220 // until we hit a limit.
1221 boolean incX = true;
1222 boolean hitMaxX = xSize >= spanX;
1223 boolean hitMaxY = ySize >= spanY;
1224 while (!(hitMaxX && hitMaxY)) {
1225 if (incX && !hitMaxX) {
1226 for (int j = 0; j < ySize; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001227 if (x + xSize > countX -1 || mOccupied[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001228 // We can't move out horizontally
1229 hitMaxX = true;
1230 }
1231 }
1232 if (!hitMaxX) {
1233 xSize++;
1234 }
1235 } else if (!hitMaxY) {
1236 for (int i = 0; i < xSize; i++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001237 if (y + ySize > countY - 1 || mOccupied[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001238 // We can't move out vertically
1239 hitMaxY = true;
1240 }
1241 }
1242 if (!hitMaxY) {
1243 ySize++;
1244 }
1245 }
1246 hitMaxX |= xSize >= spanX;
1247 hitMaxY |= ySize >= spanY;
1248 incX = !incX;
1249 }
1250 incX = true;
1251 hitMaxX = xSize >= spanX;
1252 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001253 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001254 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001255 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256
Adam Cohend41fbf52012-02-16 23:53:59 -08001257 // We verify that the current rect is not a sub-rect of any of our previous
1258 // candidates. In this case, the current rect is disqualified in favour of the
1259 // containing rect.
1260 Rect currentRect = mTempRectStack.pop();
1261 currentRect.set(x, y, x + xSize, y + ySize);
1262 boolean contained = false;
1263 for (Rect r : validRegions) {
1264 if (r.contains(currentRect)) {
1265 contained = true;
1266 break;
1267 }
1268 }
1269 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001270 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001271
Adam Cohend41fbf52012-02-16 23:53:59 -08001272 if ((distance <= bestDistance && !contained) ||
1273 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001274 bestDistance = distance;
1275 bestXY[0] = x;
1276 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001277 if (resultSpan != null) {
1278 resultSpan[0] = xSize;
1279 resultSpan[1] = ySize;
1280 }
1281 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001282 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
1284 }
1285
Adam Cohenc0dcf592011-06-01 15:30:43 -07001286 // Return -1, -1 if no suitable location found
1287 if (bestDistance == Double.MAX_VALUE) {
1288 bestXY[0] = -1;
1289 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001290 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001291 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001292 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001294
Adam Cohen482ed822012-03-02 14:15:13 -08001295 /**
1296 * Find a vacant area that will fit the given bounds nearest the requested
1297 * cell location, and will also weigh in a suggested direction vector of the
1298 * desired location. This method computers distance based on unit grid distances,
1299 * not pixel distances.
1300 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001301 * @param cellX The X cell nearest to which you want to search for a vacant area.
1302 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001303 * @param spanX Horizontal span of the object.
1304 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001305 * @param direction The favored direction in which the views should move from x, y
1306 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1307 * matches exactly. Otherwise we find the best matching direction.
1308 * @param occoupied The array which represents which cells in the CellLayout are occupied
1309 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001310 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001311 * @param result Array in which to place the result, or null (in which case a new array will
1312 * be allocated)
1313 * @return The X, Y cell of a vacant area that can contain this object,
1314 * nearest the requested location.
1315 */
1316 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001317 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001318 // Keep track of best-scoring drop area
1319 final int[] bestXY = result != null ? result : new int[2];
1320 float bestDistance = Float.MAX_VALUE;
1321 int bestDirectionScore = Integer.MIN_VALUE;
1322
1323 final int countX = mCountX;
1324 final int countY = mCountY;
1325
1326 for (int y = 0; y < countY - (spanY - 1); y++) {
1327 inner:
1328 for (int x = 0; x < countX - (spanX - 1); x++) {
1329 // First, let's see if this thing fits anywhere
1330 for (int i = 0; i < spanX; i++) {
1331 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001332 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001333 continue inner;
1334 }
1335 }
1336 }
1337
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001338 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001339 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001340 computeDirectionVector(x - cellX, y - cellY, curDirection);
1341 // The direction score is just the dot product of the two candidate direction
1342 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001343 int curDirectionScore = direction[0] * curDirection[0] +
1344 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001345 boolean exactDirectionOnly = false;
1346 boolean directionMatches = direction[0] == curDirection[0] &&
1347 direction[0] == curDirection[0];
1348 if ((directionMatches || !exactDirectionOnly) &&
1349 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001350 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1351 bestDistance = distance;
1352 bestDirectionScore = curDirectionScore;
1353 bestXY[0] = x;
1354 bestXY[1] = y;
1355 }
1356 }
1357 }
1358
1359 // Return -1, -1 if no suitable location found
1360 if (bestDistance == Float.MAX_VALUE) {
1361 bestXY[0] = -1;
1362 bestXY[1] = -1;
1363 }
1364 return bestXY;
1365 }
1366
1367 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001368 int[] direction, ItemConfiguration currentState) {
1369 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001370 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001371 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001372 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1373
Adam Cohen8baab352012-03-20 17:39:21 -07001374 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001375
1376 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001377 c.x = mTempLocation[0];
1378 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001379 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001380 }
Adam Cohen8baab352012-03-20 17:39:21 -07001381 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001382 return success;
1383 }
1384
Adam Cohenf3900c22012-11-16 18:28:11 -08001385 /**
1386 * This helper class defines a cluster of views. It helps with defining complex edges
1387 * of the cluster and determining how those edges interact with other views. The edges
1388 * essentially define a fine-grained boundary around the cluster of views -- like a more
1389 * precise version of a bounding box.
1390 */
1391 private class ViewCluster {
1392 final static int LEFT = 0;
1393 final static int TOP = 1;
1394 final static int RIGHT = 2;
1395 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001396
Adam Cohenf3900c22012-11-16 18:28:11 -08001397 ArrayList<View> views;
1398 ItemConfiguration config;
1399 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001400
Adam Cohenf3900c22012-11-16 18:28:11 -08001401 int[] leftEdge = new int[mCountY];
1402 int[] rightEdge = new int[mCountY];
1403 int[] topEdge = new int[mCountX];
1404 int[] bottomEdge = new int[mCountX];
1405 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1406
1407 @SuppressWarnings("unchecked")
1408 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1409 this.views = (ArrayList<View>) views.clone();
1410 this.config = config;
1411 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001412 }
1413
Adam Cohenf3900c22012-11-16 18:28:11 -08001414 void resetEdges() {
1415 for (int i = 0; i < mCountX; i++) {
1416 topEdge[i] = -1;
1417 bottomEdge[i] = -1;
1418 }
1419 for (int i = 0; i < mCountY; i++) {
1420 leftEdge[i] = -1;
1421 rightEdge[i] = -1;
1422 }
1423 leftEdgeDirty = true;
1424 rightEdgeDirty = true;
1425 bottomEdgeDirty = true;
1426 topEdgeDirty = true;
1427 boundingRectDirty = true;
1428 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001429
Adam Cohenf3900c22012-11-16 18:28:11 -08001430 void computeEdge(int which, int[] edge) {
1431 int count = views.size();
1432 for (int i = 0; i < count; i++) {
1433 CellAndSpan cs = config.map.get(views.get(i));
1434 switch (which) {
1435 case LEFT:
1436 int left = cs.x;
1437 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1438 if (left < edge[j] || edge[j] < 0) {
1439 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001440 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001441 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001442 break;
1443 case RIGHT:
1444 int right = cs.x + cs.spanX;
1445 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1446 if (right > edge[j]) {
1447 edge[j] = right;
1448 }
1449 }
1450 break;
1451 case TOP:
1452 int top = cs.y;
1453 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1454 if (top < edge[j] || edge[j] < 0) {
1455 edge[j] = top;
1456 }
1457 }
1458 break;
1459 case BOTTOM:
1460 int bottom = cs.y + cs.spanY;
1461 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1462 if (bottom > edge[j]) {
1463 edge[j] = bottom;
1464 }
1465 }
1466 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001467 }
1468 }
1469 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001470
1471 boolean isViewTouchingEdge(View v, int whichEdge) {
1472 CellAndSpan cs = config.map.get(v);
1473
1474 int[] edge = getEdge(whichEdge);
1475
1476 switch (whichEdge) {
1477 case LEFT:
1478 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1479 if (edge[i] == cs.x + cs.spanX) {
1480 return true;
1481 }
1482 }
1483 break;
1484 case RIGHT:
1485 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1486 if (edge[i] == cs.x) {
1487 return true;
1488 }
1489 }
1490 break;
1491 case TOP:
1492 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1493 if (edge[i] == cs.y + cs.spanY) {
1494 return true;
1495 }
1496 }
1497 break;
1498 case BOTTOM:
1499 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1500 if (edge[i] == cs.y) {
1501 return true;
1502 }
1503 }
1504 break;
1505 }
1506 return false;
1507 }
1508
1509 void shift(int whichEdge, int delta) {
1510 for (View v: views) {
1511 CellAndSpan c = config.map.get(v);
1512 switch (whichEdge) {
1513 case LEFT:
1514 c.x -= delta;
1515 break;
1516 case RIGHT:
1517 c.x += delta;
1518 break;
1519 case TOP:
1520 c.y -= delta;
1521 break;
1522 case BOTTOM:
1523 default:
1524 c.y += delta;
1525 break;
1526 }
1527 }
1528 resetEdges();
1529 }
1530
1531 public void addView(View v) {
1532 views.add(v);
1533 resetEdges();
1534 }
1535
1536 public Rect getBoundingRect() {
1537 if (boundingRectDirty) {
1538 boolean first = true;
1539 for (View v: views) {
1540 CellAndSpan c = config.map.get(v);
1541 if (first) {
1542 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1543 first = false;
1544 } else {
1545 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1546 }
1547 }
1548 }
1549 return boundingRect;
1550 }
1551
1552 public int[] getEdge(int which) {
1553 switch (which) {
1554 case LEFT:
1555 return getLeftEdge();
1556 case RIGHT:
1557 return getRightEdge();
1558 case TOP:
1559 return getTopEdge();
1560 case BOTTOM:
1561 default:
1562 return getBottomEdge();
1563 }
1564 }
1565
1566 public int[] getLeftEdge() {
1567 if (leftEdgeDirty) {
1568 computeEdge(LEFT, leftEdge);
1569 }
1570 return leftEdge;
1571 }
1572
1573 public int[] getRightEdge() {
1574 if (rightEdgeDirty) {
1575 computeEdge(RIGHT, rightEdge);
1576 }
1577 return rightEdge;
1578 }
1579
1580 public int[] getTopEdge() {
1581 if (topEdgeDirty) {
1582 computeEdge(TOP, topEdge);
1583 }
1584 return topEdge;
1585 }
1586
1587 public int[] getBottomEdge() {
1588 if (bottomEdgeDirty) {
1589 computeEdge(BOTTOM, bottomEdge);
1590 }
1591 return bottomEdge;
1592 }
1593
1594 PositionComparator comparator = new PositionComparator();
1595 class PositionComparator implements Comparator<View> {
1596 int whichEdge = 0;
1597 public int compare(View left, View right) {
1598 CellAndSpan l = config.map.get(left);
1599 CellAndSpan r = config.map.get(right);
1600 switch (whichEdge) {
1601 case LEFT:
1602 return (r.x + r.spanX) - (l.x + l.spanX);
1603 case RIGHT:
1604 return l.x - r.x;
1605 case TOP:
1606 return (r.y + r.spanY) - (l.y + l.spanY);
1607 case BOTTOM:
1608 default:
1609 return l.y - r.y;
1610 }
1611 }
1612 }
1613
1614 public void sortConfigurationForEdgePush(int edge) {
1615 comparator.whichEdge = edge;
1616 Collections.sort(config.sortedViews, comparator);
1617 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001618 }
1619
Adam Cohenf3900c22012-11-16 18:28:11 -08001620 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1621 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001622
Adam Cohenf3900c22012-11-16 18:28:11 -08001623 ViewCluster cluster = new ViewCluster(views, currentState);
1624 Rect clusterRect = cluster.getBoundingRect();
1625 int whichEdge;
1626 int pushDistance;
1627 boolean fail = false;
1628
1629 // Determine the edge of the cluster that will be leading the push and how far
1630 // the cluster must be shifted.
1631 if (direction[0] < 0) {
1632 whichEdge = ViewCluster.LEFT;
1633 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001634 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001635 whichEdge = ViewCluster.RIGHT;
1636 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1637 } else if (direction[1] < 0) {
1638 whichEdge = ViewCluster.TOP;
1639 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1640 } else {
1641 whichEdge = ViewCluster.BOTTOM;
1642 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001643 }
1644
Adam Cohenf3900c22012-11-16 18:28:11 -08001645 // Break early for invalid push distance.
1646 if (pushDistance <= 0) {
1647 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001648 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001649
1650 // Mark the occupied state as false for the group of views we want to move.
1651 for (View v: views) {
1652 CellAndSpan c = currentState.map.get(v);
1653 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1654 }
1655
1656 // We save the current configuration -- if we fail to find a solution we will revert
1657 // to the initial state. The process of finding a solution modifies the configuration
1658 // in place, hence the need for revert in the failure case.
1659 currentState.save();
1660
1661 // The pushing algorithm is simplified by considering the views in the order in which
1662 // they would be pushed by the cluster. For example, if the cluster is leading with its
1663 // left edge, we consider sort the views by their right edge, from right to left.
1664 cluster.sortConfigurationForEdgePush(whichEdge);
1665
1666 while (pushDistance > 0 && !fail) {
1667 for (View v: currentState.sortedViews) {
1668 // For each view that isn't in the cluster, we see if the leading edge of the
1669 // cluster is contacting the edge of that view. If so, we add that view to the
1670 // cluster.
1671 if (!cluster.views.contains(v) && v != dragView) {
1672 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1673 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1674 if (!lp.canReorder) {
1675 // The push solution includes the all apps button, this is not viable.
1676 fail = true;
1677 break;
1678 }
1679 cluster.addView(v);
1680 CellAndSpan c = currentState.map.get(v);
1681
1682 // Adding view to cluster, mark it as not occupied.
1683 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1684 }
1685 }
1686 }
1687 pushDistance--;
1688
1689 // The cluster has been completed, now we move the whole thing over in the appropriate
1690 // direction.
1691 cluster.shift(whichEdge, 1);
1692 }
1693
1694 boolean foundSolution = false;
1695 clusterRect = cluster.getBoundingRect();
1696
1697 // Due to the nature of the algorithm, the only check required to verify a valid solution
1698 // is to ensure that completed shifted cluster lies completely within the cell layout.
1699 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1700 clusterRect.bottom <= mCountY) {
1701 foundSolution = true;
1702 } else {
1703 currentState.restore();
1704 }
1705
1706 // In either case, we set the occupied array as marked for the location of the views
1707 for (View v: cluster.views) {
1708 CellAndSpan c = currentState.map.get(v);
1709 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1710 }
1711
1712 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001713 }
1714
Adam Cohen482ed822012-03-02 14:15:13 -08001715 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001716 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001717 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001718
Adam Cohen8baab352012-03-20 17:39:21 -07001719 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001720 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001721 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001722 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001723 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001724 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001725 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001726 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001727 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001728 }
1729 }
Adam Cohen8baab352012-03-20 17:39:21 -07001730
Adam Cohen8baab352012-03-20 17:39:21 -07001731 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001732 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001733 CellAndSpan c = currentState.map.get(v);
1734 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1735 }
1736
Adam Cohen47a876d2012-03-19 13:21:41 -07001737 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1738 int top = boundingRect.top;
1739 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001740 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001741 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001742 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001743 CellAndSpan c = currentState.map.get(v);
1744 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001745 }
1746
Adam Cohen482ed822012-03-02 14:15:13 -08001747 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1748
Adam Cohenf3900c22012-11-16 18:28:11 -08001749 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1750 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001751
Adam Cohen8baab352012-03-20 17:39:21 -07001752 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001753 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001754 int deltaX = mTempLocation[0] - boundingRect.left;
1755 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001756 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001757 CellAndSpan c = currentState.map.get(v);
1758 c.x += deltaX;
1759 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001760 }
1761 success = true;
1762 }
Adam Cohen8baab352012-03-20 17:39:21 -07001763
1764 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001765 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001766 CellAndSpan c = currentState.map.get(v);
1767 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001768 }
1769 return success;
1770 }
1771
1772 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1773 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1774 }
1775
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001776 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1777 // to push items in each of the cardinal directions, in an order based on the direction vector
1778 // passed.
1779 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1780 int[] direction, View ignoreView, ItemConfiguration solution) {
1781 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001782 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001783 // separately in each of the components.
1784 int temp = direction[1];
1785 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001786
1787 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001788 ignoreView, solution)) {
1789 return true;
1790 }
1791 direction[1] = temp;
1792 temp = direction[0];
1793 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001794
1795 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001796 ignoreView, solution)) {
1797 return true;
1798 }
1799 // Revert the direction
1800 direction[0] = temp;
1801
1802 // Now we try pushing in each component of the opposite direction
1803 direction[0] *= -1;
1804 direction[1] *= -1;
1805 temp = direction[1];
1806 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001807 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001808 ignoreView, solution)) {
1809 return true;
1810 }
1811
1812 direction[1] = temp;
1813 temp = direction[0];
1814 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001815 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001816 ignoreView, solution)) {
1817 return true;
1818 }
1819 // revert the direction
1820 direction[0] = temp;
1821 direction[0] *= -1;
1822 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001823
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001824 } else {
1825 // If the direction vector has a single non-zero component, we push first in the
1826 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001827 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001828 ignoreView, solution)) {
1829 return true;
1830 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001831 // Then we try the opposite direction
1832 direction[0] *= -1;
1833 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001834 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001835 ignoreView, solution)) {
1836 return true;
1837 }
1838 // Switch the direction back
1839 direction[0] *= -1;
1840 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001841
1842 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001843 // to find a solution by pushing along the perpendicular axis.
1844
1845 // Swap the components
1846 int temp = direction[1];
1847 direction[1] = direction[0];
1848 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001849 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001850 ignoreView, solution)) {
1851 return true;
1852 }
1853
1854 // Then we try the opposite direction
1855 direction[0] *= -1;
1856 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001857 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001858 ignoreView, solution)) {
1859 return true;
1860 }
1861 // Switch the direction back
1862 direction[0] *= -1;
1863 direction[1] *= -1;
1864
1865 // Swap the components back
1866 temp = direction[1];
1867 direction[1] = direction[0];
1868 direction[0] = temp;
1869 }
1870 return false;
1871 }
1872
Adam Cohen482ed822012-03-02 14:15:13 -08001873 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001874 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001875 // Return early if get invalid cell positions
1876 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001877
Adam Cohen8baab352012-03-20 17:39:21 -07001878 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001879 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001880
Adam Cohen8baab352012-03-20 17:39:21 -07001881 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001882 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001883 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001884 if (c != null) {
1885 c.x = cellX;
1886 c.y = cellY;
1887 }
Adam Cohen482ed822012-03-02 14:15:13 -08001888 }
Adam Cohen482ed822012-03-02 14:15:13 -08001889 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1890 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001891 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001892 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001893 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001894 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001895 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001896 if (Rect.intersects(r0, r1)) {
1897 if (!lp.canReorder) {
1898 return false;
1899 }
1900 mIntersectingViews.add(child);
1901 }
1902 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001903
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001904 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1905
Winson Chung5f8afe62013-08-12 16:19:28 -07001906 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001907 // we try to find a solution such that no displaced item travels through another item
1908 // without also displacing that item.
1909 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001910 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001911 return true;
1912 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001913
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001914 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001915 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001916 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001917 return true;
1918 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001919
Adam Cohen482ed822012-03-02 14:15:13 -08001920 // Ok, they couldn't move as a block, let's move them individually
1921 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001922 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001923 return false;
1924 }
1925 }
1926 return true;
1927 }
1928
1929 /*
1930 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1931 * the provided point and the provided cell
1932 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001933 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001934 double angle = Math.atan(((float) deltaY) / deltaX);
1935
1936 result[0] = 0;
1937 result[1] = 0;
1938 if (Math.abs(Math.cos(angle)) > 0.5f) {
1939 result[0] = (int) Math.signum(deltaX);
1940 }
1941 if (Math.abs(Math.sin(angle)) > 0.5f) {
1942 result[1] = (int) Math.signum(deltaY);
1943 }
1944 }
1945
Adam Cohen8baab352012-03-20 17:39:21 -07001946 private void copyOccupiedArray(boolean[][] occupied) {
1947 for (int i = 0; i < mCountX; i++) {
1948 for (int j = 0; j < mCountY; j++) {
1949 occupied[i][j] = mOccupied[i][j];
1950 }
1951 }
1952 }
1953
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001954 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001955 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1956 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001957 // Copy the current state into the solution. This solution will be manipulated as necessary.
1958 copyCurrentStateToSolution(solution, false);
1959 // Copy the current occupied array into the temporary occupied array. This array will be
1960 // manipulated as necessary to find a solution.
1961 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001962
1963 // We find the nearest cell into which we would place the dragged item, assuming there's
1964 // nothing in its way.
1965 int result[] = new int[2];
1966 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1967
1968 boolean success = false;
1969 // First we try the exact nearest position of the item being dragged,
1970 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001971 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1972 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001973
1974 if (!success) {
1975 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1976 // x, then 1 in y etc.
1977 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001978 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1979 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001980 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001981 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1982 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001983 }
1984 solution.isSolution = false;
1985 } else {
1986 solution.isSolution = true;
1987 solution.dragViewX = result[0];
1988 solution.dragViewY = result[1];
1989 solution.dragViewSpanX = spanX;
1990 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001991 }
1992 return solution;
1993 }
1994
1995 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001996 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001997 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001998 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001999 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002000 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002001 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002002 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002003 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002004 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002005 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002006 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002007 }
2008 }
2009
2010 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2011 for (int i = 0; i < mCountX; i++) {
2012 for (int j = 0; j < mCountY; j++) {
2013 mTmpOccupied[i][j] = false;
2014 }
2015 }
2016
Michael Jurkaa52570f2012-03-20 03:18:20 -07002017 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002018 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002019 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002020 if (child == dragView) continue;
2021 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002022 CellAndSpan c = solution.map.get(child);
2023 if (c != null) {
2024 lp.tmpCellX = c.x;
2025 lp.tmpCellY = c.y;
2026 lp.cellHSpan = c.spanX;
2027 lp.cellVSpan = c.spanY;
2028 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002029 }
2030 }
2031 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2032 solution.dragViewSpanY, mTmpOccupied, true);
2033 }
2034
2035 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2036 commitDragView) {
2037
2038 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2039 for (int i = 0; i < mCountX; i++) {
2040 for (int j = 0; j < mCountY; j++) {
2041 occupied[i][j] = false;
2042 }
2043 }
2044
Michael Jurkaa52570f2012-03-20 03:18:20 -07002045 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002046 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002047 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002048 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002049 CellAndSpan c = solution.map.get(child);
2050 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002051 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2052 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002053 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002054 }
2055 }
2056 if (commitDragView) {
2057 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2058 solution.dragViewSpanY, occupied, true);
2059 }
2060 }
2061
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002062
2063 // This method starts or changes the reorder preview animations
2064 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2065 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002066 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002067 for (int i = 0; i < childCount; i++) {
2068 View child = mShortcutsAndWidgets.getChildAt(i);
2069 if (child == dragView) continue;
2070 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002071 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2072 != null && !solution.intersectingViews.contains(child);
2073
Adam Cohen19f37922012-03-21 11:59:11 -07002074 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002075 if (c != null && !skip) {
2076 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2077 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002078 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002079 }
2080 }
2081 }
2082
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002083 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002084 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002085 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002086 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002087 float finalDeltaX;
2088 float finalDeltaY;
2089 float initDeltaX;
2090 float initDeltaY;
2091 float finalScale;
2092 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002093 int mode;
2094 boolean repeating = false;
2095 private static final int PREVIEW_DURATION = 300;
2096 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2097
2098 public static final int MODE_HINT = 0;
2099 public static final int MODE_PREVIEW = 1;
2100
Adam Cohene7587d22012-05-24 18:50:02 -07002101 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002102
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002103 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2104 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002105 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2106 final int x0 = mTmpPoint[0];
2107 final int y0 = mTmpPoint[1];
2108 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2109 final int x1 = mTmpPoint[0];
2110 final int y1 = mTmpPoint[1];
2111 final int dX = x1 - x0;
2112 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002113 finalDeltaX = 0;
2114 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002115 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002116 if (dX == dY && dX == 0) {
2117 } else {
2118 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002119 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002120 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002121 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002122 } else {
2123 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002124 finalDeltaX = (int) (- dir * Math.signum(dX) *
2125 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2126 finalDeltaY = (int) (- dir * Math.signum(dY) *
2127 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002128 }
2129 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002130 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002131 initDeltaX = child.getTranslationX();
2132 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002133 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002134 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002135 this.child = child;
2136 }
2137
Adam Cohend024f982012-05-23 18:26:45 -07002138 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002139 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002140 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002141 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002142 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002143 if (finalDeltaX == 0 && finalDeltaY == 0) {
2144 completeAnimationImmediately();
2145 return;
2146 }
Adam Cohen19f37922012-03-21 11:59:11 -07002147 }
Adam Cohend024f982012-05-23 18:26:45 -07002148 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002149 return;
2150 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002151 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002152 a = va;
Tony Wickham489fc562015-09-02 14:45:39 -07002153
2154 // Animations are disabled in power save mode, causing the repeated animation to jump
2155 // spastically between beginning and end states. Since this looks bad, we don't repeat
2156 // the animation in power save mode.
2157 PowerManager powerManager = (PowerManager) getContext()
2158 .getSystemService(Context.POWER_SERVICE);
2159 boolean powerSaverOn = Utilities.ATLEAST_LOLLIPOP && powerManager.isPowerSaveMode();
2160 if (!powerSaverOn) {
2161 va.setRepeatMode(ValueAnimator.REVERSE);
2162 va.setRepeatCount(ValueAnimator.INFINITE);
2163 }
2164
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002165 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002166 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002167 va.addUpdateListener(new AnimatorUpdateListener() {
2168 @Override
2169 public void onAnimationUpdate(ValueAnimator animation) {
2170 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002171 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2172 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2173 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002174 child.setTranslationX(x);
2175 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002176 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002177 child.setScaleX(s);
2178 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002179 }
2180 });
2181 va.addListener(new AnimatorListenerAdapter() {
2182 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002183 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002184 initDeltaX = 0;
2185 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002186 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002187 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002188 }
2189 });
Adam Cohen19f37922012-03-21 11:59:11 -07002190 mShakeAnimators.put(child, this);
2191 va.start();
2192 }
2193
Adam Cohend024f982012-05-23 18:26:45 -07002194 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002195 if (a != null) {
2196 a.cancel();
2197 }
Adam Cohen19f37922012-03-21 11:59:11 -07002198 }
Adam Cohene7587d22012-05-24 18:50:02 -07002199
Adam Cohen091440a2015-03-18 14:16:05 -07002200 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002201 if (a != null) {
2202 a.cancel();
2203 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002204
Michael Jurka2ecf9952012-06-18 12:52:28 -07002205 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002206 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002207 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002208 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2209 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002210 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2211 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002212 );
2213 s.setDuration(REORDER_ANIMATION_DURATION);
2214 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2215 s.start();
2216 }
Adam Cohen19f37922012-03-21 11:59:11 -07002217 }
2218
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002219 private void completeAndClearReorderPreviewAnimations() {
2220 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002221 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002222 }
2223 mShakeAnimators.clear();
2224 }
2225
Adam Cohen482ed822012-03-02 14:15:13 -08002226 private void commitTempPlacement() {
2227 for (int i = 0; i < mCountX; i++) {
2228 for (int j = 0; j < mCountY; j++) {
2229 mOccupied[i][j] = mTmpOccupied[i][j];
2230 }
2231 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002232 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002233 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002234 View child = mShortcutsAndWidgets.getChildAt(i);
2235 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2236 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002237 // We do a null check here because the item info can be null in the case of the
2238 // AllApps button in the hotseat.
2239 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002240 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2241 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2242 info.requiresDbUpdate = true;
2243 }
Adam Cohen2acce882012-03-28 19:03:19 -07002244 info.cellX = lp.cellX = lp.tmpCellX;
2245 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002246 info.spanX = lp.cellHSpan;
2247 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002248 }
Adam Cohen482ed822012-03-02 14:15:13 -08002249 }
Adam Cohen2acce882012-03-28 19:03:19 -07002250 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002251 }
2252
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002253 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002254 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002255 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002256 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002257 lp.useTmpCoords = useTempCoords;
2258 }
2259 }
2260
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002261 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002262 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2263 int[] result = new int[2];
2264 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002265 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002266 resultSpan);
2267 if (result[0] >= 0 && result[1] >= 0) {
2268 copyCurrentStateToSolution(solution, false);
2269 solution.dragViewX = result[0];
2270 solution.dragViewY = result[1];
2271 solution.dragViewSpanX = resultSpan[0];
2272 solution.dragViewSpanY = resultSpan[1];
2273 solution.isSolution = true;
2274 } else {
2275 solution.isSolution = false;
2276 }
2277 return solution;
2278 }
2279
2280 public void prepareChildForDrag(View child) {
2281 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002282 }
2283
Adam Cohen19f37922012-03-21 11:59:11 -07002284 /* This seems like it should be obvious and straight-forward, but when the direction vector
2285 needs to match with the notion of the dragView pushing other views, we have to employ
2286 a slightly more subtle notion of the direction vector. The question is what two points is
2287 the vector between? The center of the dragView and its desired destination? Not quite, as
2288 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2289 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2290 or right, which helps make pushing feel right.
2291 */
2292 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2293 int spanY, View dragView, int[] resultDirection) {
2294 int[] targetDestination = new int[2];
2295
2296 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2297 Rect dragRect = new Rect();
2298 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2299 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2300
2301 Rect dropRegionRect = new Rect();
2302 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2303 dragView, dropRegionRect, mIntersectingViews);
2304
2305 int dropRegionSpanX = dropRegionRect.width();
2306 int dropRegionSpanY = dropRegionRect.height();
2307
2308 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2309 dropRegionRect.height(), dropRegionRect);
2310
2311 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2312 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2313
2314 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2315 deltaX = 0;
2316 }
2317 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2318 deltaY = 0;
2319 }
2320
2321 if (deltaX == 0 && deltaY == 0) {
2322 // No idea what to do, give a random direction.
2323 resultDirection[0] = 1;
2324 resultDirection[1] = 0;
2325 } else {
2326 computeDirectionVector(deltaX, deltaY, resultDirection);
2327 }
2328 }
2329
2330 // For a given cell and span, fetch the set of views intersecting the region.
2331 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2332 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2333 if (boundingRect != null) {
2334 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2335 }
2336 intersectingViews.clear();
2337 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2338 Rect r1 = new Rect();
2339 final int count = mShortcutsAndWidgets.getChildCount();
2340 for (int i = 0; i < count; i++) {
2341 View child = mShortcutsAndWidgets.getChildAt(i);
2342 if (child == dragView) continue;
2343 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2344 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2345 if (Rect.intersects(r0, r1)) {
2346 mIntersectingViews.add(child);
2347 if (boundingRect != null) {
2348 boundingRect.union(r1);
2349 }
2350 }
2351 }
2352 }
2353
2354 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2355 View dragView, int[] result) {
2356 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2357 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2358 mIntersectingViews);
2359 return !mIntersectingViews.isEmpty();
2360 }
2361
2362 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002363 completeAndClearReorderPreviewAnimations();
2364 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2365 final int count = mShortcutsAndWidgets.getChildCount();
2366 for (int i = 0; i < count; i++) {
2367 View child = mShortcutsAndWidgets.getChildAt(i);
2368 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2369 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2370 lp.tmpCellX = lp.cellX;
2371 lp.tmpCellY = lp.cellY;
2372 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2373 0, false, false);
2374 }
Adam Cohen19f37922012-03-21 11:59:11 -07002375 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002376 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002377 }
Adam Cohen19f37922012-03-21 11:59:11 -07002378 }
2379
Adam Cohenbebf0422012-04-11 18:06:28 -07002380 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2381 View dragView, int[] direction, boolean commit) {
2382 int[] pixelXY = new int[2];
2383 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2384
2385 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002386 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002387 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2388
2389 setUseTempCoords(true);
2390 if (swapSolution != null && swapSolution.isSolution) {
2391 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2392 // committing anything or animating anything as we just want to determine if a solution
2393 // exists
2394 copySolutionToTempState(swapSolution, dragView);
2395 setItemPlacementDirty(true);
2396 animateItemsToSolution(swapSolution, dragView, commit);
2397
2398 if (commit) {
2399 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002400 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002401 setItemPlacementDirty(false);
2402 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002403 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2404 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002405 }
2406 mShortcutsAndWidgets.requestLayout();
2407 }
2408 return swapSolution.isSolution;
2409 }
2410
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002411 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002412 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002413 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002414 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002415
2416 if (resultSpan == null) {
2417 resultSpan = new int[2];
2418 }
2419
Adam Cohen19f37922012-03-21 11:59:11 -07002420 // When we are checking drop validity or actually dropping, we don't recompute the
2421 // direction vector, since we want the solution to match the preview, and it's possible
2422 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002423 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2424 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002425 mDirectionVector[0] = mPreviousReorderDirection[0];
2426 mDirectionVector[1] = mPreviousReorderDirection[1];
2427 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002428 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2429 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2430 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002431 }
2432 } else {
2433 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2434 mPreviousReorderDirection[0] = mDirectionVector[0];
2435 mPreviousReorderDirection[1] = mDirectionVector[1];
2436 }
2437
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002438 // Find a solution involving pushing / displacing any items in the way
2439 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002440 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2441
2442 // We attempt the approach which doesn't shuffle views at all
2443 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2444 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2445
2446 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002447
2448 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2449 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002450 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2451 finalSolution = swapSolution;
2452 } else if (noShuffleSolution.isSolution) {
2453 finalSolution = noShuffleSolution;
2454 }
2455
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002456 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002457 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002458 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2459 ReorderPreviewAnimation.MODE_HINT);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002460 result[0] = finalSolution.dragViewX;
2461 result[1] = finalSolution.dragViewY;
2462 resultSpan[0] = finalSolution.dragViewSpanX;
2463 resultSpan[1] = finalSolution.dragViewSpanY;
2464 } else {
2465 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2466 }
2467 return result;
2468 }
2469
Adam Cohen482ed822012-03-02 14:15:13 -08002470 boolean foundSolution = true;
2471 if (!DESTRUCTIVE_REORDER) {
2472 setUseTempCoords(true);
2473 }
2474
2475 if (finalSolution != null) {
2476 result[0] = finalSolution.dragViewX;
2477 result[1] = finalSolution.dragViewY;
2478 resultSpan[0] = finalSolution.dragViewSpanX;
2479 resultSpan[1] = finalSolution.dragViewSpanY;
2480
2481 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2482 // committing anything or animating anything as we just want to determine if a solution
2483 // exists
2484 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2485 if (!DESTRUCTIVE_REORDER) {
2486 copySolutionToTempState(finalSolution, dragView);
2487 }
2488 setItemPlacementDirty(true);
2489 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2490
Adam Cohen19f37922012-03-21 11:59:11 -07002491 if (!DESTRUCTIVE_REORDER &&
2492 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002493 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002494 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002495 setItemPlacementDirty(false);
2496 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002497 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2498 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002499 }
2500 }
2501 } else {
2502 foundSolution = false;
2503 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2504 }
2505
2506 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2507 setUseTempCoords(false);
2508 }
Adam Cohen482ed822012-03-02 14:15:13 -08002509
Michael Jurkaa52570f2012-03-20 03:18:20 -07002510 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002511 return result;
2512 }
2513
Adam Cohen19f37922012-03-21 11:59:11 -07002514 void setItemPlacementDirty(boolean dirty) {
2515 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002516 }
Adam Cohen19f37922012-03-21 11:59:11 -07002517 boolean isItemPlacementDirty() {
2518 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002519 }
2520
Adam Cohen091440a2015-03-18 14:16:05 -07002521 @Thunk class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002522 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002523 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2524 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002525 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002526 boolean isSolution = false;
2527 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2528
Adam Cohenf3900c22012-11-16 18:28:11 -08002529 void save() {
2530 // Copy current state into savedMap
2531 for (View v: map.keySet()) {
2532 map.get(v).copy(savedMap.get(v));
2533 }
2534 }
2535
2536 void restore() {
2537 // Restore current state from savedMap
2538 for (View v: savedMap.keySet()) {
2539 savedMap.get(v).copy(map.get(v));
2540 }
2541 }
2542
2543 void add(View v, CellAndSpan cs) {
2544 map.put(v, cs);
2545 savedMap.put(v, new CellAndSpan());
2546 sortedViews.add(v);
2547 }
2548
Adam Cohen482ed822012-03-02 14:15:13 -08002549 int area() {
2550 return dragViewSpanX * dragViewSpanY;
2551 }
Adam Cohen8baab352012-03-20 17:39:21 -07002552 }
2553
2554 private class CellAndSpan {
2555 int x, y;
2556 int spanX, spanY;
2557
Adam Cohenf3900c22012-11-16 18:28:11 -08002558 public CellAndSpan() {
2559 }
2560
2561 public void copy(CellAndSpan copy) {
2562 copy.x = x;
2563 copy.y = y;
2564 copy.spanX = spanX;
2565 copy.spanY = spanY;
2566 }
2567
Adam Cohen8baab352012-03-20 17:39:21 -07002568 public CellAndSpan(int x, int y, int spanX, int spanY) {
2569 this.x = x;
2570 this.y = y;
2571 this.spanX = spanX;
2572 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002573 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002574
2575 public String toString() {
2576 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2577 }
2578
Adam Cohen482ed822012-03-02 14:15:13 -08002579 }
2580
Adam Cohendf035382011-04-11 17:22:04 -07002581 /**
Adam Cohendf035382011-04-11 17:22:04 -07002582 * Find a starting cell position that will fit the given bounds nearest the requested
2583 * cell location. Uses Euclidean distance to score multiple vacant areas.
2584 *
2585 * @param pixelX The X location at which you want to search for a vacant area.
2586 * @param pixelY The Y location at which you want to search for a vacant area.
2587 * @param spanX Horizontal span of the object.
2588 * @param spanY Vertical span of the object.
2589 * @param ignoreView Considers space occupied by this view as unoccupied
2590 * @param result Previously returned value to possibly recycle.
2591 * @return The X, Y cell of a vacant area that can contain this object,
2592 * nearest the requested location.
2593 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002594 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2595 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002596 }
2597
Michael Jurka0280c3b2010-09-17 15:00:07 -07002598 boolean existsEmptyCell() {
2599 return findCellForSpan(null, 1, 1);
2600 }
2601
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002603 * Finds the upper-left coordinate of the first rectangle in the grid that can
2604 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2605 * then this method will only return coordinates for rectangles that contain the cell
2606 * (intersectX, intersectY)
2607 *
2608 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2609 * can be found.
2610 * @param spanX The horizontal span of the cell we want to find.
2611 * @param spanY The vertical span of the cell we want to find.
2612 *
2613 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002614 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002615 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Michael Jurka28750fb2010-09-24 17:43:49 -07002616 boolean foundCell = false;
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002617 final int endX = mCountX - (spanX - 1);
2618 final int endY = mCountY - (spanY - 1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002619
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002620 for (int y = 0; y < endY && !foundCell; y++) {
2621 inner:
2622 for (int x = 0; x < endX; x++) {
2623 for (int i = 0; i < spanX; i++) {
2624 for (int j = 0; j < spanY; j++) {
2625 if (mOccupied[x + i][y + j]) {
2626 // small optimization: we can skip to after the column we just found
2627 // an occupied cell
2628 x += i;
2629 continue inner;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002630 }
2631 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002632 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002633 if (cellXY != null) {
2634 cellXY[0] = x;
2635 cellXY[1] = y;
2636 }
2637 foundCell = true;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002638 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002639 }
2640 }
2641
Michael Jurka28750fb2010-09-24 17:43:49 -07002642 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002643 }
2644
2645 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002646 * A drag event has begun over this layout.
2647 * It may have begun over this layout (in which case onDragChild is called first),
2648 * or it may have begun on another layout.
2649 */
2650 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002651 mDragging = true;
2652 }
2653
2654 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002655 * Called when drag has left this CellLayout or has been completed (successfully or not)
2656 */
2657 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002658 // This can actually be called when we aren't in a drag, e.g. when adding a new
2659 // item to this layout via the customize drawer.
2660 // Guard against that case.
2661 if (mDragging) {
2662 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002663 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002664
2665 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002666 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002667 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2668 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002669 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002670 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002671 }
2672
2673 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002674 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002675 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002676 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002677 *
2678 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002680 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002681 if (child != null) {
2682 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002683 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002684 child.requestLayout();
Tony Wickham1cdb6d02015-09-17 11:08:27 -07002685 markCellsAsOccupiedForView(child);
Romain Guyd94533d2009-08-17 10:01:15 -07002686 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002687 }
2688
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002689 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002690 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002691 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692 * @param cellX X coordinate of upper left corner expressed as a cell position
2693 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002694 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002695 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002696 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002698 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002699 final int cellWidth = mCellWidth;
2700 final int cellHeight = mCellHeight;
2701 final int widthGap = mWidthGap;
2702 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002703
Winson Chung4b825dcd2011-06-19 12:41:22 -07002704 final int hStartPadding = getPaddingLeft();
2705 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002706
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002707 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2708 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2709
2710 int x = hStartPadding + cellX * (cellWidth + widthGap);
2711 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002712
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002713 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002714 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002715
Michael Jurka0280c3b2010-09-17 15:00:07 -07002716 private void clearOccupiedCells() {
2717 for (int x = 0; x < mCountX; x++) {
2718 for (int y = 0; y < mCountY; y++) {
2719 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720 }
2721 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002722 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002723
Adam Cohend4844c32011-02-18 19:25:06 -08002724 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002725 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002726 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002727 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002728 }
2729
Adam Cohend4844c32011-02-18 19:25:06 -08002730 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002731 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002732 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002733 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002734 }
2735
Adam Cohen482ed822012-03-02 14:15:13 -08002736 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2737 boolean value) {
2738 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002739 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2740 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002741 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002742 }
2743 }
2744 }
2745
Adam Cohen2801caf2011-05-13 20:57:39 -07002746 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002747 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002748 (Math.max((mCountX - 1), 0) * mWidthGap);
2749 }
2750
2751 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002752 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002753 (Math.max((mCountY - 1), 0) * mHeightGap);
2754 }
2755
Michael Jurka66d72172011-04-12 16:29:25 -07002756 public boolean isOccupied(int x, int y) {
2757 if (x < mCountX && y < mCountY) {
2758 return mOccupied[x][y];
2759 } else {
2760 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2761 }
2762 }
2763
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002764 @Override
2765 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2766 return new CellLayout.LayoutParams(getContext(), attrs);
2767 }
2768
2769 @Override
2770 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2771 return p instanceof CellLayout.LayoutParams;
2772 }
2773
2774 @Override
2775 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2776 return new CellLayout.LayoutParams(p);
2777 }
2778
2779 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2780 /**
2781 * Horizontal location of the item in the grid.
2782 */
2783 @ViewDebug.ExportedProperty
2784 public int cellX;
2785
2786 /**
2787 * Vertical location of the item in the grid.
2788 */
2789 @ViewDebug.ExportedProperty
2790 public int cellY;
2791
2792 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002793 * Temporary horizontal location of the item in the grid during reorder
2794 */
2795 public int tmpCellX;
2796
2797 /**
2798 * Temporary vertical location of the item in the grid during reorder
2799 */
2800 public int tmpCellY;
2801
2802 /**
2803 * Indicates that the temporary coordinates should be used to layout the items
2804 */
2805 public boolean useTmpCoords;
2806
2807 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002808 * Number of cells spanned horizontally by the item.
2809 */
2810 @ViewDebug.ExportedProperty
2811 public int cellHSpan;
2812
2813 /**
2814 * Number of cells spanned vertically by the item.
2815 */
2816 @ViewDebug.ExportedProperty
2817 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002818
Adam Cohen1b607ed2011-03-03 17:26:50 -08002819 /**
2820 * Indicates whether the item will set its x, y, width and height parameters freely,
2821 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2822 */
Adam Cohend4844c32011-02-18 19:25:06 -08002823 public boolean isLockedToGrid = true;
2824
Adam Cohen482ed822012-03-02 14:15:13 -08002825 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002826 * Indicates that this item should use the full extents of its parent.
2827 */
2828 public boolean isFullscreen = false;
2829
2830 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002831 * Indicates whether this item can be reordered. Always true except in the case of the
2832 * the AllApps button.
2833 */
2834 public boolean canReorder = true;
2835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836 // X coordinate of the view in the layout.
2837 @ViewDebug.ExportedProperty
2838 int x;
2839 // Y coordinate of the view in the layout.
2840 @ViewDebug.ExportedProperty
2841 int y;
2842
Romain Guy84f296c2009-11-04 15:00:44 -08002843 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 public LayoutParams(Context c, AttributeSet attrs) {
2846 super(c, attrs);
2847 cellHSpan = 1;
2848 cellVSpan = 1;
2849 }
2850
2851 public LayoutParams(ViewGroup.LayoutParams source) {
2852 super(source);
2853 cellHSpan = 1;
2854 cellVSpan = 1;
2855 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002856
2857 public LayoutParams(LayoutParams source) {
2858 super(source);
2859 this.cellX = source.cellX;
2860 this.cellY = source.cellY;
2861 this.cellHSpan = source.cellHSpan;
2862 this.cellVSpan = source.cellVSpan;
2863 }
2864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002865 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002866 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867 this.cellX = cellX;
2868 this.cellY = cellY;
2869 this.cellHSpan = cellHSpan;
2870 this.cellVSpan = cellVSpan;
2871 }
2872
Adam Cohen2374abf2013-04-16 14:56:57 -07002873 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2874 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002875 if (isLockedToGrid) {
2876 final int myCellHSpan = cellHSpan;
2877 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002878 int myCellX = useTmpCoords ? tmpCellX : cellX;
2879 int myCellY = useTmpCoords ? tmpCellY : cellY;
2880
2881 if (invertHorizontally) {
2882 myCellX = colCount - myCellX - cellHSpan;
2883 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002884
Adam Cohend4844c32011-02-18 19:25:06 -08002885 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2886 leftMargin - rightMargin;
2887 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2888 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002889 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2890 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002891 }
2892 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002893
Winson Chungaafa03c2010-06-11 17:34:16 -07002894 public String toString() {
2895 return "(" + this.cellX + ", " + this.cellY + ")";
2896 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002897
2898 public void setWidth(int width) {
2899 this.width = width;
2900 }
2901
2902 public int getWidth() {
2903 return width;
2904 }
2905
2906 public void setHeight(int height) {
2907 this.height = height;
2908 }
2909
2910 public int getHeight() {
2911 return height;
2912 }
2913
2914 public void setX(int x) {
2915 this.x = x;
2916 }
2917
2918 public int getX() {
2919 return x;
2920 }
2921
2922 public void setY(int y) {
2923 this.y = y;
2924 }
2925
2926 public int getY() {
2927 return y;
2928 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929 }
2930
Michael Jurka0280c3b2010-09-17 15:00:07 -07002931 // This class stores info for two purposes:
2932 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2933 // its spanX, spanY, and the screen it is on
2934 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2935 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2936 // the CellLayout that was long clicked
Sunny Goyal83a8f042015-05-19 12:52:12 -07002937 public static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002939 int cellX = -1;
2940 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002941 int spanX;
2942 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07002943 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002944 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002945
Sunny Goyal83a8f042015-05-19 12:52:12 -07002946 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002947 cell = v;
2948 cellX = info.cellX;
2949 cellY = info.cellY;
2950 spanX = info.spanX;
2951 spanY = info.spanY;
2952 screenId = info.screenId;
2953 container = info.container;
2954 }
2955
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 @Override
2957 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002958 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2959 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002960 }
2961 }
Michael Jurkad771c962011-08-09 15:00:48 -07002962
Sunny Goyala9116722015-04-29 13:55:58 -07002963 public boolean findVacantCell(int spanX, int spanY, int[] outXY) {
2964 return Utilities.findVacantCell(outXY, spanX, spanY, mCountX, mCountY, mOccupied);
2965 }
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002966
2967 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
2968 int x2 = x + spanX - 1;
2969 int y2 = y + spanY - 1;
2970 if (x < 0 || y < 0 || x2 >= mCountX || y2 >= mCountY) {
2971 return false;
2972 }
2973 for (int i = x; i <= x2; i++) {
2974 for (int j = y; j <= y2; j++) {
2975 if (mOccupied[i][j]) {
2976 return false;
2977 }
2978 }
2979 }
2980
2981 return true;
2982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002983}