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