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