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