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