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