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