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