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