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