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