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