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