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 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 19 | import com.android.launcher.R; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 20 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 21 | import android.animation.ValueAnimator; |
| 22 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 23 | import android.app.WallpaperManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 25 | import android.content.res.Resources; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 26 | import android.content.res.TypedArray; |
| 27 | import android.graphics.Canvas; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 28 | import android.graphics.Point; |
| 29 | import android.graphics.PointF; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.graphics.Rect; |
| 31 | import android.graphics.RectF; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 32 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 33 | import android.util.AttributeSet; |
| 34 | import android.view.ContextMenu; |
| 35 | import android.view.MotionEvent; |
| 36 | import android.view.View; |
| 37 | import android.view.ViewDebug; |
| 38 | import android.view.ViewGroup; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 39 | import android.view.animation.Animation; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 40 | import android.view.animation.DecelerateInterpolator; |
| 41 | import android.view.animation.Interpolator; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 42 | import android.view.animation.LayoutAnimationController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 44 | import java.util.Arrays; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 45 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 46 | public class CellLayout extends ViewGroup implements Dimmable { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 47 | static final String TAG = "CellLayout"; |
| 48 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 49 | private int mCellWidth; |
| 50 | private int mCellHeight; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 51 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 52 | private int mLeftPadding; |
| 53 | private int mRightPadding; |
| 54 | private int mTopPadding; |
| 55 | private int mBottomPadding; |
| 56 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 57 | private int mCountX; |
| 58 | private int mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 59 | |
| 60 | private int mWidthGap; |
| 61 | private int mHeightGap; |
| 62 | |
| 63 | private final Rect mRect = new Rect(); |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 64 | private final RectF mRectF = new RectF(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 65 | private final CellInfo mCellInfo = new CellInfo(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 66 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 67 | // These are temporary variables to prevent having to allocate a new object just to |
| 68 | // return an (x, y) value from helper functions. Do NOT use them to maintain other state. |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 69 | private final int[] mTmpCellXY = new int[2]; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 70 | private final int[] mTmpPoint = new int[2]; |
| 71 | private final PointF mTmpPointF = new PointF(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 72 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 73 | boolean[][] mOccupied; |
| 74 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 75 | private OnTouchListener mInterceptTouchListener; |
| 76 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 77 | private float mBackgroundAlpha; |
| 78 | private final Rect mBackgroundLayoutRect = new Rect(); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 79 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 80 | private Drawable mBackground; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 81 | private Drawable mBackgroundMini; |
| 82 | private Drawable mBackgroundMiniHover; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 83 | // If we're actively dragging something over this screen and it's small, mHover is true |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 84 | private boolean mHover = false; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 85 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 86 | private final Point mDragCenter = new Point(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 87 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 88 | private Drawable mDragRectDrawable; |
| 89 | |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 90 | // These arrays are used to implement the drag visualization on x-large screens. |
| 91 | // They are used as circular arrays, indexed by mDragRectCurrent. |
| 92 | private Rect[] mDragRects = new Rect[8]; |
| 93 | private int[] mDragRectAlphas = new int[mDragRects.length]; |
| 94 | private InterruptibleInOutAnimator[] mDragRectAnims = |
| 95 | new InterruptibleInOutAnimator[mDragRects.length]; |
| 96 | |
| 97 | // Used as an index into the above 3 arrays; indicates which is the most current value. |
| 98 | private int mDragRectCurrent = 0; |
| 99 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 100 | private Drawable mCrosshairsDrawable = null; |
| 101 | private ValueAnimator mCrosshairsAnimator = null; |
| 102 | private float mCrosshairsVisibility = 0.0f; |
| 103 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 104 | // When a drag operation is in progress, holds the nearest cell to the touch point |
| 105 | private final int[] mDragCell = new int[2]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 106 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 107 | private final WallpaperManager mWallpaperManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 108 | |
| 109 | public CellLayout(Context context) { |
| 110 | this(context, null); |
| 111 | } |
| 112 | |
| 113 | public CellLayout(Context context, AttributeSet attrs) { |
| 114 | this(context, attrs, 0); |
| 115 | } |
| 116 | |
| 117 | public CellLayout(Context context, AttributeSet attrs, int defStyle) { |
| 118 | super(context, attrs, defStyle); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 119 | |
| 120 | // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show |
| 121 | // the user where a dragged item will land when dropped. |
| 122 | setWillNotDraw(false); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 123 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 124 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0); |
| 125 | |
| 126 | mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10); |
| 127 | mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 128 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 129 | mLeftPadding = |
| 130 | a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10); |
| 131 | mRightPadding = |
| 132 | a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10); |
| 133 | mTopPadding = |
| 134 | a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10); |
| 135 | mBottomPadding = |
| 136 | a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 137 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 138 | mCountX = LauncherModel.getCellCountX(); |
| 139 | mCountY = LauncherModel.getCellCountY(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 140 | mOccupied = new boolean[mCountX][mCountY]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 141 | |
| 142 | a.recycle(); |
| 143 | |
| 144 | setAlwaysDrawnWithCacheEnabled(false); |
| 145 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 146 | mWallpaperManager = WallpaperManager.getInstance(context); |
| 147 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame^] | 148 | final Resources res = getResources(); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 149 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame^] | 150 | if (LauncherApplication.isScreenXLarge()) { |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 151 | mBackgroundMini = res.getDrawable(R.drawable.mini_home_screen_bg); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 152 | mBackgroundMini.setFilterBitmap(true); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 153 | mBackground = res.getDrawable(R.drawable.home_screen_bg); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 154 | mBackground.setFilterBitmap(true); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 155 | mBackgroundMiniHover = res.getDrawable(R.drawable.mini_home_screen_bg_hover); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 156 | mBackgroundMiniHover.setFilterBitmap(true); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame^] | 157 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 158 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame^] | 159 | // Initialize the data structures used for the drag visualization. |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 160 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame^] | 161 | mDragRectDrawable = res.getDrawable(R.drawable.rounded_rect_green); |
| 162 | mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs); |
| 163 | Interpolator interp = new DecelerateInterpolator(2.5f); // Quint ease out |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 164 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame^] | 165 | // Set up the animation for fading the crosshairs in and out |
| 166 | int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime); |
| 167 | mCrosshairsAnimator = new ValueAnimator<Float>(animDuration); |
| 168 | mCrosshairsAnimator.addUpdateListener(new AnimatorUpdateListener() { |
| 169 | public void onAnimationUpdate(ValueAnimator animation) { |
| 170 | mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue(); |
| 171 | CellLayout.this.invalidate(); |
| 172 | } |
| 173 | }); |
| 174 | mCrosshairsAnimator.setInterpolator(interp); |
| 175 | |
| 176 | for (int i = 0; i < mDragRects.length; i++) { |
| 177 | mDragRects[i] = new Rect(); |
| 178 | } |
| 179 | |
| 180 | // When dragging things around the home screens, we show a green outline of |
| 181 | // where the item will land. The outlines gradually fade out, leaving a trail |
| 182 | // behind the drag path. |
| 183 | // Set up all the animations that are used to implement this fading. |
| 184 | final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime); |
| 185 | final int fromAlphaValue = 0; |
| 186 | final int toAlphaValue = res.getInteger(R.integer.config_dragOutlineMaxAlpha); |
| 187 | for (int i = 0; i < mDragRectAnims.length; i++) { |
| 188 | final InterruptibleInOutAnimator anim = |
| 189 | new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue); |
| 190 | anim.setInterpolator(interp); |
| 191 | final int thisIndex = i; |
| 192 | anim.addUpdateListener(new AnimatorUpdateListener() { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 193 | public void onAnimationUpdate(ValueAnimator animation) { |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame^] | 194 | mDragRectAlphas[thisIndex] = (Integer) animation.getAnimatedValue(); |
| 195 | CellLayout.this.invalidate(mDragRects[thisIndex]); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 196 | } |
| 197 | }); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame^] | 198 | mDragRectAnims[i] = anim; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 199 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 202 | public void setHover(boolean value) { |
| 203 | if (mHover != value) { |
| 204 | invalidate(); |
| 205 | } |
| 206 | mHover = value; |
| 207 | } |
| 208 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 209 | private void animateCrosshairsTo(float value) { |
| 210 | final ValueAnimator anim = mCrosshairsAnimator; |
| 211 | long fullDuration = getResources().getInteger(R.integer.config_crosshairsFadeInTime); |
| 212 | anim.setDuration(fullDuration - anim.getCurrentPlayTime()); |
| 213 | anim.setValues(mCrosshairsVisibility, value); |
| 214 | anim.cancel(); |
| 215 | anim.start(); |
| 216 | } |
| 217 | |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 218 | @Override |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 219 | public void dispatchDraw(Canvas canvas) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 220 | if (mBackgroundAlpha > 0.0f) { |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 221 | Drawable bg; |
| 222 | if (mHover && getScaleX() < 0.5f) { |
| 223 | bg = mBackgroundMiniHover; |
| 224 | } else if (getScaleX() < 0.5f) { |
| 225 | bg = mBackgroundMini; |
| 226 | } else { |
| 227 | bg = mBackground; |
| 228 | } |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 229 | if (bg != null) { |
| 230 | bg.setAlpha((int) (mBackgroundAlpha * 255)); |
| 231 | bg.draw(canvas); |
| 232 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 233 | } |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 234 | super.dispatchDraw(canvas); |
| 235 | } |
| 236 | |
| 237 | @Override |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 238 | protected void onDraw(Canvas canvas) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 239 | if (mCrosshairsVisibility > 0.0f) { |
| 240 | final int countX = mCountX; |
| 241 | final int countY = mCountY; |
| 242 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 243 | final float MAX_ALPHA = 0.4f; |
| 244 | final int MAX_VISIBLE_DISTANCE = 600; |
| 245 | final float DISTANCE_MULTIPLIER = 0.002f; |
| 246 | |
| 247 | final Drawable d = mCrosshairsDrawable; |
| 248 | final int width = d.getIntrinsicWidth(); |
| 249 | final int height = d.getIntrinsicHeight(); |
| 250 | |
| 251 | int x = getLeftPadding() - (mWidthGap / 2) - (width / 2); |
| 252 | for (int col = 0; col <= countX; col++) { |
| 253 | int y = getTopPadding() - (mHeightGap / 2) - (height / 2); |
| 254 | for (int row = 0; row <= countY; row++) { |
| 255 | mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y); |
| 256 | float dist = mTmpPointF.length(); |
| 257 | // Crosshairs further from the drag point are more faint |
| 258 | float alpha = Math.min(MAX_ALPHA, |
| 259 | DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist)); |
| 260 | if (alpha > 0.0f) { |
| 261 | d.setBounds(x, y, x + width, y + height); |
| 262 | d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility)); |
| 263 | d.draw(canvas); |
| 264 | } |
| 265 | y += mCellHeight + mHeightGap; |
| 266 | } |
| 267 | x += mCellWidth + mWidthGap; |
| 268 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 269 | |
| 270 | for (int i = 0; i < mDragRects.length; i++) { |
| 271 | int alpha = mDragRectAlphas[i]; |
| 272 | if (alpha > 0) { |
| 273 | mDragRectDrawable.setAlpha(alpha); |
| 274 | mDragRectDrawable.setBounds(mDragRects[i]); |
| 275 | mDragRectDrawable.draw(canvas); |
| 276 | } |
| 277 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 281 | public void setDimmableProgress(float progress) { |
| 282 | for (int i = 0; i < getChildCount(); i++) { |
| 283 | Dimmable d = (Dimmable) getChildAt(i); |
| 284 | d.setDimmableProgress(progress); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | public float getDimmableProgress() { |
| 289 | if (getChildCount() > 0) { |
| 290 | return ((Dimmable) getChildAt(0)).getDimmableProgress(); |
| 291 | } |
| 292 | return 0.0f; |
| 293 | } |
| 294 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 295 | @Override |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 296 | public void cancelLongPress() { |
| 297 | super.cancelLongPress(); |
| 298 | |
| 299 | // Cancel long press for all children |
| 300 | final int count = getChildCount(); |
| 301 | for (int i = 0; i < count; i++) { |
| 302 | final View child = getChildAt(i); |
| 303 | child.cancelLongPress(); |
| 304 | } |
| 305 | } |
| 306 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 307 | public void setOnInterceptTouchListener(View.OnTouchListener listener) { |
| 308 | mInterceptTouchListener = listener; |
| 309 | } |
| 310 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 311 | int getCountX() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 312 | return mCountX; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | int getCountY() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 316 | return mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 319 | public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) { |
| 320 | final LayoutParams lp = params; |
| 321 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 322 | // Generate an id for each view, this assumes we have at most 256x256 cells |
| 323 | // per workspace screen |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 324 | 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] | 325 | // If the horizontal or vertical span is set to -1, it is taken to |
| 326 | // mean that it spans the extent of the CellLayout |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 327 | if (lp.cellHSpan < 0) lp.cellHSpan = mCountX; |
| 328 | if (lp.cellVSpan < 0) lp.cellVSpan = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 329 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 330 | child.setId(childId); |
| 331 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 332 | // We might be in the middle or end of shrinking/fading to a dimmed view |
| 333 | // Make sure this view's alpha is set the same as all the rest of the views |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 334 | child.setAlpha(getAlpha()); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 335 | addView(child, index, lp); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 336 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 337 | markCellsAsOccupiedForView(child); |
| 338 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 339 | return true; |
| 340 | } |
| 341 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | @Override |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 345 | public void removeAllViews() { |
| 346 | clearOccupiedCells(); |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public void removeAllViewsInLayout() { |
| 351 | clearOccupiedCells(); |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public void removeView(View view) { |
| 356 | markCellsAsUnoccupiedForView(view); |
| 357 | super.removeView(view); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public void removeViewAt(int index) { |
| 362 | markCellsAsUnoccupiedForView(getChildAt(index)); |
| 363 | super.removeViewAt(index); |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public void removeViewInLayout(View view) { |
| 368 | markCellsAsUnoccupiedForView(view); |
| 369 | super.removeViewInLayout(view); |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public void removeViews(int start, int count) { |
| 374 | for (int i = start; i < start + count; i++) { |
| 375 | markCellsAsUnoccupiedForView(getChildAt(i)); |
| 376 | } |
| 377 | super.removeViews(start, count); |
| 378 | } |
| 379 | |
| 380 | @Override |
| 381 | public void removeViewsInLayout(int start, int count) { |
| 382 | for (int i = start; i < start + count; i++) { |
| 383 | markCellsAsUnoccupiedForView(getChildAt(i)); |
| 384 | } |
| 385 | super.removeViewsInLayout(start, count); |
| 386 | } |
| 387 | |
| 388 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 389 | public void requestChildFocus(View child, View focused) { |
| 390 | super.requestChildFocus(child, focused); |
| 391 | if (child != null) { |
| 392 | Rect r = new Rect(); |
| 393 | child.getDrawingRect(r); |
| 394 | requestRectangleOnScreen(r); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | protected void onAttachedToWindow() { |
| 400 | super.onAttachedToWindow(); |
| 401 | mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this); |
| 402 | } |
| 403 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 404 | public void setTagToCellInfoForPoint(int touchX, int touchY) { |
| 405 | final CellInfo cellInfo = mCellInfo; |
| 406 | final Rect frame = mRect; |
| 407 | final int x = touchX + mScrollX; |
| 408 | final int y = touchY + mScrollY; |
| 409 | final int count = getChildCount(); |
| 410 | |
| 411 | boolean found = false; |
| 412 | for (int i = count - 1; i >= 0; i--) { |
| 413 | final View child = getChildAt(i); |
| 414 | |
| 415 | if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) { |
| 416 | child.getHitRect(frame); |
| 417 | if (frame.contains(x, y)) { |
| 418 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 419 | cellInfo.cell = child; |
| 420 | cellInfo.cellX = lp.cellX; |
| 421 | cellInfo.cellY = lp.cellY; |
| 422 | cellInfo.spanX = lp.cellHSpan; |
| 423 | cellInfo.spanY = lp.cellVSpan; |
| 424 | cellInfo.valid = true; |
| 425 | found = true; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 426 | break; |
| 427 | } |
| 428 | } |
| 429 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 430 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 431 | if (!found) { |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 432 | final int cellXY[] = mTmpCellXY; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 433 | pointToCellExact(x, y, cellXY); |
| 434 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 435 | cellInfo.cell = null; |
| 436 | cellInfo.cellX = cellXY[0]; |
| 437 | cellInfo.cellY = cellXY[1]; |
| 438 | cellInfo.spanX = 1; |
| 439 | cellInfo.spanY = 1; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 440 | cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX && |
| 441 | cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]]; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 442 | } |
| 443 | setTag(cellInfo); |
| 444 | } |
| 445 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 446 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 447 | @Override |
| 448 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 449 | if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) { |
| 450 | return true; |
| 451 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 452 | final int action = ev.getAction(); |
| 453 | final CellInfo cellInfo = mCellInfo; |
| 454 | |
| 455 | if (action == MotionEvent.ACTION_DOWN) { |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 456 | setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 457 | } else if (action == MotionEvent.ACTION_UP) { |
| 458 | cellInfo.cell = null; |
| 459 | cellInfo.cellX = -1; |
| 460 | cellInfo.cellY = -1; |
| 461 | cellInfo.spanX = 0; |
| 462 | cellInfo.spanY = 0; |
| 463 | cellInfo.valid = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 464 | setTag(cellInfo); |
| 465 | } |
| 466 | |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | @Override |
| 471 | public CellInfo getTag() { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 472 | return (CellInfo) super.getTag(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 475 | /** |
| 476 | * Check if the row 'y' is empty from columns 'left' to 'right', inclusive. |
| 477 | */ |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 478 | private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) { |
| 479 | for (int x = left; x <= right; x++) { |
| 480 | if (occupied[x][y]) { |
| 481 | return false; |
| 482 | } |
| 483 | } |
| 484 | return true; |
| 485 | } |
| 486 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 487 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 488 | * 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] | 489 | * @param x X coordinate of the point |
| 490 | * @param y Y coordinate of the point |
| 491 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 492 | */ |
| 493 | void pointToCellExact(int x, int y, int[] result) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 494 | final int hStartPadding = getLeftPadding(); |
| 495 | final int vStartPadding = getTopPadding(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 496 | |
| 497 | result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap); |
| 498 | result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap); |
| 499 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 500 | final int xAxis = mCountX; |
| 501 | final int yAxis = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 502 | |
| 503 | if (result[0] < 0) result[0] = 0; |
| 504 | if (result[0] >= xAxis) result[0] = xAxis - 1; |
| 505 | if (result[1] < 0) result[1] = 0; |
| 506 | if (result[1] >= yAxis) result[1] = yAxis - 1; |
| 507 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 508 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 509 | /** |
| 510 | * Given a point, return the cell that most closely encloses that point |
| 511 | * @param x X coordinate of the point |
| 512 | * @param y Y coordinate of the point |
| 513 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 514 | */ |
| 515 | void pointToCellRounded(int x, int y, int[] result) { |
| 516 | pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * 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] | 521 | * |
| 522 | * @param cellX X coordinate of the cell |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 523 | * @param cellY Y coordinate of the cell |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 524 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 525 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 526 | */ |
| 527 | void cellToPoint(int cellX, int cellY, int[] result) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 528 | final int hStartPadding = getLeftPadding(); |
| 529 | final int vStartPadding = getTopPadding(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 530 | |
| 531 | result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap); |
| 532 | result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap); |
| 533 | } |
| 534 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 535 | int getCellWidth() { |
| 536 | return mCellWidth; |
| 537 | } |
| 538 | |
| 539 | int getCellHeight() { |
| 540 | return mCellHeight; |
| 541 | } |
| 542 | |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 543 | int getLeftPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 544 | return mLeftPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | int getTopPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 548 | return mTopPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | int getRightPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 552 | return mRightPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | int getBottomPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 556 | return mBottomPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 557 | } |
| 558 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 559 | @Override |
| 560 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 561 | // TODO: currently ignoring padding |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 562 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 563 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 564 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 565 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 566 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 567 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 568 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 569 | if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { |
| 570 | throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); |
| 571 | } |
| 572 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 573 | final int cellWidth = mCellWidth; |
| 574 | final int cellHeight = mCellHeight; |
| 575 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 576 | int numWidthGaps = mCountX - 1; |
| 577 | int numHeightGaps = mCountY - 1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 578 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 579 | int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY); |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 580 | mHeightGap = vSpaceLeft / numHeightGaps; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 581 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 582 | int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX); |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 583 | mWidthGap = hSpaceLeft / numWidthGaps; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 584 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 585 | // center it around the min gaps |
| 586 | int minGap = Math.min(mWidthGap, mHeightGap); |
| 587 | mWidthGap = mHeightGap = minGap; |
| 588 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 589 | int count = getChildCount(); |
| 590 | |
| 591 | for (int i = 0; i < count; i++) { |
| 592 | View child = getChildAt(i); |
| 593 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 594 | lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, |
| 595 | mLeftPadding, mTopPadding); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 596 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 597 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 598 | int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, |
| 599 | MeasureSpec.EXACTLY); |
| 600 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 601 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 602 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 603 | if (widthSpecMode == MeasureSpec.AT_MOST) { |
| 604 | int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) + |
| 605 | ((mCountX - 1) * minGap); |
| 606 | int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) + |
| 607 | ((mCountY - 1) * minGap); |
| 608 | setMeasuredDimension(newWidth, newHeight); |
| 609 | } else if (widthSpecMode == MeasureSpec.EXACTLY) { |
| 610 | setMeasuredDimension(widthSpecSize, heightSpecSize); |
| 611 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 615 | 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] | 616 | int count = getChildCount(); |
| 617 | |
| 618 | for (int i = 0; i < count; i++) { |
| 619 | View child = getChildAt(i); |
| 620 | if (child.getVisibility() != GONE) { |
| 621 | |
| 622 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 623 | |
| 624 | int childLeft = lp.x; |
| 625 | int childTop = lp.y; |
| 626 | child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 627 | |
| 628 | if (lp.dropped) { |
| 629 | lp.dropped = false; |
| 630 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 631 | final int[] cellXY = mTmpCellXY; |
Romain Guy | 06762ab | 2010-01-25 16:51:08 -0800 | [diff] [blame] | 632 | getLocationOnScreen(cellXY); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 633 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop", |
Romain Guy | 06762ab | 2010-01-25 16:51:08 -0800 | [diff] [blame] | 634 | cellXY[0] + childLeft + lp.width / 2, |
| 635 | cellXY[1] + childTop + lp.height / 2, 0, null); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 636 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | @Override |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 642 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 643 | super.onSizeChanged(w, h, oldw, oldh); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 644 | mBackgroundLayoutRect.set(0, 0, w, h); |
| 645 | if (mBackground != null) { |
| 646 | mBackground.setBounds(mBackgroundLayoutRect); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 647 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 648 | if (mBackgroundMiniHover != null) { |
| 649 | mBackgroundMiniHover.setBounds(mBackgroundLayoutRect); |
| 650 | } |
| 651 | if (mBackgroundMini != null) { |
| 652 | mBackgroundMini.setBounds(mBackgroundLayoutRect); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 653 | } |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 657 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 658 | final int count = getChildCount(); |
| 659 | for (int i = 0; i < count; i++) { |
| 660 | final View view = getChildAt(i); |
| 661 | view.setDrawingCacheEnabled(enabled); |
| 662 | // Update the drawing caches |
Adam Powell | fefa0ce | 2010-05-03 10:23:50 -0700 | [diff] [blame] | 663 | view.buildDrawingCache(true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | @Override |
| 668 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 669 | super.setChildrenDrawnWithCacheEnabled(enabled); |
| 670 | } |
| 671 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 672 | public float getBackgroundAlpha() { |
| 673 | return mBackgroundAlpha; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 676 | public void setBackgroundAlpha(float alpha) { |
| 677 | mBackgroundAlpha = alpha; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 678 | invalidate(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 681 | // Need to return true to let the view system know we know how to handle alpha-- this is |
| 682 | // because when our children have an alpha of 0.0f, they are still rendering their "dimmed" |
| 683 | // versions |
| 684 | @Override |
| 685 | protected boolean onSetAlpha(int alpha) { |
| 686 | return true; |
| 687 | } |
| 688 | |
| 689 | public void setAlpha(float alpha) { |
| 690 | setChildrenAlpha(alpha); |
| 691 | super.setAlpha(alpha); |
| 692 | } |
| 693 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 694 | private void setChildrenAlpha(float alpha) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 695 | final int childCount = getChildCount(); |
| 696 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 697 | getChildAt(i).setAlpha(alpha); |
| 698 | } |
| 699 | } |
| 700 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 701 | private boolean isVacantIgnoring( |
| 702 | int originX, int originY, int spanX, int spanY, View ignoreView) { |
| 703 | if (ignoreView != null) { |
| 704 | markCellsAsUnoccupiedForView(ignoreView); |
| 705 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 706 | boolean isVacant = true; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 707 | for (int i = 0; i < spanY; i++) { |
| 708 | if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) { |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 709 | isVacant = false; |
| 710 | break; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 711 | } |
| 712 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 713 | if (ignoreView != null) { |
| 714 | markCellsAsOccupiedForView(ignoreView); |
| 715 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 716 | return isVacant; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 719 | private boolean isVacant(int originX, int originY, int spanX, int spanY) { |
| 720 | return isVacantIgnoring(originX, originY, spanX, spanY, null); |
| 721 | } |
| 722 | |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 723 | public View getChildAt(int x, int y) { |
| 724 | final int count = getChildCount(); |
| 725 | for (int i = 0; i < count; i++) { |
| 726 | View child = getChildAt(i); |
| 727 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 728 | |
| 729 | if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) && |
| 730 | (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) { |
| 731 | return child; |
| 732 | } |
| 733 | } |
| 734 | return null; |
| 735 | } |
| 736 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 737 | /** |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 738 | * Estimate the size that a child with the given dimensions will take in the layout. |
| 739 | */ |
| 740 | void estimateChildSize(int minWidth, int minHeight, int[] result) { |
| 741 | // Assuming it's placed at 0, 0, find where the bottom right cell will land |
| 742 | rectToCell(minWidth, minHeight, result); |
| 743 | |
| 744 | // Then figure out the rect it will occupy |
| 745 | cellToRect(0, 0, result[0], result[1], mRectF); |
| 746 | result[0] = (int)mRectF.width(); |
| 747 | result[1] = (int)mRectF.height(); |
| 748 | } |
| 749 | |
| 750 | /** |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 751 | * Estimate where the top left cell of the dragged item will land if it is dropped. |
| 752 | * |
| 753 | * @param originX The X value of the top left corner of the item |
| 754 | * @param originY The Y value of the top left corner of the item |
| 755 | * @param spanX The number of horizontal cells that the item spans |
| 756 | * @param spanY The number of vertical cells that the item spans |
| 757 | * @param result The estimated drop cell X and Y. |
| 758 | */ |
| 759 | void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 760 | final int countX = mCountX; |
| 761 | final int countY = mCountY; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 762 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 763 | // pointToCellRounded takes the top left of a cell but will pad that with |
| 764 | // cellWidth/2 and cellHeight/2 when finding the matching cell |
| 765 | pointToCellRounded(originX, originY, result); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 766 | |
| 767 | // If the item isn't fully on this screen, snap to the edges |
| 768 | int rightOverhang = result[0] + spanX - countX; |
| 769 | if (rightOverhang > 0) { |
| 770 | result[0] -= rightOverhang; // Snap to right |
| 771 | } |
| 772 | result[0] = Math.max(0, result[0]); // Snap to left |
| 773 | int bottomOverhang = result[1] + spanY - countY; |
| 774 | if (bottomOverhang > 0) { |
| 775 | result[1] -= bottomOverhang; // Snap to bottom |
| 776 | } |
| 777 | result[1] = Math.max(0, result[1]); // Snap to top |
| 778 | } |
| 779 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 780 | void visualizeDropLocation(View view, int originX, int originY, int spanX, int spanY) { |
| 781 | final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, view, mDragCell); |
| 782 | mDragCenter.set(originX + (view.getWidth() / 2), originY + (view.getHeight() / 2)); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 783 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 784 | if (nearest != null) { |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 785 | // Find the top left corner of the rect the object will occupy |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 786 | final int[] topLeft = mTmpPoint; |
| 787 | cellToPoint(nearest[0], nearest[1], topLeft); |
| 788 | |
| 789 | // Need to copy these, because the next call to cellToPoint will overwrite them |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 790 | final int left = topLeft[0]; |
| 791 | final int top = topLeft[1]; |
| 792 | |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 793 | final Rect dragRect = mDragRects[mDragRectCurrent]; |
| 794 | |
| 795 | if (dragRect.isEmpty() || left != dragRect.left || top != dragRect.top) { |
| 796 | // Now find the bottom right |
| 797 | final int[] bottomRight = mTmpPoint; |
| 798 | cellToPoint(nearest[0] + spanX - 1, nearest[1] + spanY - 1, bottomRight); |
| 799 | bottomRight[0] += mCellWidth; |
| 800 | bottomRight[1] += mCellHeight; |
| 801 | |
| 802 | final int oldIndex = mDragRectCurrent; |
| 803 | mDragRectCurrent = (oldIndex + 1) % mDragRects.length; |
| 804 | |
| 805 | mDragRects[mDragRectCurrent].set(left, top, bottomRight[0], bottomRight[1]); |
| 806 | |
| 807 | mDragRectAnims[oldIndex].animateOut(); |
| 808 | mDragRectAnims[mDragRectCurrent].animateIn(); |
| 809 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 813 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 814 | * Find a vacant area that will fit the given bounds nearest the requested |
| 815 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 816 | * |
Romain Guy | 51afc02 | 2009-05-04 18:03:43 -0700 | [diff] [blame] | 817 | * @param pixelX The X location at which you want to search for a vacant area. |
| 818 | * @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] | 819 | * @param spanX Horizontal span of the object. |
| 820 | * @param spanY Vertical span of the object. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 821 | * @param result Array in which to place the result, or null (in which case a new array will |
| 822 | * be allocated) |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 823 | * @return The X, Y cell of a vacant area that can contain this object, |
| 824 | * nearest the requested location. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 825 | */ |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 826 | int[] findNearestVacantArea( |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 827 | int pixelX, int pixelY, int spanX, int spanY, int[] result) { |
| 828 | return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result); |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 829 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 830 | |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 831 | /** |
| 832 | * Find a vacant area that will fit the given bounds nearest the requested |
| 833 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 834 | * |
| 835 | * @param pixelX The X location at which you want to search for a vacant area. |
| 836 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 837 | * @param spanX Horizontal span of the object. |
| 838 | * @param spanY Vertical span of the object. |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 839 | * @param ignoreView Considers space occupied by this view as unoccupied |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 840 | * @param result Previously returned value to possibly recycle. |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 841 | * @return The X, Y cell of a vacant area that can contain this object, |
| 842 | * nearest the requested location. |
| 843 | */ |
| 844 | int[] findNearestVacantArea( |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 845 | int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 846 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
| 847 | markCellsAsUnoccupiedForView(ignoreView); |
| 848 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 849 | // Keep track of best-scoring drop area |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 850 | final int[] bestXY = result != null ? result : new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 851 | double bestDistance = Double.MAX_VALUE; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 852 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 853 | final int countX = mCountX; |
| 854 | final int countY = mCountY; |
| 855 | final boolean[][] occupied = mOccupied; |
| 856 | |
| 857 | for (int x = 0; x < countX - (spanX - 1); x++) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 858 | inner: |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 859 | for (int y = 0; y < countY - (spanY - 1); y++) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 860 | for (int i = 0; i < spanX; i++) { |
| 861 | for (int j = 0; j < spanY; j++) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 862 | if (occupied[x + i][y + j]) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 863 | // small optimization: we can skip to below the row we just found |
| 864 | // an occupied cell |
| 865 | y += j; |
| 866 | continue inner; |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | final int[] cellXY = mTmpCellXY; |
| 871 | cellToPoint(x, y, cellXY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 872 | |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 873 | double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2) |
| 874 | + Math.pow(cellXY[1] - pixelY, 2)); |
| 875 | if (distance <= bestDistance) { |
| 876 | bestDistance = distance; |
| 877 | bestXY[0] = x; |
| 878 | bestXY[1] = y; |
| 879 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 880 | } |
| 881 | } |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 882 | // re-mark space taken by ignoreView as occupied |
| 883 | markCellsAsOccupiedForView(ignoreView); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 884 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 885 | // Return null if no suitable location found |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 886 | if (bestDistance < Double.MAX_VALUE) { |
| 887 | return bestXY; |
| 888 | } else { |
| 889 | return null; |
| 890 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 891 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 892 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 893 | boolean existsEmptyCell() { |
| 894 | return findCellForSpan(null, 1, 1); |
| 895 | } |
| 896 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 897 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 898 | * Finds the upper-left coordinate of the first rectangle in the grid that can |
| 899 | * hold a cell of the specified dimensions. If intersectX and intersectY are not -1, |
| 900 | * then this method will only return coordinates for rectangles that contain the cell |
| 901 | * (intersectX, intersectY) |
| 902 | * |
| 903 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 904 | * can be found. |
| 905 | * @param spanX The horizontal span of the cell we want to find. |
| 906 | * @param spanY The vertical span of the cell we want to find. |
| 907 | * |
| 908 | * @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] | 909 | */ |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 910 | boolean findCellForSpan(int[] cellXY, int spanX, int spanY) { |
| 911 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null); |
| 912 | } |
| 913 | |
| 914 | /** |
| 915 | * Like above, but ignores any cells occupied by the item "ignoreView" |
| 916 | * |
| 917 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 918 | * can be found. |
| 919 | * @param spanX The horizontal span of the cell we want to find. |
| 920 | * @param spanY The vertical span of the cell we want to find. |
| 921 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 922 | * @return |
| 923 | */ |
| 924 | boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) { |
| 925 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView); |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * Like above, but if intersectX and intersectY are not -1, then this method will try to |
| 930 | * return coordinates for rectangles that contain the cell [intersectX, intersectY] |
| 931 | * |
| 932 | * @param spanX The horizontal span of the cell we want to find. |
| 933 | * @param spanY The vertical span of the cell we want to find. |
| 934 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 935 | * @param intersectX The X coordinate of the cell that we should try to overlap |
| 936 | * @param intersectX The Y coordinate of the cell that we should try to overlap |
| 937 | * |
| 938 | * @return True if a vacant cell of the specified dimension was found, false otherwise. |
| 939 | */ |
| 940 | boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY, |
| 941 | int intersectX, int intersectY) { |
| 942 | return findCellForSpanThatIntersectsIgnoring( |
| 943 | cellXY, spanX, spanY, intersectX, intersectY, null); |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * The superset of the above two methods |
| 948 | */ |
| 949 | boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY, |
| 950 | int intersectX, int intersectY, View ignoreView) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 951 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
| 952 | markCellsAsUnoccupiedForView(ignoreView); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 953 | |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 954 | boolean foundCell = false; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 955 | while (true) { |
| 956 | int startX = 0; |
| 957 | if (intersectX >= 0) { |
| 958 | startX = Math.max(startX, intersectX - (spanX - 1)); |
| 959 | } |
| 960 | int endX = mCountX - (spanX - 1); |
| 961 | if (intersectX >= 0) { |
| 962 | endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0)); |
| 963 | } |
| 964 | int startY = 0; |
| 965 | if (intersectY >= 0) { |
| 966 | startY = Math.max(startY, intersectY - (spanY - 1)); |
| 967 | } |
| 968 | int endY = mCountY - (spanY - 1); |
| 969 | if (intersectY >= 0) { |
| 970 | endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0)); |
| 971 | } |
| 972 | |
| 973 | for (int x = startX; x < endX; x++) { |
| 974 | inner: |
| 975 | for (int y = startY; y < endY; y++) { |
| 976 | for (int i = 0; i < spanX; i++) { |
| 977 | for (int j = 0; j < spanY; j++) { |
| 978 | if (mOccupied[x + i][y + j]) { |
| 979 | // small optimization: we can skip to below the row we just found |
| 980 | // an occupied cell |
| 981 | y += j; |
| 982 | continue inner; |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | if (cellXY != null) { |
| 987 | cellXY[0] = x; |
| 988 | cellXY[1] = y; |
| 989 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 990 | foundCell = true; |
| 991 | break; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | if (intersectX == -1 && intersectY == -1) { |
| 995 | break; |
| 996 | } else { |
| 997 | // if we failed to find anything, try again but without any requirements of |
| 998 | // intersecting |
| 999 | intersectX = -1; |
| 1000 | intersectY = -1; |
| 1001 | continue; |
| 1002 | } |
| 1003 | } |
| 1004 | |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1005 | // re-mark space taken by ignoreView as occupied |
| 1006 | markCellsAsOccupiedForView(ignoreView); |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1007 | return foundCell; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * Called when drag has left this CellLayout or has been completed (successfully or not) |
| 1012 | */ |
| 1013 | void onDragExit() { |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1014 | // Invalidate the drag data |
| 1015 | mDragCell[0] = -1; |
| 1016 | mDragCell[1] = -1; |
| 1017 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1018 | setHover(false); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1019 | invalidate(); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1020 | |
| 1021 | // Fade out the drag indicators |
| 1022 | if (mCrosshairsAnimator != null) { |
| 1023 | animateCrosshairsTo(0.0f); |
| 1024 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1025 | |
| 1026 | mDragRectAnims[mDragRectCurrent].animateOut(); |
| 1027 | mDragRectCurrent = (mDragRectCurrent + 1) % mDragRects.length; |
| 1028 | mDragRects[mDragRectCurrent].setEmpty(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1032 | * Mark a child as having been dropped. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1033 | * At the beginning of the drag operation, the child may have been on another |
| 1034 | * screen, but it is reparented before this method is called. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1035 | * |
| 1036 | * @param child The child that is being dropped |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1037 | */ |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1038 | void onDropChild(View child) { |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1039 | if (child != null) { |
| 1040 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1041 | lp.isDragging = false; |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 1042 | lp.dropped = true; |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1043 | child.requestLayout(); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1044 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1045 | onDragExit(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | void onDropAborted(View child) { |
| 1049 | if (child != null) { |
| 1050 | ((LayoutParams) child.getLayoutParams()).isDragging = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1051 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1052 | onDragExit(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * Start dragging the specified child |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1057 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1058 | * @param child The child that is being dragged |
| 1059 | */ |
| 1060 | void onDragChild(View child) { |
| 1061 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1062 | lp.isDragging = true; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /** |
| 1066 | * A drag event has begun over this layout. |
| 1067 | * It may have begun over this layout (in which case onDragChild is called first), |
| 1068 | * or it may have begun on another layout. |
| 1069 | */ |
| 1070 | void onDragEnter(View dragView) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1071 | // Fade in the drag indicators |
| 1072 | if (mCrosshairsAnimator != null) { |
| 1073 | animateCrosshairsTo(1.0f); |
| 1074 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1075 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1076 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1077 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1078 | * Computes a bounding rectangle for a range of cells |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1079 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1080 | * @param cellX X coordinate of upper left corner expressed as a cell position |
| 1081 | * @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] | 1082 | * @param cellHSpan Width in cells |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1083 | * @param cellVSpan Height in cells |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1084 | * @param resultRect Rect into which to put the results |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1085 | */ |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1086 | 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] | 1087 | final int cellWidth = mCellWidth; |
| 1088 | final int cellHeight = mCellHeight; |
| 1089 | final int widthGap = mWidthGap; |
| 1090 | final int heightGap = mHeightGap; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1091 | |
| 1092 | final int hStartPadding = getLeftPadding(); |
| 1093 | final int vStartPadding = getTopPadding(); |
| 1094 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1095 | int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap); |
| 1096 | int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap); |
| 1097 | |
| 1098 | int x = hStartPadding + cellX * (cellWidth + widthGap); |
| 1099 | int y = vStartPadding + cellY * (cellHeight + heightGap); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1100 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1101 | resultRect.set(x, y, x + width, y + height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1102 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1103 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1104 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1105 | * 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] | 1106 | * fit the given rectangle. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1107 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1108 | * @param width Width in pixels |
| 1109 | * @param height Height in pixels |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1110 | * @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] | 1111 | */ |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1112 | public int[] rectToCell(int width, int height, int[] result) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1113 | // Always assume we're working with the smallest span to make sure we |
| 1114 | // reserve enough space in both orientations. |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1115 | final Resources resources = getResources(); |
| 1116 | int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width); |
| 1117 | int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1118 | int smallerSize = Math.min(actualWidth, actualHeight); |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1119 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1120 | // Always round up to next largest cell |
| 1121 | int spanX = (width + smallerSize) / smallerSize; |
| 1122 | int spanY = (height + smallerSize) / smallerSize; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1123 | |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1124 | if (result == null) { |
| 1125 | return new int[] { spanX, spanY }; |
| 1126 | } |
| 1127 | result[0] = spanX; |
| 1128 | result[1] = spanY; |
| 1129 | return result; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | /** |
| 1133 | * Find the first vacant cell, if there is one. |
| 1134 | * |
| 1135 | * @param vacant Holds the x and y coordinate of the vacant cell |
| 1136 | * @param spanX Horizontal cell span. |
| 1137 | * @param spanY Vertical cell span. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1138 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1139 | * @return True if a vacant cell was found |
| 1140 | */ |
| 1141 | public boolean getVacantCell(int[] vacant, int spanX, int spanY) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1142 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1143 | return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | static boolean findVacantCell(int[] vacant, int spanX, int spanY, |
| 1147 | int xCount, int yCount, boolean[][] occupied) { |
| 1148 | |
| 1149 | for (int x = 0; x < xCount; x++) { |
| 1150 | for (int y = 0; y < yCount; y++) { |
| 1151 | boolean available = !occupied[x][y]; |
| 1152 | out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) { |
| 1153 | for (int j = y; j < y + spanY - 1 && y < yCount; j++) { |
| 1154 | available = available && !occupied[i][j]; |
| 1155 | if (!available) break out; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | if (available) { |
| 1160 | vacant[0] = x; |
| 1161 | vacant[1] = y; |
| 1162 | return true; |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | return false; |
| 1168 | } |
| 1169 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1170 | /** |
| 1171 | * Update the array of occupied cells (mOccupied), and return a flattened copy of the array. |
| 1172 | */ |
| 1173 | boolean[] getOccupiedCellsFlattened() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 1174 | final int xCount = mCountX; |
| 1175 | final int yCount = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1176 | final boolean[][] occupied = mOccupied; |
| 1177 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1178 | final boolean[] flat = new boolean[xCount * yCount]; |
| 1179 | for (int y = 0; y < yCount; y++) { |
| 1180 | for (int x = 0; x < xCount; x++) { |
| 1181 | flat[y * xCount + x] = occupied[x][y]; |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | return flat; |
| 1186 | } |
| 1187 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1188 | private void clearOccupiedCells() { |
| 1189 | for (int x = 0; x < mCountX; x++) { |
| 1190 | for (int y = 0; y < mCountY; y++) { |
| 1191 | mOccupied[x][y] = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1192 | } |
| 1193 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1194 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1195 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1196 | public void onMove(View view, int newCellX, int newCellY) { |
| 1197 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1198 | markCellsAsUnoccupiedForView(view); |
| 1199 | markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true); |
| 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 | private void markCellsAsOccupiedForView(View view) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1203 | if (view == null || view.getParent() != this) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1204 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1205 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true); |
| 1206 | } |
| 1207 | |
| 1208 | private void markCellsAsUnoccupiedForView(View view) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1209 | if (view == null || view.getParent() != this) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1210 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1211 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false); |
| 1212 | } |
| 1213 | |
| 1214 | private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) { |
| 1215 | for (int x = cellX; x < cellX + spanX && x < mCountX; x++) { |
| 1216 | for (int y = cellY; y < cellY + spanY && y < mCountY; y++) { |
| 1217 | mOccupied[x][y] = value; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | @Override |
| 1223 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 1224 | return new CellLayout.LayoutParams(getContext(), attrs); |
| 1225 | } |
| 1226 | |
| 1227 | @Override |
| 1228 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 1229 | return p instanceof CellLayout.LayoutParams; |
| 1230 | } |
| 1231 | |
| 1232 | @Override |
| 1233 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 1234 | return new CellLayout.LayoutParams(p); |
| 1235 | } |
| 1236 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1237 | public static class CellLayoutAnimationController extends LayoutAnimationController { |
| 1238 | public CellLayoutAnimationController(Animation animation, float delay) { |
| 1239 | super(animation, delay); |
| 1240 | } |
| 1241 | |
| 1242 | @Override |
| 1243 | protected long getDelayForView(View view) { |
| 1244 | return (int) (Math.random() * 150); |
| 1245 | } |
| 1246 | } |
| 1247 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1248 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
| 1249 | /** |
| 1250 | * Horizontal location of the item in the grid. |
| 1251 | */ |
| 1252 | @ViewDebug.ExportedProperty |
| 1253 | public int cellX; |
| 1254 | |
| 1255 | /** |
| 1256 | * Vertical location of the item in the grid. |
| 1257 | */ |
| 1258 | @ViewDebug.ExportedProperty |
| 1259 | public int cellY; |
| 1260 | |
| 1261 | /** |
| 1262 | * Number of cells spanned horizontally by the item. |
| 1263 | */ |
| 1264 | @ViewDebug.ExportedProperty |
| 1265 | public int cellHSpan; |
| 1266 | |
| 1267 | /** |
| 1268 | * Number of cells spanned vertically by the item. |
| 1269 | */ |
| 1270 | @ViewDebug.ExportedProperty |
| 1271 | public int cellVSpan; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1272 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1273 | /** |
| 1274 | * Is this item currently being dragged |
| 1275 | */ |
| 1276 | public boolean isDragging; |
| 1277 | |
| 1278 | // X coordinate of the view in the layout. |
| 1279 | @ViewDebug.ExportedProperty |
| 1280 | int x; |
| 1281 | // Y coordinate of the view in the layout. |
| 1282 | @ViewDebug.ExportedProperty |
| 1283 | int y; |
| 1284 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 1285 | boolean dropped; |
Romain Guy | fcb9e71 | 2009-10-02 16:06:52 -0700 | [diff] [blame] | 1286 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1287 | public LayoutParams(Context c, AttributeSet attrs) { |
| 1288 | super(c, attrs); |
| 1289 | cellHSpan = 1; |
| 1290 | cellVSpan = 1; |
| 1291 | } |
| 1292 | |
| 1293 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 1294 | super(source); |
| 1295 | cellHSpan = 1; |
| 1296 | cellVSpan = 1; |
| 1297 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1298 | |
| 1299 | public LayoutParams(LayoutParams source) { |
| 1300 | super(source); |
| 1301 | this.cellX = source.cellX; |
| 1302 | this.cellY = source.cellY; |
| 1303 | this.cellHSpan = source.cellHSpan; |
| 1304 | this.cellVSpan = source.cellVSpan; |
| 1305 | } |
| 1306 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1307 | public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) { |
Romain Guy | 8f19cdd | 2010-01-08 15:07:00 -0800 | [diff] [blame] | 1308 | super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1309 | this.cellX = cellX; |
| 1310 | this.cellY = cellY; |
| 1311 | this.cellHSpan = cellHSpan; |
| 1312 | this.cellVSpan = cellVSpan; |
| 1313 | } |
| 1314 | |
| 1315 | public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap, |
| 1316 | int hStartPadding, int vStartPadding) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1317 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1318 | final int myCellHSpan = cellHSpan; |
| 1319 | final int myCellVSpan = cellVSpan; |
| 1320 | final int myCellX = cellX; |
| 1321 | final int myCellY = cellY; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1322 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1323 | width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) - |
| 1324 | leftMargin - rightMargin; |
| 1325 | height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) - |
| 1326 | topMargin - bottomMargin; |
| 1327 | |
| 1328 | x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin; |
| 1329 | y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin; |
| 1330 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1331 | |
| 1332 | public String toString() { |
| 1333 | return "(" + this.cellX + ", " + this.cellY + ")"; |
| 1334 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1335 | } |
| 1336 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1337 | // This class stores info for two purposes: |
| 1338 | // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY, |
| 1339 | // its spanX, spanY, and the screen it is on |
| 1340 | // 2. When long clicking on an empty cell in a CellLayout, we save information about the |
| 1341 | // cellX and cellY coordinates and which page was clicked. We then set this as a tag on |
| 1342 | // the CellLayout that was long clicked |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1343 | static final class CellInfo implements ContextMenu.ContextMenuInfo { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1344 | View cell; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1345 | int cellX = -1; |
| 1346 | int cellY = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1347 | int spanX; |
| 1348 | int spanY; |
| 1349 | int screen; |
| 1350 | boolean valid; |
| 1351 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1352 | @Override |
| 1353 | public String toString() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1354 | return "Cell[view=" + (cell == null ? "null" : cell.getClass()) |
| 1355 | + ", x=" + cellX + ", y=" + cellY + "]"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1356 | } |
| 1357 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1358 | } |