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