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