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