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