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