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