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