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