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