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