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