The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Sunny Goyal | f0b6db7 | 2018-08-13 16:10:14 -0700 | [diff] [blame] | 19 | import static com.android.launcher3.anim.Interpolators.DEACCEL_1_5; |
| 20 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 21 | import android.animation.Animator; |
Michael Jurka | 629758f | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 22 | import android.animation.AnimatorListenerAdapter; |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 23 | import android.animation.ObjectAnimator; |
Chet Haase | 00397b1 | 2010-10-07 11:13:10 -0700 | [diff] [blame] | 24 | import android.animation.TimeInterpolator; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 25 | import android.animation.ValueAnimator; |
| 26 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 27 | import android.annotation.SuppressLint; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 28 | import android.content.Context; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 29 | import android.content.res.Resources; |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 30 | import android.content.res.TypedArray; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 31 | import android.graphics.Bitmap; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 32 | import android.graphics.Canvas; |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 33 | import android.graphics.Color; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 34 | import android.graphics.Paint; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 35 | import android.graphics.Point; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | import android.graphics.Rect; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 37 | import android.graphics.drawable.ColorDrawable; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 38 | import android.graphics.drawable.Drawable; |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 39 | import android.os.Parcelable; |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 40 | import android.util.ArrayMap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | import android.util.AttributeSet; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 42 | import android.util.Log; |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 43 | import android.util.Property; |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 44 | import android.util.SparseArray; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 45 | import android.view.MotionEvent; |
| 46 | import android.view.View; |
| 47 | import android.view.ViewDebug; |
| 48 | import android.view.ViewGroup; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 49 | import android.view.accessibility.AccessibilityEvent; |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 50 | |
vadimt | 04f356f | 2019-02-14 18:46:36 -0800 | [diff] [blame] | 51 | import androidx.annotation.IntDef; |
| 52 | import androidx.core.view.ViewCompat; |
| 53 | |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 54 | import com.android.launcher3.LauncherSettings.Favorites; |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 55 | import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate; |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 56 | import com.android.launcher3.anim.Interpolators; |
Sunny Goyal | 9e76f68 | 2017-02-13 12:13:43 -0800 | [diff] [blame] | 57 | import com.android.launcher3.anim.PropertyListBuilder; |
Sunny Goyal | 3d706ad | 2017-03-06 16:56:39 -0800 | [diff] [blame] | 58 | import com.android.launcher3.config.FeatureFlags; |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 59 | import com.android.launcher3.dragndrop.DraggableView; |
Jon Miranda | a0233f7 | 2017-06-22 18:34:45 -0700 | [diff] [blame] | 60 | import com.android.launcher3.folder.PreviewBackground; |
Sunny Goyal | 06e21a2 | 2016-08-11 16:02:02 -0700 | [diff] [blame] | 61 | import com.android.launcher3.graphics.DragPreviewProvider; |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 62 | import com.android.launcher3.graphics.RotationMode; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 63 | import com.android.launcher3.util.CellAndSpan; |
| 64 | import com.android.launcher3.util.GridOccupancy; |
Sunny Goyal | e2fd14b | 2015-08-27 17:45:46 -0700 | [diff] [blame] | 65 | import com.android.launcher3.util.ParcelableSparseArray; |
Sunny Goyal | 9b29ca5 | 2017-02-17 10:39:44 -0800 | [diff] [blame] | 66 | import com.android.launcher3.util.Themes; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 67 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 68 | import com.android.launcher3.views.ActivityContext; |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 69 | import com.android.launcher3.views.Transposable; |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 70 | import com.android.launcher3.widget.LauncherAppWidgetHostView; |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 71 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 72 | import java.lang.annotation.Retention; |
| 73 | import java.lang.annotation.RetentionPolicy; |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 74 | import java.util.ArrayList; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 75 | import java.util.Arrays; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 76 | import java.util.Collections; |
| 77 | import java.util.Comparator; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 78 | import java.util.Stack; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 79 | |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 80 | public class CellLayout extends ViewGroup implements Transposable { |
Tony Wickham | a0628cc | 2015-10-14 15:23:04 -0700 | [diff] [blame] | 81 | private static final String TAG = "CellLayout"; |
| 82 | private static final boolean LOGD = false; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 83 | |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 84 | protected final ActivityContext mActivity; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 85 | @ViewDebug.ExportedProperty(category = "launcher") |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 86 | @Thunk int mCellWidth; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 87 | @ViewDebug.ExportedProperty(category = "launcher") |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 88 | @Thunk int mCellHeight; |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 89 | private int mFixedCellWidth; |
| 90 | private int mFixedCellHeight; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 91 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 92 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 93 | private int mCountX; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 94 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 95 | private int mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 96 | |
Adam Cohen | 917e388 | 2013-10-31 15:03:35 -0700 | [diff] [blame] | 97 | private boolean mDropPending = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 98 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 99 | // These are temporary variables to prevent having to allocate a new object just to |
| 100 | // return an (x, y) value from helper functions. Do NOT use them to maintain other state. |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 101 | @Thunk final int[] mTmpPoint = new int[2]; |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 102 | @Thunk final int[] mTempLocation = new int[2]; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 103 | |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 104 | // Used to visualize / debug the Grid of the CellLayout |
| 105 | private static final boolean VISUALIZE_GRID = false; |
| 106 | private Rect mVisualizeGridRect = new Rect(); |
| 107 | private Paint mVisualizeGridPaint = new Paint(); |
| 108 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 109 | private GridOccupancy mOccupied; |
| 110 | private GridOccupancy mTmpOccupied; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 111 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 112 | private OnTouchListener mInterceptTouchListener; |
| 113 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 114 | private final ArrayList<DelegatedCellDrawing> mDelegatedCellDrawings = new ArrayList<>(); |
Jon Miranda | a0233f7 | 2017-06-22 18:34:45 -0700 | [diff] [blame] | 115 | final PreviewBackground mFolderLeaveBehind = new PreviewBackground(); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 116 | |
Sunny Goyal | f5440cb | 2016-12-14 15:13:00 -0800 | [diff] [blame] | 117 | private static final int[] BACKGROUND_STATE_ACTIVE = new int[] { android.R.attr.state_active }; |
Sunny Goyal | e15e2a8 | 2017-12-15 13:05:42 -0800 | [diff] [blame] | 118 | private static final int[] BACKGROUND_STATE_DEFAULT = EMPTY_STATE_SET; |
Sunny Goyal | f5440cb | 2016-12-14 15:13:00 -0800 | [diff] [blame] | 119 | private final Drawable mBackground; |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 120 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 121 | // These values allow a fixed measurement to be set on the CellLayout. |
| 122 | private int mFixedWidth = -1; |
| 123 | private int mFixedHeight = -1; |
| 124 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 125 | // If we're actively dragging something over this screen, mIsDragOverlapping is true |
| 126 | private boolean mIsDragOverlapping = false; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 127 | |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 128 | // These arrays are used to implement the drag visualization on x-large screens. |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 129 | // They are used as circular arrays, indexed by mDragOutlineCurrent. |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 130 | @Thunk final Rect[] mDragOutlines = new Rect[4]; |
| 131 | @Thunk final float[] mDragOutlineAlphas = new float[mDragOutlines.length]; |
| 132 | private final InterruptibleInOutAnimator[] mDragOutlineAnims = |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 133 | new InterruptibleInOutAnimator[mDragOutlines.length]; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 134 | |
| 135 | // Used as an index into the above 3 arrays; indicates which is the most current value. |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 136 | private int mDragOutlineCurrent = 0; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 137 | private final Paint mDragOutlinePaint = new Paint(); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 138 | |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 139 | @Thunk final ArrayMap<LayoutParams, Animator> mReorderAnimators = new ArrayMap<>(); |
| 140 | @Thunk final ArrayMap<View, ReorderPreviewAnimation> mShakeAnimators = new ArrayMap<>(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 141 | |
| 142 | private boolean mItemPlacementDirty = false; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 143 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 144 | // When a drag operation is in progress, holds the nearest cell to the touch point |
| 145 | private final int[] mDragCell = new int[2]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 146 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 147 | private boolean mDragging = false; |
| 148 | |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 149 | private final TimeInterpolator mEaseOutInterpolator; |
| 150 | private final ShortcutAndWidgetContainer mShortcutsAndWidgets; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 151 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 152 | @Retention(RetentionPolicy.SOURCE) |
| 153 | @IntDef({WORKSPACE, HOTSEAT, FOLDER}) |
| 154 | public @interface ContainerType{} |
| 155 | public static final int WORKSPACE = 0; |
| 156 | public static final int HOTSEAT = 1; |
| 157 | public static final int FOLDER = 2; |
| 158 | |
| 159 | @ContainerType private final int mContainerType; |
| 160 | |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 161 | private final float mChildScale = 1f; |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 162 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 163 | public static final int MODE_SHOW_REORDER_HINT = 0; |
| 164 | public static final int MODE_DRAG_OVER = 1; |
| 165 | public static final int MODE_ON_DROP = 2; |
| 166 | public static final int MODE_ON_DROP_EXTERNAL = 3; |
| 167 | public static final int MODE_ACCEPT_DROP = 4; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 168 | private static final boolean DESTRUCTIVE_REORDER = false; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 169 | private static final boolean DEBUG_VISUALIZE_OCCUPIED = false; |
| 170 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 171 | private static final float REORDER_PREVIEW_MAGNITUDE = 0.12f; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 172 | private static final int REORDER_ANIMATION_DURATION = 150; |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 173 | @Thunk final float mReorderPreviewAnimationMagnitude; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 174 | |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 175 | private final ArrayList<View> mIntersectingViews = new ArrayList<>(); |
| 176 | private final Rect mOccupiedRect = new Rect(); |
| 177 | private final int[] mDirectionVector = new int[2]; |
| 178 | final int[] mPreviousReorderDirection = new int[2]; |
Adam Cohen | b209e63 | 2012-03-27 17:09:36 -0700 | [diff] [blame] | 179 | private static final int INVALID_DIRECTION = -100; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 180 | |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 181 | private final Rect mTempRect = new Rect(); |
Winson Chung | 3a6e7f3 | 2013-10-09 15:50:52 -0700 | [diff] [blame] | 182 | |
Sunny Goyal | 73b5a27 | 2019-12-09 14:55:56 -0800 | [diff] [blame] | 183 | private static final Paint sPaint = new Paint(); |
Romain Guy | 8a0bff5 | 2012-05-06 13:14:33 -0700 | [diff] [blame] | 184 | |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 185 | // Related to accessible drag and drop |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 186 | private boolean mUseTouchHelper = false; |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 187 | private RotationMode mRotationMode = RotationMode.NORMAL; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 188 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 189 | public CellLayout(Context context) { |
| 190 | this(context, null); |
| 191 | } |
| 192 | |
| 193 | public CellLayout(Context context, AttributeSet attrs) { |
| 194 | this(context, attrs, 0); |
| 195 | } |
| 196 | |
| 197 | public CellLayout(Context context, AttributeSet attrs, int defStyle) { |
| 198 | super(context, attrs, defStyle); |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 199 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0); |
| 200 | mContainerType = a.getInteger(R.styleable.CellLayout_containerType, WORKSPACE); |
| 201 | a.recycle(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 202 | |
| 203 | // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show |
| 204 | // the user where a dragged item will land when dropped. |
| 205 | setWillNotDraw(false); |
Romain Guy | ce3cbd1 | 2013-02-25 15:00:36 -0800 | [diff] [blame] | 206 | setClipToPadding(false); |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 207 | mActivity = ActivityContext.lookupContext(context); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 208 | |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 209 | DeviceProfile grid = mActivity.getWallpaperDeviceProfile(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 210 | |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 211 | mCellWidth = mCellHeight = -1; |
Nilesh Agrawal | 5f7099a | 2014-01-02 15:54:57 -0800 | [diff] [blame] | 212 | mFixedCellWidth = mFixedCellHeight = -1; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 213 | |
| 214 | mCountX = grid.inv.numColumns; |
| 215 | mCountY = grid.inv.numRows; |
| 216 | mOccupied = new GridOccupancy(mCountX, mCountY); |
| 217 | mTmpOccupied = new GridOccupancy(mCountX, mCountY); |
| 218 | |
Adam Cohen | 5b53f29 | 2012-03-29 14:30:35 -0700 | [diff] [blame] | 219 | mPreviousReorderDirection[0] = INVALID_DIRECTION; |
| 220 | mPreviousReorderDirection[1] = INVALID_DIRECTION; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 221 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 222 | mFolderLeaveBehind.mDelegateCellX = -1; |
| 223 | mFolderLeaveBehind.mDelegateCellY = -1; |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 224 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 225 | setAlwaysDrawnWithCacheEnabled(false); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 226 | final Resources res = getResources(); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 227 | |
Sunny Goyal | f5440cb | 2016-12-14 15:13:00 -0800 | [diff] [blame] | 228 | mBackground = res.getDrawable(R.drawable.bg_celllayout); |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 229 | mBackground.setCallback(this); |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 230 | mBackground.setAlpha(0); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 231 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 232 | mReorderPreviewAnimationMagnitude = (REORDER_PREVIEW_MAGNITUDE * grid.iconSizePx); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 233 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 234 | // Initialize the data structures used for the drag visualization. |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 235 | mEaseOutInterpolator = Interpolators.DEACCEL_2_5; // Quint ease out |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 236 | mDragCell[0] = mDragCell[1] = -1; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 237 | for (int i = 0; i < mDragOutlines.length; i++) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 238 | mDragOutlines[i] = new Rect(-1, -1, -1, -1); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 239 | } |
Mario Bertschler | 54ba601 | 2017-06-08 10:53:53 -0700 | [diff] [blame] | 240 | mDragOutlinePaint.setColor(Themes.getAttrColor(context, R.attr.workspaceTextColor)); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 241 | |
| 242 | // When dragging things around the home screens, we show a green outline of |
| 243 | // where the item will land. The outlines gradually fade out, leaving a trail |
| 244 | // behind the drag path. |
| 245 | // Set up all the animations that are used to implement this fading. |
| 246 | final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 247 | final float fromAlphaValue = 0; |
| 248 | final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 249 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 250 | Arrays.fill(mDragOutlineAlphas, fromAlphaValue); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 251 | |
| 252 | for (int i = 0; i < mDragOutlineAnims.length; i++) { |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 253 | final InterruptibleInOutAnimator anim = |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 254 | new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 255 | anim.getAnimator().setInterpolator(mEaseOutInterpolator); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 256 | final int thisIndex = i; |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 257 | anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 258 | public void onAnimationUpdate(ValueAnimator animation) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 259 | final Bitmap outline = (Bitmap)anim.getTag(); |
| 260 | |
| 261 | // If an animation is started and then stopped very quickly, we can still |
| 262 | // get spurious updates we've cleared the tag. Guard against this. |
| 263 | if (outline == null) { |
Tony Wickham | a0628cc | 2015-10-14 15:23:04 -0700 | [diff] [blame] | 264 | if (LOGD) { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 265 | Object val = animation.getAnimatedValue(); |
| 266 | Log.d(TAG, "anim " + thisIndex + " update: " + val + |
| 267 | ", isStopped " + anim.isStopped()); |
| 268 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 269 | // Try to prevent it from continuing to run |
| 270 | animation.cancel(); |
| 271 | } else { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 272 | mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue(); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 273 | CellLayout.this.invalidate(mDragOutlines[thisIndex]); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 274 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 275 | } |
| 276 | }); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 277 | // The animation holds a reference to the drag outline bitmap as long is it's |
| 278 | // running. This way the bitmap can be GCed when the animations are complete. |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 279 | anim.getAnimator().addListener(new AnimatorListenerAdapter() { |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 280 | @Override |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 281 | public void onAnimationEnd(Animator animation) { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 282 | if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 283 | anim.setTag(null); |
| 284 | } |
| 285 | } |
| 286 | }); |
| 287 | mDragOutlineAnims[i] = anim; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 288 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 289 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 290 | mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType); |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 291 | mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 292 | addView(mShortcutsAndWidgets); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Sunny Goyal | 9b18010 | 2020-03-11 10:02:29 -0700 | [diff] [blame] | 295 | |
| 296 | /** |
| 297 | * Sets or clears a delegate used for accessible drag and drop |
| 298 | */ |
| 299 | public void setDragAndDropAccessibilityDelegate(DragAndDropAccessibilityDelegate delegate) { |
| 300 | setOnClickListener(delegate); |
| 301 | setOnHoverListener(delegate); |
| 302 | ViewCompat.setAccessibilityDelegate(this, delegate); |
| 303 | |
| 304 | mUseTouchHelper = delegate != null; |
| 305 | int accessibilityFlag = mUseTouchHelper |
| 306 | ? IMPORTANT_FOR_ACCESSIBILITY_YES : IMPORTANT_FOR_ACCESSIBILITY_NO; |
| 307 | setImportantForAccessibility(accessibilityFlag); |
| 308 | getShortcutsAndWidgets().setImportantForAccessibility(accessibilityFlag); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 309 | |
| 310 | // Invalidate the accessibility hierarchy |
| 311 | if (getParent() != null) { |
| 312 | getParent().notifySubtreeAccessibilityStateChanged( |
| 313 | this, this, AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE); |
| 314 | } |
| 315 | } |
| 316 | |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 317 | public void setRotationMode(RotationMode mode) { |
| 318 | if (mRotationMode != mode) { |
| 319 | mRotationMode = mode; |
| 320 | requestLayout(); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public RotationMode getRotationMode() { |
| 326 | return mRotationMode; |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public void setPadding(int left, int top, int right, int bottom) { |
| 331 | mRotationMode.mapRect(left, top, right, bottom, mTempRect); |
| 332 | super.setPadding(mTempRect.left, mTempRect.top, mTempRect.right, mTempRect.bottom); |
| 333 | } |
| 334 | |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 335 | @Override |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 336 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 337 | if (mUseTouchHelper || |
| 338 | (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev))) { |
| 339 | return true; |
| 340 | } |
| 341 | return false; |
| 342 | } |
| 343 | |
Chris Craik | 01f2d7f | 2013-10-01 14:41:56 -0700 | [diff] [blame] | 344 | public void enableHardwareLayer(boolean hasLayer) { |
| 345 | mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint); |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 346 | } |
| 347 | |
vadimt | 04f356f | 2019-02-14 18:46:36 -0800 | [diff] [blame] | 348 | public boolean isHardwareLayerEnabled() { |
| 349 | return mShortcutsAndWidgets.getLayerType() == LAYER_TYPE_HARDWARE; |
| 350 | } |
| 351 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 352 | public void setCellDimensions(int width, int height) { |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 353 | mFixedCellWidth = mCellWidth = width; |
| 354 | mFixedCellHeight = mCellHeight = height; |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 355 | mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 358 | public void setGridSize(int x, int y) { |
| 359 | mCountX = x; |
| 360 | mCountY = y; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 361 | mOccupied = new GridOccupancy(mCountX, mCountY); |
| 362 | mTmpOccupied = new GridOccupancy(mCountX, mCountY); |
Adam Cohen | 7fbec10 | 2012-03-27 12:42:19 -0700 | [diff] [blame] | 363 | mTempRectStack.clear(); |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 364 | mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 365 | requestLayout(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Adam Cohen | 2374abf | 2013-04-16 14:56:57 -0700 | [diff] [blame] | 368 | // Set whether or not to invert the layout horizontally if the layout is in RTL mode. |
| 369 | public void setInvertIfRtl(boolean invert) { |
| 370 | mShortcutsAndWidgets.setInvertIfRtl(invert); |
| 371 | } |
| 372 | |
Adam Cohen | 917e388 | 2013-10-31 15:03:35 -0700 | [diff] [blame] | 373 | public void setDropPending(boolean pending) { |
| 374 | mDropPending = pending; |
| 375 | } |
| 376 | |
| 377 | public boolean isDropPending() { |
| 378 | return mDropPending; |
| 379 | } |
| 380 | |
Adam Cohen | c50438c | 2014-08-19 17:43:05 -0700 | [diff] [blame] | 381 | void setIsDragOverlapping(boolean isDragOverlapping) { |
| 382 | if (mIsDragOverlapping != isDragOverlapping) { |
| 383 | mIsDragOverlapping = isDragOverlapping; |
Sunny Goyal | f5440cb | 2016-12-14 15:13:00 -0800 | [diff] [blame] | 384 | mBackground.setState(mIsDragOverlapping |
| 385 | ? BACKGROUND_STATE_ACTIVE : BACKGROUND_STATE_DEFAULT); |
Adam Cohen | c50438c | 2014-08-19 17:43:05 -0700 | [diff] [blame] | 386 | invalidate(); |
| 387 | } |
| 388 | } |
| 389 | |
Sunny Goyal | e2fd14b | 2015-08-27 17:45:46 -0700 | [diff] [blame] | 390 | @Override |
| 391 | protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 392 | ParcelableSparseArray jail = getJailedArray(container); |
| 393 | super.dispatchSaveInstanceState(jail); |
| 394 | container.put(R.id.cell_layout_jail_id, jail); |
Sunny Goyal | e2fd14b | 2015-08-27 17:45:46 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | @Override |
| 398 | protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 399 | super.dispatchRestoreInstanceState(getJailedArray(container)); |
Sunny Goyal | e2fd14b | 2015-08-27 17:45:46 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 402 | /** |
| 403 | * Wrap the SparseArray in another Parcelable so that the item ids do not conflict with our |
| 404 | * our internal resource ids |
| 405 | */ |
Sunny Goyal | e2fd14b | 2015-08-27 17:45:46 -0700 | [diff] [blame] | 406 | private ParcelableSparseArray getJailedArray(SparseArray<Parcelable> container) { |
| 407 | final Parcelable parcelable = container.get(R.id.cell_layout_jail_id); |
| 408 | return parcelable instanceof ParcelableSparseArray ? |
| 409 | (ParcelableSparseArray) parcelable : new ParcelableSparseArray(); |
| 410 | } |
| 411 | |
Tony Wickham | 0f97b78 | 2015-12-02 17:55:07 -0800 | [diff] [blame] | 412 | public boolean getIsDragOverlapping() { |
| 413 | return mIsDragOverlapping; |
| 414 | } |
| 415 | |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 416 | @Override |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 417 | protected void onDraw(Canvas canvas) { |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 418 | // When we're large, we are either drawn in a "hover" state (ie when dragging an item to |
| 419 | // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f) |
| 420 | // When we're small, we are either drawn normally or in the "accepts drops" state (during |
| 421 | // a drag). However, we also drag the mini hover background *over* one of those two |
| 422 | // backgrounds |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 423 | if (mBackground.getAlpha() > 0) { |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 424 | mBackground.draw(canvas); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 425 | } |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 426 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 427 | final Paint paint = mDragOutlinePaint; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 428 | for (int i = 0; i < mDragOutlines.length; i++) { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 429 | final float alpha = mDragOutlineAlphas[i]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 430 | if (alpha > 0) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 431 | final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag(); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 432 | paint.setAlpha((int)(alpha + .5f)); |
Sunny Goyal | 106bf64 | 2015-07-16 12:18:06 -0700 | [diff] [blame] | 433 | canvas.drawBitmap(b, null, mDragOutlines[i], paint); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 434 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 435 | } |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 436 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 437 | if (DEBUG_VISUALIZE_OCCUPIED) { |
| 438 | int[] pt = new int[2]; |
| 439 | ColorDrawable cd = new ColorDrawable(Color.RED); |
Adam Cohen | e7587d2 | 2012-05-24 18:50:02 -0700 | [diff] [blame] | 440 | cd.setBounds(0, 0, mCellWidth, mCellHeight); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 441 | for (int i = 0; i < mCountX; i++) { |
| 442 | for (int j = 0; j < mCountY; j++) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 443 | if (mOccupied.cells[i][j]) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 444 | cellToPoint(i, j, pt); |
| 445 | canvas.save(); |
| 446 | canvas.translate(pt[0], pt[1]); |
| 447 | cd.draw(canvas); |
| 448 | canvas.restore(); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 454 | for (int i = 0; i < mDelegatedCellDrawings.size(); i++) { |
| 455 | DelegatedCellDrawing cellDrawing = mDelegatedCellDrawings.get(i); |
| 456 | cellToPoint(cellDrawing.mDelegateCellX, cellDrawing.mDelegateCellY, mTempLocation); |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 457 | canvas.save(); |
| 458 | canvas.translate(mTempLocation[0], mTempLocation[1]); |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 459 | cellDrawing.drawUnderItem(canvas); |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 460 | canvas.restore(); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 461 | } |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 462 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 463 | if (mFolderLeaveBehind.mDelegateCellX >= 0 && mFolderLeaveBehind.mDelegateCellY >= 0) { |
| 464 | cellToPoint(mFolderLeaveBehind.mDelegateCellX, |
| 465 | mFolderLeaveBehind.mDelegateCellY, mTempLocation); |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 466 | canvas.save(); |
| 467 | canvas.translate(mTempLocation[0], mTempLocation[1]); |
Sunny Goyal | 19b93b7 | 2017-02-19 20:21:37 -0800 | [diff] [blame] | 468 | mFolderLeaveBehind.drawLeaveBehind(canvas); |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 469 | canvas.restore(); |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 470 | } |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 471 | |
| 472 | if (VISUALIZE_GRID) { |
| 473 | visualizeGrid(canvas); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | protected void visualizeGrid(Canvas canvas) { |
| 478 | mVisualizeGridRect.set(0, 0, mCellWidth, mCellHeight); |
| 479 | mVisualizeGridPaint.setStrokeWidth(4); |
| 480 | |
| 481 | for (int i = 0; i < mCountX; i++) { |
| 482 | for (int j = 0; j < mCountY; j++) { |
| 483 | canvas.save(); |
| 484 | |
| 485 | int transX = i * mCellWidth; |
| 486 | int transY = j * mCellHeight; |
| 487 | |
| 488 | canvas.translate(getPaddingLeft() + transX, getPaddingTop() + transY); |
| 489 | |
| 490 | mVisualizeGridPaint.setStyle(Paint.Style.FILL); |
| 491 | mVisualizeGridPaint.setColor(Color.argb(80, 255, 100, 100)); |
| 492 | |
| 493 | canvas.drawRect(mVisualizeGridRect, mVisualizeGridPaint); |
| 494 | |
| 495 | mVisualizeGridPaint.setStyle(Paint.Style.STROKE); |
| 496 | mVisualizeGridPaint.setColor(Color.argb(255, 255, 100, 100)); |
| 497 | |
| 498 | canvas.drawRect(mVisualizeGridRect, mVisualizeGridPaint); |
| 499 | canvas.restore(); |
| 500 | } |
| 501 | } |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 504 | @Override |
| 505 | protected void dispatchDraw(Canvas canvas) { |
| 506 | super.dispatchDraw(canvas); |
| 507 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 508 | for (int i = 0; i < mDelegatedCellDrawings.size(); i++) { |
| 509 | DelegatedCellDrawing bg = mDelegatedCellDrawings.get(i); |
| 510 | cellToPoint(bg.mDelegateCellX, bg.mDelegateCellY, mTempLocation); |
| 511 | canvas.save(); |
| 512 | canvas.translate(mTempLocation[0], mTempLocation[1]); |
| 513 | bg.drawOverItem(canvas); |
| 514 | canvas.restore(); |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 515 | } |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 518 | /** |
| 519 | * Add Delegated cell drawing |
| 520 | */ |
| 521 | public void addDelegatedCellDrawing(DelegatedCellDrawing bg) { |
| 522 | mDelegatedCellDrawings.add(bg); |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 523 | } |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 524 | |
| 525 | /** |
| 526 | * Remove item from DelegatedCellDrawings |
| 527 | */ |
| 528 | public void removeDelegatedCellDrawing(DelegatedCellDrawing bg) { |
| 529 | mDelegatedCellDrawings.remove(bg); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 532 | public void setFolderLeaveBehindCell(int x, int y) { |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 533 | View child = getChildAt(x, y); |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 534 | mFolderLeaveBehind.setup(getContext(), mActivity, null, |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 535 | child.getMeasuredWidth(), child.getPaddingTop()); |
| 536 | |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 537 | mFolderLeaveBehind.mDelegateCellX = x; |
| 538 | mFolderLeaveBehind.mDelegateCellY = y; |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 539 | invalidate(); |
| 540 | } |
| 541 | |
| 542 | public void clearFolderLeaveBehind() { |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 543 | mFolderLeaveBehind.mDelegateCellX = -1; |
| 544 | mFolderLeaveBehind.mDelegateCellY = -1; |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 545 | invalidate(); |
| 546 | } |
| 547 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 548 | @Override |
Michael Jurka | e6235dd | 2011-10-04 15:02:05 -0700 | [diff] [blame] | 549 | public boolean shouldDelayChildPressedState() { |
| 550 | return false; |
| 551 | } |
| 552 | |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 553 | public void restoreInstanceState(SparseArray<Parcelable> states) { |
Sunny Goyal | 33a152f | 2014-07-22 12:13:14 -0700 | [diff] [blame] | 554 | try { |
| 555 | dispatchRestoreInstanceState(states); |
| 556 | } catch (IllegalArgumentException ex) { |
Zak Cohen | 3eeb41d | 2020-02-14 14:15:13 -0800 | [diff] [blame] | 557 | if (FeatureFlags.IS_STUDIO_BUILD) { |
Sunny Goyal | 33a152f | 2014-07-22 12:13:14 -0700 | [diff] [blame] | 558 | throw ex; |
| 559 | } |
| 560 | // Mismatched viewId / viewType preventing restore. Skip restore on production builds. |
| 561 | Log.e(TAG, "Ignoring an error while restoring a view instance state", ex); |
| 562 | } |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Michael Jurka | e6235dd | 2011-10-04 15:02:05 -0700 | [diff] [blame] | 565 | @Override |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 566 | public void cancelLongPress() { |
| 567 | super.cancelLongPress(); |
| 568 | |
| 569 | // Cancel long press for all children |
| 570 | final int count = getChildCount(); |
| 571 | for (int i = 0; i < count; i++) { |
| 572 | final View child = getChildAt(i); |
| 573 | child.cancelLongPress(); |
| 574 | } |
| 575 | } |
| 576 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 577 | public void setOnInterceptTouchListener(View.OnTouchListener listener) { |
| 578 | mInterceptTouchListener = listener; |
| 579 | } |
| 580 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 581 | public int getCountX() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 582 | return mCountX; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 583 | } |
| 584 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 585 | public int getCountY() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 586 | return mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 589 | public boolean acceptsWidget() { |
| 590 | return mContainerType == WORKSPACE; |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 593 | public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params, |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 594 | boolean markCells) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 595 | final LayoutParams lp = params; |
| 596 | |
Andrew Flynn | de38e42 | 2012-05-08 11:22:15 -0700 | [diff] [blame] | 597 | // Hotseat icons - remove text |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 598 | if (child instanceof BubbleTextView) { |
| 599 | BubbleTextView bubbleChild = (BubbleTextView) child; |
Jon Miranda | f1eae80 | 2017-10-04 11:23:33 -0700 | [diff] [blame] | 600 | bubbleChild.setTextVisibility(mContainerType != HOTSEAT); |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 603 | child.setScaleX(mChildScale); |
| 604 | child.setScaleY(mChildScale); |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 605 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 606 | // Generate an id for each view, this assumes we have at most 256x256 cells |
| 607 | // per workspace screen |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 608 | if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 609 | // If the horizontal or vertical span is set to -1, it is taken to |
| 610 | // mean that it spans the extent of the CellLayout |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 611 | if (lp.cellHSpan < 0) lp.cellHSpan = mCountX; |
| 612 | if (lp.cellVSpan < 0) lp.cellVSpan = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 613 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 614 | child.setId(childId); |
Tony Wickham | a0628cc | 2015-10-14 15:23:04 -0700 | [diff] [blame] | 615 | if (LOGD) { |
| 616 | Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child); |
| 617 | } |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 618 | mShortcutsAndWidgets.addView(child, index, lp); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 619 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 620 | if (markCells) markCellsAsOccupiedForView(child); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 621 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 622 | return true; |
| 623 | } |
| 624 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 625 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 626 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 627 | @Override |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 628 | public void removeAllViews() { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 629 | mOccupied.clear(); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 630 | mShortcutsAndWidgets.removeAllViews(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | @Override |
| 634 | public void removeAllViewsInLayout() { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 635 | if (mShortcutsAndWidgets.getChildCount() > 0) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 636 | mOccupied.clear(); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 637 | mShortcutsAndWidgets.removeAllViewsInLayout(); |
Michael Jurka | 7cfc282 | 2011-08-02 20:19:24 -0700 | [diff] [blame] | 638 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public void removeView(View view) { |
| 643 | markCellsAsUnoccupiedForView(view); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 644 | mShortcutsAndWidgets.removeView(view); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | @Override |
| 648 | public void removeViewAt(int index) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 649 | markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index)); |
| 650 | mShortcutsAndWidgets.removeViewAt(index); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | @Override |
| 654 | public void removeViewInLayout(View view) { |
| 655 | markCellsAsUnoccupiedForView(view); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 656 | mShortcutsAndWidgets.removeViewInLayout(view); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | @Override |
| 660 | public void removeViews(int start, int count) { |
| 661 | for (int i = start; i < start + count; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 662 | markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i)); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 663 | } |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 664 | mShortcutsAndWidgets.removeViews(start, count); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | @Override |
| 668 | public void removeViewsInLayout(int start, int count) { |
| 669 | for (int i = start; i < start + count; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 670 | markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i)); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 671 | } |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 672 | mShortcutsAndWidgets.removeViewsInLayout(start, count); |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 675 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 676 | * Given a point, return the cell that strictly encloses that point |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 677 | * @param x X coordinate of the point |
| 678 | * @param y Y coordinate of the point |
| 679 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 680 | */ |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 681 | public void pointToCellExact(int x, int y, int[] result) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 682 | final int hStartPadding = getPaddingLeft(); |
| 683 | final int vStartPadding = getPaddingTop(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 684 | |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 685 | result[0] = (x - hStartPadding) / mCellWidth; |
| 686 | result[1] = (y - vStartPadding) / mCellHeight; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 687 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 688 | final int xAxis = mCountX; |
| 689 | final int yAxis = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 690 | |
| 691 | if (result[0] < 0) result[0] = 0; |
| 692 | if (result[0] >= xAxis) result[0] = xAxis - 1; |
| 693 | if (result[1] < 0) result[1] = 0; |
| 694 | if (result[1] >= yAxis) result[1] = yAxis - 1; |
| 695 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 696 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 697 | /** |
| 698 | * Given a point, return the cell that most closely encloses that point |
| 699 | * @param x X coordinate of the point |
| 700 | * @param y Y coordinate of the point |
| 701 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 702 | */ |
| 703 | void pointToCellRounded(int x, int y, int[] result) { |
| 704 | pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result); |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Given a cell coordinate, return the point that represents the upper left corner of that cell |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 709 | * |
| 710 | * @param cellX X coordinate of the cell |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 711 | * @param cellY Y coordinate of the cell |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 712 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 713 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 714 | */ |
| 715 | void cellToPoint(int cellX, int cellY, int[] result) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 716 | final int hStartPadding = getPaddingLeft(); |
| 717 | final int vStartPadding = getPaddingTop(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 718 | |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 719 | result[0] = hStartPadding + cellX * mCellWidth; |
| 720 | result[1] = vStartPadding + cellY * mCellHeight; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 723 | /** |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 724 | * Given a cell coordinate, return the point that represents the center of the cell |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 725 | * |
| 726 | * @param cellX X coordinate of the cell |
| 727 | * @param cellY Y coordinate of the cell |
| 728 | * |
| 729 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 730 | */ |
| 731 | void cellToCenterPoint(int cellX, int cellY, int[] result) { |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 732 | regionToCenterPoint(cellX, cellY, 1, 1, result); |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Given a cell coordinate and span return the point that represents the center of the regio |
| 737 | * |
| 738 | * @param cellX X coordinate of the cell |
| 739 | * @param cellY Y coordinate of the cell |
| 740 | * |
| 741 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 742 | */ |
| 743 | void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 744 | final int hStartPadding = getPaddingLeft(); |
| 745 | final int vStartPadding = getPaddingTop(); |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 746 | result[0] = hStartPadding + cellX * mCellWidth + (spanX * mCellWidth) / 2; |
| 747 | result[1] = vStartPadding + cellY * mCellHeight + (spanY * mCellHeight) / 2; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 750 | /** |
| 751 | * Given a cell coordinate and span fills out a corresponding pixel rect |
| 752 | * |
| 753 | * @param cellX X coordinate of the cell |
| 754 | * @param cellY Y coordinate of the cell |
| 755 | * @param result Rect in which to write the result |
| 756 | */ |
| 757 | void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) { |
| 758 | final int hStartPadding = getPaddingLeft(); |
| 759 | final int vStartPadding = getPaddingTop(); |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 760 | final int left = hStartPadding + cellX * mCellWidth; |
| 761 | final int top = vStartPadding + cellY * mCellHeight; |
| 762 | result.set(left, top, left + (spanX * mCellWidth), top + (spanY * mCellHeight)); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 765 | public float getDistanceFromCell(float x, float y, int[] cell) { |
| 766 | cellToCenterPoint(cell[0], cell[1], mTmpPoint); |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 767 | return (float) Math.hypot(x - mTmpPoint[0], y - mTmpPoint[1]); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 770 | public int getCellWidth() { |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 771 | return mCellWidth; |
| 772 | } |
| 773 | |
Sunny Goyal | 0b754e5 | 2017-08-07 07:42:45 -0700 | [diff] [blame] | 774 | public int getCellHeight() { |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 775 | return mCellHeight; |
| 776 | } |
| 777 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 778 | public void setFixedSize(int width, int height) { |
| 779 | mFixedWidth = width; |
| 780 | mFixedHeight = height; |
| 781 | } |
| 782 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 783 | @Override |
| 784 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 785 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 786 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 787 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 788 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 789 | int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight()); |
| 790 | int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom()); |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 791 | |
| 792 | mShortcutsAndWidgets.setRotation(mRotationMode.surfaceRotation); |
| 793 | if (mRotationMode.isTransposed) { |
| 794 | int tmp = childWidthSize; |
| 795 | childWidthSize = childHeightSize; |
| 796 | childHeightSize = tmp; |
| 797 | } |
| 798 | |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 799 | if (mFixedCellWidth < 0 || mFixedCellHeight < 0) { |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 800 | int cw = DeviceProfile.calculateCellWidth(childWidthSize, mCountX); |
| 801 | int ch = DeviceProfile.calculateCellHeight(childHeightSize, mCountY); |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 802 | if (cw != mCellWidth || ch != mCellHeight) { |
| 803 | mCellWidth = cw; |
| 804 | mCellHeight = ch; |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 805 | mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY); |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 806 | } |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 807 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 808 | |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 809 | int newWidth = childWidthSize; |
| 810 | int newHeight = childHeightSize; |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 811 | if (mFixedWidth > 0 && mFixedHeight > 0) { |
| 812 | newWidth = mFixedWidth; |
| 813 | newHeight = mFixedHeight; |
| 814 | } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 815 | throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); |
| 816 | } |
| 817 | |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 818 | mShortcutsAndWidgets.measure( |
| 819 | MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY), |
| 820 | MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY)); |
| 821 | |
| 822 | int maxWidth = mShortcutsAndWidgets.getMeasuredWidth(); |
| 823 | int maxHeight = mShortcutsAndWidgets.getMeasuredHeight(); |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 824 | if (mFixedWidth > 0 && mFixedHeight > 0) { |
| 825 | setMeasuredDimension(maxWidth, maxHeight); |
| 826 | } else { |
| 827 | setMeasuredDimension(widthSize, heightSize); |
| 828 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 832 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
Tony Wickham | 26b0142 | 2015-11-10 14:44:32 -0800 | [diff] [blame] | 833 | int left = getPaddingLeft(); |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 834 | left += (int) Math.ceil(getUnusedHorizontalSpace() / 2f); |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 835 | int right = r - l - getPaddingRight(); |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 836 | right -= (int) Math.ceil(getUnusedHorizontalSpace() / 2f); |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 837 | |
Winson Chung | 38848ca | 2013-10-08 12:03:44 -0700 | [diff] [blame] | 838 | int top = getPaddingTop(); |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 839 | int bottom = b - t - getPaddingBottom(); |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 840 | |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 841 | // Expand the background drawing bounds by the padding baked into the background drawable |
| 842 | mBackground.getPadding(mTempRect); |
| 843 | mBackground.setBounds( |
Jon Miranda | 2803200 | 2017-07-13 16:18:56 -0700 | [diff] [blame] | 844 | left - mTempRect.left - getPaddingLeft(), |
| 845 | top - mTempRect.top - getPaddingTop(), |
| 846 | right + mTempRect.right + getPaddingRight(), |
| 847 | bottom + mTempRect.bottom + getPaddingBottom()); |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 848 | |
| 849 | if (mRotationMode.isTransposed) { |
| 850 | int halfW = mShortcutsAndWidgets.getMeasuredWidth() / 2; |
| 851 | int halfH = mShortcutsAndWidgets.getMeasuredHeight() / 2; |
| 852 | int cX = (left + right) / 2; |
| 853 | int cY = (top + bottom) / 2; |
| 854 | mShortcutsAndWidgets.layout(cX - halfW, cY - halfH, cX + halfW, cY + halfH); |
| 855 | } else { |
| 856 | mShortcutsAndWidgets.layout(left, top, right, bottom); |
| 857 | } |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Tony Wickham | a501d49 | 2015-11-03 18:05:01 -0800 | [diff] [blame] | 860 | /** |
| 861 | * Returns the amount of space left over after subtracting padding and cells. This space will be |
| 862 | * very small, a few pixels at most, and is a result of rounding down when calculating the cell |
| 863 | * width in {@link DeviceProfile#calculateCellWidth(int, int)}. |
| 864 | */ |
| 865 | public int getUnusedHorizontalSpace() { |
Sunny Goyal | 41d51a0 | 2019-05-14 09:44:34 -0700 | [diff] [blame] | 866 | return (mRotationMode.isTransposed ? getMeasuredHeight() : getMeasuredWidth()) |
| 867 | - getPaddingLeft() - getPaddingRight() - (mCountX * mCellWidth); |
Tony Wickham | a501d49 | 2015-11-03 18:05:01 -0800 | [diff] [blame] | 868 | } |
| 869 | |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 870 | public Drawable getScrimBackground() { |
| 871 | return mBackground; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 874 | @Override |
| 875 | protected boolean verifyDrawable(Drawable who) { |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 876 | return super.verifyDrawable(who) || (who == mBackground); |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 879 | public ShortcutAndWidgetContainer getShortcutsAndWidgets() { |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 880 | return mShortcutsAndWidgets; |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 883 | public View getChildAt(int x, int y) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 884 | return mShortcutsAndWidgets.getChildAt(x, y); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 887 | public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 888 | int delay, boolean permanent, boolean adjustOccupied) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 889 | ShortcutAndWidgetContainer clc = getShortcutsAndWidgets(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 890 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 891 | if (clc.indexOfChild(child) != -1) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 892 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 893 | final ItemInfo info = (ItemInfo) child.getTag(); |
| 894 | |
| 895 | // We cancel any existing animations |
| 896 | if (mReorderAnimators.containsKey(lp)) { |
| 897 | mReorderAnimators.get(lp).cancel(); |
| 898 | mReorderAnimators.remove(lp); |
| 899 | } |
| 900 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 901 | final int oldX = lp.x; |
| 902 | final int oldY = lp.y; |
| 903 | if (adjustOccupied) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 904 | GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied; |
| 905 | occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false); |
| 906 | occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 907 | } |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 908 | lp.isLockedToGrid = true; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 909 | if (permanent) { |
| 910 | lp.cellX = info.cellX = cellX; |
| 911 | lp.cellY = info.cellY = cellY; |
| 912 | } else { |
| 913 | lp.tmpCellX = cellX; |
| 914 | lp.tmpCellY = cellY; |
| 915 | } |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 916 | clc.setupLp(child); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 917 | lp.isLockedToGrid = false; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 918 | final int newX = lp.x; |
| 919 | final int newY = lp.y; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 920 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 921 | lp.x = oldX; |
| 922 | lp.y = oldY; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 923 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 924 | // Exit early if we're not actually moving the view |
| 925 | if (oldX == newX && oldY == newY) { |
| 926 | lp.isLockedToGrid = true; |
| 927 | return true; |
| 928 | } |
| 929 | |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 930 | ValueAnimator va = ValueAnimator.ofFloat(0f, 1f); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 931 | va.setDuration(duration); |
| 932 | mReorderAnimators.put(lp, va); |
| 933 | |
| 934 | va.addUpdateListener(new AnimatorUpdateListener() { |
| 935 | @Override |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 936 | public void onAnimationUpdate(ValueAnimator animation) { |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 937 | float r = (Float) animation.getAnimatedValue(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 938 | lp.x = (int) ((1 - r) * oldX + r * newX); |
| 939 | lp.y = (int) ((1 - r) * oldY + r * newY); |
Adam Cohen | 6b8a02d | 2012-03-22 15:13:40 -0700 | [diff] [blame] | 940 | child.requestLayout(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 941 | } |
| 942 | }); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 943 | va.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 944 | boolean cancelled = false; |
| 945 | public void onAnimationEnd(Animator animation) { |
| 946 | // If the animation was cancelled, it means that another animation |
| 947 | // has interrupted this one, and we don't want to lock the item into |
| 948 | // place just yet. |
| 949 | if (!cancelled) { |
| 950 | lp.isLockedToGrid = true; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 951 | child.requestLayout(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 952 | } |
| 953 | if (mReorderAnimators.containsKey(lp)) { |
| 954 | mReorderAnimators.remove(lp); |
| 955 | } |
| 956 | } |
| 957 | public void onAnimationCancel(Animator animation) { |
| 958 | cancelled = true; |
| 959 | } |
| 960 | }); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 961 | va.setStartDelay(delay); |
| 962 | va.start(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 963 | return true; |
| 964 | } |
| 965 | return false; |
| 966 | } |
| 967 | |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 968 | void visualizeDropLocation(DraggableView v, DragPreviewProvider outlineProvider, int cellX, int |
| 969 | cellY, int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) { |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 970 | final int oldDragCellX = mDragCell[0]; |
| 971 | final int oldDragCellY = mDragCell[1]; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 972 | |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 973 | if (outlineProvider == null || outlineProvider.generatedDragOutline == null) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 974 | return; |
| 975 | } |
| 976 | |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 977 | Bitmap dragOutline = outlineProvider.generatedDragOutline; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 978 | if (cellX != oldDragCellX || cellY != oldDragCellY) { |
| 979 | mDragCell[0] = cellX; |
| 980 | mDragCell[1] = cellY; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 981 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 982 | final int oldIndex = mDragOutlineCurrent; |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 983 | mDragOutlineAnims[oldIndex].animateOut(); |
| 984 | mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 985 | Rect r = mDragOutlines[mDragOutlineCurrent]; |
Sunny Goyal | 106bf64 | 2015-07-16 12:18:06 -0700 | [diff] [blame] | 986 | |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 987 | cellToRect(cellX, cellY, spanX, spanY, r); |
| 988 | int left = r.left; |
| 989 | int top = r.top; |
| 990 | |
| 991 | int width = dragOutline.getWidth(); |
| 992 | int height = dragOutline.getHeight(); |
| 993 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 994 | if (resize) { |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 995 | width = r.width(); |
| 996 | height = r.height(); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 997 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 998 | |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 999 | if (v != null && v.getViewType() == DraggableView.DRAGGABLE_ICON) { |
| 1000 | left += ((mCellWidth * spanX) - dragOutline.getWidth()) / 2; |
| 1001 | int cHeight = getShortcutsAndWidgets().getCellContentHeight(); |
| 1002 | int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f)); |
| 1003 | top += cellPaddingY; |
| 1004 | } |
| 1005 | |
| 1006 | r.set(left, top, left + width, top + height); |
| 1007 | |
Jon Miranda | 6f6a06a | 2016-12-15 11:24:18 -0800 | [diff] [blame] | 1008 | Utilities.scaleRectAboutCenter(r, mChildScale); |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1009 | mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline); |
| 1010 | mDragOutlineAnims[mDragOutlineCurrent].animateIn(); |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 1011 | |
| 1012 | if (dragObject.stateAnnouncer != null) { |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 1013 | dragObject.stateAnnouncer.announce(getItemMoveDescription(cellX, cellY)); |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 1014 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 1018 | @SuppressLint("StringFormatMatches") |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 1019 | public String getItemMoveDescription(int cellX, int cellY) { |
| 1020 | if (mContainerType == HOTSEAT) { |
| 1021 | return getContext().getString(R.string.move_to_hotseat_position, |
| 1022 | Math.max(cellX, cellY) + 1); |
| 1023 | } else { |
| 1024 | return getContext().getString(R.string.move_to_empty_cell, |
| 1025 | cellY + 1, cellX + 1); |
| 1026 | } |
| 1027 | } |
| 1028 | |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 1029 | public void clearDragOutlines() { |
| 1030 | final int oldIndex = mDragOutlineCurrent; |
| 1031 | mDragOutlineAnims[oldIndex].animateOut(); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1032 | mDragCell[0] = mDragCell[1] = -1; |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1035 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1036 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1037 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1038 | * |
Romain Guy | 51afc02 | 2009-05-04 18:03:43 -0700 | [diff] [blame] | 1039 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1040 | * @param pixelY The Y location at which you want to search for a vacant area. |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1041 | * @param minSpanX The minimum horizontal span required |
| 1042 | * @param minSpanY The minimum vertical span required |
| 1043 | * @param spanX Horizontal span of the object. |
| 1044 | * @param spanY Vertical span of the object. |
| 1045 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1046 | * be allocated) |
| 1047 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1048 | * nearest the requested location. |
| 1049 | */ |
| 1050 | int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, |
| 1051 | int spanY, int[] result, int[] resultSpan) { |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 1052 | return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true, |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1053 | result, resultSpan); |
| 1054 | } |
| 1055 | |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 1056 | private final Stack<Rect> mTempRectStack = new Stack<>(); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1057 | private void lazyInitTempRectStack() { |
| 1058 | if (mTempRectStack.isEmpty()) { |
| 1059 | for (int i = 0; i < mCountX * mCountY; i++) { |
| 1060 | mTempRectStack.push(new Rect()); |
| 1061 | } |
| 1062 | } |
| 1063 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1064 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1065 | private void recycleTempRects(Stack<Rect> used) { |
| 1066 | while (!used.isEmpty()) { |
| 1067 | mTempRectStack.push(used.pop()); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1073 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 1074 | * |
| 1075 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1076 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 1077 | * @param minSpanX The minimum horizontal span required |
| 1078 | * @param minSpanY The minimum vertical span required |
| 1079 | * @param spanX Horizontal span of the object. |
| 1080 | * @param spanY Vertical span of the object. |
| 1081 | * @param ignoreOccupied If true, the result can be an occupied cell |
| 1082 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1083 | * be allocated) |
| 1084 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1085 | * nearest the requested location. |
| 1086 | */ |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 1087 | private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, |
| 1088 | int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1089 | lazyInitTempRectStack(); |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1090 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1091 | // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds |
| 1092 | // to the center of the item, but we are searching based on the top-left cell, so |
| 1093 | // we translate the point over to correspond to the top-left. |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 1094 | pixelX -= mCellWidth * (spanX - 1) / 2f; |
| 1095 | pixelY -= mCellHeight * (spanY - 1) / 2f; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1096 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1097 | // Keep track of best-scoring drop area |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1098 | final int[] bestXY = result != null ? result : new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1099 | double bestDistance = Double.MAX_VALUE; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1100 | final Rect bestRect = new Rect(-1, -1, -1, -1); |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 1101 | final Stack<Rect> validRegions = new Stack<>(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1102 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1103 | final int countX = mCountX; |
| 1104 | final int countY = mCountY; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1105 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1106 | if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 || |
| 1107 | spanX < minSpanX || spanY < minSpanY) { |
| 1108 | return bestXY; |
| 1109 | } |
| 1110 | |
| 1111 | for (int y = 0; y < countY - (minSpanY - 1); y++) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1112 | inner: |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1113 | for (int x = 0; x < countX - (minSpanX - 1); x++) { |
| 1114 | int ySize = -1; |
| 1115 | int xSize = -1; |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1116 | if (ignoreOccupied) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1117 | // First, let's see if this thing fits anywhere |
| 1118 | for (int i = 0; i < minSpanX; i++) { |
| 1119 | for (int j = 0; j < minSpanY; j++) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1120 | if (mOccupied.cells[x + i][y + j]) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1121 | continue inner; |
| 1122 | } |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1123 | } |
| 1124 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1125 | xSize = minSpanX; |
| 1126 | ySize = minSpanY; |
| 1127 | |
| 1128 | // We know that the item will fit at _some_ acceptable size, now let's see |
| 1129 | // how big we can make it. We'll alternate between incrementing x and y spans |
| 1130 | // until we hit a limit. |
| 1131 | boolean incX = true; |
| 1132 | boolean hitMaxX = xSize >= spanX; |
| 1133 | boolean hitMaxY = ySize >= spanY; |
| 1134 | while (!(hitMaxX && hitMaxY)) { |
| 1135 | if (incX && !hitMaxX) { |
| 1136 | for (int j = 0; j < ySize; j++) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1137 | if (x + xSize > countX -1 || mOccupied.cells[x + xSize][y + j]) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1138 | // We can't move out horizontally |
| 1139 | hitMaxX = true; |
| 1140 | } |
| 1141 | } |
| 1142 | if (!hitMaxX) { |
| 1143 | xSize++; |
| 1144 | } |
| 1145 | } else if (!hitMaxY) { |
| 1146 | for (int i = 0; i < xSize; i++) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1147 | if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1148 | // We can't move out vertically |
| 1149 | hitMaxY = true; |
| 1150 | } |
| 1151 | } |
| 1152 | if (!hitMaxY) { |
| 1153 | ySize++; |
| 1154 | } |
| 1155 | } |
| 1156 | hitMaxX |= xSize >= spanX; |
| 1157 | hitMaxY |= ySize >= spanY; |
| 1158 | incX = !incX; |
| 1159 | } |
| 1160 | incX = true; |
| 1161 | hitMaxX = xSize >= spanX; |
| 1162 | hitMaxY = ySize >= spanY; |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1163 | } |
Sunny Goyal | 2805e63 | 2015-05-20 15:35:32 -0700 | [diff] [blame] | 1164 | final int[] cellXY = mTmpPoint; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1165 | cellToCenterPoint(x, y, cellXY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1166 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1167 | // We verify that the current rect is not a sub-rect of any of our previous |
| 1168 | // candidates. In this case, the current rect is disqualified in favour of the |
| 1169 | // containing rect. |
| 1170 | Rect currentRect = mTempRectStack.pop(); |
| 1171 | currentRect.set(x, y, x + xSize, y + ySize); |
| 1172 | boolean contained = false; |
| 1173 | for (Rect r : validRegions) { |
| 1174 | if (r.contains(currentRect)) { |
| 1175 | contained = true; |
| 1176 | break; |
| 1177 | } |
| 1178 | } |
| 1179 | validRegions.push(currentRect); |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 1180 | double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1181 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1182 | if ((distance <= bestDistance && !contained) || |
| 1183 | currentRect.contains(bestRect)) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1184 | bestDistance = distance; |
| 1185 | bestXY[0] = x; |
| 1186 | bestXY[1] = y; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1187 | if (resultSpan != null) { |
| 1188 | resultSpan[0] = xSize; |
| 1189 | resultSpan[1] = ySize; |
| 1190 | } |
| 1191 | bestRect.set(currentRect); |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1192 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1196 | // Return -1, -1 if no suitable location found |
| 1197 | if (bestDistance == Double.MAX_VALUE) { |
| 1198 | bestXY[0] = -1; |
| 1199 | bestXY[1] = -1; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1200 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1201 | recycleTempRects(validRegions); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1202 | return bestXY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1203 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1204 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1205 | /** |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1206 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1207 | * cell location, and will also weigh in a suggested direction vector of the |
| 1208 | * desired location. This method computers distance based on unit grid distances, |
| 1209 | * not pixel distances. |
| 1210 | * |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1211 | * @param cellX The X cell nearest to which you want to search for a vacant area. |
| 1212 | * @param cellY The Y cell nearest which you want to search for a vacant area. |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1213 | * @param spanX Horizontal span of the object. |
| 1214 | * @param spanY Vertical span of the object. |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1215 | * @param direction The favored direction in which the views should move from x, y |
Sunny Goyal | 9eba1fd | 2015-10-16 08:58:57 -0700 | [diff] [blame] | 1216 | * @param occupied The array which represents which cells in the CellLayout are occupied |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1217 | * @param blockOccupied The array which represents which cells in the specified block (cellX, |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1218 | * cellY, spanX, spanY) are occupied. This is used when try to move a group of views. |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1219 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1220 | * be allocated) |
| 1221 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1222 | * nearest the requested location. |
| 1223 | */ |
| 1224 | private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction, |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1225 | boolean[][] occupied, boolean blockOccupied[][], int[] result) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1226 | // Keep track of best-scoring drop area |
| 1227 | final int[] bestXY = result != null ? result : new int[2]; |
| 1228 | float bestDistance = Float.MAX_VALUE; |
| 1229 | int bestDirectionScore = Integer.MIN_VALUE; |
| 1230 | |
| 1231 | final int countX = mCountX; |
| 1232 | final int countY = mCountY; |
| 1233 | |
| 1234 | for (int y = 0; y < countY - (spanY - 1); y++) { |
| 1235 | inner: |
| 1236 | for (int x = 0; x < countX - (spanX - 1); x++) { |
| 1237 | // First, let's see if this thing fits anywhere |
| 1238 | for (int i = 0; i < spanX; i++) { |
| 1239 | for (int j = 0; j < spanY; j++) { |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1240 | if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1241 | continue inner; |
| 1242 | } |
| 1243 | } |
| 1244 | } |
| 1245 | |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 1246 | float distance = (float) Math.hypot(x - cellX, y - cellY); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1247 | int[] curDirection = mTmpPoint; |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1248 | computeDirectionVector(x - cellX, y - cellY, curDirection); |
| 1249 | // The direction score is just the dot product of the two candidate direction |
| 1250 | // and that passed in. |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1251 | int curDirectionScore = direction[0] * curDirection[0] + |
| 1252 | direction[1] * curDirection[1]; |
Sunny Goyal | 8f90dcf | 2016-08-18 15:01:11 -0700 | [diff] [blame] | 1253 | if (Float.compare(distance, bestDistance) < 0 || |
| 1254 | (Float.compare(distance, bestDistance) == 0 |
| 1255 | && curDirectionScore > bestDirectionScore)) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1256 | bestDistance = distance; |
| 1257 | bestDirectionScore = curDirectionScore; |
| 1258 | bestXY[0] = x; |
| 1259 | bestXY[1] = y; |
| 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | // Return -1, -1 if no suitable location found |
| 1265 | if (bestDistance == Float.MAX_VALUE) { |
| 1266 | bestXY[0] = -1; |
| 1267 | bestXY[1] = -1; |
| 1268 | } |
| 1269 | return bestXY; |
| 1270 | } |
| 1271 | |
| 1272 | private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop, |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1273 | int[] direction, ItemConfiguration currentState) { |
| 1274 | CellAndSpan c = currentState.map.get(v); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1275 | boolean success = false; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1276 | mTmpOccupied.markCells(c, false); |
| 1277 | mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1278 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1279 | findNearestArea(c.cellX, c.cellY, c.spanX, c.spanY, direction, |
| 1280 | mTmpOccupied.cells, null, mTempLocation); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1281 | |
| 1282 | if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1283 | c.cellX = mTempLocation[0]; |
| 1284 | c.cellY = mTempLocation[1]; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1285 | success = true; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1286 | } |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1287 | mTmpOccupied.markCells(c, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1288 | return success; |
| 1289 | } |
| 1290 | |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1291 | /** |
| 1292 | * This helper class defines a cluster of views. It helps with defining complex edges |
| 1293 | * of the cluster and determining how those edges interact with other views. The edges |
| 1294 | * essentially define a fine-grained boundary around the cluster of views -- like a more |
| 1295 | * precise version of a bounding box. |
| 1296 | */ |
| 1297 | private class ViewCluster { |
Sunny Goyal | 6dc98b9 | 2016-04-03 15:25:29 -0700 | [diff] [blame] | 1298 | final static int LEFT = 1 << 0; |
| 1299 | final static int TOP = 1 << 1; |
| 1300 | final static int RIGHT = 1 << 2; |
| 1301 | final static int BOTTOM = 1 << 3; |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1302 | |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 1303 | final ArrayList<View> views; |
| 1304 | final ItemConfiguration config; |
| 1305 | final Rect boundingRect = new Rect(); |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1306 | |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 1307 | final int[] leftEdge = new int[mCountY]; |
| 1308 | final int[] rightEdge = new int[mCountY]; |
| 1309 | final int[] topEdge = new int[mCountX]; |
| 1310 | final int[] bottomEdge = new int[mCountX]; |
Sunny Goyal | 6dc98b9 | 2016-04-03 15:25:29 -0700 | [diff] [blame] | 1311 | int dirtyEdges; |
| 1312 | boolean boundingRectDirty; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1313 | |
| 1314 | @SuppressWarnings("unchecked") |
| 1315 | public ViewCluster(ArrayList<View> views, ItemConfiguration config) { |
| 1316 | this.views = (ArrayList<View>) views.clone(); |
| 1317 | this.config = config; |
| 1318 | resetEdges(); |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1321 | void resetEdges() { |
| 1322 | for (int i = 0; i < mCountX; i++) { |
| 1323 | topEdge[i] = -1; |
| 1324 | bottomEdge[i] = -1; |
| 1325 | } |
| 1326 | for (int i = 0; i < mCountY; i++) { |
| 1327 | leftEdge[i] = -1; |
| 1328 | rightEdge[i] = -1; |
| 1329 | } |
Sunny Goyal | 6dc98b9 | 2016-04-03 15:25:29 -0700 | [diff] [blame] | 1330 | dirtyEdges = LEFT | TOP | RIGHT | BOTTOM; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1331 | boundingRectDirty = true; |
| 1332 | } |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1333 | |
Sunny Goyal | 6dc98b9 | 2016-04-03 15:25:29 -0700 | [diff] [blame] | 1334 | void computeEdge(int which) { |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1335 | int count = views.size(); |
| 1336 | for (int i = 0; i < count; i++) { |
| 1337 | CellAndSpan cs = config.map.get(views.get(i)); |
| 1338 | switch (which) { |
| 1339 | case LEFT: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1340 | int left = cs.cellX; |
| 1341 | for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) { |
Sunny Goyal | 6dc98b9 | 2016-04-03 15:25:29 -0700 | [diff] [blame] | 1342 | if (left < leftEdge[j] || leftEdge[j] < 0) { |
| 1343 | leftEdge[j] = left; |
Adam Cohen | a56dc10 | 2012-07-13 13:41:42 -0700 | [diff] [blame] | 1344 | } |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1345 | } |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1346 | break; |
| 1347 | case RIGHT: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1348 | int right = cs.cellX + cs.spanX; |
| 1349 | for (int j = cs.cellY; j < cs.cellY + cs.spanY; j++) { |
Sunny Goyal | 6dc98b9 | 2016-04-03 15:25:29 -0700 | [diff] [blame] | 1350 | if (right > rightEdge[j]) { |
| 1351 | rightEdge[j] = right; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | break; |
| 1355 | case TOP: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1356 | int top = cs.cellY; |
| 1357 | for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) { |
Sunny Goyal | 6dc98b9 | 2016-04-03 15:25:29 -0700 | [diff] [blame] | 1358 | if (top < topEdge[j] || topEdge[j] < 0) { |
| 1359 | topEdge[j] = top; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | break; |
| 1363 | case BOTTOM: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1364 | int bottom = cs.cellY + cs.spanY; |
| 1365 | for (int j = cs.cellX; j < cs.cellX + cs.spanX; j++) { |
Sunny Goyal | 6dc98b9 | 2016-04-03 15:25:29 -0700 | [diff] [blame] | 1366 | if (bottom > bottomEdge[j]) { |
| 1367 | bottomEdge[j] = bottom; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1368 | } |
| 1369 | } |
| 1370 | break; |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | } |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1374 | |
| 1375 | boolean isViewTouchingEdge(View v, int whichEdge) { |
| 1376 | CellAndSpan cs = config.map.get(v); |
| 1377 | |
Sunny Goyal | 6dc98b9 | 2016-04-03 15:25:29 -0700 | [diff] [blame] | 1378 | if ((dirtyEdges & whichEdge) == whichEdge) { |
| 1379 | computeEdge(whichEdge); |
| 1380 | dirtyEdges &= ~whichEdge; |
| 1381 | } |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1382 | |
| 1383 | switch (whichEdge) { |
| 1384 | case LEFT: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1385 | for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) { |
| 1386 | if (leftEdge[i] == cs.cellX + cs.spanX) { |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1387 | return true; |
| 1388 | } |
| 1389 | } |
| 1390 | break; |
| 1391 | case RIGHT: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1392 | for (int i = cs.cellY; i < cs.cellY + cs.spanY; i++) { |
| 1393 | if (rightEdge[i] == cs.cellX) { |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1394 | return true; |
| 1395 | } |
| 1396 | } |
| 1397 | break; |
| 1398 | case TOP: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1399 | for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) { |
| 1400 | if (topEdge[i] == cs.cellY + cs.spanY) { |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1401 | return true; |
| 1402 | } |
| 1403 | } |
| 1404 | break; |
| 1405 | case BOTTOM: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1406 | for (int i = cs.cellX; i < cs.cellX + cs.spanX; i++) { |
| 1407 | if (bottomEdge[i] == cs.cellY) { |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1408 | return true; |
| 1409 | } |
| 1410 | } |
| 1411 | break; |
| 1412 | } |
| 1413 | return false; |
| 1414 | } |
| 1415 | |
| 1416 | void shift(int whichEdge, int delta) { |
| 1417 | for (View v: views) { |
| 1418 | CellAndSpan c = config.map.get(v); |
| 1419 | switch (whichEdge) { |
| 1420 | case LEFT: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1421 | c.cellX -= delta; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1422 | break; |
| 1423 | case RIGHT: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1424 | c.cellX += delta; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1425 | break; |
| 1426 | case TOP: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1427 | c.cellY -= delta; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1428 | break; |
| 1429 | case BOTTOM: |
| 1430 | default: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1431 | c.cellY += delta; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1432 | break; |
| 1433 | } |
| 1434 | } |
| 1435 | resetEdges(); |
| 1436 | } |
| 1437 | |
| 1438 | public void addView(View v) { |
| 1439 | views.add(v); |
| 1440 | resetEdges(); |
| 1441 | } |
| 1442 | |
| 1443 | public Rect getBoundingRect() { |
| 1444 | if (boundingRectDirty) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1445 | config.getBoundingRectForViews(views, boundingRect); |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1446 | } |
| 1447 | return boundingRect; |
| 1448 | } |
| 1449 | |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 1450 | final PositionComparator comparator = new PositionComparator(); |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1451 | class PositionComparator implements Comparator<View> { |
| 1452 | int whichEdge = 0; |
| 1453 | public int compare(View left, View right) { |
| 1454 | CellAndSpan l = config.map.get(left); |
| 1455 | CellAndSpan r = config.map.get(right); |
| 1456 | switch (whichEdge) { |
| 1457 | case LEFT: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1458 | return (r.cellX + r.spanX) - (l.cellX + l.spanX); |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1459 | case RIGHT: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1460 | return l.cellX - r.cellX; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1461 | case TOP: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1462 | return (r.cellY + r.spanY) - (l.cellY + l.spanY); |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1463 | case BOTTOM: |
| 1464 | default: |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1465 | return l.cellY - r.cellY; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1466 | } |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | public void sortConfigurationForEdgePush(int edge) { |
| 1471 | comparator.whichEdge = edge; |
| 1472 | Collections.sort(config.sortedViews, comparator); |
| 1473 | } |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1476 | private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop, |
| 1477 | int[] direction, View dragView, ItemConfiguration currentState) { |
Adam Cohen | e048950 | 2012-08-27 15:18:53 -0700 | [diff] [blame] | 1478 | |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1479 | ViewCluster cluster = new ViewCluster(views, currentState); |
| 1480 | Rect clusterRect = cluster.getBoundingRect(); |
| 1481 | int whichEdge; |
| 1482 | int pushDistance; |
| 1483 | boolean fail = false; |
| 1484 | |
| 1485 | // Determine the edge of the cluster that will be leading the push and how far |
| 1486 | // the cluster must be shifted. |
| 1487 | if (direction[0] < 0) { |
| 1488 | whichEdge = ViewCluster.LEFT; |
| 1489 | pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left; |
Adam Cohen | e048950 | 2012-08-27 15:18:53 -0700 | [diff] [blame] | 1490 | } else if (direction[0] > 0) { |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1491 | whichEdge = ViewCluster.RIGHT; |
| 1492 | pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left; |
| 1493 | } else if (direction[1] < 0) { |
| 1494 | whichEdge = ViewCluster.TOP; |
| 1495 | pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top; |
| 1496 | } else { |
| 1497 | whichEdge = ViewCluster.BOTTOM; |
| 1498 | pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top; |
Adam Cohen | e048950 | 2012-08-27 15:18:53 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1501 | // Break early for invalid push distance. |
| 1502 | if (pushDistance <= 0) { |
| 1503 | return false; |
Adam Cohen | e048950 | 2012-08-27 15:18:53 -0700 | [diff] [blame] | 1504 | } |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1505 | |
| 1506 | // Mark the occupied state as false for the group of views we want to move. |
| 1507 | for (View v: views) { |
| 1508 | CellAndSpan c = currentState.map.get(v); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1509 | mTmpOccupied.markCells(c, false); |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | // We save the current configuration -- if we fail to find a solution we will revert |
| 1513 | // to the initial state. The process of finding a solution modifies the configuration |
| 1514 | // in place, hence the need for revert in the failure case. |
| 1515 | currentState.save(); |
| 1516 | |
| 1517 | // The pushing algorithm is simplified by considering the views in the order in which |
| 1518 | // they would be pushed by the cluster. For example, if the cluster is leading with its |
| 1519 | // left edge, we consider sort the views by their right edge, from right to left. |
| 1520 | cluster.sortConfigurationForEdgePush(whichEdge); |
| 1521 | |
| 1522 | while (pushDistance > 0 && !fail) { |
| 1523 | for (View v: currentState.sortedViews) { |
| 1524 | // For each view that isn't in the cluster, we see if the leading edge of the |
| 1525 | // cluster is contacting the edge of that view. If so, we add that view to the |
| 1526 | // cluster. |
| 1527 | if (!cluster.views.contains(v) && v != dragView) { |
| 1528 | if (cluster.isViewTouchingEdge(v, whichEdge)) { |
| 1529 | LayoutParams lp = (LayoutParams) v.getLayoutParams(); |
| 1530 | if (!lp.canReorder) { |
| 1531 | // The push solution includes the all apps button, this is not viable. |
| 1532 | fail = true; |
| 1533 | break; |
| 1534 | } |
| 1535 | cluster.addView(v); |
| 1536 | CellAndSpan c = currentState.map.get(v); |
| 1537 | |
| 1538 | // Adding view to cluster, mark it as not occupied. |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1539 | mTmpOccupied.markCells(c, false); |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1540 | } |
| 1541 | } |
| 1542 | } |
| 1543 | pushDistance--; |
| 1544 | |
| 1545 | // The cluster has been completed, now we move the whole thing over in the appropriate |
| 1546 | // direction. |
| 1547 | cluster.shift(whichEdge, 1); |
| 1548 | } |
| 1549 | |
| 1550 | boolean foundSolution = false; |
| 1551 | clusterRect = cluster.getBoundingRect(); |
| 1552 | |
| 1553 | // Due to the nature of the algorithm, the only check required to verify a valid solution |
| 1554 | // is to ensure that completed shifted cluster lies completely within the cell layout. |
| 1555 | if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 && |
| 1556 | clusterRect.bottom <= mCountY) { |
| 1557 | foundSolution = true; |
| 1558 | } else { |
| 1559 | currentState.restore(); |
| 1560 | } |
| 1561 | |
| 1562 | // In either case, we set the occupied array as marked for the location of the views |
| 1563 | for (View v: cluster.views) { |
| 1564 | CellAndSpan c = currentState.map.get(v); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1565 | mTmpOccupied.markCells(c, true); |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | return foundSolution; |
Adam Cohen | e048950 | 2012-08-27 15:18:53 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1571 | private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop, |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1572 | int[] direction, View dragView, ItemConfiguration currentState) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1573 | if (views.size() == 0) return true; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1574 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1575 | boolean success = false; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1576 | Rect boundingRect = new Rect(); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1577 | // We construct a rect which represents the entire group of views passed in |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1578 | currentState.getBoundingRectForViews(views, boundingRect); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1579 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1580 | // Mark the occupied state as false for the group of views we want to move. |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1581 | for (View v: views) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1582 | CellAndSpan c = currentState.map.get(v); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1583 | mTmpOccupied.markCells(c, false); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1586 | GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height()); |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1587 | int top = boundingRect.top; |
| 1588 | int left = boundingRect.left; |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1589 | // We mark more precisely which parts of the bounding rect are truly occupied, allowing |
Adam Cohen | a56dc10 | 2012-07-13 13:41:42 -0700 | [diff] [blame] | 1590 | // for interlocking. |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1591 | for (View v: views) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1592 | CellAndSpan c = currentState.map.get(v); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1593 | blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true); |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1594 | } |
| 1595 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1596 | mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1597 | |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1598 | findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(), |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1599 | boundingRect.height(), direction, |
| 1600 | mTmpOccupied.cells, blockOccupied.cells, mTempLocation); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1601 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1602 | // If we successfuly found a location by pushing the block of views, we commit it |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1603 | if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1604 | int deltaX = mTempLocation[0] - boundingRect.left; |
| 1605 | int deltaY = mTempLocation[1] - boundingRect.top; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1606 | for (View v: views) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1607 | CellAndSpan c = currentState.map.get(v); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1608 | c.cellX += deltaX; |
| 1609 | c.cellY += deltaY; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1610 | } |
| 1611 | success = true; |
| 1612 | } |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1613 | |
| 1614 | // In either case, we set the occupied array as marked for the location of the views |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1615 | for (View v: views) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1616 | CellAndSpan c = currentState.map.get(v); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1617 | mTmpOccupied.markCells(c, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1618 | } |
| 1619 | return success; |
| 1620 | } |
| 1621 | |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1622 | // This method tries to find a reordering solution which satisfies the push mechanic by trying |
| 1623 | // to push items in each of the cardinal directions, in an order based on the direction vector |
| 1624 | // passed. |
| 1625 | private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied, |
| 1626 | int[] direction, View ignoreView, ItemConfiguration solution) { |
| 1627 | if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1628 | // If the direction vector has two non-zero components, we try pushing |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1629 | // separately in each of the components. |
| 1630 | int temp = direction[1]; |
| 1631 | direction[1] = 0; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1632 | |
| 1633 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1634 | ignoreView, solution)) { |
| 1635 | return true; |
| 1636 | } |
| 1637 | direction[1] = temp; |
| 1638 | temp = direction[0]; |
| 1639 | direction[0] = 0; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1640 | |
| 1641 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1642 | ignoreView, solution)) { |
| 1643 | return true; |
| 1644 | } |
| 1645 | // Revert the direction |
| 1646 | direction[0] = temp; |
| 1647 | |
| 1648 | // Now we try pushing in each component of the opposite direction |
| 1649 | direction[0] *= -1; |
| 1650 | direction[1] *= -1; |
| 1651 | temp = direction[1]; |
| 1652 | direction[1] = 0; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1653 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1654 | ignoreView, solution)) { |
| 1655 | return true; |
| 1656 | } |
| 1657 | |
| 1658 | direction[1] = temp; |
| 1659 | temp = direction[0]; |
| 1660 | direction[0] = 0; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1661 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1662 | ignoreView, solution)) { |
| 1663 | return true; |
| 1664 | } |
| 1665 | // revert the direction |
| 1666 | direction[0] = temp; |
| 1667 | direction[0] *= -1; |
| 1668 | direction[1] *= -1; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1669 | |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1670 | } else { |
| 1671 | // If the direction vector has a single non-zero component, we push first in the |
| 1672 | // direction of the vector |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1673 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1674 | ignoreView, solution)) { |
| 1675 | return true; |
| 1676 | } |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1677 | // Then we try the opposite direction |
| 1678 | direction[0] *= -1; |
| 1679 | direction[1] *= -1; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1680 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1681 | ignoreView, solution)) { |
| 1682 | return true; |
| 1683 | } |
| 1684 | // Switch the direction back |
| 1685 | direction[0] *= -1; |
| 1686 | direction[1] *= -1; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1687 | |
| 1688 | // If we have failed to find a push solution with the above, then we try |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1689 | // to find a solution by pushing along the perpendicular axis. |
| 1690 | |
| 1691 | // Swap the components |
| 1692 | int temp = direction[1]; |
| 1693 | direction[1] = direction[0]; |
| 1694 | direction[0] = temp; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1695 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1696 | ignoreView, solution)) { |
| 1697 | return true; |
| 1698 | } |
| 1699 | |
| 1700 | // Then we try the opposite direction |
| 1701 | direction[0] *= -1; |
| 1702 | direction[1] *= -1; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1703 | if (pushViewsToTempLocation(intersectingViews, occupied, direction, |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1704 | ignoreView, solution)) { |
| 1705 | return true; |
| 1706 | } |
| 1707 | // Switch the direction back |
| 1708 | direction[0] *= -1; |
| 1709 | direction[1] *= -1; |
| 1710 | |
| 1711 | // Swap the components back |
| 1712 | temp = direction[1]; |
| 1713 | direction[1] = direction[0]; |
| 1714 | direction[0] = temp; |
| 1715 | } |
| 1716 | return false; |
| 1717 | } |
| 1718 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1719 | private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction, |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1720 | View ignoreView, ItemConfiguration solution) { |
Winson Chung | e3e03bc | 2012-05-01 15:10:11 -0700 | [diff] [blame] | 1721 | // Return early if get invalid cell positions |
| 1722 | if (cellX < 0 || cellY < 0) return false; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1723 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1724 | mIntersectingViews.clear(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1725 | mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1726 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1727 | // Mark the desired location of the view currently being dragged. |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1728 | if (ignoreView != null) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1729 | CellAndSpan c = solution.map.get(ignoreView); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1730 | if (c != null) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1731 | c.cellX = cellX; |
| 1732 | c.cellY = cellY; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1733 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1734 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1735 | Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY); |
| 1736 | Rect r1 = new Rect(); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1737 | for (View child: solution.map.keySet()) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1738 | if (child == ignoreView) continue; |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1739 | CellAndSpan c = solution.map.get(child); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1740 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1741 | r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1742 | if (Rect.intersects(r0, r1)) { |
| 1743 | if (!lp.canReorder) { |
| 1744 | return false; |
| 1745 | } |
| 1746 | mIntersectingViews.add(child); |
| 1747 | } |
| 1748 | } |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1749 | |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 1750 | solution.intersectingViews = new ArrayList<>(mIntersectingViews); |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1751 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1752 | // First we try to find a solution which respects the push mechanic. That is, |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1753 | // we try to find a solution such that no displaced item travels through another item |
| 1754 | // without also displacing that item. |
| 1755 | if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView, |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1756 | solution)) { |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1757 | return true; |
| 1758 | } |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1759 | |
Adam Cohen | 4abc5bd | 2012-05-29 21:06:03 -0700 | [diff] [blame] | 1760 | // Next we try moving the views as a block, but without requiring the push mechanic. |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1761 | if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView, |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1762 | solution)) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1763 | return true; |
| 1764 | } |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1765 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1766 | // Ok, they couldn't move as a block, let's move them individually |
| 1767 | for (View v : mIntersectingViews) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1768 | if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1769 | return false; |
| 1770 | } |
| 1771 | } |
| 1772 | return true; |
| 1773 | } |
| 1774 | |
| 1775 | /* |
| 1776 | * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between |
| 1777 | * the provided point and the provided cell |
| 1778 | */ |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1779 | private void computeDirectionVector(float deltaX, float deltaY, int[] result) { |
Jon Miranda | e96798e | 2016-12-07 12:10:44 -0800 | [diff] [blame] | 1780 | double angle = Math.atan(deltaY / deltaX); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1781 | |
| 1782 | result[0] = 0; |
| 1783 | result[1] = 0; |
| 1784 | if (Math.abs(Math.cos(angle)) > 0.5f) { |
| 1785 | result[0] = (int) Math.signum(deltaX); |
| 1786 | } |
| 1787 | if (Math.abs(Math.sin(angle)) > 0.5f) { |
| 1788 | result[1] = (int) Math.signum(deltaY); |
| 1789 | } |
| 1790 | } |
| 1791 | |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 1792 | private ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY, |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1793 | int spanX, int spanY, int[] direction, View dragView, boolean decX, |
| 1794 | ItemConfiguration solution) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1795 | // Copy the current state into the solution. This solution will be manipulated as necessary. |
| 1796 | copyCurrentStateToSolution(solution, false); |
| 1797 | // Copy the current occupied array into the temporary occupied array. This array will be |
| 1798 | // manipulated as necessary to find a solution. |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1799 | mOccupied.copyTo(mTmpOccupied); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1800 | |
| 1801 | // We find the nearest cell into which we would place the dragged item, assuming there's |
| 1802 | // nothing in its way. |
| 1803 | int result[] = new int[2]; |
| 1804 | result = findNearestArea(pixelX, pixelY, spanX, spanY, result); |
| 1805 | |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 1806 | boolean success; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1807 | // First we try the exact nearest position of the item being dragged, |
| 1808 | // we will then want to try to move this around to other neighbouring positions |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1809 | success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView, |
| 1810 | solution); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1811 | |
| 1812 | if (!success) { |
| 1813 | // We try shrinking the widget down to size in an alternating pattern, shrink 1 in |
| 1814 | // x, then 1 in y etc. |
| 1815 | if (spanX > minSpanX && (minSpanY == spanY || decX)) { |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1816 | return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, |
| 1817 | direction, dragView, false, solution); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1818 | } else if (spanY > minSpanY) { |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1819 | return findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, |
| 1820 | direction, dragView, true, solution); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1821 | } |
| 1822 | solution.isSolution = false; |
| 1823 | } else { |
| 1824 | solution.isSolution = true; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1825 | solution.cellX = result[0]; |
| 1826 | solution.cellY = result[1]; |
| 1827 | solution.spanX = spanX; |
| 1828 | solution.spanY = spanY; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1829 | } |
| 1830 | return solution; |
| 1831 | } |
| 1832 | |
| 1833 | private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1834 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1835 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1836 | View child = mShortcutsAndWidgets.getChildAt(i); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1837 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1838 | CellAndSpan c; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1839 | if (temp) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1840 | c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1841 | } else { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1842 | c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1843 | } |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 1844 | solution.add(child, c); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1845 | } |
| 1846 | } |
| 1847 | |
| 1848 | private void copySolutionToTempState(ItemConfiguration solution, View dragView) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1849 | mTmpOccupied.clear(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1850 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1851 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1852 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1853 | View child = mShortcutsAndWidgets.getChildAt(i); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1854 | if (child == dragView) continue; |
| 1855 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1856 | CellAndSpan c = solution.map.get(child); |
| 1857 | if (c != null) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1858 | lp.tmpCellX = c.cellX; |
| 1859 | lp.tmpCellY = c.cellY; |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1860 | lp.cellHSpan = c.spanX; |
| 1861 | lp.cellVSpan = c.spanY; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1862 | mTmpOccupied.markCells(c, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1863 | } |
| 1864 | } |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1865 | mTmpOccupied.markCells(solution, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean |
| 1869 | commitDragView) { |
| 1870 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1871 | GridOccupancy occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied; |
| 1872 | occupied.clear(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1873 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1874 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1875 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1876 | View child = mShortcutsAndWidgets.getChildAt(i); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1877 | if (child == dragView) continue; |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1878 | CellAndSpan c = solution.map.get(child); |
| 1879 | if (c != null) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1880 | animateChildToPosition(child, c.cellX, c.cellY, REORDER_ANIMATION_DURATION, 0, |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1881 | DESTRUCTIVE_REORDER, false); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1882 | occupied.markCells(c, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1883 | } |
| 1884 | } |
| 1885 | if (commitDragView) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1886 | occupied.markCells(solution, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1887 | } |
| 1888 | } |
| 1889 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1890 | |
| 1891 | // This method starts or changes the reorder preview animations |
| 1892 | private void beginOrAdjustReorderPreviewAnimations(ItemConfiguration solution, |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 1893 | View dragView, int mode) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1894 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1895 | for (int i = 0; i < childCount; i++) { |
| 1896 | View child = mShortcutsAndWidgets.getChildAt(i); |
| 1897 | if (child == dragView) continue; |
| 1898 | CellAndSpan c = solution.map.get(child); |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1899 | boolean skip = mode == ReorderPreviewAnimation.MODE_HINT && solution.intersectingViews |
| 1900 | != null && !solution.intersectingViews.contains(child); |
| 1901 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1902 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1903 | if (c != null && !skip) { |
| 1904 | ReorderPreviewAnimation rha = new ReorderPreviewAnimation(child, mode, lp.cellX, |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1905 | lp.cellY, c.cellX, c.cellY, c.spanX, c.spanY); |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 1906 | rha.animate(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1907 | } |
| 1908 | } |
| 1909 | } |
| 1910 | |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 1911 | private static final Property<ReorderPreviewAnimation, Float> ANIMATION_PROGRESS = |
| 1912 | new Property<ReorderPreviewAnimation, Float>(float.class, "animationProgress") { |
| 1913 | @Override |
| 1914 | public Float get(ReorderPreviewAnimation anim) { |
| 1915 | return anim.animationProgress; |
| 1916 | } |
| 1917 | |
| 1918 | @Override |
| 1919 | public void set(ReorderPreviewAnimation anim, Float progress) { |
| 1920 | anim.setAnimationProgress(progress); |
| 1921 | } |
| 1922 | }; |
| 1923 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1924 | // Class which represents the reorder preview animations. These animations show that an item is |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1925 | // in a temporary state, and hint at where the item will return to. |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1926 | class ReorderPreviewAnimation { |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 1927 | final View child; |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 1928 | float finalDeltaX; |
| 1929 | float finalDeltaY; |
| 1930 | float initDeltaX; |
| 1931 | float initDeltaY; |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 1932 | final float finalScale; |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 1933 | float initScale; |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 1934 | final int mode; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1935 | boolean repeating = false; |
| 1936 | private static final int PREVIEW_DURATION = 300; |
| 1937 | private static final int HINT_DURATION = Workspace.REORDER_TIMEOUT; |
| 1938 | |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1939 | private static final float CHILD_DIVIDEND = 4.0f; |
| 1940 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1941 | public static final int MODE_HINT = 0; |
| 1942 | public static final int MODE_PREVIEW = 1; |
| 1943 | |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 1944 | float animationProgress = 0; |
Sunny Goyal | f0b6db7 | 2018-08-13 16:10:14 -0700 | [diff] [blame] | 1945 | ValueAnimator a; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1946 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1947 | public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1, |
| 1948 | int cellY1, int spanX, int spanY) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1949 | regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint); |
| 1950 | final int x0 = mTmpPoint[0]; |
| 1951 | final int y0 = mTmpPoint[1]; |
| 1952 | regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint); |
| 1953 | final int x1 = mTmpPoint[0]; |
| 1954 | final int y1 = mTmpPoint[1]; |
| 1955 | final int dX = x1 - x0; |
| 1956 | final int dY = y1 - y0; |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1957 | |
| 1958 | this.child = child; |
| 1959 | this.mode = mode; |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 1960 | |
| 1961 | // TODO issue! |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1962 | setInitialAnimationValues(false); |
| 1963 | finalScale = (mChildScale - (CHILD_DIVIDEND / child.getWidth())) * initScale; |
| 1964 | finalDeltaX = initDeltaX; |
| 1965 | finalDeltaY = initDeltaY; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1966 | int dir = mode == MODE_HINT ? -1 : 1; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1967 | if (dX == dY && dX == 0) { |
| 1968 | } else { |
| 1969 | if (dY == 0) { |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1970 | finalDeltaX += - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1971 | } else if (dX == 0) { |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1972 | finalDeltaY += - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1973 | } else { |
| 1974 | double angle = Math.atan( (float) (dY) / dX); |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1975 | finalDeltaX += (int) (- dir * Math.signum(dX) * |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1976 | Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude)); |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1977 | finalDeltaY += (int) (- dir * Math.signum(dY) * |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 1978 | Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude)); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 1979 | } |
| 1980 | } |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | void setInitialAnimationValues(boolean restoreOriginalValues) { |
| 1984 | if (restoreOriginalValues) { |
| 1985 | if (child instanceof LauncherAppWidgetHostView) { |
| 1986 | LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) child; |
| 1987 | initScale = lahv.getScaleToFit(); |
| 1988 | initDeltaX = lahv.getTranslationForCentering().x; |
| 1989 | initDeltaY = lahv.getTranslationForCentering().y; |
| 1990 | } else { |
| 1991 | initScale = mChildScale; |
| 1992 | initDeltaX = 0; |
| 1993 | initDeltaY = 0; |
| 1994 | } |
| 1995 | } else { |
| 1996 | initScale = child.getScaleX(); |
| 1997 | initDeltaX = child.getTranslationX(); |
| 1998 | initDeltaY = child.getTranslationY(); |
| 1999 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 2002 | void animate() { |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 2003 | boolean noMovement = (finalDeltaX == initDeltaX) && (finalDeltaY == initDeltaY); |
| 2004 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2005 | if (mShakeAnimators.containsKey(child)) { |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2006 | ReorderPreviewAnimation oldAnimation = mShakeAnimators.get(child); |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 2007 | oldAnimation.cancel(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2008 | mShakeAnimators.remove(child); |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 2009 | if (noMovement) { |
Adam Cohen | e7587d2 | 2012-05-24 18:50:02 -0700 | [diff] [blame] | 2010 | completeAnimationImmediately(); |
| 2011 | return; |
| 2012 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2013 | } |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 2014 | if (noMovement) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2015 | return; |
| 2016 | } |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 2017 | ValueAnimator va = ObjectAnimator.ofFloat(this, ANIMATION_PROGRESS, 0, 1); |
Adam Cohen | e7587d2 | 2012-05-24 18:50:02 -0700 | [diff] [blame] | 2018 | a = va; |
Tony Wickham | 9e0702f | 2015-09-02 14:45:39 -0700 | [diff] [blame] | 2019 | |
| 2020 | // Animations are disabled in power save mode, causing the repeated animation to jump |
| 2021 | // spastically between beginning and end states. Since this looks bad, we don't repeat |
| 2022 | // the animation in power save mode. |
Sunny Goyal | 7368fa4 | 2019-04-22 09:58:14 -0700 | [diff] [blame] | 2023 | if (Utilities.areAnimationsEnabled(getContext())) { |
Tony Wickham | 9e0702f | 2015-09-02 14:45:39 -0700 | [diff] [blame] | 2024 | va.setRepeatMode(ValueAnimator.REVERSE); |
| 2025 | va.setRepeatCount(ValueAnimator.INFINITE); |
| 2026 | } |
| 2027 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2028 | va.setDuration(mode == MODE_HINT ? HINT_DURATION : PREVIEW_DURATION); |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 2029 | va.setStartDelay((int) (Math.random() * 60)); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2030 | va.addListener(new AnimatorListenerAdapter() { |
| 2031 | public void onAnimationRepeat(Animator animation) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2032 | // We make sure to end only after a full period |
Jon Miranda | 2126691 | 2016-12-19 14:12:05 -0800 | [diff] [blame] | 2033 | setInitialAnimationValues(true); |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2034 | repeating = true; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2035 | } |
| 2036 | }); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2037 | mShakeAnimators.put(child, this); |
| 2038 | va.start(); |
| 2039 | } |
| 2040 | |
Sunny Goyal | 849c6a2 | 2018-08-08 16:33:46 -0700 | [diff] [blame] | 2041 | private void setAnimationProgress(float progress) { |
| 2042 | animationProgress = progress; |
| 2043 | float r1 = (mode == MODE_HINT && repeating) ? 1.0f : animationProgress; |
| 2044 | float x = r1 * finalDeltaX + (1 - r1) * initDeltaX; |
| 2045 | float y = r1 * finalDeltaY + (1 - r1) * initDeltaY; |
| 2046 | child.setTranslationX(x); |
| 2047 | child.setTranslationY(y); |
| 2048 | float s = animationProgress * finalScale + (1 - animationProgress) * initScale; |
| 2049 | child.setScaleX(s); |
| 2050 | child.setScaleY(s); |
| 2051 | } |
| 2052 | |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 2053 | private void cancel() { |
Adam Cohen | e7587d2 | 2012-05-24 18:50:02 -0700 | [diff] [blame] | 2054 | if (a != null) { |
| 2055 | a.cancel(); |
| 2056 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2057 | } |
Adam Cohen | e7587d2 | 2012-05-24 18:50:02 -0700 | [diff] [blame] | 2058 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 2059 | @Thunk void completeAnimationImmediately() { |
Adam Cohen | e7587d2 | 2012-05-24 18:50:02 -0700 | [diff] [blame] | 2060 | if (a != null) { |
| 2061 | a.cancel(); |
| 2062 | } |
Brandon Keely | 50e6e56 | 2012-05-08 16:28:49 -0700 | [diff] [blame] | 2063 | |
Jon Miranda | 72dbd91 | 2017-01-04 14:03:07 -0800 | [diff] [blame] | 2064 | setInitialAnimationValues(true); |
Sunny Goyal | f0b6db7 | 2018-08-13 16:10:14 -0700 | [diff] [blame] | 2065 | a = new PropertyListBuilder() |
| 2066 | .scale(initScale) |
| 2067 | .translationX(initDeltaX) |
| 2068 | .translationY(initDeltaY) |
| 2069 | .build(child) |
Sunny Goyal | 9e76f68 | 2017-02-13 12:13:43 -0800 | [diff] [blame] | 2070 | .setDuration(REORDER_ANIMATION_DURATION); |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 2071 | Launcher.cast(mActivity).getDragController().addFirstFrameAnimationHelper(a); |
Sunny Goyal | f0b6db7 | 2018-08-13 16:10:14 -0700 | [diff] [blame] | 2072 | a.setInterpolator(DEACCEL_1_5); |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 2073 | a.start(); |
Brandon Keely | 50e6e56 | 2012-05-08 16:28:49 -0700 | [diff] [blame] | 2074 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2075 | } |
| 2076 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2077 | private void completeAndClearReorderPreviewAnimations() { |
| 2078 | for (ReorderPreviewAnimation a: mShakeAnimators.values()) { |
Brandon Keely | 50e6e56 | 2012-05-08 16:28:49 -0700 | [diff] [blame] | 2079 | a.completeAnimationImmediately(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2080 | } |
| 2081 | mShakeAnimators.clear(); |
| 2082 | } |
| 2083 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2084 | private void commitTempPlacement() { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2085 | mTmpOccupied.copyTo(mOccupied); |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 2086 | |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 2087 | int screenId = Launcher.cast(mActivity).getWorkspace().getIdForScreen(this); |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 2088 | int container = Favorites.CONTAINER_DESKTOP; |
| 2089 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 2090 | if (mContainerType == HOTSEAT) { |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 2091 | screenId = -1; |
| 2092 | container = Favorites.CONTAINER_HOTSEAT; |
| 2093 | } |
| 2094 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2095 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2096 | for (int i = 0; i < childCount; i++) { |
Adam Cohen | ea889a2 | 2012-03-27 16:45:39 -0700 | [diff] [blame] | 2097 | View child = mShortcutsAndWidgets.getChildAt(i); |
| 2098 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 2099 | ItemInfo info = (ItemInfo) child.getTag(); |
Adam Cohen | 2acce88 | 2012-03-28 19:03:19 -0700 | [diff] [blame] | 2100 | // We do a null check here because the item info can be null in the case of the |
| 2101 | // AllApps button in the hotseat. |
| 2102 | if (info != null) { |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 2103 | final boolean requiresDbUpdate = (info.cellX != lp.tmpCellX |
| 2104 | || info.cellY != lp.tmpCellY || info.spanX != lp.cellHSpan |
| 2105 | || info.spanY != lp.cellVSpan); |
| 2106 | |
Adam Cohen | 2acce88 | 2012-03-28 19:03:19 -0700 | [diff] [blame] | 2107 | info.cellX = lp.cellX = lp.tmpCellX; |
| 2108 | info.cellY = lp.cellY = lp.tmpCellY; |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 2109 | info.spanX = lp.cellHSpan; |
| 2110 | info.spanY = lp.cellVSpan; |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 2111 | |
| 2112 | if (requiresDbUpdate) { |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 2113 | Launcher.cast(mActivity).getModelWriter().modifyItemInDatabase(info, container, |
| 2114 | screenId, info.cellX, info.cellY, info.spanX, info.spanY); |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 2115 | } |
Adam Cohen | 2acce88 | 2012-03-28 19:03:19 -0700 | [diff] [blame] | 2116 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2117 | } |
| 2118 | } |
| 2119 | |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 2120 | private void setUseTempCoords(boolean useTempCoords) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2121 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2122 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2123 | LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2124 | lp.useTmpCoords = useTempCoords; |
| 2125 | } |
| 2126 | } |
| 2127 | |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 2128 | private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2129 | int spanX, int spanY, View dragView, ItemConfiguration solution) { |
| 2130 | int[] result = new int[2]; |
| 2131 | int[] resultSpan = new int[2]; |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 2132 | findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2133 | resultSpan); |
| 2134 | if (result[0] >= 0 && result[1] >= 0) { |
| 2135 | copyCurrentStateToSolution(solution, false); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2136 | solution.cellX = result[0]; |
| 2137 | solution.cellY = result[1]; |
| 2138 | solution.spanX = resultSpan[0]; |
| 2139 | solution.spanY = resultSpan[1]; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2140 | solution.isSolution = true; |
| 2141 | } else { |
| 2142 | solution.isSolution = false; |
| 2143 | } |
| 2144 | return solution; |
| 2145 | } |
| 2146 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2147 | /* This seems like it should be obvious and straight-forward, but when the direction vector |
| 2148 | needs to match with the notion of the dragView pushing other views, we have to employ |
| 2149 | a slightly more subtle notion of the direction vector. The question is what two points is |
| 2150 | the vector between? The center of the dragView and its desired destination? Not quite, as |
| 2151 | this doesn't necessarily coincide with the interaction of the dragView and items occupying |
| 2152 | those cells. Instead we use some heuristics to often lock the vector to up, down, left |
| 2153 | or right, which helps make pushing feel right. |
| 2154 | */ |
| 2155 | private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX, |
| 2156 | int spanY, View dragView, int[] resultDirection) { |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 2157 | |
| 2158 | //TODO(adamcohen) b/151776141 use the items visual center for the direction vector |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2159 | int[] targetDestination = new int[2]; |
| 2160 | |
| 2161 | findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination); |
| 2162 | Rect dragRect = new Rect(); |
| 2163 | regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect); |
| 2164 | dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY()); |
| 2165 | |
| 2166 | Rect dropRegionRect = new Rect(); |
| 2167 | getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY, |
| 2168 | dragView, dropRegionRect, mIntersectingViews); |
| 2169 | |
| 2170 | int dropRegionSpanX = dropRegionRect.width(); |
| 2171 | int dropRegionSpanY = dropRegionRect.height(); |
| 2172 | |
| 2173 | regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(), |
| 2174 | dropRegionRect.height(), dropRegionRect); |
| 2175 | |
| 2176 | int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX; |
| 2177 | int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY; |
| 2178 | |
| 2179 | if (dropRegionSpanX == mCountX || spanX == mCountX) { |
| 2180 | deltaX = 0; |
| 2181 | } |
| 2182 | if (dropRegionSpanY == mCountY || spanY == mCountY) { |
| 2183 | deltaY = 0; |
| 2184 | } |
| 2185 | |
| 2186 | if (deltaX == 0 && deltaY == 0) { |
| 2187 | // No idea what to do, give a random direction. |
| 2188 | resultDirection[0] = 1; |
| 2189 | resultDirection[1] = 0; |
| 2190 | } else { |
| 2191 | computeDirectionVector(deltaX, deltaY, resultDirection); |
| 2192 | } |
| 2193 | } |
| 2194 | |
| 2195 | // For a given cell and span, fetch the set of views intersecting the region. |
| 2196 | private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY, |
| 2197 | View dragView, Rect boundingRect, ArrayList<View> intersectingViews) { |
| 2198 | if (boundingRect != null) { |
| 2199 | boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY); |
| 2200 | } |
| 2201 | intersectingViews.clear(); |
| 2202 | Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY); |
| 2203 | Rect r1 = new Rect(); |
| 2204 | final int count = mShortcutsAndWidgets.getChildCount(); |
| 2205 | for (int i = 0; i < count; i++) { |
| 2206 | View child = mShortcutsAndWidgets.getChildAt(i); |
| 2207 | if (child == dragView) continue; |
| 2208 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 2209 | r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan); |
| 2210 | if (Rect.intersects(r0, r1)) { |
| 2211 | mIntersectingViews.add(child); |
| 2212 | if (boundingRect != null) { |
| 2213 | boundingRect.union(r1); |
| 2214 | } |
| 2215 | } |
| 2216 | } |
| 2217 | } |
| 2218 | |
| 2219 | boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY, |
| 2220 | View dragView, int[] result) { |
| 2221 | result = findNearestArea(pixelX, pixelY, spanX, spanY, result); |
| 2222 | getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null, |
| 2223 | mIntersectingViews); |
| 2224 | return !mIntersectingViews.isEmpty(); |
| 2225 | } |
| 2226 | |
| 2227 | void revertTempState() { |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2228 | completeAndClearReorderPreviewAnimations(); |
| 2229 | if (isItemPlacementDirty() && !DESTRUCTIVE_REORDER) { |
| 2230 | final int count = mShortcutsAndWidgets.getChildCount(); |
| 2231 | for (int i = 0; i < count; i++) { |
| 2232 | View child = mShortcutsAndWidgets.getChildAt(i); |
| 2233 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 2234 | if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) { |
| 2235 | lp.tmpCellX = lp.cellX; |
| 2236 | lp.tmpCellY = lp.cellY; |
| 2237 | animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION, |
| 2238 | 0, false, false); |
| 2239 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2240 | } |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2241 | setItemPlacementDirty(false); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2242 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 2245 | boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY, |
| 2246 | View dragView, int[] direction, boolean commit) { |
| 2247 | int[] pixelXY = new int[2]; |
| 2248 | regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY); |
| 2249 | |
| 2250 | // First we determine if things have moved enough to cause a different layout |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2251 | ItemConfiguration swapSolution = findReorderSolution(pixelXY[0], pixelXY[1], spanX, spanY, |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 2252 | spanX, spanY, direction, dragView, true, new ItemConfiguration()); |
| 2253 | |
| 2254 | setUseTempCoords(true); |
| 2255 | if (swapSolution != null && swapSolution.isSolution) { |
| 2256 | // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother |
| 2257 | // committing anything or animating anything as we just want to determine if a solution |
| 2258 | // exists |
| 2259 | copySolutionToTempState(swapSolution, dragView); |
| 2260 | setItemPlacementDirty(true); |
| 2261 | animateItemsToSolution(swapSolution, dragView, commit); |
| 2262 | |
| 2263 | if (commit) { |
| 2264 | commitTempPlacement(); |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2265 | completeAndClearReorderPreviewAnimations(); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 2266 | setItemPlacementDirty(false); |
| 2267 | } else { |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2268 | beginOrAdjustReorderPreviewAnimations(swapSolution, dragView, |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 2269 | ReorderPreviewAnimation.MODE_PREVIEW); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 2270 | } |
| 2271 | mShortcutsAndWidgets.requestLayout(); |
| 2272 | } |
| 2273 | return swapSolution.isSolution; |
| 2274 | } |
| 2275 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2276 | int[] performReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2277 | View dragView, int[] result, int resultSpan[], int mode) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2278 | // First we determine if things have moved enough to cause a different layout |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 2279 | result = findNearestArea(pixelX, pixelY, spanX, spanY, result); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2280 | |
| 2281 | if (resultSpan == null) { |
| 2282 | resultSpan = new int[2]; |
| 2283 | } |
| 2284 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2285 | // When we are checking drop validity or actually dropping, we don't recompute the |
| 2286 | // direction vector, since we want the solution to match the preview, and it's possible |
| 2287 | // that the exact position of the item has changed to result in a new reordering outcome. |
Adam Cohen | b209e63 | 2012-03-27 17:09:36 -0700 | [diff] [blame] | 2288 | if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP) |
| 2289 | && mPreviousReorderDirection[0] != INVALID_DIRECTION) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2290 | mDirectionVector[0] = mPreviousReorderDirection[0]; |
| 2291 | mDirectionVector[1] = mPreviousReorderDirection[1]; |
| 2292 | // We reset this vector after drop |
Adam Cohen | b209e63 | 2012-03-27 17:09:36 -0700 | [diff] [blame] | 2293 | if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) { |
| 2294 | mPreviousReorderDirection[0] = INVALID_DIRECTION; |
| 2295 | mPreviousReorderDirection[1] = INVALID_DIRECTION; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2296 | } |
| 2297 | } else { |
| 2298 | getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector); |
| 2299 | mPreviousReorderDirection[0] = mDirectionVector[0]; |
| 2300 | mPreviousReorderDirection[1] = mDirectionVector[1]; |
| 2301 | } |
| 2302 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2303 | // Find a solution involving pushing / displacing any items in the way |
| 2304 | ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2305 | spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration()); |
| 2306 | |
| 2307 | // We attempt the approach which doesn't shuffle views at all |
| 2308 | ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX, |
| 2309 | minSpanY, spanX, spanY, dragView, new ItemConfiguration()); |
| 2310 | |
| 2311 | ItemConfiguration finalSolution = null; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2312 | |
| 2313 | // If the reorder solution requires resizing (shrinking) the item being dropped, we instead |
| 2314 | // favor a solution in which the item is not resized, but |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2315 | if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) { |
| 2316 | finalSolution = swapSolution; |
| 2317 | } else if (noShuffleSolution.isSolution) { |
| 2318 | finalSolution = noShuffleSolution; |
| 2319 | } |
| 2320 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2321 | if (mode == MODE_SHOW_REORDER_HINT) { |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2322 | if (finalSolution != null) { |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 2323 | beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, |
Adam Cohen | fe69287 | 2013-12-11 14:47:23 -0800 | [diff] [blame] | 2324 | ReorderPreviewAnimation.MODE_HINT); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2325 | result[0] = finalSolution.cellX; |
| 2326 | result[1] = finalSolution.cellY; |
| 2327 | resultSpan[0] = finalSolution.spanX; |
| 2328 | resultSpan[1] = finalSolution.spanY; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2329 | } else { |
| 2330 | result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1; |
| 2331 | } |
| 2332 | return result; |
| 2333 | } |
| 2334 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2335 | boolean foundSolution = true; |
| 2336 | if (!DESTRUCTIVE_REORDER) { |
| 2337 | setUseTempCoords(true); |
| 2338 | } |
| 2339 | |
| 2340 | if (finalSolution != null) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2341 | result[0] = finalSolution.cellX; |
| 2342 | result[1] = finalSolution.cellY; |
| 2343 | resultSpan[0] = finalSolution.spanX; |
| 2344 | resultSpan[1] = finalSolution.spanY; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2345 | |
| 2346 | // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother |
| 2347 | // committing anything or animating anything as we just want to determine if a solution |
| 2348 | // exists |
| 2349 | if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) { |
| 2350 | if (!DESTRUCTIVE_REORDER) { |
| 2351 | copySolutionToTempState(finalSolution, dragView); |
| 2352 | } |
| 2353 | setItemPlacementDirty(true); |
| 2354 | animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP); |
| 2355 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2356 | if (!DESTRUCTIVE_REORDER && |
| 2357 | (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2358 | commitTempPlacement(); |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2359 | completeAndClearReorderPreviewAnimations(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2360 | setItemPlacementDirty(false); |
| 2361 | } else { |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2362 | beginOrAdjustReorderPreviewAnimations(finalSolution, dragView, |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 2363 | ReorderPreviewAnimation.MODE_PREVIEW); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2364 | } |
| 2365 | } |
| 2366 | } else { |
| 2367 | foundSolution = false; |
| 2368 | result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1; |
| 2369 | } |
| 2370 | |
| 2371 | if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) { |
| 2372 | setUseTempCoords(false); |
| 2373 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2374 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2375 | mShortcutsAndWidgets.requestLayout(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2376 | return result; |
| 2377 | } |
| 2378 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2379 | void setItemPlacementDirty(boolean dirty) { |
| 2380 | mItemPlacementDirty = dirty; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2381 | } |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2382 | boolean isItemPlacementDirty() { |
| 2383 | return mItemPlacementDirty; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2384 | } |
| 2385 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2386 | private static class ItemConfiguration extends CellAndSpan { |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 2387 | final ArrayMap<View, CellAndSpan> map = new ArrayMap<>(); |
| 2388 | private final ArrayMap<View, CellAndSpan> savedMap = new ArrayMap<>(); |
| 2389 | final ArrayList<View> sortedViews = new ArrayList<>(); |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2390 | ArrayList<View> intersectingViews; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2391 | boolean isSolution = false; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2392 | |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 2393 | void save() { |
| 2394 | // Copy current state into savedMap |
| 2395 | for (View v: map.keySet()) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2396 | savedMap.get(v).copyFrom(map.get(v)); |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | void restore() { |
| 2401 | // Restore current state from savedMap |
| 2402 | for (View v: savedMap.keySet()) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2403 | map.get(v).copyFrom(savedMap.get(v)); |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 2404 | } |
| 2405 | } |
| 2406 | |
| 2407 | void add(View v, CellAndSpan cs) { |
| 2408 | map.put(v, cs); |
| 2409 | savedMap.put(v, new CellAndSpan()); |
| 2410 | sortedViews.add(v); |
| 2411 | } |
| 2412 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2413 | int area() { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2414 | return spanX * spanY; |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 2415 | } |
| 2416 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2417 | void getBoundingRectForViews(ArrayList<View> views, Rect outRect) { |
| 2418 | boolean first = true; |
| 2419 | for (View v: views) { |
| 2420 | CellAndSpan c = map.get(v); |
| 2421 | if (first) { |
| 2422 | outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY); |
| 2423 | first = false; |
| 2424 | } else { |
| 2425 | outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY); |
| 2426 | } |
| 2427 | } |
Adam Cohen | f3900c2 | 2012-11-16 18:28:11 -0800 | [diff] [blame] | 2428 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2429 | } |
| 2430 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2431 | /** |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2432 | * Find a starting cell position that will fit the given bounds nearest the requested |
| 2433 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 2434 | * |
| 2435 | * @param pixelX The X location at which you want to search for a vacant area. |
| 2436 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 2437 | * @param spanX Horizontal span of the object. |
| 2438 | * @param spanY Vertical span of the object. |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2439 | * @param result Previously returned value to possibly recycle. |
| 2440 | * @return The X, Y cell of a vacant area that can contain this object, |
| 2441 | * nearest the requested location. |
| 2442 | */ |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 2443 | public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) { |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 2444 | return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2447 | boolean existsEmptyCell() { |
| 2448 | return findCellForSpan(null, 1, 1); |
| 2449 | } |
| 2450 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2451 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2452 | * Finds the upper-left coordinate of the first rectangle in the grid that can |
| 2453 | * hold a cell of the specified dimensions. If intersectX and intersectY are not -1, |
| 2454 | * then this method will only return coordinates for rectangles that contain the cell |
| 2455 | * (intersectX, intersectY) |
| 2456 | * |
| 2457 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 2458 | * can be found. |
| 2459 | * @param spanX The horizontal span of the cell we want to find. |
| 2460 | * @param spanY The vertical span of the cell we want to find. |
| 2461 | * |
| 2462 | * @return True if a vacant cell of the specified dimension was found, false otherwise. |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 2463 | */ |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 2464 | public boolean findCellForSpan(int[] cellXY, int spanX, int spanY) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2465 | if (cellXY == null) { |
| 2466 | cellXY = new int[2]; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2467 | } |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2468 | return mOccupied.findVacantCell(cellXY, spanX, spanY); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | /** |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2472 | * A drag event has begun over this layout. |
| 2473 | * It may have begun over this layout (in which case onDragChild is called first), |
| 2474 | * or it may have begun on another layout. |
| 2475 | */ |
| 2476 | void onDragEnter() { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2477 | mDragging = true; |
| 2478 | } |
| 2479 | |
| 2480 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2481 | * Called when drag has left this CellLayout or has been completed (successfully or not) |
| 2482 | */ |
| 2483 | void onDragExit() { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2484 | // This can actually be called when we aren't in a drag, e.g. when adding a new |
| 2485 | // item to this layout via the customize drawer. |
| 2486 | // Guard against that case. |
| 2487 | if (mDragging) { |
| 2488 | mDragging = false; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 2489 | } |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 2490 | |
| 2491 | // Invalidate the drag data |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2492 | mDragCell[0] = mDragCell[1] = -1; |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 2493 | mDragOutlineAnims[mDragOutlineCurrent].animateOut(); |
| 2494 | mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2495 | revertTempState(); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 2496 | setIsDragOverlapping(false); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2500 | * Mark a child as having been dropped. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 2501 | * At the beginning of the drag operation, the child may have been on another |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 2502 | * screen, but it is re-parented before this method is called. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2503 | * |
| 2504 | * @param child The child that is being dropped |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2505 | */ |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 2506 | void onDropChild(View child) { |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 2507 | if (child != null) { |
| 2508 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 2509 | lp.dropped = true; |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 2510 | child.requestLayout(); |
Tony Wickham | 1cdb6d0 | 2015-09-17 11:08:27 -0700 | [diff] [blame] | 2511 | markCellsAsOccupiedForView(child); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 2512 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2513 | } |
| 2514 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2515 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2516 | * Computes a bounding rectangle for a range of cells |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2517 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2518 | * @param cellX X coordinate of upper left corner expressed as a cell position |
| 2519 | * @param cellY Y coordinate of upper left corner expressed as a cell position |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2520 | * @param cellHSpan Width in cells |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2521 | * @param cellVSpan Height in cells |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 2522 | * @param resultRect Rect into which to put the results |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2523 | */ |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2524 | public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2525 | final int cellWidth = mCellWidth; |
| 2526 | final int cellHeight = mCellHeight; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2527 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 2528 | final int hStartPadding = getPaddingLeft(); |
| 2529 | final int vStartPadding = getPaddingTop(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2530 | |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 2531 | int width = cellHSpan * cellWidth; |
| 2532 | int height = cellVSpan * cellHeight; |
| 2533 | int x = hStartPadding + cellX * cellWidth; |
| 2534 | int y = vStartPadding + cellY * cellHeight; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2535 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 2536 | resultRect.set(x, y, x + width, y + height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2537 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2538 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2539 | public void markCellsAsOccupiedForView(View view) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2540 | if (view == null || view.getParent() != mShortcutsAndWidgets) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2541 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2542 | mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2543 | } |
| 2544 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2545 | public void markCellsAsUnoccupiedForView(View view) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2546 | if (view == null || view.getParent() != mShortcutsAndWidgets) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2547 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2548 | mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2549 | } |
| 2550 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 2551 | public int getDesiredWidth() { |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 2552 | return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | public int getDesiredHeight() { |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 2556 | return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 2557 | } |
| 2558 | |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 2559 | public boolean isOccupied(int x, int y) { |
| 2560 | if (x < mCountX && y < mCountY) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2561 | return mOccupied.cells[x][y]; |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 2562 | } else { |
| 2563 | throw new RuntimeException("Position exceeds the bound of this CellLayout"); |
| 2564 | } |
| 2565 | } |
| 2566 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2567 | @Override |
| 2568 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 2569 | return new CellLayout.LayoutParams(getContext(), attrs); |
| 2570 | } |
| 2571 | |
| 2572 | @Override |
| 2573 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 2574 | return p instanceof CellLayout.LayoutParams; |
| 2575 | } |
| 2576 | |
| 2577 | @Override |
| 2578 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 2579 | return new CellLayout.LayoutParams(p); |
| 2580 | } |
| 2581 | |
| 2582 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
| 2583 | /** |
| 2584 | * Horizontal location of the item in the grid. |
| 2585 | */ |
| 2586 | @ViewDebug.ExportedProperty |
| 2587 | public int cellX; |
| 2588 | |
| 2589 | /** |
| 2590 | * Vertical location of the item in the grid. |
| 2591 | */ |
| 2592 | @ViewDebug.ExportedProperty |
| 2593 | public int cellY; |
| 2594 | |
| 2595 | /** |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2596 | * Temporary horizontal location of the item in the grid during reorder |
| 2597 | */ |
| 2598 | public int tmpCellX; |
| 2599 | |
| 2600 | /** |
| 2601 | * Temporary vertical location of the item in the grid during reorder |
| 2602 | */ |
| 2603 | public int tmpCellY; |
| 2604 | |
| 2605 | /** |
| 2606 | * Indicates that the temporary coordinates should be used to layout the items |
| 2607 | */ |
| 2608 | public boolean useTmpCoords; |
| 2609 | |
| 2610 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2611 | * Number of cells spanned horizontally by the item. |
| 2612 | */ |
| 2613 | @ViewDebug.ExportedProperty |
| 2614 | public int cellHSpan; |
| 2615 | |
| 2616 | /** |
| 2617 | * Number of cells spanned vertically by the item. |
| 2618 | */ |
| 2619 | @ViewDebug.ExportedProperty |
| 2620 | public int cellVSpan; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2621 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2622 | /** |
| 2623 | * Indicates whether the item will set its x, y, width and height parameters freely, |
| 2624 | * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan. |
| 2625 | */ |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2626 | public boolean isLockedToGrid = true; |
| 2627 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2628 | /** |
| 2629 | * Indicates whether this item can be reordered. Always true except in the case of the |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 2630 | * the AllApps button and QSB place holder. |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2631 | */ |
| 2632 | public boolean canReorder = true; |
| 2633 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2634 | // X coordinate of the view in the layout. |
| 2635 | @ViewDebug.ExportedProperty |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 2636 | public int x; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2637 | // Y coordinate of the view in the layout. |
| 2638 | @ViewDebug.ExportedProperty |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 2639 | public int y; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2640 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 2641 | boolean dropped; |
Romain Guy | fcb9e71 | 2009-10-02 16:06:52 -0700 | [diff] [blame] | 2642 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2643 | public LayoutParams(Context c, AttributeSet attrs) { |
| 2644 | super(c, attrs); |
| 2645 | cellHSpan = 1; |
| 2646 | cellVSpan = 1; |
| 2647 | } |
| 2648 | |
| 2649 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 2650 | super(source); |
| 2651 | cellHSpan = 1; |
| 2652 | cellVSpan = 1; |
| 2653 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2654 | |
| 2655 | public LayoutParams(LayoutParams source) { |
| 2656 | super(source); |
| 2657 | this.cellX = source.cellX; |
| 2658 | this.cellY = source.cellY; |
| 2659 | this.cellHSpan = source.cellHSpan; |
| 2660 | this.cellVSpan = source.cellVSpan; |
| 2661 | } |
| 2662 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2663 | public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) { |
Romain Guy | 8f19cdd | 2010-01-08 15:07:00 -0800 | [diff] [blame] | 2664 | super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2665 | this.cellX = cellX; |
| 2666 | this.cellY = cellY; |
| 2667 | this.cellHSpan = cellHSpan; |
| 2668 | this.cellVSpan = cellVSpan; |
| 2669 | } |
| 2670 | |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 2671 | public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) { |
Jon Miranda | 7ae64ff | 2016-11-21 16:18:46 -0800 | [diff] [blame] | 2672 | setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f); |
| 2673 | } |
| 2674 | |
| 2675 | /** |
| 2676 | * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs |
| 2677 | * to be scaled. |
| 2678 | * |
| 2679 | * ie. In multi-window mode, we setup widgets so that they are measured and laid out |
| 2680 | * using their full/invariant device profile sizes. |
| 2681 | */ |
| 2682 | public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount, |
| 2683 | float cellScaleX, float cellScaleY) { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2684 | if (isLockedToGrid) { |
| 2685 | final int myCellHSpan = cellHSpan; |
| 2686 | final int myCellVSpan = cellVSpan; |
Adam Cohen | 2374abf | 2013-04-16 14:56:57 -0700 | [diff] [blame] | 2687 | int myCellX = useTmpCoords ? tmpCellX : cellX; |
| 2688 | int myCellY = useTmpCoords ? tmpCellY : cellY; |
| 2689 | |
| 2690 | if (invertHorizontally) { |
| 2691 | myCellX = colCount - myCellX - cellHSpan; |
| 2692 | } |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2693 | |
Jon Miranda | 7ae64ff | 2016-11-21 16:18:46 -0800 | [diff] [blame] | 2694 | width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin); |
| 2695 | height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin); |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 2696 | x = (myCellX * cellWidth + leftMargin); |
| 2697 | y = (myCellY * cellHeight + topMargin); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2698 | } |
| 2699 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2700 | |
Sunny Goyal | 11a5074 | 2019-08-07 09:24:26 -0700 | [diff] [blame] | 2701 | /** |
| 2702 | * Sets the position to the provided point |
| 2703 | */ |
| 2704 | public void setXY(Point point) { |
| 2705 | cellX = point.x; |
| 2706 | cellY = point.y; |
| 2707 | } |
| 2708 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2709 | public String toString() { |
| 2710 | return "(" + this.cellX + ", " + this.cellY + ")"; |
| 2711 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2712 | } |
| 2713 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2714 | // This class stores info for two purposes: |
| 2715 | // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY, |
| 2716 | // its spanX, spanY, and the screen it is on |
| 2717 | // 2. When long clicking on an empty cell in a CellLayout, we save information about the |
| 2718 | // cellX and cellY coordinates and which page was clicked. We then set this as a tag on |
| 2719 | // the CellLayout that was long clicked |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2720 | public static final class CellInfo extends CellAndSpan { |
Rajeev Kumar | 9962dbe | 2017-06-12 12:16:20 -0700 | [diff] [blame] | 2721 | public final View cell; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 2722 | final int screenId; |
| 2723 | final int container; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2724 | |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 2725 | public CellInfo(View v, ItemInfo info) { |
Adam Cohen | e0aaa0d | 2014-05-12 12:44:22 -0700 | [diff] [blame] | 2726 | cellX = info.cellX; |
| 2727 | cellY = info.cellY; |
| 2728 | spanX = info.spanX; |
| 2729 | spanY = info.spanY; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2730 | cell = v; |
Adam Cohen | e0aaa0d | 2014-05-12 12:44:22 -0700 | [diff] [blame] | 2731 | screenId = info.screenId; |
| 2732 | container = info.container; |
| 2733 | } |
| 2734 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2735 | @Override |
| 2736 | public String toString() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2737 | return "Cell[view=" + (cell == null ? "null" : cell.getClass()) |
| 2738 | + ", x=" + cellX + ", y=" + cellY + "]"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2739 | } |
| 2740 | } |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 2741 | |
Tony Wickham | 8693061 | 2015-09-09 13:50:40 -0700 | [diff] [blame] | 2742 | /** |
Samuel Fufa | 1e2d004 | 2019-11-18 17:12:46 -0800 | [diff] [blame] | 2743 | * A Delegated cell Drawing for drawing on CellLayout |
| 2744 | */ |
| 2745 | public abstract static class DelegatedCellDrawing { |
| 2746 | public int mDelegateCellX; |
| 2747 | public int mDelegateCellY; |
| 2748 | |
| 2749 | /** |
| 2750 | * Draw under CellLayout |
| 2751 | */ |
| 2752 | public abstract void drawUnderItem(Canvas canvas); |
| 2753 | |
| 2754 | /** |
| 2755 | * Draw over CellLayout |
| 2756 | */ |
| 2757 | public abstract void drawOverItem(Canvas canvas); |
| 2758 | } |
| 2759 | |
| 2760 | /** |
Tony Wickham | 8693061 | 2015-09-09 13:50:40 -0700 | [diff] [blame] | 2761 | * Returns whether an item can be placed in this CellLayout (after rearranging and/or resizing |
| 2762 | * if necessary). |
| 2763 | */ |
| 2764 | public boolean hasReorderSolution(ItemInfo itemInfo) { |
| 2765 | int[] cellPoint = new int[2]; |
| 2766 | // Check for a solution starting at every cell. |
| 2767 | for (int cellX = 0; cellX < getCountX(); cellX++) { |
| 2768 | for (int cellY = 0; cellY < getCountY(); cellY++) { |
| 2769 | cellToPoint(cellX, cellY, cellPoint); |
| 2770 | if (findReorderSolution(cellPoint[0], cellPoint[1], itemInfo.minSpanX, |
| 2771 | itemInfo.minSpanY, itemInfo.spanX, itemInfo.spanY, mDirectionVector, null, |
| 2772 | true, new ItemConfiguration()).isSolution) { |
| 2773 | return true; |
| 2774 | } |
| 2775 | } |
| 2776 | } |
| 2777 | return false; |
| 2778 | } |
| 2779 | |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 2780 | /** |
| 2781 | * Finds solution to accept hotseat migration to cell layout. commits solution if commitConfig |
| 2782 | */ |
| 2783 | public boolean makeSpaceForHotseatMigration(boolean commitConfig) { |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 2784 | int[] cellPoint = new int[2]; |
| 2785 | int[] directionVector = new int[]{0, -1}; |
| 2786 | cellToPoint(0, mCountY, cellPoint); |
| 2787 | ItemConfiguration configuration = new ItemConfiguration(); |
| 2788 | if (findReorderSolution(cellPoint[0], cellPoint[1], mCountX, 1, mCountX, 1, |
| 2789 | directionVector, null, false, configuration).isSolution) { |
| 2790 | if (commitConfig) { |
| 2791 | copySolutionToTempState(configuration, null); |
| 2792 | commitTempPlacement(); |
Samuel Fufa | 82bbdac | 2020-03-09 18:24:47 -0700 | [diff] [blame] | 2793 | // undo marking cells occupied since there is actually nothing being placed yet. |
| 2794 | mOccupied.markCells(0, mCountY - 1, mCountX, 1, false); |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 2795 | } |
| 2796 | return true; |
| 2797 | } |
| 2798 | return false; |
| 2799 | } |
| 2800 | |
Samuel Fufa | 82bbdac | 2020-03-09 18:24:47 -0700 | [diff] [blame] | 2801 | /** |
| 2802 | * returns a copy of cell layout's grid occupancy |
| 2803 | */ |
| 2804 | public GridOccupancy cloneGridOccupancy() { |
| 2805 | GridOccupancy occupancy = new GridOccupancy(mCountX, mCountY); |
| 2806 | mOccupied.copyTo(occupancy); |
| 2807 | return occupancy; |
| 2808 | } |
| 2809 | |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 2810 | public boolean isRegionVacant(int x, int y, int spanX, int spanY) { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 2811 | return mOccupied.isRegionVacant(x, y, spanX, spanY); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 2812 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2813 | } |