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.AnimatorSet; |
| 22 | import android.animation.ObjectAnimator; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 23 | import android.animation.PropertyValuesHolder; |
Chet Haase | 00397b1 | 2010-10-07 11:13:10 -0700 | [diff] [blame] | 24 | import android.animation.TimeInterpolator; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 25 | import android.animation.ValueAnimator; |
| 26 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 27 | import android.content.Context; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 28 | import android.content.res.Resources; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 29 | import android.content.res.TypedArray; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 30 | import android.graphics.Bitmap; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 31 | import android.graphics.Canvas; |
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; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 35 | import android.graphics.Rect; |
| 36 | import android.graphics.RectF; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 37 | import android.graphics.Region; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 38 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 39 | import android.util.AttributeSet; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 40 | import android.util.Log; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | import android.view.MotionEvent; |
| 42 | import android.view.View; |
| 43 | import android.view.ViewDebug; |
| 44 | import android.view.ViewGroup; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 45 | import android.view.animation.Animation; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 46 | import android.view.animation.DecelerateInterpolator; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 47 | import android.view.animation.LayoutAnimationController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 48 | |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 49 | import com.android.launcher.R; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 50 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 51 | import java.util.Arrays; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 52 | import java.util.HashMap; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 53 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 54 | public class CellLayout extends ViewGroup { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 55 | static final String TAG = "CellLayout"; |
| 56 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 57 | private int mOriginalCellWidth; |
| 58 | private int mOriginalCellHeight; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 59 | private int mCellWidth; |
| 60 | private int mCellHeight; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 61 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 62 | private int mCountX; |
| 63 | private int mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 64 | |
| 65 | private int mWidthGap; |
| 66 | private int mHeightGap; |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 67 | private int mMaxGap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 68 | |
| 69 | private final Rect mRect = new Rect(); |
| 70 | private final CellInfo mCellInfo = new CellInfo(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 71 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 72 | // These are temporary variables to prevent having to allocate a new object just to |
| 73 | // 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] | 74 | private final int[] mTmpXY = new int[2]; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 75 | private final int[] mTmpPoint = new int[2]; |
| 76 | private final PointF mTmpPointF = new PointF(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 77 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 78 | boolean[][] mOccupied; |
| 79 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 80 | private OnTouchListener mInterceptTouchListener; |
| 81 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 82 | private float mBackgroundAlpha; |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 83 | private float mBackgroundAlphaMultiplier = 1.0f; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 84 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 85 | private Drawable mNormalBackground; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 86 | private Drawable mActiveBackground; |
| 87 | private Drawable mActiveGlowBackground; |
| 88 | private Drawable mNormalBackgroundMini; |
| 89 | private Drawable mNormalGlowBackgroundMini; |
| 90 | private Drawable mActiveBackgroundMini; |
| 91 | private Drawable mActiveGlowBackgroundMini; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 92 | private Rect mBackgroundRect; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 93 | private Rect mGlowBackgroundRect; |
| 94 | private float mGlowBackgroundScale; |
| 95 | private float mGlowBackgroundAlpha; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 96 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 97 | private boolean mAcceptsDrops = true; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 98 | // If we're actively dragging something over this screen, mIsDragOverlapping is true |
| 99 | private boolean mIsDragOverlapping = false; |
| 100 | private boolean mIsDragOccuring = false; |
| 101 | private boolean mIsDefaultDropTarget = false; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 102 | private final Point mDragCenter = new Point(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 103 | |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 104 | // 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] | 105 | // They are used as circular arrays, indexed by mDragOutlineCurrent. |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 106 | private Point[] mDragOutlines = new Point[4]; |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 107 | private float[] mDragOutlineAlphas = new float[mDragOutlines.length]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 108 | private InterruptibleInOutAnimator[] mDragOutlineAnims = |
| 109 | new InterruptibleInOutAnimator[mDragOutlines.length]; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 110 | |
| 111 | // 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] | 112 | private int mDragOutlineCurrent = 0; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 113 | private final Paint mDragOutlinePaint = new Paint(); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 114 | |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 115 | private BubbleTextView mPressedOrFocusedIcon; |
| 116 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 117 | private Drawable mCrosshairsDrawable = null; |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 118 | private InterruptibleInOutAnimator mCrosshairsAnimator = null; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 119 | private float mCrosshairsVisibility = 0.0f; |
| 120 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 121 | private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new |
| 122 | HashMap<CellLayout.LayoutParams, ObjectAnimator>(); |
| 123 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 124 | // When a drag operation is in progress, holds the nearest cell to the touch point |
| 125 | private final int[] mDragCell = new int[2]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 126 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 127 | private boolean mDragging = false; |
| 128 | |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 129 | private TimeInterpolator mEaseOutInterpolator; |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 130 | private CellLayoutChildren mChildren; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 131 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 132 | public CellLayout(Context context) { |
| 133 | this(context, null); |
| 134 | } |
| 135 | |
| 136 | public CellLayout(Context context, AttributeSet attrs) { |
| 137 | this(context, attrs, 0); |
| 138 | } |
| 139 | |
| 140 | public CellLayout(Context context, AttributeSet attrs, int defStyle) { |
| 141 | super(context, attrs, defStyle); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 142 | |
| 143 | // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show |
| 144 | // the user where a dragged item will land when dropped. |
| 145 | setWillNotDraw(false); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 146 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 147 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0); |
| 148 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 149 | mOriginalCellWidth = |
| 150 | mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10); |
| 151 | mOriginalCellHeight = |
| 152 | mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10); |
| 153 | mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0); |
| 154 | mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0); |
| 155 | mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 156 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 157 | mCountX = LauncherModel.getCellCountX(); |
| 158 | mCountY = LauncherModel.getCellCountY(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 159 | mOccupied = new boolean[mCountX][mCountY]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 160 | |
| 161 | a.recycle(); |
| 162 | |
| 163 | setAlwaysDrawnWithCacheEnabled(false); |
| 164 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 165 | final Resources res = getResources(); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 166 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 167 | mNormalBackground = res.getDrawable(R.drawable.homescreen_large_blue); |
| 168 | mActiveBackground = res.getDrawable(R.drawable.homescreen_large_green); |
| 169 | mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_large_green_strong); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 170 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 171 | mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue); |
| 172 | mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong); |
| 173 | mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green); |
| 174 | mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 175 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 176 | mNormalBackground.setFilterBitmap(true); |
| 177 | mActiveBackground.setFilterBitmap(true); |
| 178 | mActiveGlowBackground.setFilterBitmap(true); |
| 179 | mNormalBackgroundMini.setFilterBitmap(true); |
| 180 | mNormalGlowBackgroundMini.setFilterBitmap(true); |
| 181 | mActiveBackgroundMini.setFilterBitmap(true); |
| 182 | mActiveGlowBackgroundMini.setFilterBitmap(true); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 183 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 184 | // Initialize the data structures used for the drag visualization. |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 185 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 186 | mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 187 | mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 188 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 189 | // Set up the animation for fading the crosshairs in and out |
| 190 | int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime); |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 191 | mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 192 | mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() { |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 193 | public void onAnimationUpdate(ValueAnimator animation) { |
| 194 | mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue(); |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 195 | invalidate(); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 196 | } |
| 197 | }); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 198 | mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 199 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 200 | for (int i = 0; i < mDragOutlines.length; i++) { |
| 201 | mDragOutlines[i] = new Point(-1, -1); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // When dragging things around the home screens, we show a green outline of |
| 205 | // where the item will land. The outlines gradually fade out, leaving a trail |
| 206 | // behind the drag path. |
| 207 | // Set up all the animations that are used to implement this fading. |
| 208 | final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 209 | final float fromAlphaValue = 0; |
| 210 | final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 211 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 212 | Arrays.fill(mDragOutlineAlphas, fromAlphaValue); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 213 | |
| 214 | for (int i = 0; i < mDragOutlineAnims.length; i++) { |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 215 | final InterruptibleInOutAnimator anim = |
| 216 | new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 217 | anim.getAnimator().setInterpolator(mEaseOutInterpolator); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 218 | final int thisIndex = i; |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 219 | anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 220 | public void onAnimationUpdate(ValueAnimator animation) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 221 | final Bitmap outline = (Bitmap)anim.getTag(); |
| 222 | |
| 223 | // If an animation is started and then stopped very quickly, we can still |
| 224 | // get spurious updates we've cleared the tag. Guard against this. |
| 225 | if (outline == null) { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 226 | if (false) { |
| 227 | Object val = animation.getAnimatedValue(); |
| 228 | Log.d(TAG, "anim " + thisIndex + " update: " + val + |
| 229 | ", isStopped " + anim.isStopped()); |
| 230 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 231 | // Try to prevent it from continuing to run |
| 232 | animation.cancel(); |
| 233 | } else { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 234 | mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue(); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 235 | final int left = mDragOutlines[thisIndex].x; |
| 236 | final int top = mDragOutlines[thisIndex].y; |
| 237 | CellLayout.this.invalidate(left, top, |
| 238 | left + outline.getWidth(), top + outline.getHeight()); |
| 239 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 240 | } |
| 241 | }); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 242 | // The animation holds a reference to the drag outline bitmap as long is it's |
| 243 | // 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] | 244 | anim.getAnimator().addListener(new AnimatorListenerAdapter() { |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 245 | @Override |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 246 | public void onAnimationEnd(Animator animation) { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 247 | if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 248 | anim.setTag(null); |
| 249 | } |
| 250 | } |
| 251 | }); |
| 252 | mDragOutlineAnims[i] = anim; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 253 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 254 | |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 255 | mBackgroundRect = new Rect(); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 256 | mGlowBackgroundRect = new Rect(); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 257 | setHoverScale(1.0f); |
| 258 | setHoverAlpha(1.0f); |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 259 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 260 | mChildren = new CellLayoutChildren(context); |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 261 | mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 262 | addView(mChildren); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 265 | static int widthInPortrait(Resources r, int numCells) { |
| 266 | // We use this method from Workspace to figure out how many rows/columns Launcher should |
| 267 | // have. We ignore the left/right padding on CellLayout because it turns out in our design |
| 268 | // 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] | 269 | int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 270 | int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap), |
| 271 | r.getDimensionPixelSize(R.dimen.workspace_height_gap)); |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 272 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 273 | return minGap * (numCells - 1) + cellWidth * numCells; |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 276 | static int heightInLandscape(Resources r, int numCells) { |
| 277 | // We use this method from Workspace to figure out how many rows/columns Launcher should |
| 278 | // have. We ignore the left/right padding on CellLayout because it turns out in our design |
| 279 | // 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] | 280 | int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 281 | int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap), |
| 282 | r.getDimensionPixelSize(R.dimen.workspace_height_gap)); |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 283 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 284 | return minGap * (numCells - 1) + cellHeight * numCells; |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 287 | public void enableHardwareLayers() { |
| 288 | mChildren.enableHardwareLayers(); |
| 289 | } |
| 290 | |
| 291 | public void setGridSize(int x, int y) { |
| 292 | mCountX = x; |
| 293 | mCountY = y; |
| 294 | mOccupied = new boolean[mCountX][mCountY]; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 295 | requestLayout(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 298 | private void invalidateBubbleTextView(BubbleTextView icon) { |
| 299 | final int padding = icon.getPressedOrFocusedBackgroundPadding(); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 300 | invalidate(icon.getLeft() + getPaddingLeft() - padding, |
| 301 | icon.getTop() + getPaddingTop() - padding, |
| 302 | icon.getRight() + getPaddingLeft() + padding, |
| 303 | icon.getBottom() + getPaddingTop() + padding); |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void setPressedOrFocusedIcon(BubbleTextView icon) { |
| 307 | // We draw the pressed or focused BubbleTextView's background in CellLayout because it |
| 308 | // requires an expanded clip rect (due to the glow's blur radius) |
| 309 | BubbleTextView oldIcon = mPressedOrFocusedIcon; |
| 310 | mPressedOrFocusedIcon = icon; |
| 311 | if (oldIcon != null) { |
| 312 | invalidateBubbleTextView(oldIcon); |
| 313 | } |
| 314 | if (mPressedOrFocusedIcon != null) { |
| 315 | invalidateBubbleTextView(mPressedOrFocusedIcon); |
| 316 | } |
| 317 | } |
| 318 | |
Winson Chung | 6e31408 | 2011-01-27 16:46:51 -0800 | [diff] [blame] | 319 | public CellLayoutChildren getChildrenLayout() { |
| 320 | if (getChildCount() > 0) { |
| 321 | return (CellLayoutChildren) getChildAt(0); |
| 322 | } |
| 323 | return null; |
| 324 | } |
| 325 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 326 | public void setIsDefaultDropTarget(boolean isDefaultDropTarget) { |
| 327 | if (mIsDefaultDropTarget != isDefaultDropTarget) { |
| 328 | mIsDefaultDropTarget = isDefaultDropTarget; |
| 329 | invalidate(); |
| 330 | } |
| 331 | } |
| 332 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 333 | void setIsDragOccuring(boolean isDragOccuring) { |
| 334 | if (mIsDragOccuring != isDragOccuring) { |
| 335 | mIsDragOccuring = isDragOccuring; |
| 336 | invalidate(); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void setIsDragOverlapping(boolean isDragOverlapping) { |
| 341 | if (mIsDragOverlapping != isDragOverlapping) { |
| 342 | mIsDragOverlapping = isDragOverlapping; |
| 343 | invalidate(); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | boolean getIsDragOverlapping() { |
| 348 | return mIsDragOverlapping; |
| 349 | } |
| 350 | |
| 351 | private void updateGlowRect() { |
| 352 | float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 353 | int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left)); |
| 354 | int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top)); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 355 | mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY, |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 356 | mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY); |
| 357 | invalidate(); |
| 358 | } |
| 359 | |
| 360 | public void setHoverScale(float scaleFactor) { |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 361 | if (scaleFactor != mGlowBackgroundScale) { |
| 362 | mGlowBackgroundScale = scaleFactor; |
| 363 | updateGlowRect(); |
Michael Jurka | 8deb1e6 | 2011-01-25 16:27:43 -0800 | [diff] [blame] | 364 | if (getParent() != null) { |
| 365 | ((View) getParent()).invalidate(); |
| 366 | } |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | public float getHoverScale() { |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 371 | return mGlowBackgroundScale; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | public float getHoverAlpha() { |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 375 | return mGlowBackgroundAlpha; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | public void setHoverAlpha(float alpha) { |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 379 | mGlowBackgroundAlpha = alpha; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 380 | invalidate(); |
| 381 | } |
| 382 | |
| 383 | void animateDrop() { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 384 | Resources res = getResources(); |
| 385 | float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f; |
| 386 | ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale); |
| 387 | scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration)); |
| 388 | ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f); |
| 389 | scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration)); |
| 390 | ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 391 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 392 | alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay)); |
| 393 | alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDuration)); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 394 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 395 | AnimatorSet bouncer = new AnimatorSet(); |
| 396 | bouncer.play(scaleUp).before(scaleDown); |
| 397 | bouncer.play(scaleUp).with(alphaFadeOut); |
| 398 | bouncer.addListener(new AnimatorListenerAdapter() { |
| 399 | @Override |
| 400 | public void onAnimationStart(Animator animation) { |
| 401 | setIsDragOverlapping(true); |
| 402 | } |
| 403 | @Override |
| 404 | public void onAnimationEnd(Animator animation) { |
| 405 | setIsDragOverlapping(false); |
| 406 | setHoverScale(1.0f); |
| 407 | setHoverAlpha(1.0f); |
| 408 | } |
| 409 | }); |
| 410 | bouncer.start(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 413 | @Override |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 414 | protected void onDraw(Canvas canvas) { |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 415 | // When we're large, we are either drawn in a "hover" state (ie when dragging an item to |
| 416 | // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f) |
| 417 | // When we're small, we are either drawn normally or in the "accepts drops" state (during |
| 418 | // a drag). However, we also drag the mini hover background *over* one of those two |
| 419 | // backgrounds |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 420 | if (mBackgroundAlpha > 0.0f) { |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 421 | Drawable bg; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 422 | boolean mini = getScaleX() < 0.5f; |
| 423 | |
| 424 | if (mIsDragOverlapping) { |
| 425 | // In the mini case, we draw the active_glow bg *over* the active background |
| 426 | bg = mini ? mActiveBackgroundMini : mActiveGlowBackground; |
| 427 | } else if (mIsDragOccuring && mAcceptsDrops) { |
| 428 | bg = mini ? mActiveBackgroundMini : mActiveBackground; |
Adam Cohen | 3af863b | 2011-01-25 12:16:51 -0800 | [diff] [blame] | 429 | } else if (mIsDefaultDropTarget && mini) { |
| 430 | bg = mNormalGlowBackgroundMini; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 431 | } else { |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 432 | bg = mini ? mNormalBackgroundMini : mNormalBackground; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 433 | } |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 434 | |
| 435 | bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255)); |
| 436 | bg.setBounds(mBackgroundRect); |
| 437 | bg.draw(canvas); |
| 438 | |
| 439 | if (mini && mIsDragOverlapping) { |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 440 | boolean modifiedClipRect = false; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 441 | if (mGlowBackgroundScale > 1.0f) { |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 442 | // If the hover background's scale is greater than 1, we'll be drawing outside |
| 443 | // the bounds of this CellLayout. Get around that by temporarily increasing the |
| 444 | // size of the clip rect |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 445 | float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 446 | Rect clipRect = canvas.getClipBounds(); |
| 447 | int marginX = (int) (marginFraction * (clipRect.right - clipRect.left)); |
| 448 | int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top)); |
| 449 | canvas.save(Canvas.CLIP_SAVE_FLAG); |
| 450 | canvas.clipRect(-marginX, -marginY, |
| 451 | getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE); |
| 452 | modifiedClipRect = true; |
| 453 | } |
| 454 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 455 | mActiveGlowBackgroundMini.setAlpha( |
| 456 | (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255)); |
| 457 | mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect); |
| 458 | mActiveGlowBackgroundMini.draw(canvas); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 459 | if (modifiedClipRect) { |
| 460 | canvas.restore(); |
| 461 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 462 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 463 | } |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 464 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 465 | if (mCrosshairsVisibility > 0.0f) { |
| 466 | final int countX = mCountX; |
| 467 | final int countY = mCountY; |
| 468 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 469 | final float MAX_ALPHA = 0.4f; |
| 470 | final int MAX_VISIBLE_DISTANCE = 600; |
| 471 | final float DISTANCE_MULTIPLIER = 0.002f; |
| 472 | |
| 473 | final Drawable d = mCrosshairsDrawable; |
| 474 | final int width = d.getIntrinsicWidth(); |
| 475 | final int height = d.getIntrinsicHeight(); |
| 476 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 477 | int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 478 | for (int col = 0; col <= countX; col++) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 479 | int y = getPaddingTop() - (mHeightGap / 2) - (height / 2); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 480 | for (int row = 0; row <= countY; row++) { |
| 481 | mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y); |
| 482 | float dist = mTmpPointF.length(); |
| 483 | // Crosshairs further from the drag point are more faint |
| 484 | float alpha = Math.min(MAX_ALPHA, |
| 485 | DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist)); |
| 486 | if (alpha > 0.0f) { |
| 487 | d.setBounds(x, y, x + width, y + height); |
| 488 | d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility)); |
| 489 | d.draw(canvas); |
| 490 | } |
| 491 | y += mCellHeight + mHeightGap; |
| 492 | } |
| 493 | x += mCellWidth + mWidthGap; |
| 494 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 495 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 496 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 497 | final Paint paint = mDragOutlinePaint; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 498 | for (int i = 0; i < mDragOutlines.length; i++) { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 499 | final float alpha = mDragOutlineAlphas[i]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 500 | if (alpha > 0) { |
| 501 | final Point p = mDragOutlines[i]; |
| 502 | final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag(); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 503 | paint.setAlpha((int)(alpha + .5f)); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 504 | canvas.drawBitmap(b, p.x, p.y, paint); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 505 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 506 | } |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 507 | |
| 508 | // We draw the pressed or focused BubbleTextView's background in CellLayout because it |
| 509 | // requires an expanded clip rect (due to the glow's blur radius) |
| 510 | if (mPressedOrFocusedIcon != null) { |
| 511 | final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding(); |
| 512 | final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground(); |
| 513 | if (b != null) { |
| 514 | canvas.drawBitmap(b, |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 515 | mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding, |
| 516 | mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding, |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 517 | null); |
| 518 | } |
| 519 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | @Override |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 523 | public void cancelLongPress() { |
| 524 | super.cancelLongPress(); |
| 525 | |
| 526 | // Cancel long press for all children |
| 527 | final int count = getChildCount(); |
| 528 | for (int i = 0; i < count; i++) { |
| 529 | final View child = getChildAt(i); |
| 530 | child.cancelLongPress(); |
| 531 | } |
| 532 | } |
| 533 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 534 | public void setOnInterceptTouchListener(View.OnTouchListener listener) { |
| 535 | mInterceptTouchListener = listener; |
| 536 | } |
| 537 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 538 | int getCountX() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 539 | return mCountX; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | int getCountY() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 543 | return mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 546 | public boolean addViewToCellLayout( |
| 547 | View child, int index, int childId, LayoutParams params, boolean markCells) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 548 | final LayoutParams lp = params; |
| 549 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 550 | // Generate an id for each view, this assumes we have at most 256x256 cells |
| 551 | // per workspace screen |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 552 | 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] | 553 | // If the horizontal or vertical span is set to -1, it is taken to |
| 554 | // mean that it spans the extent of the CellLayout |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 555 | if (lp.cellHSpan < 0) lp.cellHSpan = mCountX; |
| 556 | if (lp.cellVSpan < 0) lp.cellVSpan = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 557 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 558 | child.setId(childId); |
| 559 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 560 | mChildren.addView(child, index, lp); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 561 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 562 | if (markCells) markCellsAsOccupiedForView(child); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 563 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 564 | return true; |
| 565 | } |
| 566 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 567 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 568 | |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 569 | public void setAcceptsDrops(boolean acceptsDrops) { |
| 570 | if (mAcceptsDrops != acceptsDrops) { |
| 571 | mAcceptsDrops = acceptsDrops; |
| 572 | invalidate(); |
| 573 | } |
| 574 | } |
| 575 | |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 576 | public boolean getAcceptsDrops() { |
| 577 | return mAcceptsDrops; |
| 578 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 579 | |
| 580 | @Override |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 581 | public void removeAllViews() { |
| 582 | clearOccupiedCells(); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 583 | mChildren.removeAllViews(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | @Override |
| 587 | public void removeAllViewsInLayout() { |
| 588 | clearOccupiedCells(); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 589 | mChildren.removeAllViewsInLayout(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 592 | public void removeViewWithoutMarkingCells(View view) { |
Michael Jurka | cf6125c | 2011-01-28 15:20:01 -0800 | [diff] [blame] | 593 | mChildren.removeView(view); |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 596 | @Override |
| 597 | public void removeView(View view) { |
| 598 | markCellsAsUnoccupiedForView(view); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 599 | mChildren.removeView(view); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | @Override |
| 603 | public void removeViewAt(int index) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 604 | markCellsAsUnoccupiedForView(mChildren.getChildAt(index)); |
| 605 | mChildren.removeViewAt(index); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | @Override |
| 609 | public void removeViewInLayout(View view) { |
| 610 | markCellsAsUnoccupiedForView(view); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 611 | mChildren.removeViewInLayout(view); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | @Override |
| 615 | public void removeViews(int start, int count) { |
| 616 | for (int i = start; i < start + count; i++) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 617 | markCellsAsUnoccupiedForView(mChildren.getChildAt(i)); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 618 | } |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 619 | mChildren.removeViews(start, count); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | @Override |
| 623 | public void removeViewsInLayout(int start, int count) { |
| 624 | for (int i = start; i < start + count; i++) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 625 | markCellsAsUnoccupiedForView(mChildren.getChildAt(i)); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 626 | } |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 627 | mChildren.removeViewsInLayout(start, count); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 630 | public void drawChildren(Canvas canvas) { |
| 631 | mChildren.draw(canvas); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 632 | } |
| 633 | |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 634 | void buildChildrenLayer() { |
| 635 | mChildren.buildLayer(); |
| 636 | } |
| 637 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 638 | @Override |
| 639 | protected void onAttachedToWindow() { |
| 640 | super.onAttachedToWindow(); |
| 641 | mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this); |
| 642 | } |
| 643 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 644 | public void setTagToCellInfoForPoint(int touchX, int touchY) { |
| 645 | final CellInfo cellInfo = mCellInfo; |
| 646 | final Rect frame = mRect; |
| 647 | final int x = touchX + mScrollX; |
| 648 | final int y = touchY + mScrollY; |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 649 | final int count = mChildren.getChildCount(); |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 650 | |
| 651 | boolean found = false; |
| 652 | for (int i = count - 1; i >= 0; i--) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 653 | final View child = mChildren.getChildAt(i); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 654 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 655 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 656 | if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) && |
| 657 | lp.isLockedToGrid) { |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 658 | child.getHitRect(frame); |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 659 | |
| 660 | // The child hit rect is relative to the CellLayoutChildren parent, so we need to |
| 661 | // offset that by this CellLayout's padding to test an (x,y) point that is relative |
| 662 | // to this view. |
| 663 | final int tmpXY[] = mTmpXY; |
| 664 | child.getLocationOnScreen(tmpXY); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 665 | frame.offset(mPaddingLeft, mPaddingTop); |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 666 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 667 | if (frame.contains(x, y)) { |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 668 | cellInfo.cell = child; |
| 669 | cellInfo.cellX = lp.cellX; |
| 670 | cellInfo.cellY = lp.cellY; |
| 671 | cellInfo.spanX = lp.cellHSpan; |
| 672 | cellInfo.spanY = lp.cellVSpan; |
| 673 | cellInfo.valid = true; |
| 674 | found = true; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 675 | break; |
| 676 | } |
| 677 | } |
| 678 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 679 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 680 | if (!found) { |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 681 | final int cellXY[] = mTmpXY; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 682 | pointToCellExact(x, y, cellXY); |
| 683 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 684 | cellInfo.cell = null; |
| 685 | cellInfo.cellX = cellXY[0]; |
| 686 | cellInfo.cellY = cellXY[1]; |
| 687 | cellInfo.spanX = 1; |
| 688 | cellInfo.spanY = 1; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 689 | cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX && |
| 690 | cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]]; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 691 | } |
| 692 | setTag(cellInfo); |
| 693 | } |
| 694 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 695 | @Override |
| 696 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 697 | if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) { |
| 698 | return true; |
| 699 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 700 | final int action = ev.getAction(); |
| 701 | final CellInfo cellInfo = mCellInfo; |
| 702 | |
| 703 | if (action == MotionEvent.ACTION_DOWN) { |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 704 | setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 705 | } else if (action == MotionEvent.ACTION_UP) { |
| 706 | cellInfo.cell = null; |
| 707 | cellInfo.cellX = -1; |
| 708 | cellInfo.cellY = -1; |
| 709 | cellInfo.spanX = 0; |
| 710 | cellInfo.spanY = 0; |
| 711 | cellInfo.valid = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 712 | setTag(cellInfo); |
| 713 | } |
| 714 | |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | @Override |
| 719 | public CellInfo getTag() { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 720 | return (CellInfo) super.getTag(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 723 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 724 | * 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] | 725 | * @param x X coordinate of the point |
| 726 | * @param y Y coordinate of the point |
| 727 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 728 | */ |
| 729 | void pointToCellExact(int x, int y, int[] result) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 730 | final int hStartPadding = getPaddingLeft(); |
| 731 | final int vStartPadding = getPaddingTop(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 732 | |
| 733 | result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap); |
| 734 | result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap); |
| 735 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 736 | final int xAxis = mCountX; |
| 737 | final int yAxis = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 738 | |
| 739 | if (result[0] < 0) result[0] = 0; |
| 740 | if (result[0] >= xAxis) result[0] = xAxis - 1; |
| 741 | if (result[1] < 0) result[1] = 0; |
| 742 | if (result[1] >= yAxis) result[1] = yAxis - 1; |
| 743 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 744 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 745 | /** |
| 746 | * Given a point, return the cell that most closely encloses that point |
| 747 | * @param x X coordinate of the point |
| 748 | * @param y Y coordinate of the point |
| 749 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 750 | */ |
| 751 | void pointToCellRounded(int x, int y, int[] result) { |
| 752 | pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result); |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * 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] | 757 | * |
| 758 | * @param cellX X coordinate of the cell |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 759 | * @param cellY Y coordinate of the cell |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 760 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 761 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 762 | */ |
| 763 | void cellToPoint(int cellX, int cellY, int[] result) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 764 | final int hStartPadding = getPaddingLeft(); |
| 765 | final int vStartPadding = getPaddingTop(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 766 | |
| 767 | result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap); |
| 768 | result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap); |
| 769 | } |
| 770 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 771 | /** |
| 772 | * Given a cell coordinate, return the point that represents the upper left corner of that cell |
| 773 | * |
| 774 | * @param cellX X coordinate of the cell |
| 775 | * @param cellY Y coordinate of the cell |
| 776 | * |
| 777 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 778 | */ |
| 779 | void cellToCenterPoint(int cellX, int cellY, int[] result) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 780 | final int hStartPadding = getPaddingLeft(); |
| 781 | final int vStartPadding = getPaddingTop(); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 782 | |
| 783 | result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2; |
| 784 | result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2; |
| 785 | } |
| 786 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 787 | int getCellWidth() { |
| 788 | return mCellWidth; |
| 789 | } |
| 790 | |
| 791 | int getCellHeight() { |
| 792 | return mCellHeight; |
| 793 | } |
| 794 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 795 | int getWidthGap() { |
| 796 | return mWidthGap; |
| 797 | } |
| 798 | |
| 799 | int getHeightGap() { |
| 800 | return mHeightGap; |
| 801 | } |
| 802 | |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 803 | Rect getContentRect(Rect r) { |
| 804 | if (r == null) { |
| 805 | r = new Rect(); |
| 806 | } |
| 807 | int left = getPaddingLeft(); |
| 808 | int top = getPaddingTop(); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 809 | int right = left + getWidth() - mPaddingLeft - mPaddingRight; |
| 810 | int bottom = top + getHeight() - mPaddingTop - mPaddingBottom; |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 811 | r.set(left, top, right, bottom); |
| 812 | return r; |
| 813 | } |
| 814 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 815 | @Override |
| 816 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 817 | // TODO: currently ignoring padding |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 818 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 819 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 820 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 821 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 822 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 823 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 824 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 825 | if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { |
| 826 | throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); |
| 827 | } |
| 828 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 829 | int numWidthGaps = mCountX - 1; |
| 830 | int numHeightGaps = mCountY - 1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 831 | |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 832 | if (mWidthGap < 0 || mHeightGap < 0) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 833 | int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight; |
| 834 | int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom; |
| 835 | int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth); |
| 836 | int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight); |
| 837 | mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0); |
| 838 | mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0); |
| 839 | int remainingHSpace = hFreeSpace - (numWidthGaps * mWidthGap); |
| 840 | int remainingVSpace = vFreeSpace - (numHeightGaps * mHeightGap); |
| 841 | mCellWidth = mOriginalCellWidth + remainingHSpace / mCountX; |
| 842 | mCellHeight = mOriginalCellHeight + remainingVSpace / mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 843 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 844 | mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap); |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 845 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 846 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 847 | // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY |
| 848 | int newWidth = widthSpecSize; |
| 849 | int newHeight = heightSpecSize; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 850 | if (widthSpecMode == MeasureSpec.AT_MOST) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 851 | newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) + |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 852 | ((mCountX - 1) * mWidthGap); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 853 | newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) + |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 854 | ((mCountY - 1) * mHeightGap); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 855 | setMeasuredDimension(newWidth, newHeight); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 856 | } |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 857 | |
| 858 | int count = getChildCount(); |
| 859 | for (int i = 0; i < count; i++) { |
| 860 | View child = getChildAt(i); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 861 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft - |
| 862 | mPaddingRight, MeasureSpec.EXACTLY); |
| 863 | int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop - |
| 864 | mPaddingBottom, MeasureSpec.EXACTLY); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 865 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 866 | } |
| 867 | setMeasuredDimension(newWidth, newHeight); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 871 | 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] | 872 | int count = getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 873 | for (int i = 0; i < count; i++) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 874 | View child = getChildAt(i); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 875 | child.layout(mPaddingLeft, mPaddingTop, |
| 876 | r - l - mPaddingRight, b - t - mPaddingBottom); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
| 880 | @Override |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 881 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 882 | super.onSizeChanged(w, h, oldw, oldh); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 883 | mBackgroundRect.set(0, 0, w, h); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 884 | updateGlowRect(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 888 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 889 | mChildren.setChildrenDrawingCacheEnabled(enabled); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | @Override |
| 893 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 894 | mChildren.setChildrenDrawnWithCacheEnabled(enabled); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 895 | } |
| 896 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 897 | public float getBackgroundAlpha() { |
| 898 | return mBackgroundAlpha; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 901 | public void setFastBackgroundAlpha(float alpha) { |
| 902 | mBackgroundAlpha = alpha; |
| 903 | } |
| 904 | |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 905 | public void setBackgroundAlphaMultiplier(float multiplier) { |
| 906 | mBackgroundAlphaMultiplier = multiplier; |
| 907 | } |
| 908 | |
Adam Cohen | ddb8219 | 2010-11-10 16:32:54 -0800 | [diff] [blame] | 909 | public float getBackgroundAlphaMultiplier() { |
| 910 | return mBackgroundAlphaMultiplier; |
| 911 | } |
| 912 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 913 | public void setBackgroundAlpha(float alpha) { |
| 914 | mBackgroundAlpha = alpha; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 915 | invalidate(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 918 | // Need to return true to let the view system know we know how to handle alpha-- this is |
| 919 | // because when our children have an alpha of 0.0f, they are still rendering their "dimmed" |
| 920 | // versions |
| 921 | @Override |
| 922 | protected boolean onSetAlpha(int alpha) { |
| 923 | return true; |
| 924 | } |
| 925 | |
| 926 | public void setAlpha(float alpha) { |
| 927 | setChildrenAlpha(alpha); |
| 928 | super.setAlpha(alpha); |
| 929 | } |
| 930 | |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 931 | public void setFastAlpha(float alpha) { |
| 932 | setFastChildrenAlpha(alpha); |
| 933 | super.setFastAlpha(alpha); |
| 934 | } |
| 935 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 936 | private void setChildrenAlpha(float alpha) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 937 | final int childCount = getChildCount(); |
| 938 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 939 | getChildAt(i).setAlpha(alpha); |
| 940 | } |
| 941 | } |
| 942 | |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 943 | private void setFastChildrenAlpha(float alpha) { |
| 944 | final int childCount = getChildCount(); |
| 945 | for (int i = 0; i < childCount; i++) { |
| 946 | getChildAt(i).setFastAlpha(alpha); |
| 947 | } |
| 948 | } |
| 949 | |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 950 | public View getChildAt(int x, int y) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 951 | return mChildren.getChildAt(x, y); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 954 | public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration, |
| 955 | int delay) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 956 | CellLayoutChildren clc = getChildrenLayout(); |
| 957 | if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) { |
| 958 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 959 | final ItemInfo info = (ItemInfo) child.getTag(); |
| 960 | |
| 961 | // We cancel any existing animations |
| 962 | if (mReorderAnimators.containsKey(lp)) { |
| 963 | mReorderAnimators.get(lp).cancel(); |
| 964 | mReorderAnimators.remove(lp); |
| 965 | } |
| 966 | |
| 967 | int oldX = lp.x; |
| 968 | int oldY = lp.y; |
| 969 | mOccupied[lp.cellX][lp.cellY] = false; |
| 970 | mOccupied[cellX][cellY] = true; |
| 971 | |
| 972 | lp.isLockedToGrid = true; |
| 973 | lp.cellX = info.cellX = cellX; |
| 974 | lp.cellY = info.cellY = cellY; |
| 975 | clc.setupLp(lp); |
| 976 | lp.isLockedToGrid = false; |
| 977 | int newX = lp.x; |
| 978 | int newY = lp.y; |
| 979 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 980 | lp.x = oldX; |
| 981 | lp.y = oldY; |
| 982 | child.requestLayout(); |
| 983 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 984 | PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX); |
| 985 | PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY); |
| 986 | ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y); |
| 987 | oa.setDuration(duration); |
| 988 | mReorderAnimators.put(lp, oa); |
| 989 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 990 | public void onAnimationUpdate(ValueAnimator animation) { |
| 991 | child.requestLayout(); |
| 992 | } |
| 993 | }); |
| 994 | oa.addListener(new AnimatorListenerAdapter() { |
| 995 | boolean cancelled = false; |
| 996 | public void onAnimationEnd(Animator animation) { |
| 997 | // If the animation was cancelled, it means that another animation |
| 998 | // has interrupted this one, and we don't want to lock the item into |
| 999 | // place just yet. |
| 1000 | if (!cancelled) { |
| 1001 | lp.isLockedToGrid = true; |
| 1002 | } |
| 1003 | if (mReorderAnimators.containsKey(lp)) { |
| 1004 | mReorderAnimators.remove(lp); |
| 1005 | } |
| 1006 | } |
| 1007 | public void onAnimationCancel(Animator animation) { |
| 1008 | cancelled = true; |
| 1009 | } |
| 1010 | }); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1011 | oa.setStartDelay(delay); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1012 | oa.start(); |
| 1013 | return true; |
| 1014 | } |
| 1015 | return false; |
| 1016 | } |
| 1017 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1018 | /** |
| 1019 | * Estimate where the top left cell of the dragged item will land if it is dropped. |
| 1020 | * |
| 1021 | * @param originX The X value of the top left corner of the item |
| 1022 | * @param originY The Y value of the top left corner of the item |
| 1023 | * @param spanX The number of horizontal cells that the item spans |
| 1024 | * @param spanY The number of vertical cells that the item spans |
| 1025 | * @param result The estimated drop cell X and Y. |
| 1026 | */ |
| 1027 | void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 1028 | final int countX = mCountX; |
| 1029 | final int countY = mCountY; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1030 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1031 | // pointToCellRounded takes the top left of a cell but will pad that with |
| 1032 | // cellWidth/2 and cellHeight/2 when finding the matching cell |
| 1033 | pointToCellRounded(originX, originY, result); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1034 | |
| 1035 | // If the item isn't fully on this screen, snap to the edges |
| 1036 | int rightOverhang = result[0] + spanX - countX; |
| 1037 | if (rightOverhang > 0) { |
| 1038 | result[0] -= rightOverhang; // Snap to right |
| 1039 | } |
| 1040 | result[0] = Math.max(0, result[0]); // Snap to left |
| 1041 | int bottomOverhang = result[1] + spanY - countY; |
| 1042 | if (bottomOverhang > 0) { |
| 1043 | result[1] -= bottomOverhang; // Snap to bottom |
| 1044 | } |
| 1045 | result[1] = Math.max(0, result[1]); // Snap to top |
| 1046 | } |
| 1047 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1048 | void visualizeDropLocation( |
| 1049 | View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) { |
| 1050 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1051 | final int oldDragCellX = mDragCell[0]; |
| 1052 | final int oldDragCellY = mDragCell[1]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1053 | final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell); |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1054 | if (v != null) { |
| 1055 | mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2)); |
| 1056 | } else { |
| 1057 | mDragCenter.set(originX, originY); |
| 1058 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1059 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1060 | if (dragOutline == null && v == null) { |
| 1061 | if (mCrosshairsDrawable != null) { |
| 1062 | invalidate(); |
| 1063 | } |
| 1064 | return; |
| 1065 | } |
| 1066 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1067 | if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) { |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1068 | // Find the top left corner of the rect the object will occupy |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1069 | final int[] topLeft = mTmpPoint; |
| 1070 | cellToPoint(nearest[0], nearest[1], topLeft); |
| 1071 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1072 | int left = topLeft[0]; |
| 1073 | int top = topLeft[1]; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1074 | |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1075 | if (v != null) { |
Adam Cohen | 99e8b40 | 2011-03-25 19:23:43 -0700 | [diff] [blame] | 1076 | // When drawing the drag outline, it did not account for margin offsets |
| 1077 | // added by the view's parent. |
| 1078 | MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams(); |
| 1079 | left += lp.leftMargin; |
| 1080 | top += lp.topMargin; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1081 | |
Adam Cohen | 99e8b40 | 2011-03-25 19:23:43 -0700 | [diff] [blame] | 1082 | // Offsets due to the size difference between the View and the dragOutline. |
| 1083 | // There is a size difference to account for the outer blur, which may lie |
| 1084 | // outside the bounds of the view. |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1085 | left += (v.getWidth() - dragOutline.getWidth()) / 2; |
| 1086 | top += (v.getHeight() - dragOutline.getHeight()) / 2; |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 1087 | } else { |
| 1088 | // Center the drag outline in the cell |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1089 | left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap) |
| 1090 | - dragOutline.getWidth()) / 2; |
| 1091 | top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap) |
| 1092 | - dragOutline.getHeight()) / 2; |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1093 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1094 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1095 | final int oldIndex = mDragOutlineCurrent; |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1096 | mDragOutlineAnims[oldIndex].animateOut(); |
| 1097 | mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1098 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1099 | mDragOutlines[mDragOutlineCurrent].set(left, top); |
| 1100 | mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline); |
| 1101 | mDragOutlineAnims[mDragOutlineCurrent].animateIn(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1102 | } |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 1103 | |
| 1104 | // If we are drawing crosshairs, the entire CellLayout needs to be invalidated |
| 1105 | if (mCrosshairsDrawable != null) { |
| 1106 | invalidate(); |
| 1107 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 1110 | public void clearDragOutlines() { |
| 1111 | final int oldIndex = mDragOutlineCurrent; |
| 1112 | mDragOutlineAnims[oldIndex].animateOut(); |
| 1113 | mDragCell[0] = -1; |
| 1114 | mDragCell[1] = -1; |
| 1115 | } |
| 1116 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1117 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1118 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1119 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1120 | * |
Romain Guy | 51afc02 | 2009-05-04 18:03:43 -0700 | [diff] [blame] | 1121 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1122 | * @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] | 1123 | * @param spanX Horizontal span of the object. |
| 1124 | * @param spanY Vertical span of the object. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1125 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1126 | * be allocated) |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1127 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1128 | * nearest the requested location. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1129 | */ |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1130 | int[] findNearestVacantArea( |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1131 | int pixelX, int pixelY, int spanX, int spanY, int[] result) { |
| 1132 | return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result); |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1133 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1134 | |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1135 | /** |
| 1136 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1137 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 1138 | * |
| 1139 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1140 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 1141 | * @param spanX Horizontal span of the object. |
| 1142 | * @param spanY Vertical span of the object. |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1143 | * @param ignoreOccupied If true, the result can be an occupied cell |
| 1144 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1145 | * be allocated) |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1146 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1147 | * nearest the requested location. |
| 1148 | */ |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1149 | int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView, |
| 1150 | boolean ignoreOccupied, int[] result) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1151 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
| 1152 | markCellsAsUnoccupiedForView(ignoreView); |
| 1153 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1154 | // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds |
| 1155 | // to the center of the item, but we are searching based on the top-left cell, so |
| 1156 | // we translate the point over to correspond to the top-left. |
| 1157 | pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f; |
| 1158 | pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f; |
| 1159 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1160 | // Keep track of best-scoring drop area |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1161 | final int[] bestXY = result != null ? result : new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1162 | double bestDistance = Double.MAX_VALUE; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1163 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1164 | final int countX = mCountX; |
| 1165 | final int countY = mCountY; |
| 1166 | final boolean[][] occupied = mOccupied; |
| 1167 | |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1168 | for (int y = 0; y < countY - (spanY - 1); y++) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1169 | inner: |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1170 | for (int x = 0; x < countX - (spanX - 1); x++) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1171 | if (ignoreOccupied) { |
| 1172 | for (int i = 0; i < spanX; i++) { |
| 1173 | for (int j = 0; j < spanY; j++) { |
| 1174 | if (occupied[x + i][y + j]) { |
| 1175 | // small optimization: we can skip to after the column we |
| 1176 | // just found an occupied cell |
| 1177 | x += i; |
| 1178 | continue inner; |
| 1179 | } |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | } |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 1183 | final int[] cellXY = mTmpXY; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1184 | cellToCenterPoint(x, y, cellXY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1185 | |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1186 | double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2) |
| 1187 | + Math.pow(cellXY[1] - pixelY, 2)); |
| 1188 | if (distance <= bestDistance) { |
| 1189 | bestDistance = distance; |
| 1190 | bestXY[0] = x; |
| 1191 | bestXY[1] = y; |
| 1192 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1193 | } |
| 1194 | } |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1195 | // re-mark space taken by ignoreView as occupied |
| 1196 | markCellsAsOccupiedForView(ignoreView); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1197 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1198 | // Return -1, -1 if no suitable location found |
| 1199 | if (bestDistance == Double.MAX_VALUE) { |
| 1200 | bestXY[0] = -1; |
| 1201 | bestXY[1] = -1; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1202 | } |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1203 | return bestXY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1204 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1205 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1206 | /** |
| 1207 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1208 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 1209 | * |
| 1210 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1211 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 1212 | * @param spanX Horizontal span of the object. |
| 1213 | * @param spanY Vertical span of the object. |
| 1214 | * @param ignoreView Considers space occupied by this view as unoccupied |
| 1215 | * @param result Previously returned value to possibly recycle. |
| 1216 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1217 | * nearest the requested location. |
| 1218 | */ |
| 1219 | int[] findNearestVacantArea( |
| 1220 | int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) { |
| 1221 | return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result); |
| 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * Find a starting cell position that will fit the given bounds nearest the requested |
| 1226 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 1227 | * |
| 1228 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1229 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 1230 | * @param spanX Horizontal span of the object. |
| 1231 | * @param spanY Vertical span of the object. |
| 1232 | * @param ignoreView Considers space occupied by this view as unoccupied |
| 1233 | * @param result Previously returned value to possibly recycle. |
| 1234 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1235 | * nearest the requested location. |
| 1236 | */ |
| 1237 | int[] findNearestArea( |
| 1238 | int pixelX, int pixelY, int spanX, int spanY, int[] result) { |
| 1239 | return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result); |
| 1240 | } |
| 1241 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1242 | boolean existsEmptyCell() { |
| 1243 | return findCellForSpan(null, 1, 1); |
| 1244 | } |
| 1245 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1246 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1247 | * Finds the upper-left coordinate of the first rectangle in the grid that can |
| 1248 | * hold a cell of the specified dimensions. If intersectX and intersectY are not -1, |
| 1249 | * then this method will only return coordinates for rectangles that contain the cell |
| 1250 | * (intersectX, intersectY) |
| 1251 | * |
| 1252 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 1253 | * can be found. |
| 1254 | * @param spanX The horizontal span of the cell we want to find. |
| 1255 | * @param spanY The vertical span of the cell we want to find. |
| 1256 | * |
| 1257 | * @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] | 1258 | */ |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1259 | boolean findCellForSpan(int[] cellXY, int spanX, int spanY) { |
| 1260 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null); |
| 1261 | } |
| 1262 | |
| 1263 | /** |
| 1264 | * Like above, but ignores any cells occupied by the item "ignoreView" |
| 1265 | * |
| 1266 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 1267 | * can be found. |
| 1268 | * @param spanX The horizontal span of the cell we want to find. |
| 1269 | * @param spanY The vertical span of the cell we want to find. |
| 1270 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 1271 | * @return |
| 1272 | */ |
| 1273 | boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) { |
| 1274 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView); |
| 1275 | } |
| 1276 | |
| 1277 | /** |
| 1278 | * Like above, but if intersectX and intersectY are not -1, then this method will try to |
| 1279 | * return coordinates for rectangles that contain the cell [intersectX, intersectY] |
| 1280 | * |
| 1281 | * @param spanX The horizontal span of the cell we want to find. |
| 1282 | * @param spanY The vertical span of the cell we want to find. |
| 1283 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 1284 | * @param intersectX The X coordinate of the cell that we should try to overlap |
| 1285 | * @param intersectX The Y coordinate of the cell that we should try to overlap |
| 1286 | * |
| 1287 | * @return True if a vacant cell of the specified dimension was found, false otherwise. |
| 1288 | */ |
| 1289 | boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY, |
| 1290 | int intersectX, int intersectY) { |
| 1291 | return findCellForSpanThatIntersectsIgnoring( |
| 1292 | cellXY, spanX, spanY, intersectX, intersectY, null); |
| 1293 | } |
| 1294 | |
| 1295 | /** |
| 1296 | * The superset of the above two methods |
| 1297 | */ |
| 1298 | boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY, |
| 1299 | int intersectX, int intersectY, View ignoreView) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1300 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
| 1301 | markCellsAsUnoccupiedForView(ignoreView); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1302 | |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1303 | boolean foundCell = false; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1304 | while (true) { |
| 1305 | int startX = 0; |
| 1306 | if (intersectX >= 0) { |
| 1307 | startX = Math.max(startX, intersectX - (spanX - 1)); |
| 1308 | } |
| 1309 | int endX = mCountX - (spanX - 1); |
| 1310 | if (intersectX >= 0) { |
| 1311 | endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0)); |
| 1312 | } |
| 1313 | int startY = 0; |
| 1314 | if (intersectY >= 0) { |
| 1315 | startY = Math.max(startY, intersectY - (spanY - 1)); |
| 1316 | } |
| 1317 | int endY = mCountY - (spanY - 1); |
| 1318 | if (intersectY >= 0) { |
| 1319 | endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0)); |
| 1320 | } |
| 1321 | |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1322 | for (int y = startY; y < endY && !foundCell; y++) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1323 | inner: |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1324 | for (int x = startX; x < endX; x++) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1325 | for (int i = 0; i < spanX; i++) { |
| 1326 | for (int j = 0; j < spanY; j++) { |
| 1327 | if (mOccupied[x + i][y + j]) { |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1328 | // small optimization: we can skip to after the column we just found |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1329 | // an occupied cell |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1330 | x += i; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1331 | continue inner; |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | if (cellXY != null) { |
| 1336 | cellXY[0] = x; |
| 1337 | cellXY[1] = y; |
| 1338 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1339 | foundCell = true; |
| 1340 | break; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | if (intersectX == -1 && intersectY == -1) { |
| 1344 | break; |
| 1345 | } else { |
| 1346 | // if we failed to find anything, try again but without any requirements of |
| 1347 | // intersecting |
| 1348 | intersectX = -1; |
| 1349 | intersectY = -1; |
| 1350 | continue; |
| 1351 | } |
| 1352 | } |
| 1353 | |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1354 | // re-mark space taken by ignoreView as occupied |
| 1355 | markCellsAsOccupiedForView(ignoreView); |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1356 | return foundCell; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | /** |
| 1360 | * Called when drag has left this CellLayout or has been completed (successfully or not) |
| 1361 | */ |
| 1362 | void onDragExit() { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1363 | // This can actually be called when we aren't in a drag, e.g. when adding a new |
| 1364 | // item to this layout via the customize drawer. |
| 1365 | // Guard against that case. |
| 1366 | if (mDragging) { |
| 1367 | mDragging = false; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1368 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1369 | // Fade out the drag indicators |
| 1370 | if (mCrosshairsAnimator != null) { |
| 1371 | mCrosshairsAnimator.animateOut(); |
| 1372 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1373 | } |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1374 | |
| 1375 | // Invalidate the drag data |
| 1376 | mDragCell[0] = -1; |
| 1377 | mDragCell[1] = -1; |
| 1378 | mDragOutlineAnims[mDragOutlineCurrent].animateOut(); |
| 1379 | mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length; |
| 1380 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 1381 | setIsDragOverlapping(false); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1385 | * Mark a child as having been dropped. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1386 | * 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] | 1387 | * 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] | 1388 | * |
| 1389 | * @param child The child that is being dropped |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1390 | */ |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1391 | void onDropChild(View child, boolean animate) { |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1392 | if (child != null) { |
| 1393 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1394 | lp.isDragging = false; |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 1395 | lp.dropped = true; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1396 | lp.animateDrop = animate; |
Patrick Dubroy | e3887cc | 2011-01-20 10:43:40 -0800 | [diff] [blame] | 1397 | child.setVisibility(animate ? View.INVISIBLE : View.VISIBLE); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1398 | child.requestLayout(); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1399 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1402 | /** |
| 1403 | * Start dragging the specified child |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1404 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1405 | * @param child The child that is being dragged |
| 1406 | */ |
| 1407 | void onDragChild(View child) { |
| 1408 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1409 | lp.isDragging = true; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | /** |
| 1413 | * A drag event has begun over this layout. |
| 1414 | * It may have begun over this layout (in which case onDragChild is called first), |
| 1415 | * or it may have begun on another layout. |
| 1416 | */ |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1417 | void onDragEnter() { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 1418 | if (!mDragging) { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 1419 | // Fade in the drag indicators |
| 1420 | if (mCrosshairsAnimator != null) { |
| 1421 | mCrosshairsAnimator.animateIn(); |
| 1422 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1423 | } |
| 1424 | mDragging = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1425 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1426 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1427 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1428 | * Computes a bounding rectangle for a range of cells |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1429 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1430 | * @param cellX X coordinate of upper left corner expressed as a cell position |
| 1431 | * @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] | 1432 | * @param cellHSpan Width in cells |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1433 | * @param cellVSpan Height in cells |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1434 | * @param resultRect Rect into which to put the results |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1435 | */ |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1436 | public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1437 | final int cellWidth = mCellWidth; |
| 1438 | final int cellHeight = mCellHeight; |
| 1439 | final int widthGap = mWidthGap; |
| 1440 | final int heightGap = mHeightGap; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1441 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 1442 | final int hStartPadding = getPaddingLeft(); |
| 1443 | final int vStartPadding = getPaddingTop(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1444 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1445 | int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap); |
| 1446 | int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap); |
| 1447 | |
| 1448 | int x = hStartPadding + cellX * (cellWidth + widthGap); |
| 1449 | int y = vStartPadding + cellY * (cellHeight + heightGap); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1450 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1451 | resultRect.set(x, y, x + width, y + height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1452 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1453 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1454 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1455 | * 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] | 1456 | * fit the given rectangle. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1457 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1458 | * @param width Width in pixels |
| 1459 | * @param height Height in pixels |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1460 | * @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] | 1461 | */ |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1462 | public int[] rectToCell(int width, int height, int[] result) { |
Michael Jurka | 9987a5c | 2010-10-08 16:58:12 -0700 | [diff] [blame] | 1463 | return rectToCell(getResources(), width, height, result); |
| 1464 | } |
| 1465 | |
| 1466 | 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] | 1467 | // Always assume we're working with the smallest span to make sure we |
| 1468 | // reserve enough space in both orientations. |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1469 | int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width); |
| 1470 | int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1471 | int smallerSize = Math.min(actualWidth, actualHeight); |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1472 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1473 | // Always round up to next largest cell |
| 1474 | int spanX = (width + smallerSize) / smallerSize; |
| 1475 | int spanY = (height + smallerSize) / smallerSize; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1476 | |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1477 | if (result == null) { |
| 1478 | return new int[] { spanX, spanY }; |
| 1479 | } |
| 1480 | result[0] = spanX; |
| 1481 | result[1] = spanY; |
| 1482 | return result; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1483 | } |
| 1484 | |
Michael Jurka | f12c75c | 2011-01-25 22:41:40 -0800 | [diff] [blame] | 1485 | public int[] cellSpansToSize(int hSpans, int vSpans) { |
| 1486 | int[] size = new int[2]; |
| 1487 | size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap; |
| 1488 | size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap; |
| 1489 | return size; |
| 1490 | } |
| 1491 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1492 | /** |
Patrick Dubroy | 047379a | 2010-12-19 22:02:04 -0800 | [diff] [blame] | 1493 | * Calculate the grid spans needed to fit given item |
| 1494 | */ |
| 1495 | public void calculateSpans(ItemInfo info) { |
| 1496 | final int minWidth; |
| 1497 | final int minHeight; |
| 1498 | |
| 1499 | if (info instanceof LauncherAppWidgetInfo) { |
| 1500 | minWidth = ((LauncherAppWidgetInfo) info).minWidth; |
| 1501 | minHeight = ((LauncherAppWidgetInfo) info).minHeight; |
| 1502 | } else if (info instanceof PendingAddWidgetInfo) { |
| 1503 | minWidth = ((PendingAddWidgetInfo) info).minWidth; |
| 1504 | minHeight = ((PendingAddWidgetInfo) info).minHeight; |
| 1505 | } else { |
| 1506 | // It's not a widget, so it must be 1x1 |
| 1507 | info.spanX = info.spanY = 1; |
| 1508 | return; |
| 1509 | } |
| 1510 | int[] spans = rectToCell(minWidth, minHeight, null); |
| 1511 | info.spanX = spans[0]; |
| 1512 | info.spanY = spans[1]; |
| 1513 | } |
| 1514 | |
| 1515 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1516 | * Find the first vacant cell, if there is one. |
| 1517 | * |
| 1518 | * @param vacant Holds the x and y coordinate of the vacant cell |
| 1519 | * @param spanX Horizontal cell span. |
| 1520 | * @param spanY Vertical cell span. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1521 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1522 | * @return True if a vacant cell was found |
| 1523 | */ |
| 1524 | public boolean getVacantCell(int[] vacant, int spanX, int spanY) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1525 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1526 | return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | static boolean findVacantCell(int[] vacant, int spanX, int spanY, |
| 1530 | int xCount, int yCount, boolean[][] occupied) { |
| 1531 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1532 | for (int y = 0; y < yCount; y++) { |
| 1533 | for (int x = 0; x < xCount; x++) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1534 | boolean available = !occupied[x][y]; |
| 1535 | out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) { |
| 1536 | for (int j = y; j < y + spanY - 1 && y < yCount; j++) { |
| 1537 | available = available && !occupied[i][j]; |
| 1538 | if (!available) break out; |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | if (available) { |
| 1543 | vacant[0] = x; |
| 1544 | vacant[1] = y; |
| 1545 | return true; |
| 1546 | } |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | return false; |
| 1551 | } |
| 1552 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1553 | private void clearOccupiedCells() { |
| 1554 | for (int x = 0; x < mCountX; x++) { |
| 1555 | for (int y = 0; y < mCountY; y++) { |
| 1556 | mOccupied[x][y] = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1557 | } |
| 1558 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1559 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1560 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1561 | /** |
| 1562 | * Given a view, determines how much that view can be expanded in all directions, in terms of |
| 1563 | * whether or not there are other items occupying adjacent cells. Used by the |
| 1564 | * AppWidgetResizeFrame to determine how the widget can be resized. |
| 1565 | */ |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1566 | public void getExpandabilityArrayForView(View view, int[] expandability) { |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1567 | final LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1568 | boolean flag; |
| 1569 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1570 | expandability[AppWidgetResizeFrame.LEFT] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1571 | for (int x = lp.cellX - 1; x >= 0; x--) { |
| 1572 | flag = false; |
| 1573 | for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) { |
| 1574 | if (mOccupied[x][y]) flag = true; |
| 1575 | } |
| 1576 | if (flag) break; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1577 | expandability[AppWidgetResizeFrame.LEFT]++; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1578 | } |
| 1579 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1580 | expandability[AppWidgetResizeFrame.TOP] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1581 | for (int y = lp.cellY - 1; y >= 0; y--) { |
| 1582 | flag = false; |
| 1583 | for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) { |
| 1584 | if (mOccupied[x][y]) flag = true; |
| 1585 | } |
| 1586 | if (flag) break; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1587 | expandability[AppWidgetResizeFrame.TOP]++; |
| 1588 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1589 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1590 | expandability[AppWidgetResizeFrame.RIGHT] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1591 | for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) { |
| 1592 | flag = false; |
| 1593 | for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) { |
| 1594 | if (mOccupied[x][y]) flag = true; |
| 1595 | } |
| 1596 | if (flag) break; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1597 | expandability[AppWidgetResizeFrame.RIGHT]++; |
| 1598 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1599 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1600 | expandability[AppWidgetResizeFrame.BOTTOM] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1601 | for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) { |
| 1602 | flag = false; |
| 1603 | for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) { |
| 1604 | if (mOccupied[x][y]) flag = true; |
| 1605 | } |
| 1606 | if (flag) break; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1607 | expandability[AppWidgetResizeFrame.BOTTOM]++; |
| 1608 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1609 | } |
| 1610 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1611 | public void onMove(View view, int newCellX, int newCellY) { |
| 1612 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1613 | markCellsAsUnoccupiedForView(view); |
| 1614 | markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true); |
| 1615 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1616 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1617 | public void markCellsAsOccupiedForView(View view) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 1618 | if (view == null || view.getParent() != mChildren) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1619 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1620 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true); |
| 1621 | } |
| 1622 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1623 | public void markCellsAsUnoccupiedForView(View view) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 1624 | if (view == null || view.getParent() != mChildren) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1625 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1626 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false); |
| 1627 | } |
| 1628 | |
| 1629 | private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) { |
| 1630 | for (int x = cellX; x < cellX + spanX && x < mCountX; x++) { |
| 1631 | for (int y = cellY; y < cellY + spanY && y < mCountY; y++) { |
| 1632 | mOccupied[x][y] = value; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1633 | } |
| 1634 | } |
| 1635 | } |
| 1636 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1637 | public int getDesiredWidth() { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 1638 | return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) + |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1639 | (Math.max((mCountX - 1), 0) * mWidthGap); |
| 1640 | } |
| 1641 | |
| 1642 | public int getDesiredHeight() { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame^] | 1643 | return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) + |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1644 | (Math.max((mCountY - 1), 0) * mHeightGap); |
| 1645 | } |
| 1646 | |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 1647 | public boolean isOccupied(int x, int y) { |
| 1648 | if (x < mCountX && y < mCountY) { |
| 1649 | return mOccupied[x][y]; |
| 1650 | } else { |
| 1651 | throw new RuntimeException("Position exceeds the bound of this CellLayout"); |
| 1652 | } |
| 1653 | } |
| 1654 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1655 | @Override |
| 1656 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 1657 | return new CellLayout.LayoutParams(getContext(), attrs); |
| 1658 | } |
| 1659 | |
| 1660 | @Override |
| 1661 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 1662 | return p instanceof CellLayout.LayoutParams; |
| 1663 | } |
| 1664 | |
| 1665 | @Override |
| 1666 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 1667 | return new CellLayout.LayoutParams(p); |
| 1668 | } |
| 1669 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1670 | public static class CellLayoutAnimationController extends LayoutAnimationController { |
| 1671 | public CellLayoutAnimationController(Animation animation, float delay) { |
| 1672 | super(animation, delay); |
| 1673 | } |
| 1674 | |
| 1675 | @Override |
| 1676 | protected long getDelayForView(View view) { |
| 1677 | return (int) (Math.random() * 150); |
| 1678 | } |
| 1679 | } |
| 1680 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1681 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
| 1682 | /** |
| 1683 | * Horizontal location of the item in the grid. |
| 1684 | */ |
| 1685 | @ViewDebug.ExportedProperty |
| 1686 | public int cellX; |
| 1687 | |
| 1688 | /** |
| 1689 | * Vertical location of the item in the grid. |
| 1690 | */ |
| 1691 | @ViewDebug.ExportedProperty |
| 1692 | public int cellY; |
| 1693 | |
| 1694 | /** |
| 1695 | * Number of cells spanned horizontally by the item. |
| 1696 | */ |
| 1697 | @ViewDebug.ExportedProperty |
| 1698 | public int cellHSpan; |
| 1699 | |
| 1700 | /** |
| 1701 | * Number of cells spanned vertically by the item. |
| 1702 | */ |
| 1703 | @ViewDebug.ExportedProperty |
| 1704 | public int cellVSpan; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1705 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1706 | /** |
| 1707 | * Indicates whether the item will set its x, y, width and height parameters freely, |
| 1708 | * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan. |
| 1709 | */ |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1710 | public boolean isLockedToGrid = true; |
| 1711 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1712 | /** |
| 1713 | * Is this item currently being dragged |
| 1714 | */ |
| 1715 | public boolean isDragging; |
| 1716 | |
| 1717 | // X coordinate of the view in the layout. |
| 1718 | @ViewDebug.ExportedProperty |
| 1719 | int x; |
| 1720 | // Y coordinate of the view in the layout. |
| 1721 | @ViewDebug.ExportedProperty |
| 1722 | int y; |
| 1723 | |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 1724 | /** |
| 1725 | * The old X coordinate of this item, relative to its current parent. |
| 1726 | * Used to animate the item into its new position. |
| 1727 | */ |
| 1728 | int oldX; |
| 1729 | |
| 1730 | /** |
| 1731 | * The old Y coordinate of this item, relative to its current parent. |
| 1732 | * Used to animate the item into its new position. |
| 1733 | */ |
| 1734 | int oldY; |
| 1735 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 1736 | boolean dropped; |
Romain Guy | fcb9e71 | 2009-10-02 16:06:52 -0700 | [diff] [blame] | 1737 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1738 | boolean animateDrop; |
| 1739 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1740 | public LayoutParams(Context c, AttributeSet attrs) { |
| 1741 | super(c, attrs); |
| 1742 | cellHSpan = 1; |
| 1743 | cellVSpan = 1; |
| 1744 | } |
| 1745 | |
| 1746 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 1747 | super(source); |
| 1748 | cellHSpan = 1; |
| 1749 | cellVSpan = 1; |
| 1750 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1751 | |
| 1752 | public LayoutParams(LayoutParams source) { |
| 1753 | super(source); |
| 1754 | this.cellX = source.cellX; |
| 1755 | this.cellY = source.cellY; |
| 1756 | this.cellHSpan = source.cellHSpan; |
| 1757 | this.cellVSpan = source.cellVSpan; |
| 1758 | } |
| 1759 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1760 | public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) { |
Romain Guy | 8f19cdd | 2010-01-08 15:07:00 -0800 | [diff] [blame] | 1761 | super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1762 | this.cellX = cellX; |
| 1763 | this.cellY = cellY; |
| 1764 | this.cellHSpan = cellHSpan; |
| 1765 | this.cellVSpan = cellVSpan; |
| 1766 | } |
| 1767 | |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 1768 | public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1769 | if (isLockedToGrid) { |
| 1770 | final int myCellHSpan = cellHSpan; |
| 1771 | final int myCellVSpan = cellVSpan; |
| 1772 | final int myCellX = cellX; |
| 1773 | final int myCellY = cellY; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1774 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1775 | width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) - |
| 1776 | leftMargin - rightMargin; |
| 1777 | height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) - |
| 1778 | topMargin - bottomMargin; |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 1779 | x = myCellX * (cellWidth + widthGap) + leftMargin; |
| 1780 | y = myCellY * (cellHeight + heightGap) + topMargin; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1781 | } |
| 1782 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1783 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1784 | public String toString() { |
| 1785 | return "(" + this.cellX + ", " + this.cellY + ")"; |
| 1786 | } |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 1787 | |
| 1788 | public void setWidth(int width) { |
| 1789 | this.width = width; |
| 1790 | } |
| 1791 | |
| 1792 | public int getWidth() { |
| 1793 | return width; |
| 1794 | } |
| 1795 | |
| 1796 | public void setHeight(int height) { |
| 1797 | this.height = height; |
| 1798 | } |
| 1799 | |
| 1800 | public int getHeight() { |
| 1801 | return height; |
| 1802 | } |
| 1803 | |
| 1804 | public void setX(int x) { |
| 1805 | this.x = x; |
| 1806 | } |
| 1807 | |
| 1808 | public int getX() { |
| 1809 | return x; |
| 1810 | } |
| 1811 | |
| 1812 | public void setY(int y) { |
| 1813 | this.y = y; |
| 1814 | } |
| 1815 | |
| 1816 | public int getY() { |
| 1817 | return y; |
| 1818 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1819 | } |
| 1820 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1821 | // This class stores info for two purposes: |
| 1822 | // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY, |
| 1823 | // its spanX, spanY, and the screen it is on |
| 1824 | // 2. When long clicking on an empty cell in a CellLayout, we save information about the |
| 1825 | // cellX and cellY coordinates and which page was clicked. We then set this as a tag on |
| 1826 | // the CellLayout that was long clicked |
Michael Jurka | e5fb0f2 | 2011-04-11 13:27:46 -0700 | [diff] [blame] | 1827 | static final class CellInfo { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1828 | View cell; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1829 | int cellX = -1; |
| 1830 | int cellY = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1831 | int spanX; |
| 1832 | int spanY; |
| 1833 | int screen; |
| 1834 | boolean valid; |
| 1835 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1836 | @Override |
| 1837 | public String toString() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1838 | return "Cell[view=" + (cell == null ? "null" : cell.getClass()) |
| 1839 | + ", x=" + cellX + ", y=" + cellY + "]"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1840 | } |
| 1841 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1842 | } |