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 | |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame^] | 56 | public class CellLayout extends ViewGroup implements Dimmable, VisibilityChangedListener { |
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; |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame^] | 412 | invalidate(); |
| 413 | } |
| 414 | |
| 415 | public void receiveVisibilityChangedMessage(View v) { |
| 416 | invalidateCache(); |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | public void updateCache() { |
| 420 | mCacheCanvas.drawColor(0x00000000, Mode.CLEAR); |
| 421 | |
| 422 | float alpha = getAlpha(); |
| 423 | setAlpha(1.0f); |
| 424 | drawChildren(mCacheCanvas); |
| 425 | setAlpha(alpha); |
| 426 | |
| 427 | mIsCacheDirty = false; |
| 428 | } |
| 429 | |
| 430 | public void dispatchDraw(Canvas canvas) { |
| 431 | final int count = getChildCount(); |
| 432 | |
| 433 | if (!mIsCacheDirty) { |
| 434 | // Check if one of the children (an icon or widget) is dirty |
| 435 | for (int i = 0; i < count; i++) { |
| 436 | final View child = getChildAt(i); |
| 437 | if (child.isDirty()) { |
| 438 | mIsCacheDirty = true; |
| 439 | break; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
Michael Jurka | 026f555 | 2011-01-07 12:58:57 -0800 | [diff] [blame] | 444 | boolean useBitmapCache = mIsCacheEnabled && getScaleX() < mMaxScaleForUsingBitmapCache; |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 445 | if (mForceCacheUpdate || |
Michael Jurka | 026f555 | 2011-01-07 12:58:57 -0800 | [diff] [blame] | 446 | (useBitmapCache && !mDisableCacheUpdates)) { |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 447 | // Sometimes we force a cache update-- this is used to make sure the cache will look as |
| 448 | // up-to-date as possible right when we disable cache updates |
| 449 | if (mIsCacheDirty) { |
| 450 | updateCache(); |
| 451 | } |
| 452 | mForceCacheUpdate = false; |
| 453 | } |
| 454 | |
Michael Jurka | 026f555 | 2011-01-07 12:58:57 -0800 | [diff] [blame] | 455 | if (useBitmapCache) { |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 456 | mCachePaint.setAlpha((int)(255*getAlpha())); |
| 457 | canvas.drawBitmap(mCache, mCacheRect, mBackgroundRect, mCachePaint); |
| 458 | } else { |
| 459 | super.dispatchDraw(canvas); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | private void prepareCacheBitmap() { |
| 464 | if (mCache == null) { |
| 465 | mCache = Bitmap.createBitmap((int) (getWidth() * mBitmapCacheScale), |
| 466 | (int) (getHeight() * mBitmapCacheScale), Config.ARGB_8888); |
| 467 | |
| 468 | mCachePaint = new Paint(); |
| 469 | mCachePaint.setFilterBitmap(true); |
| 470 | mCacheCanvas.setBitmap(mCache); |
| 471 | mCacheCanvas.scale(mBitmapCacheScale, mBitmapCacheScale); |
| 472 | } |
| 473 | } |
| 474 | |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 475 | @Override |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 476 | protected void onDraw(Canvas canvas) { |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 477 | // When we're large, we are either drawn in a "hover" state (ie when dragging an item to |
| 478 | // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f) |
| 479 | // When we're small, we are either drawn normally or in the "accepts drops" state (during |
| 480 | // a drag). However, we also drag the mini hover background *over* one of those two |
| 481 | // backgrounds |
Winson Chung | 26cbf3a | 2011-01-06 16:25:55 -0800 | [diff] [blame] | 482 | if (LauncherApplication.isScreenXLarge() && mBackgroundAlpha > 0.0f) { |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 483 | Drawable bg; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 484 | boolean mini = getScaleX() < 0.5f; |
| 485 | |
| 486 | if (mIsDragOverlapping) { |
| 487 | // In the mini case, we draw the active_glow bg *over* the active background |
| 488 | bg = mini ? mActiveBackgroundMini : mActiveGlowBackground; |
| 489 | } else if (mIsDragOccuring && mAcceptsDrops) { |
| 490 | bg = mini ? mActiveBackgroundMini : mActiveBackground; |
| 491 | } else if (mIsDefaultDropTarget) { |
| 492 | bg = mini ? mNormalGlowBackgroundMini : mNormalGlowBackground; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 493 | } else { |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 494 | bg = mini ? mNormalBackgroundMini : mNormalBackground; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 495 | } |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 496 | |
| 497 | bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255)); |
| 498 | bg.setBounds(mBackgroundRect); |
| 499 | bg.draw(canvas); |
| 500 | |
| 501 | if (mini && mIsDragOverlapping) { |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 502 | boolean modifiedClipRect = false; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 503 | if (mGlowBackgroundScale > 1.0f) { |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 504 | // If the hover background's scale is greater than 1, we'll be drawing outside |
| 505 | // the bounds of this CellLayout. Get around that by temporarily increasing the |
| 506 | // size of the clip rect |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 507 | float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 508 | Rect clipRect = canvas.getClipBounds(); |
| 509 | int marginX = (int) (marginFraction * (clipRect.right - clipRect.left)); |
| 510 | int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top)); |
| 511 | canvas.save(Canvas.CLIP_SAVE_FLAG); |
| 512 | canvas.clipRect(-marginX, -marginY, |
| 513 | getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE); |
| 514 | modifiedClipRect = true; |
| 515 | } |
| 516 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 517 | mActiveGlowBackgroundMini.setAlpha( |
| 518 | (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255)); |
| 519 | mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect); |
| 520 | mActiveGlowBackgroundMini.draw(canvas); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 521 | if (modifiedClipRect) { |
| 522 | canvas.restore(); |
| 523 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 524 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 525 | } |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 526 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 527 | if (mCrosshairsVisibility > 0.0f) { |
| 528 | final int countX = mCountX; |
| 529 | final int countY = mCountY; |
| 530 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 531 | final float MAX_ALPHA = 0.4f; |
| 532 | final int MAX_VISIBLE_DISTANCE = 600; |
| 533 | final float DISTANCE_MULTIPLIER = 0.002f; |
| 534 | |
| 535 | final Drawable d = mCrosshairsDrawable; |
| 536 | final int width = d.getIntrinsicWidth(); |
| 537 | final int height = d.getIntrinsicHeight(); |
| 538 | |
| 539 | int x = getLeftPadding() - (mWidthGap / 2) - (width / 2); |
| 540 | for (int col = 0; col <= countX; col++) { |
| 541 | int y = getTopPadding() - (mHeightGap / 2) - (height / 2); |
| 542 | for (int row = 0; row <= countY; row++) { |
| 543 | mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y); |
| 544 | float dist = mTmpPointF.length(); |
| 545 | // Crosshairs further from the drag point are more faint |
| 546 | float alpha = Math.min(MAX_ALPHA, |
| 547 | DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist)); |
| 548 | if (alpha > 0.0f) { |
| 549 | d.setBounds(x, y, x + width, y + height); |
| 550 | d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility)); |
| 551 | d.draw(canvas); |
| 552 | } |
| 553 | y += mCellHeight + mHeightGap; |
| 554 | } |
| 555 | x += mCellWidth + mWidthGap; |
| 556 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 557 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 558 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 559 | final Paint paint = mDragOutlinePaint; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 560 | for (int i = 0; i < mDragOutlines.length; i++) { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 561 | final float alpha = mDragOutlineAlphas[i]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 562 | if (alpha > 0) { |
| 563 | final Point p = mDragOutlines[i]; |
| 564 | final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag(); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 565 | paint.setAlpha((int)(alpha + .5f)); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 566 | canvas.drawBitmap(b, p.x, p.y, paint); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 567 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 571 | public void setDimmableProgress(float progress) { |
| 572 | for (int i = 0; i < getChildCount(); i++) { |
| 573 | Dimmable d = (Dimmable) getChildAt(i); |
| 574 | d.setDimmableProgress(progress); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | public float getDimmableProgress() { |
| 579 | if (getChildCount() > 0) { |
| 580 | return ((Dimmable) getChildAt(0)).getDimmableProgress(); |
| 581 | } |
| 582 | return 0.0f; |
| 583 | } |
| 584 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 585 | @Override |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 586 | public void cancelLongPress() { |
| 587 | super.cancelLongPress(); |
| 588 | |
| 589 | // Cancel long press for all children |
| 590 | final int count = getChildCount(); |
| 591 | for (int i = 0; i < count; i++) { |
| 592 | final View child = getChildAt(i); |
| 593 | child.cancelLongPress(); |
| 594 | } |
| 595 | } |
| 596 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 597 | public void setOnInterceptTouchListener(View.OnTouchListener listener) { |
| 598 | mInterceptTouchListener = listener; |
| 599 | } |
| 600 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 601 | int getCountX() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 602 | return mCountX; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | int getCountY() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 606 | return mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 609 | public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) { |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 610 | return addViewToCellLayout(child, index, childId, params, true); |
| 611 | } |
| 612 | |
| 613 | public boolean addViewToCellLayout( |
| 614 | View child, int index, int childId, LayoutParams params, boolean markCells) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 615 | final LayoutParams lp = params; |
| 616 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 617 | // Generate an id for each view, this assumes we have at most 256x256 cells |
| 618 | // per workspace screen |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 619 | 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] | 620 | // If the horizontal or vertical span is set to -1, it is taken to |
| 621 | // mean that it spans the extent of the CellLayout |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 622 | if (lp.cellHSpan < 0) lp.cellHSpan = mCountX; |
| 623 | if (lp.cellVSpan < 0) lp.cellVSpan = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 624 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 625 | child.setId(childId); |
| 626 | |
| 627 | addView(child, index, lp); |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame^] | 628 | if (child instanceof VisibilityChangedBroadcaster) { |
| 629 | VisibilityChangedBroadcaster v = (VisibilityChangedBroadcaster) child; |
| 630 | v.setVisibilityChangedListener(this); |
| 631 | } |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 632 | |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 633 | // invalidate the cache to have it reflect the new item |
| 634 | invalidateCache(); |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 635 | if (markCells) markCellsAsOccupiedForView(child); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 636 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 637 | return true; |
| 638 | } |
| 639 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 640 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 641 | |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 642 | public void setAcceptsDrops(boolean acceptsDrops) { |
| 643 | if (mAcceptsDrops != acceptsDrops) { |
| 644 | mAcceptsDrops = acceptsDrops; |
| 645 | invalidate(); |
| 646 | } |
| 647 | } |
| 648 | |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 649 | public boolean getAcceptsDrops() { |
| 650 | return mAcceptsDrops; |
| 651 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 652 | |
| 653 | @Override |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 654 | public void removeAllViews() { |
Joe Onorato | 7cef289 | 2011-01-07 10:08:48 -0800 | [diff] [blame] | 655 | super.removeAllViews(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 656 | clearOccupiedCells(); |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame^] | 657 | invalidateCache(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | @Override |
| 661 | public void removeAllViewsInLayout() { |
Joe Onorato | 7cef289 | 2011-01-07 10:08:48 -0800 | [diff] [blame] | 662 | super.removeAllViewsInLayout(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 663 | clearOccupiedCells(); |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame^] | 664 | invalidateCache(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 667 | public void removeViewWithoutMarkingCells(View view) { |
| 668 | super.removeView(view); |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame^] | 669 | invalidateCache(); |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 672 | @Override |
| 673 | public void removeView(View view) { |
| 674 | markCellsAsUnoccupiedForView(view); |
| 675 | super.removeView(view); |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame^] | 676 | invalidateCache(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | @Override |
| 680 | public void removeViewAt(int index) { |
| 681 | markCellsAsUnoccupiedForView(getChildAt(index)); |
| 682 | super.removeViewAt(index); |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame^] | 683 | invalidateCache(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | @Override |
| 687 | public void removeViewInLayout(View view) { |
| 688 | markCellsAsUnoccupiedForView(view); |
| 689 | super.removeViewInLayout(view); |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame^] | 690 | invalidateCache(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | @Override |
| 694 | public void removeViews(int start, int count) { |
| 695 | for (int i = start; i < start + count; i++) { |
| 696 | markCellsAsUnoccupiedForView(getChildAt(i)); |
| 697 | } |
| 698 | super.removeViews(start, count); |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame^] | 699 | invalidateCache(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | @Override |
| 703 | public void removeViewsInLayout(int start, int count) { |
| 704 | for (int i = start; i < start + count; i++) { |
| 705 | markCellsAsUnoccupiedForView(getChildAt(i)); |
| 706 | } |
| 707 | super.removeViewsInLayout(start, count); |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame^] | 708 | invalidateCache(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 712 | public void requestChildFocus(View child, View focused) { |
| 713 | super.requestChildFocus(child, focused); |
| 714 | if (child != null) { |
| 715 | Rect r = new Rect(); |
| 716 | child.getDrawingRect(r); |
| 717 | requestRectangleOnScreen(r); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | @Override |
| 722 | protected void onAttachedToWindow() { |
| 723 | super.onAttachedToWindow(); |
| 724 | mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this); |
| 725 | } |
| 726 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 727 | public void setTagToCellInfoForPoint(int touchX, int touchY) { |
| 728 | final CellInfo cellInfo = mCellInfo; |
| 729 | final Rect frame = mRect; |
| 730 | final int x = touchX + mScrollX; |
| 731 | final int y = touchY + mScrollY; |
| 732 | final int count = getChildCount(); |
| 733 | |
| 734 | boolean found = false; |
| 735 | for (int i = count - 1; i >= 0; i--) { |
| 736 | final View child = getChildAt(i); |
| 737 | |
| 738 | if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) { |
| 739 | child.getHitRect(frame); |
| 740 | if (frame.contains(x, y)) { |
| 741 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 742 | cellInfo.cell = child; |
| 743 | cellInfo.cellX = lp.cellX; |
| 744 | cellInfo.cellY = lp.cellY; |
| 745 | cellInfo.spanX = lp.cellHSpan; |
| 746 | cellInfo.spanY = lp.cellVSpan; |
| 747 | cellInfo.valid = true; |
| 748 | found = true; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 749 | break; |
| 750 | } |
| 751 | } |
| 752 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 753 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 754 | if (!found) { |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 755 | final int cellXY[] = mTmpCellXY; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 756 | pointToCellExact(x, y, cellXY); |
| 757 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 758 | cellInfo.cell = null; |
| 759 | cellInfo.cellX = cellXY[0]; |
| 760 | cellInfo.cellY = cellXY[1]; |
| 761 | cellInfo.spanX = 1; |
| 762 | cellInfo.spanY = 1; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 763 | cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX && |
| 764 | cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]]; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 765 | } |
| 766 | setTag(cellInfo); |
| 767 | } |
| 768 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 769 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 770 | @Override |
| 771 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 772 | if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) { |
| 773 | return true; |
| 774 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 775 | final int action = ev.getAction(); |
| 776 | final CellInfo cellInfo = mCellInfo; |
| 777 | |
| 778 | if (action == MotionEvent.ACTION_DOWN) { |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 779 | setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 780 | } else if (action == MotionEvent.ACTION_UP) { |
| 781 | cellInfo.cell = null; |
| 782 | cellInfo.cellX = -1; |
| 783 | cellInfo.cellY = -1; |
| 784 | cellInfo.spanX = 0; |
| 785 | cellInfo.spanY = 0; |
| 786 | cellInfo.valid = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 787 | setTag(cellInfo); |
| 788 | } |
| 789 | |
| 790 | return false; |
| 791 | } |
| 792 | |
| 793 | @Override |
| 794 | public CellInfo getTag() { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 795 | return (CellInfo) super.getTag(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 796 | } |
| 797 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 798 | /** |
| 799 | * Check if the row 'y' is empty from columns 'left' to 'right', inclusive. |
| 800 | */ |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 801 | private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) { |
| 802 | for (int x = left; x <= right; x++) { |
| 803 | if (occupied[x][y]) { |
| 804 | return false; |
| 805 | } |
| 806 | } |
| 807 | return true; |
| 808 | } |
| 809 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 810 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 811 | * 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] | 812 | * @param x X coordinate of the point |
| 813 | * @param y Y coordinate of the point |
| 814 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 815 | */ |
| 816 | void pointToCellExact(int x, int y, int[] result) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 817 | final int hStartPadding = getLeftPadding(); |
| 818 | final int vStartPadding = getTopPadding(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 819 | |
| 820 | result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap); |
| 821 | result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap); |
| 822 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 823 | final int xAxis = mCountX; |
| 824 | final int yAxis = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 825 | |
| 826 | if (result[0] < 0) result[0] = 0; |
| 827 | if (result[0] >= xAxis) result[0] = xAxis - 1; |
| 828 | if (result[1] < 0) result[1] = 0; |
| 829 | if (result[1] >= yAxis) result[1] = yAxis - 1; |
| 830 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 831 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 832 | /** |
| 833 | * Given a point, return the cell that most closely encloses that point |
| 834 | * @param x X coordinate of the point |
| 835 | * @param y Y coordinate of the point |
| 836 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 837 | */ |
| 838 | void pointToCellRounded(int x, int y, int[] result) { |
| 839 | pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result); |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * 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] | 844 | * |
| 845 | * @param cellX X coordinate of the cell |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 846 | * @param cellY Y coordinate of the cell |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 847 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 848 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 849 | */ |
| 850 | void cellToPoint(int cellX, int cellY, int[] result) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 851 | final int hStartPadding = getLeftPadding(); |
| 852 | final int vStartPadding = getTopPadding(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 853 | |
| 854 | result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap); |
| 855 | result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap); |
| 856 | } |
| 857 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 858 | int getCellWidth() { |
| 859 | return mCellWidth; |
| 860 | } |
| 861 | |
| 862 | int getCellHeight() { |
| 863 | return mCellHeight; |
| 864 | } |
| 865 | |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 866 | int getLeftPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 867 | return mLeftPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | int getTopPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 871 | return mTopPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | int getRightPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 875 | return mRightPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | int getBottomPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 879 | return mBottomPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 880 | } |
| 881 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 882 | @Override |
| 883 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 884 | // TODO: currently ignoring padding |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 885 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 886 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 887 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 888 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 889 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 890 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 891 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 892 | if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { |
| 893 | throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); |
| 894 | } |
| 895 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 896 | final int cellWidth = mCellWidth; |
| 897 | final int cellHeight = mCellHeight; |
| 898 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 899 | int numWidthGaps = mCountX - 1; |
| 900 | int numHeightGaps = mCountY - 1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 901 | |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 902 | if (mWidthGap < 0 || mHeightGap < 0) { |
| 903 | int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY); |
| 904 | mHeightGap = vSpaceLeft / numHeightGaps; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 905 | |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 906 | int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX); |
| 907 | mWidthGap = hSpaceLeft / numWidthGaps; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 908 | |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 909 | // center it around the min gaps |
| 910 | int minGap = Math.min(mWidthGap, mHeightGap); |
| 911 | mWidthGap = mHeightGap = minGap; |
| 912 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 913 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 914 | int count = getChildCount(); |
| 915 | |
| 916 | for (int i = 0; i < count; i++) { |
| 917 | View child = getChildAt(i); |
| 918 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 919 | lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, |
| 920 | mLeftPadding, mTopPadding); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 921 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 922 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 923 | int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, |
| 924 | MeasureSpec.EXACTLY); |
| 925 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 926 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 927 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 928 | if (widthSpecMode == MeasureSpec.AT_MOST) { |
| 929 | int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) + |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 930 | ((mCountX - 1) * mWidthGap); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 931 | int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) + |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 932 | ((mCountY - 1) * mHeightGap); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 933 | setMeasuredDimension(newWidth, newHeight); |
| 934 | } else if (widthSpecMode == MeasureSpec.EXACTLY) { |
| 935 | setMeasuredDimension(widthSpecSize, heightSpecSize); |
| 936 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 940 | 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] | 941 | int count = getChildCount(); |
| 942 | |
| 943 | for (int i = 0; i < count; i++) { |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 944 | final View child = getChildAt(i); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 945 | if (child.getVisibility() != GONE) { |
| 946 | |
| 947 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 948 | |
| 949 | int childLeft = lp.x; |
| 950 | int childTop = lp.y; |
| 951 | child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 952 | |
| 953 | if (lp.dropped) { |
| 954 | lp.dropped = false; |
| 955 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 956 | final int[] cellXY = mTmpCellXY; |
Romain Guy | 06762ab | 2010-01-25 16:51:08 -0800 | [diff] [blame] | 957 | getLocationOnScreen(cellXY); |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 958 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), |
| 959 | WallpaperManager.COMMAND_DROP, |
Romain Guy | 06762ab | 2010-01-25 16:51:08 -0800 | [diff] [blame] | 960 | cellXY[0] + childLeft + lp.width / 2, |
| 961 | cellXY[1] + childTop + lp.height / 2, 0, null); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 962 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 963 | if (lp.animateDrop) { |
| 964 | lp.animateDrop = false; |
Adam Cohen | c42d5e3 | 2011-01-07 19:17:51 -0800 | [diff] [blame] | 965 | |
| 966 | // This call needs to be posted since it results in a requestLayout() |
| 967 | // which shouldn't be called from within a layout pass (can cause a bad |
| 968 | // state of the tree). |
| 969 | post(new Runnable() { |
| 970 | public void run() { |
| 971 | ((Workspace) mParent).animateViewIntoPosition(child); |
| 972 | } |
| 973 | }); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 974 | } |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 975 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 976 | } |
| 977 | } |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 978 | prepareCacheBitmap(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | @Override |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 982 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 983 | super.onSizeChanged(w, h, oldw, oldh); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 984 | mBackgroundRect.set(0, 0, w, h); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 985 | updateGlowRect(); |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 986 | mCacheRect.set(0, 0, (int) (mBitmapCacheScale * w), (int) (mBitmapCacheScale * h)); |
| 987 | mCache = null; |
| 988 | prepareCacheBitmap(); |
| 989 | invalidateCache(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 993 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 994 | final int count = getChildCount(); |
| 995 | for (int i = 0; i < count; i++) { |
| 996 | final View view = getChildAt(i); |
| 997 | view.setDrawingCacheEnabled(enabled); |
| 998 | // Update the drawing caches |
Romain Guy | 3cf4c03 | 2010-10-26 14:29:35 -0700 | [diff] [blame] | 999 | if (!view.isHardwareAccelerated()) { |
| 1000 | view.buildDrawingCache(true); |
| 1001 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | @Override |
| 1006 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 1007 | super.setChildrenDrawnWithCacheEnabled(enabled); |
| 1008 | } |
| 1009 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1010 | public float getBackgroundAlpha() { |
| 1011 | return mBackgroundAlpha; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 1014 | public void setBackgroundAlphaMultiplier(float multiplier) { |
| 1015 | mBackgroundAlphaMultiplier = multiplier; |
| 1016 | } |
| 1017 | |
Adam Cohen | ddb8219 | 2010-11-10 16:32:54 -0800 | [diff] [blame] | 1018 | public float getBackgroundAlphaMultiplier() { |
| 1019 | return mBackgroundAlphaMultiplier; |
| 1020 | } |
| 1021 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1022 | public void setBackgroundAlpha(float alpha) { |
| 1023 | mBackgroundAlpha = alpha; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1024 | invalidate(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1027 | // Need to return true to let the view system know we know how to handle alpha-- this is |
| 1028 | // because when our children have an alpha of 0.0f, they are still rendering their "dimmed" |
| 1029 | // versions |
| 1030 | @Override |
| 1031 | protected boolean onSetAlpha(int alpha) { |
| 1032 | return true; |
| 1033 | } |
| 1034 | |
| 1035 | public void setAlpha(float alpha) { |
| 1036 | setChildrenAlpha(alpha); |
| 1037 | super.setAlpha(alpha); |
| 1038 | } |
| 1039 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1040 | private void setChildrenAlpha(float alpha) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1041 | final int childCount = getChildCount(); |
| 1042 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1043 | getChildAt(i).setAlpha(alpha); |
| 1044 | } |
| 1045 | } |
| 1046 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1047 | private boolean isVacantIgnoring( |
| 1048 | int originX, int originY, int spanX, int spanY, View ignoreView) { |
| 1049 | if (ignoreView != null) { |
| 1050 | markCellsAsUnoccupiedForView(ignoreView); |
| 1051 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1052 | boolean isVacant = true; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1053 | for (int i = 0; i < spanY; i++) { |
| 1054 | if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) { |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1055 | isVacant = false; |
| 1056 | break; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1057 | } |
| 1058 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1059 | if (ignoreView != null) { |
| 1060 | markCellsAsOccupiedForView(ignoreView); |
| 1061 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1062 | return isVacant; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1065 | private boolean isVacant(int originX, int originY, int spanX, int spanY) { |
| 1066 | return isVacantIgnoring(originX, originY, spanX, spanY, null); |
| 1067 | } |
| 1068 | |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 1069 | public View getChildAt(int x, int y) { |
| 1070 | final int count = getChildCount(); |
| 1071 | for (int i = 0; i < count; i++) { |
| 1072 | View child = getChildAt(i); |
| 1073 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1074 | |
| 1075 | if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) && |
| 1076 | (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) { |
| 1077 | return child; |
| 1078 | } |
| 1079 | } |
| 1080 | return null; |
| 1081 | } |
| 1082 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1083 | /** |
| 1084 | * Estimate where the top left cell of the dragged item will land if it is dropped. |
| 1085 | * |
| 1086 | * @param originX The X value of the top left corner of the item |
| 1087 | * @param originY The Y value of the top left corner of the item |
| 1088 | * @param spanX The number of horizontal cells that the item spans |
| 1089 | * @param spanY The number of vertical cells that the item spans |
| 1090 | * @param result The estimated drop cell X and Y. |
| 1091 | */ |
| 1092 | void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 1093 | final int countX = mCountX; |
| 1094 | final int countY = mCountY; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1095 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1096 | // pointToCellRounded takes the top left of a cell but will pad that with |
| 1097 | // cellWidth/2 and cellHeight/2 when finding the matching cell |
| 1098 | pointToCellRounded(originX, originY, result); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1099 | |
| 1100 | // If the item isn't fully on this screen, snap to the edges |
| 1101 | int rightOverhang = result[0] + spanX - countX; |
| 1102 | if (rightOverhang > 0) { |
| 1103 | result[0] -= rightOverhang; // Snap to right |
| 1104 | } |
| 1105 | result[0] = Math.max(0, result[0]); // Snap to left |
| 1106 | int bottomOverhang = result[1] + spanY - countY; |
| 1107 | if (bottomOverhang > 0) { |
| 1108 | result[1] -= bottomOverhang; // Snap to bottom |
| 1109 | } |
| 1110 | result[1] = Math.max(0, result[1]); // Snap to top |
| 1111 | } |
| 1112 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1113 | void visualizeDropLocation( |
| 1114 | View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) { |
| 1115 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1116 | final int oldDragCellX = mDragCell[0]; |
| 1117 | final int oldDragCellY = mDragCell[1]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1118 | final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell); |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1119 | if (v != null) { |
| 1120 | mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2)); |
| 1121 | } else { |
| 1122 | mDragCenter.set(originX, originY); |
| 1123 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1124 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1125 | if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) { |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1126 | // Find the top left corner of the rect the object will occupy |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1127 | final int[] topLeft = mTmpPoint; |
| 1128 | cellToPoint(nearest[0], nearest[1], topLeft); |
| 1129 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1130 | int left = topLeft[0]; |
| 1131 | int top = topLeft[1]; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1132 | |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1133 | if (v != null) { |
| 1134 | if (v.getParent() instanceof CellLayout) { |
| 1135 | LayoutParams lp = (LayoutParams) v.getLayoutParams(); |
| 1136 | left += lp.leftMargin; |
| 1137 | top += lp.topMargin; |
| 1138 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1139 | |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1140 | // Offsets due to the size difference between the View and the dragOutline |
| 1141 | left += (v.getWidth() - dragOutline.getWidth()) / 2; |
| 1142 | top += (v.getHeight() - dragOutline.getHeight()) / 2; |
| 1143 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1144 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1145 | final int oldIndex = mDragOutlineCurrent; |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1146 | mDragOutlineAnims[oldIndex].animateOut(); |
| 1147 | mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1148 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1149 | mDragOutlines[mDragOutlineCurrent].set(left, top); |
| 1150 | mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline); |
| 1151 | mDragOutlineAnims[mDragOutlineCurrent].animateIn(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1152 | } |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 1153 | |
| 1154 | // If we are drawing crosshairs, the entire CellLayout needs to be invalidated |
| 1155 | if (mCrosshairsDrawable != null) { |
| 1156 | invalidate(); |
| 1157 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1160 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1161 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1162 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1163 | * |
Romain Guy | 51afc02 | 2009-05-04 18:03:43 -0700 | [diff] [blame] | 1164 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1165 | * @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] | 1166 | * @param spanX Horizontal span of the object. |
| 1167 | * @param spanY Vertical span of the object. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1168 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1169 | * be allocated) |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1170 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1171 | * nearest the requested location. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1172 | */ |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1173 | int[] findNearestVacantArea( |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1174 | int pixelX, int pixelY, int spanX, int spanY, int[] result) { |
| 1175 | return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result); |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1176 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1177 | |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1178 | /** |
| 1179 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1180 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 1181 | * |
| 1182 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1183 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 1184 | * @param spanX Horizontal span of the object. |
| 1185 | * @param spanY Vertical span of the object. |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1186 | * @param ignoreView Considers space occupied by this view as unoccupied |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1187 | * @param result Previously returned value to possibly recycle. |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1188 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1189 | * nearest the requested location. |
| 1190 | */ |
| 1191 | int[] findNearestVacantArea( |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1192 | int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1193 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
| 1194 | markCellsAsUnoccupiedForView(ignoreView); |
| 1195 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1196 | // Keep track of best-scoring drop area |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1197 | final int[] bestXY = result != null ? result : new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1198 | double bestDistance = Double.MAX_VALUE; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1199 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1200 | final int countX = mCountX; |
| 1201 | final int countY = mCountY; |
| 1202 | final boolean[][] occupied = mOccupied; |
| 1203 | |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1204 | for (int y = 0; y < countY - (spanY - 1); y++) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1205 | inner: |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1206 | for (int x = 0; x < countX - (spanX - 1); x++) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1207 | for (int i = 0; i < spanX; i++) { |
| 1208 | for (int j = 0; j < spanY; j++) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1209 | if (occupied[x + i][y + j]) { |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1210 | // small optimization: we can skip to after the column we just found |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1211 | // an occupied cell |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1212 | x += i; |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1213 | continue inner; |
| 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | final int[] cellXY = mTmpCellXY; |
| 1218 | cellToPoint(x, y, cellXY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1219 | |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1220 | double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2) |
| 1221 | + Math.pow(cellXY[1] - pixelY, 2)); |
| 1222 | if (distance <= bestDistance) { |
| 1223 | bestDistance = distance; |
| 1224 | bestXY[0] = x; |
| 1225 | bestXY[1] = y; |
| 1226 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1227 | } |
| 1228 | } |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1229 | // re-mark space taken by ignoreView as occupied |
| 1230 | markCellsAsOccupiedForView(ignoreView); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1231 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1232 | // Return null if no suitable location found |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1233 | if (bestDistance < Double.MAX_VALUE) { |
| 1234 | return bestXY; |
| 1235 | } else { |
| 1236 | return null; |
| 1237 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1238 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1239 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1240 | boolean existsEmptyCell() { |
| 1241 | return findCellForSpan(null, 1, 1); |
| 1242 | } |
| 1243 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1244 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1245 | * Finds the upper-left coordinate of the first rectangle in the grid that can |
| 1246 | * hold a cell of the specified dimensions. If intersectX and intersectY are not -1, |
| 1247 | * then this method will only return coordinates for rectangles that contain the cell |
| 1248 | * (intersectX, intersectY) |
| 1249 | * |
| 1250 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 1251 | * can be found. |
| 1252 | * @param spanX The horizontal span of the cell we want to find. |
| 1253 | * @param spanY The vertical span of the cell we want to find. |
| 1254 | * |
| 1255 | * @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] | 1256 | */ |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1257 | boolean findCellForSpan(int[] cellXY, int spanX, int spanY) { |
| 1258 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null); |
| 1259 | } |
| 1260 | |
| 1261 | /** |
| 1262 | * Like above, but ignores any cells occupied by the item "ignoreView" |
| 1263 | * |
| 1264 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 1265 | * can be found. |
| 1266 | * @param spanX The horizontal span of the cell we want to find. |
| 1267 | * @param spanY The vertical span of the cell we want to find. |
| 1268 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 1269 | * @return |
| 1270 | */ |
| 1271 | boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) { |
| 1272 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView); |
| 1273 | } |
| 1274 | |
| 1275 | /** |
| 1276 | * Like above, but if intersectX and intersectY are not -1, then this method will try to |
| 1277 | * return coordinates for rectangles that contain the cell [intersectX, intersectY] |
| 1278 | * |
| 1279 | * @param spanX The horizontal span of the cell we want to find. |
| 1280 | * @param spanY The vertical span of the cell we want to find. |
| 1281 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 1282 | * @param intersectX The X coordinate of the cell that we should try to overlap |
| 1283 | * @param intersectX The Y coordinate of the cell that we should try to overlap |
| 1284 | * |
| 1285 | * @return True if a vacant cell of the specified dimension was found, false otherwise. |
| 1286 | */ |
| 1287 | boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY, |
| 1288 | int intersectX, int intersectY) { |
| 1289 | return findCellForSpanThatIntersectsIgnoring( |
| 1290 | cellXY, spanX, spanY, intersectX, intersectY, null); |
| 1291 | } |
| 1292 | |
| 1293 | /** |
| 1294 | * The superset of the above two methods |
| 1295 | */ |
| 1296 | boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY, |
| 1297 | int intersectX, int intersectY, View ignoreView) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1298 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
| 1299 | markCellsAsUnoccupiedForView(ignoreView); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1300 | |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1301 | boolean foundCell = false; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1302 | while (true) { |
| 1303 | int startX = 0; |
| 1304 | if (intersectX >= 0) { |
| 1305 | startX = Math.max(startX, intersectX - (spanX - 1)); |
| 1306 | } |
| 1307 | int endX = mCountX - (spanX - 1); |
| 1308 | if (intersectX >= 0) { |
| 1309 | endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0)); |
| 1310 | } |
| 1311 | int startY = 0; |
| 1312 | if (intersectY >= 0) { |
| 1313 | startY = Math.max(startY, intersectY - (spanY - 1)); |
| 1314 | } |
| 1315 | int endY = mCountY - (spanY - 1); |
| 1316 | if (intersectY >= 0) { |
| 1317 | endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0)); |
| 1318 | } |
| 1319 | |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1320 | for (int y = startY; y < endY && !foundCell; y++) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1321 | inner: |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1322 | for (int x = startX; x < endX; x++) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1323 | for (int i = 0; i < spanX; i++) { |
| 1324 | for (int j = 0; j < spanY; j++) { |
| 1325 | if (mOccupied[x + i][y + j]) { |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1326 | // small optimization: we can skip to after the column we just found |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1327 | // an occupied cell |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1328 | x += i; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1329 | continue inner; |
| 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | if (cellXY != null) { |
| 1334 | cellXY[0] = x; |
| 1335 | cellXY[1] = y; |
| 1336 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1337 | foundCell = true; |
| 1338 | break; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | if (intersectX == -1 && intersectY == -1) { |
| 1342 | break; |
| 1343 | } else { |
| 1344 | // if we failed to find anything, try again but without any requirements of |
| 1345 | // intersecting |
| 1346 | intersectX = -1; |
| 1347 | intersectY = -1; |
| 1348 | continue; |
| 1349 | } |
| 1350 | } |
| 1351 | |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1352 | // re-mark space taken by ignoreView as occupied |
| 1353 | markCellsAsOccupiedForView(ignoreView); |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1354 | return foundCell; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | /** |
| 1358 | * Called when drag has left this CellLayout or has been completed (successfully or not) |
| 1359 | */ |
| 1360 | void onDragExit() { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1361 | // This can actually be called when we aren't in a drag, e.g. when adding a new |
| 1362 | // item to this layout via the customize drawer. |
| 1363 | // Guard against that case. |
| 1364 | if (mDragging) { |
| 1365 | mDragging = false; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1366 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1367 | // Fade out the drag indicators |
| 1368 | if (mCrosshairsAnimator != null) { |
| 1369 | mCrosshairsAnimator.animateOut(); |
| 1370 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1371 | } |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1372 | |
| 1373 | // Invalidate the drag data |
| 1374 | mDragCell[0] = -1; |
| 1375 | mDragCell[1] = -1; |
| 1376 | mDragOutlineAnims[mDragOutlineCurrent].animateOut(); |
| 1377 | mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length; |
| 1378 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 1379 | setIsDragOverlapping(false); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1383 | * Mark a child as having been dropped. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1384 | * 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] | 1385 | * 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] | 1386 | * |
| 1387 | * @param child The child that is being dropped |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1388 | */ |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1389 | void onDropChild(View child, boolean animate) { |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1390 | if (child != null) { |
| 1391 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1392 | lp.isDragging = false; |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 1393 | lp.dropped = true; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1394 | lp.animateDrop = animate; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 1395 | child.setVisibility(View.VISIBLE); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1396 | child.requestLayout(); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1397 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1398 | } |
| 1399 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1400 | /** |
| 1401 | * Start dragging the specified child |
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 | * @param child The child that is being dragged |
| 1404 | */ |
| 1405 | void onDragChild(View child) { |
| 1406 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1407 | lp.isDragging = true; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | /** |
| 1411 | * A drag event has begun over this layout. |
| 1412 | * It may have begun over this layout (in which case onDragChild is called first), |
| 1413 | * or it may have begun on another layout. |
| 1414 | */ |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1415 | void onDragEnter() { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 1416 | if (!mDragging) { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 1417 | // Fade in the drag indicators |
| 1418 | if (mCrosshairsAnimator != null) { |
| 1419 | mCrosshairsAnimator.animateIn(); |
| 1420 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1421 | } |
| 1422 | mDragging = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1423 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1424 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1425 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1426 | * Computes a bounding rectangle for a range of cells |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1427 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1428 | * @param cellX X coordinate of upper left corner expressed as a cell position |
| 1429 | * @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] | 1430 | * @param cellHSpan Width in cells |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1431 | * @param cellVSpan Height in cells |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1432 | * @param resultRect Rect into which to put the results |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1433 | */ |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1434 | 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] | 1435 | final int cellWidth = mCellWidth; |
| 1436 | final int cellHeight = mCellHeight; |
| 1437 | final int widthGap = mWidthGap; |
| 1438 | final int heightGap = mHeightGap; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1439 | |
| 1440 | final int hStartPadding = getLeftPadding(); |
| 1441 | final int vStartPadding = getTopPadding(); |
| 1442 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1443 | int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap); |
| 1444 | int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap); |
| 1445 | |
| 1446 | int x = hStartPadding + cellX * (cellWidth + widthGap); |
| 1447 | int y = vStartPadding + cellY * (cellHeight + heightGap); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1448 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1449 | resultRect.set(x, y, x + width, y + height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1450 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1451 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1452 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1453 | * 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] | 1454 | * fit the given rectangle. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1455 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1456 | * @param width Width in pixels |
| 1457 | * @param height Height in pixels |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1458 | * @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] | 1459 | */ |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1460 | public int[] rectToCell(int width, int height, int[] result) { |
Michael Jurka | 9987a5c | 2010-10-08 16:58:12 -0700 | [diff] [blame] | 1461 | return rectToCell(getResources(), width, height, result); |
| 1462 | } |
| 1463 | |
| 1464 | 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] | 1465 | // Always assume we're working with the smallest span to make sure we |
| 1466 | // reserve enough space in both orientations. |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1467 | int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width); |
| 1468 | int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1469 | int smallerSize = Math.min(actualWidth, actualHeight); |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1470 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1471 | // Always round up to next largest cell |
| 1472 | int spanX = (width + smallerSize) / smallerSize; |
| 1473 | int spanY = (height + smallerSize) / smallerSize; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1474 | |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1475 | if (result == null) { |
| 1476 | return new int[] { spanX, spanY }; |
| 1477 | } |
| 1478 | result[0] = spanX; |
| 1479 | result[1] = spanY; |
| 1480 | return result; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | /** |
Patrick Dubroy | 047379a | 2010-12-19 22:02:04 -0800 | [diff] [blame] | 1484 | * Calculate the grid spans needed to fit given item |
| 1485 | */ |
| 1486 | public void calculateSpans(ItemInfo info) { |
| 1487 | final int minWidth; |
| 1488 | final int minHeight; |
| 1489 | |
| 1490 | if (info instanceof LauncherAppWidgetInfo) { |
| 1491 | minWidth = ((LauncherAppWidgetInfo) info).minWidth; |
| 1492 | minHeight = ((LauncherAppWidgetInfo) info).minHeight; |
| 1493 | } else if (info instanceof PendingAddWidgetInfo) { |
| 1494 | minWidth = ((PendingAddWidgetInfo) info).minWidth; |
| 1495 | minHeight = ((PendingAddWidgetInfo) info).minHeight; |
| 1496 | } else { |
| 1497 | // It's not a widget, so it must be 1x1 |
| 1498 | info.spanX = info.spanY = 1; |
| 1499 | return; |
| 1500 | } |
| 1501 | int[] spans = rectToCell(minWidth, minHeight, null); |
| 1502 | info.spanX = spans[0]; |
| 1503 | info.spanY = spans[1]; |
| 1504 | } |
| 1505 | |
| 1506 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1507 | * Find the first vacant cell, if there is one. |
| 1508 | * |
| 1509 | * @param vacant Holds the x and y coordinate of the vacant cell |
| 1510 | * @param spanX Horizontal cell span. |
| 1511 | * @param spanY Vertical cell span. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1512 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1513 | * @return True if a vacant cell was found |
| 1514 | */ |
| 1515 | public boolean getVacantCell(int[] vacant, int spanX, int spanY) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1516 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1517 | return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | static boolean findVacantCell(int[] vacant, int spanX, int spanY, |
| 1521 | int xCount, int yCount, boolean[][] occupied) { |
| 1522 | |
| 1523 | for (int x = 0; x < xCount; x++) { |
| 1524 | for (int y = 0; y < yCount; y++) { |
| 1525 | boolean available = !occupied[x][y]; |
| 1526 | out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) { |
| 1527 | for (int j = y; j < y + spanY - 1 && y < yCount; j++) { |
| 1528 | available = available && !occupied[i][j]; |
| 1529 | if (!available) break out; |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | if (available) { |
| 1534 | vacant[0] = x; |
| 1535 | vacant[1] = y; |
| 1536 | return true; |
| 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | return false; |
| 1542 | } |
| 1543 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1544 | private void clearOccupiedCells() { |
| 1545 | for (int x = 0; x < mCountX; x++) { |
| 1546 | for (int y = 0; y < mCountY; y++) { |
| 1547 | mOccupied[x][y] = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1548 | } |
| 1549 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1550 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1551 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1552 | public void onMove(View view, int newCellX, int newCellY) { |
| 1553 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1554 | markCellsAsUnoccupiedForView(view); |
| 1555 | markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true); |
| 1556 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1557 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1558 | private void markCellsAsOccupiedForView(View view) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1559 | if (view == null || view.getParent() != this) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1560 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1561 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true); |
| 1562 | } |
| 1563 | |
| 1564 | private void markCellsAsUnoccupiedForView(View view) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1565 | if (view == null || view.getParent() != this) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1566 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1567 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false); |
| 1568 | } |
| 1569 | |
| 1570 | private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) { |
| 1571 | for (int x = cellX; x < cellX + spanX && x < mCountX; x++) { |
| 1572 | for (int y = cellY; y < cellY + spanY && y < mCountY; y++) { |
| 1573 | mOccupied[x][y] = value; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1574 | } |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | @Override |
| 1579 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 1580 | return new CellLayout.LayoutParams(getContext(), attrs); |
| 1581 | } |
| 1582 | |
| 1583 | @Override |
| 1584 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 1585 | return p instanceof CellLayout.LayoutParams; |
| 1586 | } |
| 1587 | |
| 1588 | @Override |
| 1589 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 1590 | return new CellLayout.LayoutParams(p); |
| 1591 | } |
| 1592 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1593 | public static class CellLayoutAnimationController extends LayoutAnimationController { |
| 1594 | public CellLayoutAnimationController(Animation animation, float delay) { |
| 1595 | super(animation, delay); |
| 1596 | } |
| 1597 | |
| 1598 | @Override |
| 1599 | protected long getDelayForView(View view) { |
| 1600 | return (int) (Math.random() * 150); |
| 1601 | } |
| 1602 | } |
| 1603 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1604 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
| 1605 | /** |
| 1606 | * Horizontal location of the item in the grid. |
| 1607 | */ |
| 1608 | @ViewDebug.ExportedProperty |
| 1609 | public int cellX; |
| 1610 | |
| 1611 | /** |
| 1612 | * Vertical location of the item in the grid. |
| 1613 | */ |
| 1614 | @ViewDebug.ExportedProperty |
| 1615 | public int cellY; |
| 1616 | |
| 1617 | /** |
| 1618 | * Number of cells spanned horizontally by the item. |
| 1619 | */ |
| 1620 | @ViewDebug.ExportedProperty |
| 1621 | public int cellHSpan; |
| 1622 | |
| 1623 | /** |
| 1624 | * Number of cells spanned vertically by the item. |
| 1625 | */ |
| 1626 | @ViewDebug.ExportedProperty |
| 1627 | public int cellVSpan; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1628 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1629 | /** |
| 1630 | * Is this item currently being dragged |
| 1631 | */ |
| 1632 | public boolean isDragging; |
| 1633 | |
| 1634 | // X coordinate of the view in the layout. |
| 1635 | @ViewDebug.ExportedProperty |
| 1636 | int x; |
| 1637 | // Y coordinate of the view in the layout. |
| 1638 | @ViewDebug.ExportedProperty |
| 1639 | int y; |
| 1640 | |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 1641 | /** |
| 1642 | * The old X coordinate of this item, relative to its current parent. |
| 1643 | * Used to animate the item into its new position. |
| 1644 | */ |
| 1645 | int oldX; |
| 1646 | |
| 1647 | /** |
| 1648 | * The old Y coordinate of this item, relative to its current parent. |
| 1649 | * Used to animate the item into its new position. |
| 1650 | */ |
| 1651 | int oldY; |
| 1652 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 1653 | boolean dropped; |
Romain Guy | fcb9e71 | 2009-10-02 16:06:52 -0700 | [diff] [blame] | 1654 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1655 | boolean animateDrop; |
| 1656 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1657 | public LayoutParams(Context c, AttributeSet attrs) { |
| 1658 | super(c, attrs); |
| 1659 | cellHSpan = 1; |
| 1660 | cellVSpan = 1; |
| 1661 | } |
| 1662 | |
| 1663 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 1664 | super(source); |
| 1665 | cellHSpan = 1; |
| 1666 | cellVSpan = 1; |
| 1667 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1668 | |
| 1669 | public LayoutParams(LayoutParams source) { |
| 1670 | super(source); |
| 1671 | this.cellX = source.cellX; |
| 1672 | this.cellY = source.cellY; |
| 1673 | this.cellHSpan = source.cellHSpan; |
| 1674 | this.cellVSpan = source.cellVSpan; |
| 1675 | } |
| 1676 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1677 | public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) { |
Romain Guy | 8f19cdd | 2010-01-08 15:07:00 -0800 | [diff] [blame] | 1678 | super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1679 | this.cellX = cellX; |
| 1680 | this.cellY = cellY; |
| 1681 | this.cellHSpan = cellHSpan; |
| 1682 | this.cellVSpan = cellVSpan; |
| 1683 | } |
| 1684 | |
| 1685 | public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap, |
| 1686 | int hStartPadding, int vStartPadding) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1687 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1688 | final int myCellHSpan = cellHSpan; |
| 1689 | final int myCellVSpan = cellVSpan; |
| 1690 | final int myCellX = cellX; |
| 1691 | final int myCellY = cellY; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1692 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1693 | width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) - |
| 1694 | leftMargin - rightMargin; |
| 1695 | height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) - |
| 1696 | topMargin - bottomMargin; |
| 1697 | |
| 1698 | x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin; |
| 1699 | y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin; |
| 1700 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1701 | |
| 1702 | public String toString() { |
| 1703 | return "(" + this.cellX + ", " + this.cellY + ")"; |
| 1704 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1705 | } |
| 1706 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1707 | // This class stores info for two purposes: |
| 1708 | // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY, |
| 1709 | // its spanX, spanY, and the screen it is on |
| 1710 | // 2. When long clicking on an empty cell in a CellLayout, we save information about the |
| 1711 | // cellX and cellY coordinates and which page was clicked. We then set this as a tag on |
| 1712 | // the CellLayout that was long clicked |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1713 | static final class CellInfo implements ContextMenu.ContextMenuInfo { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1714 | View cell; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1715 | int cellX = -1; |
| 1716 | int cellY = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1717 | int spanX; |
| 1718 | int spanY; |
| 1719 | int screen; |
| 1720 | boolean valid; |
| 1721 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1722 | @Override |
| 1723 | public String toString() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1724 | return "Cell[view=" + (cell == null ? "null" : cell.getClass()) |
| 1725 | + ", x=" + cellX + ", y=" + cellY + "]"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1726 | } |
| 1727 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1728 | } |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame^] | 1729 | |
| 1730 | // Custom interfaces used to listen to "visibility changed" events of *children* of Views. Avoided |
| 1731 | // using "onVisibilityChanged" in the names because there's a method of that name in framework |
| 1732 | // (which can only can be used to listen to ancestors' "visibility changed" events) |
| 1733 | interface VisibilityChangedBroadcaster { |
| 1734 | public void setVisibilityChangedListener(VisibilityChangedListener listener); |
| 1735 | } |
| 1736 | |
| 1737 | interface VisibilityChangedListener { |
| 1738 | public void receiveVisibilityChangedMessage(View v); |
| 1739 | } |