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