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