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