blob: 5f8b71c986438c87121ddc42e7921159f065e4eb [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;
Adam Cohenc9735cf2015-01-23 16:11:55 -080040import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070042import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070043import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.view.MotionEvent;
45import android.view.View;
46import android.view.ViewDebug;
47import android.view.ViewGroup;
Adam Cohenc9735cf2015-01-23 16:11:55 -080048import android.view.accessibility.AccessibilityEvent;
Winson Chung150fbab2010-09-29 17:14:26 -070049import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Sunny Goyal4b6eb262015-05-14 19:24:40 -070051import com.android.launcher3.BubbleTextView.BubbleTextShadowHandler;
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.FolderIcon.FolderRingAnimator;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070053import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale9b651e2015-04-24 11:44:51 -070054import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
55import com.android.launcher3.accessibility.FolderAccessibilityHelper;
56import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
Sunny Goyal6c56c682015-07-16 14:09:05 -070057import com.android.launcher3.config.ProviderConfig;
Adam Cohen091440a2015-03-18 14:16:05 -070058import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -070059import com.android.launcher3.widget.PendingAddWidgetInfo;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070060
Adam Cohen69ce2e52011-07-03 19:25:21 -070061import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070062import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080063import java.util.Collections;
64import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070065import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080066import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070067
Sunny Goyal4b6eb262015-05-14 19:24:40 -070068public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
Sunny Goyale9b651e2015-04-24 11:44:51 -070069 public static final int WORKSPACE_ACCESSIBILITY_DRAG = 2;
70 public static final int FOLDER_ACCESSIBILITY_DRAG = 1;
71
Winson Chungaafa03c2010-06-11 17:34:16 -070072 static final String TAG = "CellLayout";
73
Adam Cohen2acce882012-03-28 19:03:19 -070074 private Launcher mLauncher;
Adam Cohen091440a2015-03-18 14:16:05 -070075 @Thunk int mCellWidth;
76 @Thunk int mCellHeight;
Winson Chung11a1a532013-09-13 11:14:45 -070077 private int mFixedCellWidth;
78 private int mFixedCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070079
Adam Cohen091440a2015-03-18 14:16:05 -070080 @Thunk int mCountX;
81 @Thunk int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082
Adam Cohen234c4cd2011-07-17 21:03:04 -070083 private int mOriginalWidthGap;
84 private int mOriginalHeightGap;
Adam Cohen091440a2015-03-18 14:16:05 -070085 @Thunk int mWidthGap;
86 @Thunk int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070087 private int mMaxGap;
Adam Cohen917e3882013-10-31 15:03:35 -070088 private boolean mDropPending = false;
Adam Cohenc50438c2014-08-19 17:43:05 -070089 private boolean mIsDragTarget = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Patrick Dubroyde7658b2010-09-27 11:15:43 -070091 // These are temporary variables to prevent having to allocate a new object just to
92 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Adam Cohen091440a2015-03-18 14:16:05 -070093 @Thunk final int[] mTmpPoint = new int[2];
Sunny Goyal2805e632015-05-20 15:35:32 -070094 @Thunk final int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070095
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080097 boolean[][] mTmpOccupied;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098
Michael Jurkadee05892010-07-27 10:01:56 -070099 private OnTouchListener mInterceptTouchListener;
Mady Melloref044dd2015-06-02 15:35:07 -0700100 private StylusEventHelper mStylusEventHelper;
Michael Jurkadee05892010-07-27 10:01:56 -0700101
Adam Cohen69ce2e52011-07-03 19:25:21 -0700102 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -0700103 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -0700104
Michael Jurka5f1c5092010-09-03 14:15:02 -0700105 private float mBackgroundAlpha;
Adam Cohenf34bab52010-09-30 14:11:56 -0700106
Sunny Goyal2805e632015-05-20 15:35:32 -0700107 private static final int BACKGROUND_ACTIVATE_DURATION = 120;
108 private final TransitionDrawable mBackground;
109
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700110 // These values allow a fixed measurement to be set on the CellLayout.
111 private int mFixedWidth = -1;
112 private int mFixedHeight = -1;
113
Michael Jurka33945b22010-12-21 18:19:38 -0800114 // If we're actively dragging something over this screen, mIsDragOverlapping is true
115 private boolean mIsDragOverlapping = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700116
Winson Chung150fbab2010-09-29 17:14:26 -0700117 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700118 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk Rect[] mDragOutlines = new Rect[4];
120 @Thunk float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700121 private InterruptibleInOutAnimator[] mDragOutlineAnims =
122 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700123
124 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700125 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700126 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700127
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700128 private final ClickShadowView mTouchFeedbackView;
Patrick Dubroy96864c32011-03-10 17:17:23 -0800129
Sunny Goyal316490e2015-06-02 09:38:28 -0700130 @Thunk HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new HashMap<>();
131 @Thunk HashMap<View, ReorderPreviewAnimation> mShakeAnimators = new HashMap<>();
Adam Cohen19f37922012-03-21 11:59:11 -0700132
133 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700134
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700135 // When a drag operation is in progress, holds the nearest cell to the touch point
136 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Joe Onorato4be866d2010-10-10 11:26:02 -0700138 private boolean mDragging = false;
139
Patrick Dubroyce34a972010-10-19 10:34:32 -0700140 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700141 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700142
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800143 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700144 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800145
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800146 public static final int MODE_SHOW_REORDER_HINT = 0;
147 public static final int MODE_DRAG_OVER = 1;
148 public static final int MODE_ON_DROP = 2;
149 public static final int MODE_ON_DROP_EXTERNAL = 3;
150 public static final int MODE_ACCEPT_DROP = 4;
Adam Cohen19f37922012-03-21 11:59:11 -0700151 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800152 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
153
Adam Cohena897f392012-04-27 18:12:05 -0700154 static final int LANDSCAPE = 0;
155 static final int PORTRAIT = 1;
156
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800157 private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700158 private static final int REORDER_ANIMATION_DURATION = 150;
Adam Cohen091440a2015-03-18 14:16:05 -0700159 @Thunk float mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -0700160
Adam Cohen482ed822012-03-02 14:15:13 -0800161 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
162 private Rect mOccupiedRect = new Rect();
163 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700164 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700165 private static final int INVALID_DIRECTION = -100;
Adam Cohen482ed822012-03-02 14:15:13 -0800166
Sunny Goyal2805e632015-05-20 15:35:32 -0700167 private final Rect mTempRect = new Rect();
Winson Chung3a6e7f32013-10-09 15:50:52 -0700168
Michael Jurkaca993832012-06-29 15:17:04 -0700169 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700170
Adam Cohenc9735cf2015-01-23 16:11:55 -0800171 // Related to accessible drag and drop
Sunny Goyale9b651e2015-04-24 11:44:51 -0700172 private DragAndDropAccessibilityDelegate mTouchHelper;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800173 private boolean mUseTouchHelper = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 public CellLayout(Context context) {
176 this(context, null);
177 }
178
179 public CellLayout(Context context, AttributeSet attrs) {
180 this(context, attrs, 0);
181 }
182
183 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
184 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700185
186 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
187 // the user where a dragged item will land when dropped.
188 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800189 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700190 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700191
Adam Cohen2e6da152015-05-06 11:42:25 -0700192 DeviceProfile grid = mLauncher.getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
194
Winson Chung11a1a532013-09-13 11:14:45 -0700195 mCellWidth = mCellHeight = -1;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800196 mFixedCellWidth = mFixedCellHeight = -1;
Winson Chung5f8afe62013-08-12 16:19:28 -0700197 mWidthGap = mOriginalWidthGap = 0;
198 mHeightGap = mOriginalHeightGap = 0;
199 mMaxGap = Integer.MAX_VALUE;
Adam Cohen2e6da152015-05-06 11:42:25 -0700200 mCountX = (int) grid.inv.numColumns;
201 mCountY = (int) grid.inv.numRows;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700202 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800203 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700204 mPreviousReorderDirection[0] = INVALID_DIRECTION;
205 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
207 a.recycle();
208
209 setAlwaysDrawnWithCacheEnabled(false);
210
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700211 final Resources res = getResources();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700212 mHotseatScale = (float) grid.hotseatIconSizePx / grid.iconSizePx;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700213
Sunny Goyal2805e632015-05-20 15:35:32 -0700214 mBackground = (TransitionDrawable) res.getDrawable(R.drawable.bg_screenpanel);
215 mBackground.setCallback(this);
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 {
419 mBackground.reverseTransition(BACKGROUND_ACTIVATE_DURATION);
420 }
Adam Cohenc50438c2014-08-19 17:43:05 -0700421 invalidate();
422 }
423 }
424
Michael Jurka33945b22010-12-21 18:19:38 -0800425 boolean getIsDragOverlapping() {
426 return mIsDragOverlapping;
427 }
428
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700429 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700430 protected void onDraw(Canvas canvas) {
Sunny Goyal05739772015-05-19 19:59:09 -0700431 if (!mIsDragTarget) {
432 return;
433 }
434
Michael Jurka3e7c7632010-10-02 16:01:03 -0700435 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
436 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
437 // When we're small, we are either drawn normally or in the "accepts drops" state (during
438 // a drag). However, we also drag the mini hover background *over* one of those two
439 // backgrounds
Sunny Goyal05739772015-05-19 19:59:09 -0700440 if (mBackgroundAlpha > 0.0f) {
Sunny Goyal2805e632015-05-20 15:35:32 -0700441 mBackground.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700442 }
Romain Guya6abce82009-11-10 02:54:41 -0800443
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700444 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700445 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700446 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700447 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800448 final Rect r = mDragOutlines[i];
Winson Chung3a6e7f32013-10-09 15:50:52 -0700449 mTempRect.set(r);
450 Utilities.scaleRectAboutCenter(mTempRect, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700451 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700452 paint.setAlpha((int)(alpha + .5f));
Winson Chung3a6e7f32013-10-09 15:50:52 -0700453 canvas.drawBitmap(b, null, mTempRect, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700454 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700455 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800456
Adam Cohen482ed822012-03-02 14:15:13 -0800457 if (DEBUG_VISUALIZE_OCCUPIED) {
458 int[] pt = new int[2];
459 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700460 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800461 for (int i = 0; i < mCountX; i++) {
462 for (int j = 0; j < mCountY; j++) {
463 if (mOccupied[i][j]) {
464 cellToPoint(i, j, pt);
465 canvas.save();
466 canvas.translate(pt[0], pt[1]);
467 cd.draw(canvas);
468 canvas.restore();
469 }
470 }
471 }
472 }
473
Andrew Flynn850d2e72012-04-26 16:51:20 -0700474 int previewOffset = FolderRingAnimator.sPreviewSize;
475
Adam Cohen69ce2e52011-07-03 19:25:21 -0700476 // The folder outer / inner ring image(s)
Adam Cohen2e6da152015-05-06 11:42:25 -0700477 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700478 for (int i = 0; i < mFolderOuterRings.size(); i++) {
479 FolderRingAnimator fra = mFolderOuterRings.get(i);
480
Adam Cohen5108bc02013-09-20 17:04:51 -0700481 Drawable d;
482 int width, height;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700483 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700484 View child = getChildAt(fra.mCellX, fra.mCellY);
Adam Cohen558f1c22013-10-09 15:15:24 -0700485
Winson Chung89f97052013-09-20 11:32:26 -0700486 if (child != null) {
Adam Cohen558f1c22013-10-09 15:15:24 -0700487 int centerX = mTempLocation[0] + mCellWidth / 2;
488 int centerY = mTempLocation[1] + previewOffset / 2 +
489 child.getPaddingTop() + grid.folderBackgroundOffset;
490
Adam Cohen5108bc02013-09-20 17:04:51 -0700491 // Draw outer ring, if it exists
492 if (FolderIcon.HAS_OUTER_RING) {
493 d = FolderRingAnimator.sSharedOuterRingDrawable;
494 width = (int) (fra.getOuterRingSize() * getChildrenScale());
495 height = width;
496 canvas.save();
497 canvas.translate(centerX - width / 2, centerY - height / 2);
498 d.setBounds(0, 0, width, height);
499 d.draw(canvas);
500 canvas.restore();
501 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700502
Winson Chung89f97052013-09-20 11:32:26 -0700503 // Draw inner ring
504 d = FolderRingAnimator.sSharedInnerRingDrawable;
505 width = (int) (fra.getInnerRingSize() * getChildrenScale());
506 height = width;
Winson Chung89f97052013-09-20 11:32:26 -0700507 canvas.save();
508 canvas.translate(centerX - width / 2, centerY - width / 2);
509 d.setBounds(0, 0, width, height);
510 d.draw(canvas);
511 canvas.restore();
512 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700513 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700514
515 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
516 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
517 int width = d.getIntrinsicWidth();
518 int height = d.getIntrinsicHeight();
519
520 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700521 View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
Winson Chung89f97052013-09-20 11:32:26 -0700522 if (child != null) {
523 int centerX = mTempLocation[0] + mCellWidth / 2;
524 int centerY = mTempLocation[1] + previewOffset / 2 +
525 child.getPaddingTop() + grid.folderBackgroundOffset;
Adam Cohenc51934b2011-07-26 21:07:43 -0700526
Winson Chung89f97052013-09-20 11:32:26 -0700527 canvas.save();
528 canvas.translate(centerX - width / 2, centerY - width / 2);
529 d.setBounds(0, 0, width, height);
530 d.draw(canvas);
531 canvas.restore();
532 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700533 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700534 }
535
536 public void showFolderAccept(FolderRingAnimator fra) {
537 mFolderOuterRings.add(fra);
538 }
539
540 public void hideFolderAccept(FolderRingAnimator fra) {
541 if (mFolderOuterRings.contains(fra)) {
542 mFolderOuterRings.remove(fra);
543 }
544 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700545 }
546
Adam Cohenc51934b2011-07-26 21:07:43 -0700547 public void setFolderLeaveBehindCell(int x, int y) {
548 mFolderLeaveBehindCell[0] = x;
549 mFolderLeaveBehindCell[1] = y;
550 invalidate();
551 }
552
553 public void clearFolderLeaveBehind() {
554 mFolderLeaveBehindCell[0] = -1;
555 mFolderLeaveBehindCell[1] = -1;
556 invalidate();
557 }
558
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700559 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700560 public boolean shouldDelayChildPressedState() {
561 return false;
562 }
563
Adam Cohen1462de32012-07-24 22:34:36 -0700564 public void restoreInstanceState(SparseArray<Parcelable> states) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700565 try {
566 dispatchRestoreInstanceState(states);
567 } catch (IllegalArgumentException ex) {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700568 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal33a152f2014-07-22 12:13:14 -0700569 throw ex;
570 }
571 // Mismatched viewId / viewType preventing restore. Skip restore on production builds.
572 Log.e(TAG, "Ignoring an error while restoring a view instance state", ex);
573 }
Adam Cohen1462de32012-07-24 22:34:36 -0700574 }
575
Michael Jurkae6235dd2011-10-04 15:02:05 -0700576 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700577 public void cancelLongPress() {
578 super.cancelLongPress();
579
580 // Cancel long press for all children
581 final int count = getChildCount();
582 for (int i = 0; i < count; i++) {
583 final View child = getChildAt(i);
584 child.cancelLongPress();
585 }
586 }
587
Michael Jurkadee05892010-07-27 10:01:56 -0700588 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
589 mInterceptTouchListener = listener;
590 }
591
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800592 public int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700593 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 }
595
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800596 public int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700597 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
599
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800600 public void setIsHotseat(boolean isHotseat) {
601 mIsHotseat = isHotseat;
Winson Chung5f8afe62013-08-12 16:19:28 -0700602 mShortcutsAndWidgets.setIsHotseat(isHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800603 }
604
Sunny Goyale9b651e2015-04-24 11:44:51 -0700605 public boolean isHotseat() {
606 return mIsHotseat;
607 }
608
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800609 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700610 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700611 final LayoutParams lp = params;
612
Andrew Flynnde38e422012-05-08 11:22:15 -0700613 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800614 if (child instanceof BubbleTextView) {
615 BubbleTextView bubbleChild = (BubbleTextView) child;
Winson Chung5f8afe62013-08-12 16:19:28 -0700616 bubbleChild.setTextVisibility(!mIsHotseat);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800617 }
618
Adam Cohen307fe232012-08-16 17:55:58 -0700619 child.setScaleX(getChildrenScale());
620 child.setScaleY(getChildrenScale());
621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 // Generate an id for each view, this assumes we have at most 256x256 cells
623 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700624 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700625 // If the horizontal or vertical span is set to -1, it is taken to
626 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700627 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
628 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629
Winson Chungaafa03c2010-06-11 17:34:16 -0700630 child.setId(childId);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700631 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700632
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700633 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700634
Winson Chungaafa03c2010-06-11 17:34:16 -0700635 return true;
636 }
637 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700639
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700641 public void removeAllViews() {
642 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700643 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644 }
645
646 @Override
647 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700648 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700649 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700650 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700651 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700652 }
653
654 @Override
655 public void removeView(View view) {
656 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700657 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700658 }
659
660 @Override
661 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700662 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
663 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 }
665
666 @Override
667 public void removeViewInLayout(View view) {
668 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700669 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700670 }
671
672 @Override
673 public void removeViews(int start, int count) {
674 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700675 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700677 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 }
679
680 @Override
681 public void removeViewsInLayout(int start, int count) {
682 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700685 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800686 }
687
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700688 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700689 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 * @param x X coordinate of the point
691 * @param y Y coordinate of the point
692 * @param result Array of 2 ints to hold the x and y coordinate of the cell
693 */
Sunny Goyale9b651e2015-04-24 11:44:51 -0700694 public void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700695 final int hStartPadding = getPaddingLeft();
696 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697
698 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
699 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
700
Adam Cohend22015c2010-07-26 22:02:18 -0700701 final int xAxis = mCountX;
702 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703
704 if (result[0] < 0) result[0] = 0;
705 if (result[0] >= xAxis) result[0] = xAxis - 1;
706 if (result[1] < 0) result[1] = 0;
707 if (result[1] >= yAxis) result[1] = yAxis - 1;
708 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 /**
711 * Given a point, return the cell that most closely encloses that point
712 * @param x X coordinate of the point
713 * @param y Y coordinate of the point
714 * @param result Array of 2 ints to hold the x and y coordinate of the cell
715 */
716 void pointToCellRounded(int x, int y, int[] result) {
717 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
718 }
719
720 /**
721 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700722 *
723 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700725 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 * @param result Array of 2 ints to hold the x and y coordinate of the point
727 */
728 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700729 final int hStartPadding = getPaddingLeft();
730 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731
732 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
733 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
734 }
735
Adam Cohene3e27a82011-04-15 12:07:39 -0700736 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800737 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700738 *
739 * @param cellX X coordinate of the cell
740 * @param cellY Y coordinate of the cell
741 *
742 * @param result Array of 2 ints to hold the x and y coordinate of the point
743 */
744 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700745 regionToCenterPoint(cellX, cellY, 1, 1, result);
746 }
747
748 /**
749 * Given a cell coordinate and span return the point that represents the center of the regio
750 *
751 * @param cellX X coordinate of the cell
752 * @param cellY Y coordinate of the cell
753 *
754 * @param result Array of 2 ints to hold the x and y coordinate of the point
755 */
756 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700757 final int hStartPadding = getPaddingLeft();
758 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700759 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
760 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
761 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
762 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700763 }
764
Adam Cohen19f37922012-03-21 11:59:11 -0700765 /**
766 * Given a cell coordinate and span fills out a corresponding pixel rect
767 *
768 * @param cellX X coordinate of the cell
769 * @param cellY Y coordinate of the cell
770 * @param result Rect in which to write the result
771 */
772 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
773 final int hStartPadding = getPaddingLeft();
774 final int vStartPadding = getPaddingTop();
775 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
776 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
777 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
778 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
779 }
780
Adam Cohen482ed822012-03-02 14:15:13 -0800781 public float getDistanceFromCell(float x, float y, int[] cell) {
782 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700783 return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]);
Adam Cohen482ed822012-03-02 14:15:13 -0800784 }
785
Romain Guy84f296c2009-11-04 15:00:44 -0800786 int getCellWidth() {
787 return mCellWidth;
788 }
789
790 int getCellHeight() {
791 return mCellHeight;
792 }
793
Adam Cohend4844c32011-02-18 19:25:06 -0800794 int getWidthGap() {
795 return mWidthGap;
796 }
797
798 int getHeightGap() {
799 return mHeightGap;
800 }
801
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700802 public void setFixedSize(int width, int height) {
803 mFixedWidth = width;
804 mFixedHeight = height;
805 }
806
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 @Override
808 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung5f8afe62013-08-12 16:19:28 -0700811 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
812 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung2d75f122013-09-23 16:53:31 -0700813 int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
814 int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
Winson Chung11a1a532013-09-13 11:14:45 -0700815 if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700816 int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX);
817 int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY);
Winson Chung11a1a532013-09-13 11:14:45 -0700818 if (cw != mCellWidth || ch != mCellHeight) {
819 mCellWidth = cw;
820 mCellHeight = ch;
821 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
822 mHeightGap, mCountX, mCountY);
823 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700824 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700825
Winson Chung2d75f122013-09-23 16:53:31 -0700826 int newWidth = childWidthSize;
827 int newHeight = childHeightSize;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700828 if (mFixedWidth > 0 && mFixedHeight > 0) {
829 newWidth = mFixedWidth;
830 newHeight = mFixedHeight;
831 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
833 }
834
Adam Cohend22015c2010-07-26 22:02:18 -0700835 int numWidthGaps = mCountX - 1;
836 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837
Adam Cohen234c4cd2011-07-17 21:03:04 -0700838 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung2d75f122013-09-23 16:53:31 -0700839 int hSpace = childWidthSize;
840 int vSpace = childHeightSize;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700841 int hFreeSpace = hSpace - (mCountX * mCellWidth);
842 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700843 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
844 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700845 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap,
846 mHeightGap, mCountX, mCountY);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700847 } else {
848 mWidthGap = mOriginalWidthGap;
849 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700850 }
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700851
852 // Make the feedback view large enough to hold the blur bitmap.
853 mTouchFeedbackView.measure(
854 MeasureSpec.makeMeasureSpec(mCellWidth + mTouchFeedbackView.getExtraSize(),
855 MeasureSpec.EXACTLY),
856 MeasureSpec.makeMeasureSpec(mCellHeight + mTouchFeedbackView.getExtraSize(),
857 MeasureSpec.EXACTLY));
858
859 mShortcutsAndWidgets.measure(
860 MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY),
861 MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
862
863 int maxWidth = mShortcutsAndWidgets.getMeasuredWidth();
864 int maxHeight = mShortcutsAndWidgets.getMeasuredHeight();
Winson Chung2d75f122013-09-23 16:53:31 -0700865 if (mFixedWidth > 0 && mFixedHeight > 0) {
866 setMeasuredDimension(maxWidth, maxHeight);
867 } else {
868 setMeasuredDimension(widthSize, heightSize);
869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 }
871
872 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700873 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Winson Chung38848ca2013-10-08 12:03:44 -0700874 int offset = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() -
875 (mCountX * mCellWidth);
876 int left = getPaddingLeft() + (int) Math.ceil(offset / 2f);
877 int top = getPaddingTop();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700878
879 mTouchFeedbackView.layout(left, top,
880 left + mTouchFeedbackView.getMeasuredWidth(),
881 top + mTouchFeedbackView.getMeasuredHeight());
882 mShortcutsAndWidgets.layout(left, top,
883 left + r - l,
884 top + b - t);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886
887 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700888 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
889 super.onSizeChanged(w, h, oldw, oldh);
Winson Chung82a9bd22013-10-08 16:02:34 -0700890
891 // Expand the background drawing bounds by the padding baked into the background drawable
Sunny Goyal2805e632015-05-20 15:35:32 -0700892 mBackground.getPadding(mTempRect);
893 mBackground.setBounds(-mTempRect.left, -mTempRect.top,
894 w + mTempRect.right, h + mTempRect.bottom);
Michael Jurkadee05892010-07-27 10:01:56 -0700895 }
896
897 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700899 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 }
901
902 @Override
903 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700904 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
906
Michael Jurka5f1c5092010-09-03 14:15:02 -0700907 public float getBackgroundAlpha() {
908 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700909 }
910
Michael Jurka5f1c5092010-09-03 14:15:02 -0700911 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -0800912 if (mBackgroundAlpha != alpha) {
913 mBackgroundAlpha = alpha;
Sunny Goyal2805e632015-05-20 15:35:32 -0700914 mBackground.setAlpha((int) (mBackgroundAlpha * 255));
Michael Jurkaafaa0502011-12-13 18:22:50 -0800915 }
Michael Jurkadee05892010-07-27 10:01:56 -0700916 }
917
Sunny Goyal2805e632015-05-20 15:35:32 -0700918 @Override
919 protected boolean verifyDrawable(Drawable who) {
920 return super.verifyDrawable(who) || (mIsDragTarget && who == mBackground);
921 }
922
Michael Jurkaa52570f2012-03-20 03:18:20 -0700923 public void setShortcutAndWidgetAlpha(float alpha) {
Sunny Goyal02b50812014-09-10 15:44:42 -0700924 mShortcutsAndWidgets.setAlpha(alpha);
Michael Jurkadee05892010-07-27 10:01:56 -0700925 }
926
Michael Jurkaa52570f2012-03-20 03:18:20 -0700927 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700928 return mShortcutsAndWidgets;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700929 }
930
Patrick Dubroy440c3602010-07-13 17:50:32 -0700931 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700932 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700933 }
934
Adam Cohen76fc0852011-06-17 13:26:23 -0700935 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -0800936 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700937 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -0800938 boolean[][] occupied = mOccupied;
939 if (!permanent) {
940 occupied = mTmpOccupied;
941 }
942
Adam Cohen19f37922012-03-21 11:59:11 -0700943 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700944 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
945 final ItemInfo info = (ItemInfo) child.getTag();
946
947 // We cancel any existing animations
948 if (mReorderAnimators.containsKey(lp)) {
949 mReorderAnimators.get(lp).cancel();
950 mReorderAnimators.remove(lp);
951 }
952
Adam Cohen482ed822012-03-02 14:15:13 -0800953 final int oldX = lp.x;
954 final int oldY = lp.y;
955 if (adjustOccupied) {
956 occupied[lp.cellX][lp.cellY] = false;
957 occupied[cellX][cellY] = true;
958 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700959 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -0800960 if (permanent) {
961 lp.cellX = info.cellX = cellX;
962 lp.cellY = info.cellY = cellY;
963 } else {
964 lp.tmpCellX = cellX;
965 lp.tmpCellY = cellY;
966 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700967 clc.setupLp(lp);
968 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800969 final int newX = lp.x;
970 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700971
Adam Cohen76fc0852011-06-17 13:26:23 -0700972 lp.x = oldX;
973 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700974
Adam Cohen482ed822012-03-02 14:15:13 -0800975 // Exit early if we're not actually moving the view
976 if (oldX == newX && oldY == newY) {
977 lp.isLockedToGrid = true;
978 return true;
979 }
980
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100981 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -0800982 va.setDuration(duration);
983 mReorderAnimators.put(lp, va);
984
985 va.addUpdateListener(new AnimatorUpdateListener() {
986 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -0700987 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -0800988 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -0700989 lp.x = (int) ((1 - r) * oldX + r * newX);
990 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -0700991 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700992 }
993 });
Adam Cohen482ed822012-03-02 14:15:13 -0800994 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700995 boolean cancelled = false;
996 public void onAnimationEnd(Animator animation) {
997 // If the animation was cancelled, it means that another animation
998 // has interrupted this one, and we don't want to lock the item into
999 // place just yet.
1000 if (!cancelled) {
1001 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001002 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001003 }
1004 if (mReorderAnimators.containsKey(lp)) {
1005 mReorderAnimators.remove(lp);
1006 }
1007 }
1008 public void onAnimationCancel(Animator animation) {
1009 cancelled = true;
1010 }
1011 });
Adam Cohen482ed822012-03-02 14:15:13 -08001012 va.setStartDelay(delay);
1013 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001014 return true;
1015 }
1016 return false;
1017 }
1018
Adam Cohen482ed822012-03-02 14:15:13 -08001019 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1020 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001021 final int oldDragCellX = mDragCell[0];
1022 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001023
Adam Cohen2801caf2011-05-13 20:57:39 -07001024 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001025 return;
1026 }
1027
Adam Cohen482ed822012-03-02 14:15:13 -08001028 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1029 mDragCell[0] = cellX;
1030 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001031 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001032 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001033 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001034
Joe Onorato4be866d2010-10-10 11:26:02 -07001035 int left = topLeft[0];
1036 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001037
Winson Chungb8c69f32011-10-19 21:36:08 -07001038 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001039 // When drawing the drag outline, it did not account for margin offsets
1040 // added by the view's parent.
1041 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1042 left += lp.leftMargin;
1043 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001044
Adam Cohen99e8b402011-03-25 19:23:43 -07001045 // Offsets due to the size difference between the View and the dragOutline.
1046 // There is a size difference to account for the outer blur, which may lie
1047 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001048 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001049 // We center about the x axis
1050 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1051 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001052 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001053 if (dragOffset != null && dragRegion != null) {
1054 // Center the drag region *horizontally* in the cell and apply a drag
1055 // outline offset
1056 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1057 - dragRegion.width()) / 2;
Winson Chung69737c32013-10-08 17:00:19 -07001058 int cHeight = getShortcutsAndWidgets().getCellContentHeight();
1059 int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
1060 top += dragOffset.y + cellPaddingY;
Winson Chungb8c69f32011-10-19 21:36:08 -07001061 } else {
1062 // Center the drag outline in the cell
1063 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1064 - dragOutline.getWidth()) / 2;
1065 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1066 - dragOutline.getHeight()) / 2;
1067 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001068 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001069 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001070 mDragOutlineAnims[oldIndex].animateOut();
1071 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001072 Rect r = mDragOutlines[mDragOutlineCurrent];
1073 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1074 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001075 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001076 }
Winson Chung150fbab2010-09-29 17:14:26 -07001077
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001078 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1079 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001080 }
1081 }
1082
Adam Cohene0310962011-04-18 16:15:31 -07001083 public void clearDragOutlines() {
1084 final int oldIndex = mDragOutlineCurrent;
1085 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001086 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001087 }
1088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001090 * Find a vacant area that will fit the given bounds nearest the requested
1091 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001092 *
Romain Guy51afc022009-05-04 18:03:43 -07001093 * @param pixelX The X location at which you want to search for a vacant area.
1094 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001095 * @param minSpanX The minimum horizontal span required
1096 * @param minSpanY The minimum vertical span required
1097 * @param spanX Horizontal span of the object.
1098 * @param spanY Vertical span of the object.
1099 * @param result Array in which to place the result, or null (in which case a new array will
1100 * be allocated)
1101 * @return The X, Y cell of a vacant area that can contain this object,
1102 * nearest the requested location.
1103 */
1104 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1105 int spanY, int[] result, int[] resultSpan) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001106 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true,
Adam Cohend41fbf52012-02-16 23:53:59 -08001107 result, resultSpan);
1108 }
1109
Adam Cohend41fbf52012-02-16 23:53:59 -08001110 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1111 private void lazyInitTempRectStack() {
1112 if (mTempRectStack.isEmpty()) {
1113 for (int i = 0; i < mCountX * mCountY; i++) {
1114 mTempRectStack.push(new Rect());
1115 }
1116 }
1117 }
Adam Cohen482ed822012-03-02 14:15:13 -08001118
Adam Cohend41fbf52012-02-16 23:53:59 -08001119 private void recycleTempRects(Stack<Rect> used) {
1120 while (!used.isEmpty()) {
1121 mTempRectStack.push(used.pop());
1122 }
1123 }
1124
1125 /**
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.
1131 * @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 ignoreOccupied If true, the result can be an occupied cell
1136 * @param result Array in which to place the result, or null (in which case a new array will
1137 * be allocated)
1138 * @return The X, Y cell of a vacant area that can contain this object,
1139 * nearest the requested location.
1140 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001141 private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1142 int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001143 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001144
Adam Cohene3e27a82011-04-15 12:07:39 -07001145 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1146 // to the center of the item, but we are searching based on the top-left cell, so
1147 // we translate the point over to correspond to the top-left.
1148 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1149 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1150
Jeff Sharkey70864282009-04-07 21:08:40 -07001151 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001152 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001153 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001154 final Rect bestRect = new Rect(-1, -1, -1, -1);
1155 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001156
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001157 final int countX = mCountX;
1158 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001159
Adam Cohend41fbf52012-02-16 23:53:59 -08001160 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1161 spanX < minSpanX || spanY < minSpanY) {
1162 return bestXY;
1163 }
1164
1165 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001166 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001167 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1168 int ySize = -1;
1169 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001170 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001171 // First, let's see if this thing fits anywhere
1172 for (int i = 0; i < minSpanX; i++) {
1173 for (int j = 0; j < minSpanY; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001174 if (mOccupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001175 continue inner;
1176 }
Michael Jurkac28de512010-08-13 11:27:44 -07001177 }
1178 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001179 xSize = minSpanX;
1180 ySize = minSpanY;
1181
1182 // We know that the item will fit at _some_ acceptable size, now let's see
1183 // how big we can make it. We'll alternate between incrementing x and y spans
1184 // until we hit a limit.
1185 boolean incX = true;
1186 boolean hitMaxX = xSize >= spanX;
1187 boolean hitMaxY = ySize >= spanY;
1188 while (!(hitMaxX && hitMaxY)) {
1189 if (incX && !hitMaxX) {
1190 for (int j = 0; j < ySize; j++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001191 if (x + xSize > countX -1 || mOccupied[x + xSize][y + j]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001192 // We can't move out horizontally
1193 hitMaxX = true;
1194 }
1195 }
1196 if (!hitMaxX) {
1197 xSize++;
1198 }
1199 } else if (!hitMaxY) {
1200 for (int i = 0; i < xSize; i++) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001201 if (y + ySize > countY - 1 || mOccupied[x + i][y + ySize]) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001202 // We can't move out vertically
1203 hitMaxY = true;
1204 }
1205 }
1206 if (!hitMaxY) {
1207 ySize++;
1208 }
1209 }
1210 hitMaxX |= xSize >= spanX;
1211 hitMaxY |= ySize >= spanY;
1212 incX = !incX;
1213 }
1214 incX = true;
1215 hitMaxX = xSize >= spanX;
1216 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001217 }
Sunny Goyal2805e632015-05-20 15:35:32 -07001218 final int[] cellXY = mTmpPoint;
Adam Cohene3e27a82011-04-15 12:07:39 -07001219 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220
Adam Cohend41fbf52012-02-16 23:53:59 -08001221 // We verify that the current rect is not a sub-rect of any of our previous
1222 // candidates. In this case, the current rect is disqualified in favour of the
1223 // containing rect.
1224 Rect currentRect = mTempRectStack.pop();
1225 currentRect.set(x, y, x + xSize, y + ySize);
1226 boolean contained = false;
1227 for (Rect r : validRegions) {
1228 if (r.contains(currentRect)) {
1229 contained = true;
1230 break;
1231 }
1232 }
1233 validRegions.push(currentRect);
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001234 double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY);
Adam Cohen482ed822012-03-02 14:15:13 -08001235
Adam Cohend41fbf52012-02-16 23:53:59 -08001236 if ((distance <= bestDistance && !contained) ||
1237 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001238 bestDistance = distance;
1239 bestXY[0] = x;
1240 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001241 if (resultSpan != null) {
1242 resultSpan[0] = xSize;
1243 resultSpan[1] = ySize;
1244 }
1245 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001246 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
1248 }
1249
Adam Cohenc0dcf592011-06-01 15:30:43 -07001250 // Return -1, -1 if no suitable location found
1251 if (bestDistance == Double.MAX_VALUE) {
1252 bestXY[0] = -1;
1253 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001254 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001255 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001256 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001258
Adam Cohen482ed822012-03-02 14:15:13 -08001259 /**
1260 * Find a vacant area that will fit the given bounds nearest the requested
1261 * cell location, and will also weigh in a suggested direction vector of the
1262 * desired location. This method computers distance based on unit grid distances,
1263 * not pixel distances.
1264 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001265 * @param cellX The X cell nearest to which you want to search for a vacant area.
1266 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001267 * @param spanX Horizontal span of the object.
1268 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001269 * @param direction The favored direction in which the views should move from x, y
1270 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1271 * matches exactly. Otherwise we find the best matching direction.
1272 * @param occoupied The array which represents which cells in the CellLayout are occupied
1273 * @param blockOccupied The array which represents which cells in the specified block (cellX,
Winson Chung5f8afe62013-08-12 16:19:28 -07001274 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001275 * @param result Array in which to place the result, or null (in which case a new array will
1276 * be allocated)
1277 * @return The X, Y cell of a vacant area that can contain this object,
1278 * nearest the requested location.
1279 */
1280 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001281 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001282 // Keep track of best-scoring drop area
1283 final int[] bestXY = result != null ? result : new int[2];
1284 float bestDistance = Float.MAX_VALUE;
1285 int bestDirectionScore = Integer.MIN_VALUE;
1286
1287 final int countX = mCountX;
1288 final int countY = mCountY;
1289
1290 for (int y = 0; y < countY - (spanY - 1); y++) {
1291 inner:
1292 for (int x = 0; x < countX - (spanX - 1); x++) {
1293 // First, let's see if this thing fits anywhere
1294 for (int i = 0; i < spanX; i++) {
1295 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001296 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001297 continue inner;
1298 }
1299 }
1300 }
1301
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001302 float distance = (float) Math.hypot(x - cellX, y - cellY);
Adam Cohen482ed822012-03-02 14:15:13 -08001303 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001304 computeDirectionVector(x - cellX, y - cellY, curDirection);
1305 // The direction score is just the dot product of the two candidate direction
1306 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001307 int curDirectionScore = direction[0] * curDirection[0] +
1308 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001309 boolean exactDirectionOnly = false;
1310 boolean directionMatches = direction[0] == curDirection[0] &&
1311 direction[0] == curDirection[0];
1312 if ((directionMatches || !exactDirectionOnly) &&
1313 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001314 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1315 bestDistance = distance;
1316 bestDirectionScore = curDirectionScore;
1317 bestXY[0] = x;
1318 bestXY[1] = y;
1319 }
1320 }
1321 }
1322
1323 // Return -1, -1 if no suitable location found
1324 if (bestDistance == Float.MAX_VALUE) {
1325 bestXY[0] = -1;
1326 bestXY[1] = -1;
1327 }
1328 return bestXY;
1329 }
1330
1331 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001332 int[] direction, ItemConfiguration currentState) {
1333 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001334 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001335 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001336 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1337
Adam Cohen8baab352012-03-20 17:39:21 -07001338 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001339
1340 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001341 c.x = mTempLocation[0];
1342 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001343 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001344 }
Adam Cohen8baab352012-03-20 17:39:21 -07001345 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001346 return success;
1347 }
1348
Adam Cohenf3900c22012-11-16 18:28:11 -08001349 /**
1350 * This helper class defines a cluster of views. It helps with defining complex edges
1351 * of the cluster and determining how those edges interact with other views. The edges
1352 * essentially define a fine-grained boundary around the cluster of views -- like a more
1353 * precise version of a bounding box.
1354 */
1355 private class ViewCluster {
1356 final static int LEFT = 0;
1357 final static int TOP = 1;
1358 final static int RIGHT = 2;
1359 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001360
Adam Cohenf3900c22012-11-16 18:28:11 -08001361 ArrayList<View> views;
1362 ItemConfiguration config;
1363 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001364
Adam Cohenf3900c22012-11-16 18:28:11 -08001365 int[] leftEdge = new int[mCountY];
1366 int[] rightEdge = new int[mCountY];
1367 int[] topEdge = new int[mCountX];
1368 int[] bottomEdge = new int[mCountX];
1369 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1370
1371 @SuppressWarnings("unchecked")
1372 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1373 this.views = (ArrayList<View>) views.clone();
1374 this.config = config;
1375 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001376 }
1377
Adam Cohenf3900c22012-11-16 18:28:11 -08001378 void resetEdges() {
1379 for (int i = 0; i < mCountX; i++) {
1380 topEdge[i] = -1;
1381 bottomEdge[i] = -1;
1382 }
1383 for (int i = 0; i < mCountY; i++) {
1384 leftEdge[i] = -1;
1385 rightEdge[i] = -1;
1386 }
1387 leftEdgeDirty = true;
1388 rightEdgeDirty = true;
1389 bottomEdgeDirty = true;
1390 topEdgeDirty = true;
1391 boundingRectDirty = true;
1392 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001393
Adam Cohenf3900c22012-11-16 18:28:11 -08001394 void computeEdge(int which, int[] edge) {
1395 int count = views.size();
1396 for (int i = 0; i < count; i++) {
1397 CellAndSpan cs = config.map.get(views.get(i));
1398 switch (which) {
1399 case LEFT:
1400 int left = cs.x;
1401 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1402 if (left < edge[j] || edge[j] < 0) {
1403 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001404 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001405 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001406 break;
1407 case RIGHT:
1408 int right = cs.x + cs.spanX;
1409 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1410 if (right > edge[j]) {
1411 edge[j] = right;
1412 }
1413 }
1414 break;
1415 case TOP:
1416 int top = cs.y;
1417 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1418 if (top < edge[j] || edge[j] < 0) {
1419 edge[j] = top;
1420 }
1421 }
1422 break;
1423 case BOTTOM:
1424 int bottom = cs.y + cs.spanY;
1425 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1426 if (bottom > edge[j]) {
1427 edge[j] = bottom;
1428 }
1429 }
1430 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001431 }
1432 }
1433 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001434
1435 boolean isViewTouchingEdge(View v, int whichEdge) {
1436 CellAndSpan cs = config.map.get(v);
1437
1438 int[] edge = getEdge(whichEdge);
1439
1440 switch (whichEdge) {
1441 case LEFT:
1442 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1443 if (edge[i] == cs.x + cs.spanX) {
1444 return true;
1445 }
1446 }
1447 break;
1448 case RIGHT:
1449 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1450 if (edge[i] == cs.x) {
1451 return true;
1452 }
1453 }
1454 break;
1455 case TOP:
1456 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1457 if (edge[i] == cs.y + cs.spanY) {
1458 return true;
1459 }
1460 }
1461 break;
1462 case BOTTOM:
1463 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1464 if (edge[i] == cs.y) {
1465 return true;
1466 }
1467 }
1468 break;
1469 }
1470 return false;
1471 }
1472
1473 void shift(int whichEdge, int delta) {
1474 for (View v: views) {
1475 CellAndSpan c = config.map.get(v);
1476 switch (whichEdge) {
1477 case LEFT:
1478 c.x -= delta;
1479 break;
1480 case RIGHT:
1481 c.x += delta;
1482 break;
1483 case TOP:
1484 c.y -= delta;
1485 break;
1486 case BOTTOM:
1487 default:
1488 c.y += delta;
1489 break;
1490 }
1491 }
1492 resetEdges();
1493 }
1494
1495 public void addView(View v) {
1496 views.add(v);
1497 resetEdges();
1498 }
1499
1500 public Rect getBoundingRect() {
1501 if (boundingRectDirty) {
1502 boolean first = true;
1503 for (View v: views) {
1504 CellAndSpan c = config.map.get(v);
1505 if (first) {
1506 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1507 first = false;
1508 } else {
1509 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1510 }
1511 }
1512 }
1513 return boundingRect;
1514 }
1515
1516 public int[] getEdge(int which) {
1517 switch (which) {
1518 case LEFT:
1519 return getLeftEdge();
1520 case RIGHT:
1521 return getRightEdge();
1522 case TOP:
1523 return getTopEdge();
1524 case BOTTOM:
1525 default:
1526 return getBottomEdge();
1527 }
1528 }
1529
1530 public int[] getLeftEdge() {
1531 if (leftEdgeDirty) {
1532 computeEdge(LEFT, leftEdge);
1533 }
1534 return leftEdge;
1535 }
1536
1537 public int[] getRightEdge() {
1538 if (rightEdgeDirty) {
1539 computeEdge(RIGHT, rightEdge);
1540 }
1541 return rightEdge;
1542 }
1543
1544 public int[] getTopEdge() {
1545 if (topEdgeDirty) {
1546 computeEdge(TOP, topEdge);
1547 }
1548 return topEdge;
1549 }
1550
1551 public int[] getBottomEdge() {
1552 if (bottomEdgeDirty) {
1553 computeEdge(BOTTOM, bottomEdge);
1554 }
1555 return bottomEdge;
1556 }
1557
1558 PositionComparator comparator = new PositionComparator();
1559 class PositionComparator implements Comparator<View> {
1560 int whichEdge = 0;
1561 public int compare(View left, View right) {
1562 CellAndSpan l = config.map.get(left);
1563 CellAndSpan r = config.map.get(right);
1564 switch (whichEdge) {
1565 case LEFT:
1566 return (r.x + r.spanX) - (l.x + l.spanX);
1567 case RIGHT:
1568 return l.x - r.x;
1569 case TOP:
1570 return (r.y + r.spanY) - (l.y + l.spanY);
1571 case BOTTOM:
1572 default:
1573 return l.y - r.y;
1574 }
1575 }
1576 }
1577
1578 public void sortConfigurationForEdgePush(int edge) {
1579 comparator.whichEdge = edge;
1580 Collections.sort(config.sortedViews, comparator);
1581 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001582 }
1583
Adam Cohenf3900c22012-11-16 18:28:11 -08001584 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1585 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001586
Adam Cohenf3900c22012-11-16 18:28:11 -08001587 ViewCluster cluster = new ViewCluster(views, currentState);
1588 Rect clusterRect = cluster.getBoundingRect();
1589 int whichEdge;
1590 int pushDistance;
1591 boolean fail = false;
1592
1593 // Determine the edge of the cluster that will be leading the push and how far
1594 // the cluster must be shifted.
1595 if (direction[0] < 0) {
1596 whichEdge = ViewCluster.LEFT;
1597 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001598 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001599 whichEdge = ViewCluster.RIGHT;
1600 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1601 } else if (direction[1] < 0) {
1602 whichEdge = ViewCluster.TOP;
1603 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1604 } else {
1605 whichEdge = ViewCluster.BOTTOM;
1606 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001607 }
1608
Adam Cohenf3900c22012-11-16 18:28:11 -08001609 // Break early for invalid push distance.
1610 if (pushDistance <= 0) {
1611 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001612 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001613
1614 // Mark the occupied state as false for the group of views we want to move.
1615 for (View v: views) {
1616 CellAndSpan c = currentState.map.get(v);
1617 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1618 }
1619
1620 // We save the current configuration -- if we fail to find a solution we will revert
1621 // to the initial state. The process of finding a solution modifies the configuration
1622 // in place, hence the need for revert in the failure case.
1623 currentState.save();
1624
1625 // The pushing algorithm is simplified by considering the views in the order in which
1626 // they would be pushed by the cluster. For example, if the cluster is leading with its
1627 // left edge, we consider sort the views by their right edge, from right to left.
1628 cluster.sortConfigurationForEdgePush(whichEdge);
1629
1630 while (pushDistance > 0 && !fail) {
1631 for (View v: currentState.sortedViews) {
1632 // For each view that isn't in the cluster, we see if the leading edge of the
1633 // cluster is contacting the edge of that view. If so, we add that view to the
1634 // cluster.
1635 if (!cluster.views.contains(v) && v != dragView) {
1636 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1637 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1638 if (!lp.canReorder) {
1639 // The push solution includes the all apps button, this is not viable.
1640 fail = true;
1641 break;
1642 }
1643 cluster.addView(v);
1644 CellAndSpan c = currentState.map.get(v);
1645
1646 // Adding view to cluster, mark it as not occupied.
1647 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1648 }
1649 }
1650 }
1651 pushDistance--;
1652
1653 // The cluster has been completed, now we move the whole thing over in the appropriate
1654 // direction.
1655 cluster.shift(whichEdge, 1);
1656 }
1657
1658 boolean foundSolution = false;
1659 clusterRect = cluster.getBoundingRect();
1660
1661 // Due to the nature of the algorithm, the only check required to verify a valid solution
1662 // is to ensure that completed shifted cluster lies completely within the cell layout.
1663 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1664 clusterRect.bottom <= mCountY) {
1665 foundSolution = true;
1666 } else {
1667 currentState.restore();
1668 }
1669
1670 // In either case, we set the occupied array as marked for the location of the views
1671 for (View v: cluster.views) {
1672 CellAndSpan c = currentState.map.get(v);
1673 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1674 }
1675
1676 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001677 }
1678
Adam Cohen482ed822012-03-02 14:15:13 -08001679 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001680 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001681 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001682
Adam Cohen8baab352012-03-20 17:39:21 -07001683 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001684 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001685 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001686 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001687 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001688 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001689 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001690 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001691 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001692 }
1693 }
Adam Cohen8baab352012-03-20 17:39:21 -07001694
Adam Cohen8baab352012-03-20 17:39:21 -07001695 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001696 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001697 CellAndSpan c = currentState.map.get(v);
1698 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1699 }
1700
Adam Cohen47a876d2012-03-19 13:21:41 -07001701 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1702 int top = boundingRect.top;
1703 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001704 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001705 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001706 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001707 CellAndSpan c = currentState.map.get(v);
1708 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001709 }
1710
Adam Cohen482ed822012-03-02 14:15:13 -08001711 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1712
Adam Cohenf3900c22012-11-16 18:28:11 -08001713 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1714 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001715
Adam Cohen8baab352012-03-20 17:39:21 -07001716 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001717 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001718 int deltaX = mTempLocation[0] - boundingRect.left;
1719 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001720 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001721 CellAndSpan c = currentState.map.get(v);
1722 c.x += deltaX;
1723 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001724 }
1725 success = true;
1726 }
Adam Cohen8baab352012-03-20 17:39:21 -07001727
1728 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001729 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001730 CellAndSpan c = currentState.map.get(v);
1731 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001732 }
1733 return success;
1734 }
1735
1736 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1737 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1738 }
1739
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001740 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1741 // to push items in each of the cardinal directions, in an order based on the direction vector
1742 // passed.
1743 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1744 int[] direction, View ignoreView, ItemConfiguration solution) {
1745 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001746 // If the direction vector has two non-zero components, we try pushing
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001747 // separately in each of the components.
1748 int temp = direction[1];
1749 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001750
1751 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001752 ignoreView, solution)) {
1753 return true;
1754 }
1755 direction[1] = temp;
1756 temp = direction[0];
1757 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001758
1759 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001760 ignoreView, solution)) {
1761 return true;
1762 }
1763 // Revert the direction
1764 direction[0] = temp;
1765
1766 // Now we try pushing in each component of the opposite direction
1767 direction[0] *= -1;
1768 direction[1] *= -1;
1769 temp = direction[1];
1770 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001771 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001772 ignoreView, solution)) {
1773 return true;
1774 }
1775
1776 direction[1] = temp;
1777 temp = direction[0];
1778 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08001779 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001780 ignoreView, solution)) {
1781 return true;
1782 }
1783 // revert the direction
1784 direction[0] = temp;
1785 direction[0] *= -1;
1786 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001787
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001788 } else {
1789 // If the direction vector has a single non-zero component, we push first in the
1790 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08001791 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001792 ignoreView, solution)) {
1793 return true;
1794 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001795 // Then we try the opposite direction
1796 direction[0] *= -1;
1797 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001798 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001799 ignoreView, solution)) {
1800 return true;
1801 }
1802 // Switch the direction back
1803 direction[0] *= -1;
1804 direction[1] *= -1;
Winson Chung5f8afe62013-08-12 16:19:28 -07001805
1806 // If we have failed to find a push solution with the above, then we try
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001807 // to find a solution by pushing along the perpendicular axis.
1808
1809 // Swap the components
1810 int temp = direction[1];
1811 direction[1] = direction[0];
1812 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08001813 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001814 ignoreView, solution)) {
1815 return true;
1816 }
1817
1818 // Then we try the opposite direction
1819 direction[0] *= -1;
1820 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08001821 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001822 ignoreView, solution)) {
1823 return true;
1824 }
1825 // Switch the direction back
1826 direction[0] *= -1;
1827 direction[1] *= -1;
1828
1829 // Swap the components back
1830 temp = direction[1];
1831 direction[1] = direction[0];
1832 direction[0] = temp;
1833 }
1834 return false;
1835 }
1836
Adam Cohen482ed822012-03-02 14:15:13 -08001837 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001838 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001839 // Return early if get invalid cell positions
1840 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001841
Adam Cohen8baab352012-03-20 17:39:21 -07001842 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001843 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001844
Adam Cohen8baab352012-03-20 17:39:21 -07001845 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001846 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001847 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001848 if (c != null) {
1849 c.x = cellX;
1850 c.y = cellY;
1851 }
Adam Cohen482ed822012-03-02 14:15:13 -08001852 }
Adam Cohen482ed822012-03-02 14:15:13 -08001853 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1854 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001855 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001856 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001857 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001858 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001859 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001860 if (Rect.intersects(r0, r1)) {
1861 if (!lp.canReorder) {
1862 return false;
1863 }
1864 mIntersectingViews.add(child);
1865 }
1866 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001867
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001868 solution.intersectingViews = new ArrayList<View>(mIntersectingViews);
1869
Winson Chung5f8afe62013-08-12 16:19:28 -07001870 // First we try to find a solution which respects the push mechanic. That is,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001871 // we try to find a solution such that no displaced item travels through another item
1872 // without also displacing that item.
1873 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001874 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001875 return true;
1876 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001877
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001878 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08001879 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001880 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001881 return true;
1882 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001883
Adam Cohen482ed822012-03-02 14:15:13 -08001884 // Ok, they couldn't move as a block, let's move them individually
1885 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001886 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001887 return false;
1888 }
1889 }
1890 return true;
1891 }
1892
1893 /*
1894 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1895 * the provided point and the provided cell
1896 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001897 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001898 double angle = Math.atan(((float) deltaY) / deltaX);
1899
1900 result[0] = 0;
1901 result[1] = 0;
1902 if (Math.abs(Math.cos(angle)) > 0.5f) {
1903 result[0] = (int) Math.signum(deltaX);
1904 }
1905 if (Math.abs(Math.sin(angle)) > 0.5f) {
1906 result[1] = (int) Math.signum(deltaY);
1907 }
1908 }
1909
Adam Cohen8baab352012-03-20 17:39:21 -07001910 private void copyOccupiedArray(boolean[][] occupied) {
1911 for (int i = 0; i < mCountX; i++) {
1912 for (int j = 0; j < mCountY; j++) {
1913 occupied[i][j] = mOccupied[i][j];
1914 }
1915 }
1916 }
1917
Sunny Goyalf7a29e82015-04-24 15:20:43 -07001918 private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001919 int spanX, int spanY, int[] direction, View dragView, boolean decX,
1920 ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001921 // Copy the current state into the solution. This solution will be manipulated as necessary.
1922 copyCurrentStateToSolution(solution, false);
1923 // Copy the current occupied array into the temporary occupied array. This array will be
1924 // manipulated as necessary to find a solution.
1925 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001926
1927 // We find the nearest cell into which we would place the dragged item, assuming there's
1928 // nothing in its way.
1929 int result[] = new int[2];
1930 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1931
1932 boolean success = false;
1933 // First we try the exact nearest position of the item being dragged,
1934 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001935 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1936 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001937
1938 if (!success) {
1939 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1940 // x, then 1 in y etc.
1941 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001942 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY,
1943 direction, dragView, false, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001944 } else if (spanY > minSpanY) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08001945 return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1,
1946 direction, dragView, true, solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001947 }
1948 solution.isSolution = false;
1949 } else {
1950 solution.isSolution = true;
1951 solution.dragViewX = result[0];
1952 solution.dragViewY = result[1];
1953 solution.dragViewSpanX = spanX;
1954 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001955 }
1956 return solution;
1957 }
1958
1959 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001960 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001961 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001962 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001963 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001964 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001965 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001966 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001967 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001968 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001969 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001970 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001971 }
1972 }
1973
1974 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1975 for (int i = 0; i < mCountX; i++) {
1976 for (int j = 0; j < mCountY; j++) {
1977 mTmpOccupied[i][j] = false;
1978 }
1979 }
1980
Michael Jurkaa52570f2012-03-20 03:18:20 -07001981 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001982 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001983 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001984 if (child == dragView) continue;
1985 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001986 CellAndSpan c = solution.map.get(child);
1987 if (c != null) {
1988 lp.tmpCellX = c.x;
1989 lp.tmpCellY = c.y;
1990 lp.cellHSpan = c.spanX;
1991 lp.cellVSpan = c.spanY;
1992 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001993 }
1994 }
1995 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1996 solution.dragViewSpanY, mTmpOccupied, true);
1997 }
1998
1999 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2000 commitDragView) {
2001
2002 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2003 for (int i = 0; i < mCountX; i++) {
2004 for (int j = 0; j < mCountY; j++) {
2005 occupied[i][j] = false;
2006 }
2007 }
2008
Michael Jurkaa52570f2012-03-20 03:18:20 -07002009 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002010 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002011 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002012 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002013 CellAndSpan c = solution.map.get(child);
2014 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002015 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2016 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002017 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002018 }
2019 }
2020 if (commitDragView) {
2021 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2022 solution.dragViewSpanY, occupied, true);
2023 }
2024 }
2025
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002026
2027 // This method starts or changes the reorder preview animations
2028 private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution,
2029 View dragView, int delay, int mode) {
Adam Cohen19f37922012-03-21 11:59:11 -07002030 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002031 for (int i = 0; i < childCount; i++) {
2032 View child = mShortcutsAndWidgets.getChildAt(i);
2033 if (child == dragView) continue;
2034 CellAndSpan c = solution.map.get(child);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002035 boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews
2036 != null && !solution.intersectingViews.contains(child);
2037
Adam Cohen19f37922012-03-21 11:59:11 -07002038 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002039 if (c != null && !skip) {
2040 ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX,
2041 lp.cellY, c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002042 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002043 }
2044 }
2045 }
2046
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002047 // Class which represents the reorder preview animations. These animations show that an item is
Adam Cohen19f37922012-03-21 11:59:11 -07002048 // in a temporary state, and hint at where the item will return to.
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002049 class ReorderPreviewAnimation {
Adam Cohen19f37922012-03-21 11:59:11 -07002050 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002051 float finalDeltaX;
2052 float finalDeltaY;
2053 float initDeltaX;
2054 float initDeltaY;
2055 float finalScale;
2056 float initScale;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002057 int mode;
2058 boolean repeating = false;
2059 private static final int PREVIEW_DURATION = 300;
2060 private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT;
2061
2062 public static final int MODE_HINT = 0;
2063 public static final int MODE_PREVIEW = 1;
2064
Adam Cohene7587d22012-05-24 18:50:02 -07002065 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002066
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002067 public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
2068 int cellY1, int spanX, int spanY) {
Adam Cohen19f37922012-03-21 11:59:11 -07002069 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2070 final int x0 = mTmpPoint[0];
2071 final int y0 = mTmpPoint[1];
2072 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2073 final int x1 = mTmpPoint[0];
2074 final int y1 = mTmpPoint[1];
2075 final int dX = x1 - x0;
2076 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002077 finalDeltaX = 0;
2078 finalDeltaY = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002079 int dir = mode == MODE_HINT ? -1 : 1;
Adam Cohen19f37922012-03-21 11:59:11 -07002080 if (dX == dY && dX == 0) {
2081 } else {
2082 if (dY == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002083 finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002084 } else if (dX == 0) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002085 finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002086 } else {
2087 double angle = Math.atan( (float) (dY) / dX);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002088 finalDeltaX = (int) (- dir * Math.signum(dX) *
2089 Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
2090 finalDeltaY = (int) (- dir * Math.signum(dY) *
2091 Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002092 }
2093 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002094 this.mode = mode;
Adam Cohend024f982012-05-23 18:26:45 -07002095 initDeltaX = child.getTranslationX();
2096 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002097 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002098 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002099 this.child = child;
2100 }
2101
Adam Cohend024f982012-05-23 18:26:45 -07002102 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002103 if (mShakeAnimators.containsKey(child)) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002104 ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002105 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002106 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002107 if (finalDeltaX == 0 && finalDeltaY == 0) {
2108 completeAnimationImmediately();
2109 return;
2110 }
Adam Cohen19f37922012-03-21 11:59:11 -07002111 }
Adam Cohend024f982012-05-23 18:26:45 -07002112 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002113 return;
2114 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002115 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002116 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002117 va.setRepeatMode(ValueAnimator.REVERSE);
2118 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002119 va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002120 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002121 va.addUpdateListener(new AnimatorUpdateListener() {
2122 @Override
2123 public void onAnimationUpdate(ValueAnimator animation) {
2124 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002125 float r1 = (mode == MODE_HINT && repeating) ? 1.0f : r;
2126 float x = r1 * finalDeltaX + (1 - r1) * initDeltaX;
2127 float y = r1 * finalDeltaY + (1 - r1) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002128 child.setTranslationX(x);
2129 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002130 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002131 child.setScaleX(s);
2132 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002133 }
2134 });
2135 va.addListener(new AnimatorListenerAdapter() {
2136 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002137 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002138 initDeltaX = 0;
2139 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002140 initScale = getChildrenScale();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002141 repeating = true;
Adam Cohen19f37922012-03-21 11:59:11 -07002142 }
2143 });
Adam Cohen19f37922012-03-21 11:59:11 -07002144 mShakeAnimators.put(child, this);
2145 va.start();
2146 }
2147
Adam Cohend024f982012-05-23 18:26:45 -07002148 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002149 if (a != null) {
2150 a.cancel();
2151 }
Adam Cohen19f37922012-03-21 11:59:11 -07002152 }
Adam Cohene7587d22012-05-24 18:50:02 -07002153
Adam Cohen091440a2015-03-18 14:16:05 -07002154 @Thunk void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002155 if (a != null) {
2156 a.cancel();
2157 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002158
Michael Jurka2ecf9952012-06-18 12:52:28 -07002159 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002160 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002161 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002162 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2163 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002164 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2165 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002166 );
2167 s.setDuration(REORDER_ANIMATION_DURATION);
2168 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2169 s.start();
2170 }
Adam Cohen19f37922012-03-21 11:59:11 -07002171 }
2172
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002173 private void completeAndClearReorderPreviewAnimations() {
2174 for (ReorderPreviewAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002175 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002176 }
2177 mShakeAnimators.clear();
2178 }
2179
Adam Cohen482ed822012-03-02 14:15:13 -08002180 private void commitTempPlacement() {
2181 for (int i = 0; i < mCountX; i++) {
2182 for (int j = 0; j < mCountY; j++) {
2183 mOccupied[i][j] = mTmpOccupied[i][j];
2184 }
2185 }
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002186
2187 long screenId = mLauncher.getWorkspace().getIdForScreen(this);
2188 int container = Favorites.CONTAINER_DESKTOP;
2189
2190 if (mLauncher.isHotseatLayout(this)) {
2191 screenId = -1;
2192 container = Favorites.CONTAINER_HOTSEAT;
2193 }
2194
Michael Jurkaa52570f2012-03-20 03:18:20 -07002195 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002196 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002197 View child = mShortcutsAndWidgets.getChildAt(i);
2198 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2199 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002200 // We do a null check here because the item info can be null in the case of the
2201 // AllApps button in the hotseat.
2202 if (info != null) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002203 final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX
2204 || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan
2205 || info.spanY != lp.cellVSpan);
2206
Adam Cohen2acce882012-03-28 19:03:19 -07002207 info.cellX = lp.cellX = lp.tmpCellX;
2208 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002209 info.spanX = lp.cellHSpan;
2210 info.spanY = lp.cellVSpan;
Sunny Goyalaa8ef112015-06-12 20:04:41 -07002211
2212 if (requiresDbUpdate) {
2213 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId,
2214 info.cellX, info.cellY, info.spanX, info.spanY);
2215 }
Adam Cohen2acce882012-03-28 19:03:19 -07002216 }
Adam Cohen482ed822012-03-02 14:15:13 -08002217 }
2218 }
2219
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002220 private void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002221 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002222 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002223 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002224 lp.useTmpCoords = useTempCoords;
2225 }
2226 }
2227
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002228 private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002229 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2230 int[] result = new int[2];
2231 int[] resultSpan = new int[2];
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002232 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
Adam Cohen482ed822012-03-02 14:15:13 -08002233 resultSpan);
2234 if (result[0] >= 0 && result[1] >= 0) {
2235 copyCurrentStateToSolution(solution, false);
2236 solution.dragViewX = result[0];
2237 solution.dragViewY = result[1];
2238 solution.dragViewSpanX = resultSpan[0];
2239 solution.dragViewSpanY = resultSpan[1];
2240 solution.isSolution = true;
2241 } else {
2242 solution.isSolution = false;
2243 }
2244 return solution;
2245 }
2246
2247 public void prepareChildForDrag(View child) {
2248 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002249 }
2250
Adam Cohen19f37922012-03-21 11:59:11 -07002251 /* This seems like it should be obvious and straight-forward, but when the direction vector
2252 needs to match with the notion of the dragView pushing other views, we have to employ
2253 a slightly more subtle notion of the direction vector. The question is what two points is
2254 the vector between? The center of the dragView and its desired destination? Not quite, as
2255 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2256 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2257 or right, which helps make pushing feel right.
2258 */
2259 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2260 int spanY, View dragView, int[] resultDirection) {
2261 int[] targetDestination = new int[2];
2262
2263 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2264 Rect dragRect = new Rect();
2265 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2266 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2267
2268 Rect dropRegionRect = new Rect();
2269 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2270 dragView, dropRegionRect, mIntersectingViews);
2271
2272 int dropRegionSpanX = dropRegionRect.width();
2273 int dropRegionSpanY = dropRegionRect.height();
2274
2275 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2276 dropRegionRect.height(), dropRegionRect);
2277
2278 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2279 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2280
2281 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2282 deltaX = 0;
2283 }
2284 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2285 deltaY = 0;
2286 }
2287
2288 if (deltaX == 0 && deltaY == 0) {
2289 // No idea what to do, give a random direction.
2290 resultDirection[0] = 1;
2291 resultDirection[1] = 0;
2292 } else {
2293 computeDirectionVector(deltaX, deltaY, resultDirection);
2294 }
2295 }
2296
2297 // For a given cell and span, fetch the set of views intersecting the region.
2298 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2299 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2300 if (boundingRect != null) {
2301 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2302 }
2303 intersectingViews.clear();
2304 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2305 Rect r1 = new Rect();
2306 final int count = mShortcutsAndWidgets.getChildCount();
2307 for (int i = 0; i < count; i++) {
2308 View child = mShortcutsAndWidgets.getChildAt(i);
2309 if (child == dragView) continue;
2310 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2311 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2312 if (Rect.intersects(r0, r1)) {
2313 mIntersectingViews.add(child);
2314 if (boundingRect != null) {
2315 boundingRect.union(r1);
2316 }
2317 }
2318 }
2319 }
2320
2321 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2322 View dragView, int[] result) {
2323 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2324 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2325 mIntersectingViews);
2326 return !mIntersectingViews.isEmpty();
2327 }
2328
2329 void revertTempState() {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002330 completeAndClearReorderPreviewAnimations();
2331 if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) {
2332 final int count = mShortcutsAndWidgets.getChildCount();
2333 for (int i = 0; i < count; i++) {
2334 View child = mShortcutsAndWidgets.getChildAt(i);
2335 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2336 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2337 lp.tmpCellX = lp.cellX;
2338 lp.tmpCellY = lp.cellY;
2339 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2340 0, false, false);
2341 }
Adam Cohen19f37922012-03-21 11:59:11 -07002342 }
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002343 setItemPlacementDirty(false);
Adam Cohen19f37922012-03-21 11:59:11 -07002344 }
Adam Cohen19f37922012-03-21 11:59:11 -07002345 }
2346
Adam Cohenbebf0422012-04-11 18:06:28 -07002347 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2348 View dragView, int[] direction, boolean commit) {
2349 int[] pixelXY = new int[2];
2350 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2351
2352 // First we determine if things have moved enough to cause a different layout
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002353 ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY,
Adam Cohenbebf0422012-04-11 18:06:28 -07002354 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2355
2356 setUseTempCoords(true);
2357 if (swapSolution != null && swapSolution.isSolution) {
2358 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2359 // committing anything or animating anything as we just want to determine if a solution
2360 // exists
2361 copySolutionToTempState(swapSolution, dragView);
2362 setItemPlacementDirty(true);
2363 animateItemsToSolution(swapSolution, dragView, commit);
2364
2365 if (commit) {
2366 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002367 completeAndClearReorderPreviewAnimations();
Adam Cohenbebf0422012-04-11 18:06:28 -07002368 setItemPlacementDirty(false);
2369 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002370 beginOrAdjustReorderPreviewAnimations(swapSolution, dragView,
2371 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohenbebf0422012-04-11 18:06:28 -07002372 }
2373 mShortcutsAndWidgets.requestLayout();
2374 }
2375 return swapSolution.isSolution;
2376 }
2377
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002378 int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002379 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002380 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002381 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002382
2383 if (resultSpan == null) {
2384 resultSpan = new int[2];
2385 }
2386
Adam Cohen19f37922012-03-21 11:59:11 -07002387 // When we are checking drop validity or actually dropping, we don't recompute the
2388 // direction vector, since we want the solution to match the preview, and it's possible
2389 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002390 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2391 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002392 mDirectionVector[0] = mPreviousReorderDirection[0];
2393 mDirectionVector[1] = mPreviousReorderDirection[1];
2394 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002395 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2396 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2397 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002398 }
2399 } else {
2400 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2401 mPreviousReorderDirection[0] = mDirectionVector[0];
2402 mPreviousReorderDirection[1] = mDirectionVector[1];
2403 }
2404
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002405 // Find a solution involving pushing / displacing any items in the way
2406 ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002407 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2408
2409 // We attempt the approach which doesn't shuffle views at all
2410 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2411 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2412
2413 ItemConfiguration finalSolution = null;
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002414
2415 // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
2416 // favor a solution in which the item is not resized, but
Adam Cohen482ed822012-03-02 14:15:13 -08002417 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2418 finalSolution = swapSolution;
2419 } else if (noShuffleSolution.isSolution) {
2420 finalSolution = noShuffleSolution;
2421 }
2422
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002423 if (mode == MODE_SHOW_REORDER_HINT) {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002424 if (finalSolution != null) {
Adam Cohenfe692872013-12-11 14:47:23 -08002425 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, 0,
2426 ReorderPreviewAnimation.MODE_HINT);
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002427 result[0] = finalSolution.dragViewX;
2428 result[1] = finalSolution.dragViewY;
2429 resultSpan[0] = finalSolution.dragViewSpanX;
2430 resultSpan[1] = finalSolution.dragViewSpanY;
2431 } else {
2432 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2433 }
2434 return result;
2435 }
2436
Adam Cohen482ed822012-03-02 14:15:13 -08002437 boolean foundSolution = true;
2438 if (!DESTRUCTIVE_REORDER) {
2439 setUseTempCoords(true);
2440 }
2441
2442 if (finalSolution != null) {
2443 result[0] = finalSolution.dragViewX;
2444 result[1] = finalSolution.dragViewY;
2445 resultSpan[0] = finalSolution.dragViewSpanX;
2446 resultSpan[1] = finalSolution.dragViewSpanY;
2447
2448 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2449 // committing anything or animating anything as we just want to determine if a solution
2450 // exists
2451 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2452 if (!DESTRUCTIVE_REORDER) {
2453 copySolutionToTempState(finalSolution, dragView);
2454 }
2455 setItemPlacementDirty(true);
2456 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2457
Adam Cohen19f37922012-03-21 11:59:11 -07002458 if (!DESTRUCTIVE_REORDER &&
2459 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002460 commitTempPlacement();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002461 completeAndClearReorderPreviewAnimations();
Adam Cohen19f37922012-03-21 11:59:11 -07002462 setItemPlacementDirty(false);
2463 } else {
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002464 beginOrAdjustReorderPreviewAnimations(finalSolution, dragView,
2465 REORDER_ANIMATION_DURATION, ReorderPreviewAnimation.MODE_PREVIEW);
Adam Cohen482ed822012-03-02 14:15:13 -08002466 }
2467 }
2468 } else {
2469 foundSolution = false;
2470 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2471 }
2472
2473 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2474 setUseTempCoords(false);
2475 }
Adam Cohen482ed822012-03-02 14:15:13 -08002476
Michael Jurkaa52570f2012-03-20 03:18:20 -07002477 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002478 return result;
2479 }
2480
Adam Cohen19f37922012-03-21 11:59:11 -07002481 void setItemPlacementDirty(boolean dirty) {
2482 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002483 }
Adam Cohen19f37922012-03-21 11:59:11 -07002484 boolean isItemPlacementDirty() {
2485 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002486 }
2487
Adam Cohen091440a2015-03-18 14:16:05 -07002488 @Thunk class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002489 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002490 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2491 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002492 ArrayList<View> intersectingViews;
Adam Cohen482ed822012-03-02 14:15:13 -08002493 boolean isSolution = false;
2494 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2495
Adam Cohenf3900c22012-11-16 18:28:11 -08002496 void save() {
2497 // Copy current state into savedMap
2498 for (View v: map.keySet()) {
2499 map.get(v).copy(savedMap.get(v));
2500 }
2501 }
2502
2503 void restore() {
2504 // Restore current state from savedMap
2505 for (View v: savedMap.keySet()) {
2506 savedMap.get(v).copy(map.get(v));
2507 }
2508 }
2509
2510 void add(View v, CellAndSpan cs) {
2511 map.put(v, cs);
2512 savedMap.put(v, new CellAndSpan());
2513 sortedViews.add(v);
2514 }
2515
Adam Cohen482ed822012-03-02 14:15:13 -08002516 int area() {
2517 return dragViewSpanX * dragViewSpanY;
2518 }
Adam Cohen8baab352012-03-20 17:39:21 -07002519 }
2520
2521 private class CellAndSpan {
2522 int x, y;
2523 int spanX, spanY;
2524
Adam Cohenf3900c22012-11-16 18:28:11 -08002525 public CellAndSpan() {
2526 }
2527
2528 public void copy(CellAndSpan copy) {
2529 copy.x = x;
2530 copy.y = y;
2531 copy.spanX = spanX;
2532 copy.spanY = spanY;
2533 }
2534
Adam Cohen8baab352012-03-20 17:39:21 -07002535 public CellAndSpan(int x, int y, int spanX, int spanY) {
2536 this.x = x;
2537 this.y = y;
2538 this.spanX = spanX;
2539 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002540 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002541
2542 public String toString() {
2543 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2544 }
2545
Adam Cohen482ed822012-03-02 14:15:13 -08002546 }
2547
Adam Cohendf035382011-04-11 17:22:04 -07002548 /**
Adam Cohendf035382011-04-11 17:22:04 -07002549 * Find a starting cell position that will fit the given bounds nearest the requested
2550 * cell location. Uses Euclidean distance to score multiple vacant areas.
2551 *
2552 * @param pixelX The X location at which you want to search for a vacant area.
2553 * @param pixelY The Y location at which you want to search for a vacant area.
2554 * @param spanX Horizontal span of the object.
2555 * @param spanY Vertical span of the object.
2556 * @param ignoreView Considers space occupied by this view as unoccupied
2557 * @param result Previously returned value to possibly recycle.
2558 * @return The X, Y cell of a vacant area that can contain this object,
2559 * nearest the requested location.
2560 */
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002561 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2562 return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null);
Adam Cohendf035382011-04-11 17:22:04 -07002563 }
2564
Michael Jurka0280c3b2010-09-17 15:00:07 -07002565 boolean existsEmptyCell() {
2566 return findCellForSpan(null, 1, 1);
2567 }
2568
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002569 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002570 * Finds the upper-left coordinate of the first rectangle in the grid that can
2571 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2572 * then this method will only return coordinates for rectangles that contain the cell
2573 * (intersectX, intersectY)
2574 *
2575 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2576 * can be found.
2577 * @param spanX The horizontal span of the cell we want to find.
2578 * @param spanY The vertical span of the cell we want to find.
2579 *
2580 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002581 */
Hyunyoung Song3f471442015-04-08 19:01:34 -07002582 public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Michael Jurka28750fb2010-09-24 17:43:49 -07002583 boolean foundCell = false;
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002584 final int endX = mCountX - (spanX - 1);
2585 final int endY = mCountY - (spanY - 1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002586
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002587 for (int y = 0; y < endY && !foundCell; y++) {
2588 inner:
2589 for (int x = 0; x < endX; x++) {
2590 for (int i = 0; i < spanX; i++) {
2591 for (int j = 0; j < spanY; j++) {
2592 if (mOccupied[x + i][y + j]) {
2593 // small optimization: we can skip to after the column we just found
2594 // an occupied cell
2595 x += i;
2596 continue inner;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002597 }
2598 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002599 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002600 if (cellXY != null) {
2601 cellXY[0] = x;
2602 cellXY[1] = y;
2603 }
2604 foundCell = true;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002605 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002606 }
2607 }
2608
Michael Jurka28750fb2010-09-24 17:43:49 -07002609 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002610 }
2611
2612 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002613 * A drag event has begun over this layout.
2614 * It may have begun over this layout (in which case onDragChild is called first),
2615 * or it may have begun on another layout.
2616 */
2617 void onDragEnter() {
Winson Chungc07918d2011-07-01 15:35:26 -07002618 mDragging = true;
2619 }
2620
2621 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002622 * Called when drag has left this CellLayout or has been completed (successfully or not)
2623 */
2624 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002625 // This can actually be called when we aren't in a drag, e.g. when adding a new
2626 // item to this layout via the customize drawer.
2627 // Guard against that case.
2628 if (mDragging) {
2629 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002630 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002631
2632 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002633 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002634 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2635 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002636 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002637 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002638 }
2639
2640 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002641 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002642 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002643 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002644 *
2645 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002646 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002647 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002648 if (child != null) {
2649 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002650 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002651 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002652 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002653 }
2654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002655 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002656 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002657 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658 * @param cellX X coordinate of upper left corner expressed as a cell position
2659 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002660 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002661 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002662 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002663 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002664 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002665 final int cellWidth = mCellWidth;
2666 final int cellHeight = mCellHeight;
2667 final int widthGap = mWidthGap;
2668 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002669
Winson Chung4b825dcd2011-06-19 12:41:22 -07002670 final int hStartPadding = getPaddingLeft();
2671 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002672
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002673 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2674 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2675
2676 int x = hStartPadding + cellX * (cellWidth + widthGap);
2677 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002678
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002679 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002680 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002682 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002683 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002684 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002685 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002686 * @param width Width in pixels
2687 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002688 * @param result An array of length 2 in which to store the result (may be null).
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002689 */
Adam Cohen2e6da152015-05-06 11:42:25 -07002690 public static int[] rectToCell(Launcher launcher, int width, int height, int[] result) {
Adam Cohen2e6da152015-05-06 11:42:25 -07002691 DeviceProfile grid = launcher.getDeviceProfile();
Sunny Goyalc6205602015-05-21 20:46:33 -07002692 Rect padding = grid.getWorkspacePadding(Utilities.isRtl(launcher.getResources()));
Winson Chung5f8afe62013-08-12 16:19:28 -07002693
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002694 // Always assume we're working with the smallest span to make sure we
2695 // reserve enough space in both orientations.
Sunny Goyalc6205602015-05-21 20:46:33 -07002696 int parentWidth = DeviceProfile.calculateCellWidth(grid.widthPx
Adam Cohen2e6da152015-05-06 11:42:25 -07002697 - padding.left - padding.right, (int) grid.inv.numColumns);
Sunny Goyalc6205602015-05-21 20:46:33 -07002698 int parentHeight = DeviceProfile.calculateCellHeight(grid.heightPx
Adam Cohen2e6da152015-05-06 11:42:25 -07002699 - padding.top - padding.bottom, (int) grid.inv.numRows);
Winson Chung66700732013-08-20 16:56:15 -07002700 int smallerSize = Math.min(parentWidth, parentHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002701
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002702 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002703 int spanX = (int) Math.ceil(width / (float) smallerSize);
2704 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002705
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002706 if (result == null) {
2707 return new int[] { spanX, spanY };
2708 }
2709 result[0] = spanX;
2710 result[1] = spanY;
2711 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002712 }
2713
2714 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002715 * Calculate the grid spans needed to fit given item
2716 */
2717 public void calculateSpans(ItemInfo info) {
2718 final int minWidth;
2719 final int minHeight;
2720
2721 if (info instanceof LauncherAppWidgetInfo) {
2722 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2723 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2724 } else if (info instanceof PendingAddWidgetInfo) {
2725 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2726 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2727 } else {
2728 // It's not a widget, so it must be 1x1
2729 info.spanX = info.spanY = 1;
2730 return;
2731 }
Adam Cohen2e6da152015-05-06 11:42:25 -07002732 int[] spans = rectToCell(mLauncher, minWidth, minHeight, null);
Patrick Dubroy047379a2010-12-19 22:02:04 -08002733 info.spanX = spans[0];
2734 info.spanY = spans[1];
2735 }
2736
Michael Jurka0280c3b2010-09-17 15:00:07 -07002737 private void clearOccupiedCells() {
2738 for (int x = 0; x < mCountX; x++) {
2739 for (int y = 0; y < mCountY; y++) {
2740 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002741 }
2742 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002743 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002744
Adam Cohend4844c32011-02-18 19:25:06 -08002745 public void markCellsAsOccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002746 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002747 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002748 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002749 }
2750
Adam Cohend4844c32011-02-18 19:25:06 -08002751 public void markCellsAsUnoccupiedForView(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002752 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002753 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Sunny Goyalf7a29e82015-04-24 15:20:43 -07002754 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, mOccupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002755 }
2756
Adam Cohen482ed822012-03-02 14:15:13 -08002757 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2758 boolean value) {
2759 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002760 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2761 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002762 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763 }
2764 }
2765 }
2766
Adam Cohen2801caf2011-05-13 20:57:39 -07002767 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002768 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002769 (Math.max((mCountX - 1), 0) * mWidthGap);
2770 }
2771
2772 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002773 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002774 (Math.max((mCountY - 1), 0) * mHeightGap);
2775 }
2776
Michael Jurka66d72172011-04-12 16:29:25 -07002777 public boolean isOccupied(int x, int y) {
2778 if (x < mCountX && y < mCountY) {
2779 return mOccupied[x][y];
2780 } else {
2781 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2782 }
2783 }
2784
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002785 @Override
2786 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2787 return new CellLayout.LayoutParams(getContext(), attrs);
2788 }
2789
2790 @Override
2791 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2792 return p instanceof CellLayout.LayoutParams;
2793 }
2794
2795 @Override
2796 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2797 return new CellLayout.LayoutParams(p);
2798 }
2799
2800 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2801 /**
2802 * Horizontal location of the item in the grid.
2803 */
2804 @ViewDebug.ExportedProperty
2805 public int cellX;
2806
2807 /**
2808 * Vertical location of the item in the grid.
2809 */
2810 @ViewDebug.ExportedProperty
2811 public int cellY;
2812
2813 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002814 * Temporary horizontal location of the item in the grid during reorder
2815 */
2816 public int tmpCellX;
2817
2818 /**
2819 * Temporary vertical location of the item in the grid during reorder
2820 */
2821 public int tmpCellY;
2822
2823 /**
2824 * Indicates that the temporary coordinates should be used to layout the items
2825 */
2826 public boolean useTmpCoords;
2827
2828 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002829 * Number of cells spanned horizontally by the item.
2830 */
2831 @ViewDebug.ExportedProperty
2832 public int cellHSpan;
2833
2834 /**
2835 * Number of cells spanned vertically by the item.
2836 */
2837 @ViewDebug.ExportedProperty
2838 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002839
Adam Cohen1b607ed2011-03-03 17:26:50 -08002840 /**
2841 * Indicates whether the item will set its x, y, width and height parameters freely,
2842 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2843 */
Adam Cohend4844c32011-02-18 19:25:06 -08002844 public boolean isLockedToGrid = true;
2845
Adam Cohen482ed822012-03-02 14:15:13 -08002846 /**
Adam Cohenec40b2b2013-07-23 15:52:40 -07002847 * Indicates that this item should use the full extents of its parent.
2848 */
2849 public boolean isFullscreen = false;
2850
2851 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002852 * Indicates whether this item can be reordered. Always true except in the case of the
2853 * the AllApps button.
2854 */
2855 public boolean canReorder = true;
2856
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857 // X coordinate of the view in the layout.
2858 @ViewDebug.ExportedProperty
2859 int x;
2860 // Y coordinate of the view in the layout.
2861 @ViewDebug.ExportedProperty
2862 int y;
2863
Romain Guy84f296c2009-11-04 15:00:44 -08002864 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002865
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002866 public LayoutParams(Context c, AttributeSet attrs) {
2867 super(c, attrs);
2868 cellHSpan = 1;
2869 cellVSpan = 1;
2870 }
2871
2872 public LayoutParams(ViewGroup.LayoutParams source) {
2873 super(source);
2874 cellHSpan = 1;
2875 cellVSpan = 1;
2876 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002877
2878 public LayoutParams(LayoutParams source) {
2879 super(source);
2880 this.cellX = source.cellX;
2881 this.cellY = source.cellY;
2882 this.cellHSpan = source.cellHSpan;
2883 this.cellVSpan = source.cellVSpan;
2884 }
2885
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002887 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002888 this.cellX = cellX;
2889 this.cellY = cellY;
2890 this.cellHSpan = cellHSpan;
2891 this.cellVSpan = cellVSpan;
2892 }
2893
Adam Cohen2374abf2013-04-16 14:56:57 -07002894 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
2895 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08002896 if (isLockedToGrid) {
2897 final int myCellHSpan = cellHSpan;
2898 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07002899 int myCellX = useTmpCoords ? tmpCellX : cellX;
2900 int myCellY = useTmpCoords ? tmpCellY : cellY;
2901
2902 if (invertHorizontally) {
2903 myCellX = colCount - myCellX - cellHSpan;
2904 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08002905
Adam Cohend4844c32011-02-18 19:25:06 -08002906 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2907 leftMargin - rightMargin;
2908 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2909 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002910 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2911 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002912 }
2913 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002914
Winson Chungaafa03c2010-06-11 17:34:16 -07002915 public String toString() {
2916 return "(" + this.cellX + ", " + this.cellY + ")";
2917 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002918
2919 public void setWidth(int width) {
2920 this.width = width;
2921 }
2922
2923 public int getWidth() {
2924 return width;
2925 }
2926
2927 public void setHeight(int height) {
2928 this.height = height;
2929 }
2930
2931 public int getHeight() {
2932 return height;
2933 }
2934
2935 public void setX(int x) {
2936 this.x = x;
2937 }
2938
2939 public int getX() {
2940 return x;
2941 }
2942
2943 public void setY(int y) {
2944 this.y = y;
2945 }
2946
2947 public int getY() {
2948 return y;
2949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 }
2951
Michael Jurka0280c3b2010-09-17 15:00:07 -07002952 // This class stores info for two purposes:
2953 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2954 // its spanX, spanY, and the screen it is on
2955 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2956 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2957 // the CellLayout that was long clicked
Sunny Goyal83a8f042015-05-19 12:52:12 -07002958 public static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002960 int cellX = -1;
2961 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 int spanX;
2963 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07002964 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002965 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966
Sunny Goyal83a8f042015-05-19 12:52:12 -07002967 public CellInfo(View v, ItemInfo info) {
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002968 cell = v;
2969 cellX = info.cellX;
2970 cellY = info.cellY;
2971 spanX = info.spanX;
2972 spanY = info.spanY;
2973 screenId = info.screenId;
2974 container = info.container;
2975 }
2976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 @Override
2978 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002979 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2980 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002981 }
2982 }
Michael Jurkad771c962011-08-09 15:00:48 -07002983
Sunny Goyala9116722015-04-29 13:55:58 -07002984 public boolean findVacantCell(int spanX, int spanY, int[] outXY) {
2985 return Utilities.findVacantCell(outXY, spanX, spanY, mCountX, mCountY, mOccupied);
2986 }
Sunny Goyal9ca9c132015-04-29 14:57:22 -07002987
2988 public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
2989 int x2 = x + spanX - 1;
2990 int y2 = y + spanY - 1;
2991 if (x < 0 || y < 0 || x2 >= mCountX || y2 >= mCountY) {
2992 return false;
2993 }
2994 for (int i = x; i <= x2; i++) {
2995 for (int j = y; j <= y2; j++) {
2996 if (mOccupied[i][j]) {
2997 return false;
2998 }
2999 }
3000 }
3001
3002 return true;
3003 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003004}