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