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