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