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