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