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