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