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