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