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