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