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) { |
| 459 | final LayoutParams lp = params; |
| 460 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 461 | // Generate an id for each view, this assumes we have at most 256x256 cells |
| 462 | // per workspace screen |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 463 | 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] | 464 | // If the horizontal or vertical span is set to -1, it is taken to |
| 465 | // mean that it spans the extent of the CellLayout |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 466 | if (lp.cellHSpan < 0) lp.cellHSpan = mCountX; |
| 467 | if (lp.cellVSpan < 0) lp.cellVSpan = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 468 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 469 | child.setId(childId); |
| 470 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 471 | // We might be in the middle or end of shrinking/fading to a dimmed view |
| 472 | // 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] | 473 | child.setAlpha(getAlpha()); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 474 | addView(child, index, lp); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 475 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 476 | markCellsAsOccupiedForView(child); |
| 477 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 478 | return true; |
| 479 | } |
| 480 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 481 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 482 | public void setAcceptsDrops(boolean acceptsDrops) { |
| 483 | if (mAcceptsDrops != acceptsDrops) { |
| 484 | mAcceptsDrops = acceptsDrops; |
| 485 | invalidate(); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | public boolean getAcceptsDrops() { |
| 490 | return mAcceptsDrops; |
| 491 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 492 | |
| 493 | @Override |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 494 | public void removeAllViews() { |
| 495 | clearOccupiedCells(); |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public void removeAllViewsInLayout() { |
| 500 | clearOccupiedCells(); |
| 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public void removeView(View view) { |
| 505 | markCellsAsUnoccupiedForView(view); |
| 506 | super.removeView(view); |
| 507 | } |
| 508 | |
| 509 | @Override |
| 510 | public void removeViewAt(int index) { |
| 511 | markCellsAsUnoccupiedForView(getChildAt(index)); |
| 512 | super.removeViewAt(index); |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public void removeViewInLayout(View view) { |
| 517 | markCellsAsUnoccupiedForView(view); |
| 518 | super.removeViewInLayout(view); |
| 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public void removeViews(int start, int count) { |
| 523 | for (int i = start; i < start + count; i++) { |
| 524 | markCellsAsUnoccupiedForView(getChildAt(i)); |
| 525 | } |
| 526 | super.removeViews(start, count); |
| 527 | } |
| 528 | |
| 529 | @Override |
| 530 | public void removeViewsInLayout(int start, int count) { |
| 531 | for (int i = start; i < start + count; i++) { |
| 532 | markCellsAsUnoccupiedForView(getChildAt(i)); |
| 533 | } |
| 534 | super.removeViewsInLayout(start, count); |
| 535 | } |
| 536 | |
| 537 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 538 | public void requestChildFocus(View child, View focused) { |
| 539 | super.requestChildFocus(child, focused); |
| 540 | if (child != null) { |
| 541 | Rect r = new Rect(); |
| 542 | child.getDrawingRect(r); |
| 543 | requestRectangleOnScreen(r); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | @Override |
| 548 | protected void onAttachedToWindow() { |
| 549 | super.onAttachedToWindow(); |
| 550 | mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this); |
| 551 | } |
| 552 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 553 | public void setTagToCellInfoForPoint(int touchX, int touchY) { |
| 554 | final CellInfo cellInfo = mCellInfo; |
| 555 | final Rect frame = mRect; |
| 556 | final int x = touchX + mScrollX; |
| 557 | final int y = touchY + mScrollY; |
| 558 | final int count = getChildCount(); |
| 559 | |
| 560 | boolean found = false; |
| 561 | for (int i = count - 1; i >= 0; i--) { |
| 562 | final View child = getChildAt(i); |
| 563 | |
| 564 | if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) { |
| 565 | child.getHitRect(frame); |
| 566 | if (frame.contains(x, y)) { |
| 567 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 568 | cellInfo.cell = child; |
| 569 | cellInfo.cellX = lp.cellX; |
| 570 | cellInfo.cellY = lp.cellY; |
| 571 | cellInfo.spanX = lp.cellHSpan; |
| 572 | cellInfo.spanY = lp.cellVSpan; |
| 573 | cellInfo.valid = true; |
| 574 | found = true; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 575 | break; |
| 576 | } |
| 577 | } |
| 578 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 579 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 580 | if (!found) { |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 581 | final int cellXY[] = mTmpCellXY; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 582 | pointToCellExact(x, y, cellXY); |
| 583 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 584 | cellInfo.cell = null; |
| 585 | cellInfo.cellX = cellXY[0]; |
| 586 | cellInfo.cellY = cellXY[1]; |
| 587 | cellInfo.spanX = 1; |
| 588 | cellInfo.spanY = 1; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 589 | cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX && |
| 590 | cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]]; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 591 | } |
| 592 | setTag(cellInfo); |
| 593 | } |
| 594 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 595 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 596 | @Override |
| 597 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 598 | if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) { |
| 599 | return true; |
| 600 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 601 | final int action = ev.getAction(); |
| 602 | final CellInfo cellInfo = mCellInfo; |
| 603 | |
| 604 | if (action == MotionEvent.ACTION_DOWN) { |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 605 | setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 606 | } else if (action == MotionEvent.ACTION_UP) { |
| 607 | cellInfo.cell = null; |
| 608 | cellInfo.cellX = -1; |
| 609 | cellInfo.cellY = -1; |
| 610 | cellInfo.spanX = 0; |
| 611 | cellInfo.spanY = 0; |
| 612 | cellInfo.valid = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 613 | setTag(cellInfo); |
| 614 | } |
| 615 | |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | @Override |
| 620 | public CellInfo getTag() { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 621 | return (CellInfo) super.getTag(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 624 | /** |
| 625 | * Check if the row 'y' is empty from columns 'left' to 'right', inclusive. |
| 626 | */ |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 627 | private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) { |
| 628 | for (int x = left; x <= right; x++) { |
| 629 | if (occupied[x][y]) { |
| 630 | return false; |
| 631 | } |
| 632 | } |
| 633 | return true; |
| 634 | } |
| 635 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 636 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 637 | * 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] | 638 | * @param x X coordinate of the point |
| 639 | * @param y Y coordinate of the point |
| 640 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 641 | */ |
| 642 | void pointToCellExact(int x, int y, int[] result) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 643 | final int hStartPadding = getLeftPadding(); |
| 644 | final int vStartPadding = getTopPadding(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 645 | |
| 646 | result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap); |
| 647 | result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap); |
| 648 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 649 | final int xAxis = mCountX; |
| 650 | final int yAxis = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 651 | |
| 652 | if (result[0] < 0) result[0] = 0; |
| 653 | if (result[0] >= xAxis) result[0] = xAxis - 1; |
| 654 | if (result[1] < 0) result[1] = 0; |
| 655 | if (result[1] >= yAxis) result[1] = yAxis - 1; |
| 656 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 657 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 658 | /** |
| 659 | * Given a point, return the cell that most closely encloses that point |
| 660 | * @param x X coordinate of the point |
| 661 | * @param y Y coordinate of the point |
| 662 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 663 | */ |
| 664 | void pointToCellRounded(int x, int y, int[] result) { |
| 665 | pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result); |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * 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] | 670 | * |
| 671 | * @param cellX X coordinate of the cell |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 672 | * @param cellY Y coordinate of the cell |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 673 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 674 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 675 | */ |
| 676 | void cellToPoint(int cellX, int cellY, int[] result) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 677 | final int hStartPadding = getLeftPadding(); |
| 678 | final int vStartPadding = getTopPadding(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 679 | |
| 680 | result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap); |
| 681 | result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap); |
| 682 | } |
| 683 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 684 | int getCellWidth() { |
| 685 | return mCellWidth; |
| 686 | } |
| 687 | |
| 688 | int getCellHeight() { |
| 689 | return mCellHeight; |
| 690 | } |
| 691 | |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 692 | int getLeftPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 693 | return mLeftPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | int getTopPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 697 | return mTopPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | int getRightPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 701 | return mRightPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | int getBottomPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 705 | return mBottomPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 706 | } |
| 707 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 708 | @Override |
| 709 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 710 | // TODO: currently ignoring padding |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 711 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 712 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 713 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 714 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 715 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 716 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 717 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 718 | if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { |
| 719 | throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); |
| 720 | } |
| 721 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 722 | final int cellWidth = mCellWidth; |
| 723 | final int cellHeight = mCellHeight; |
| 724 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 725 | int numWidthGaps = mCountX - 1; |
| 726 | int numHeightGaps = mCountY - 1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 727 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 728 | int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY); |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 729 | mHeightGap = vSpaceLeft / numHeightGaps; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 730 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 731 | int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX); |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 732 | mWidthGap = hSpaceLeft / numWidthGaps; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 733 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 734 | // center it around the min gaps |
| 735 | int minGap = Math.min(mWidthGap, mHeightGap); |
| 736 | mWidthGap = mHeightGap = minGap; |
| 737 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 738 | int count = getChildCount(); |
| 739 | |
| 740 | for (int i = 0; i < count; i++) { |
| 741 | View child = getChildAt(i); |
| 742 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 743 | lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, |
| 744 | mLeftPadding, mTopPadding); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 745 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 746 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 747 | int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, |
| 748 | MeasureSpec.EXACTLY); |
| 749 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 750 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 751 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 752 | if (widthSpecMode == MeasureSpec.AT_MOST) { |
| 753 | int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) + |
| 754 | ((mCountX - 1) * minGap); |
| 755 | int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) + |
| 756 | ((mCountY - 1) * minGap); |
| 757 | setMeasuredDimension(newWidth, newHeight); |
| 758 | } else if (widthSpecMode == MeasureSpec.EXACTLY) { |
| 759 | setMeasuredDimension(widthSpecSize, heightSpecSize); |
| 760 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 763 | /** |
| 764 | * Animate a child of this CellLayout into its current layout position. |
| 765 | * The position to animate from is given by the oldX and oldY values in its LayoutParams. |
| 766 | */ |
| 767 | private void animateChildIntoPosition(final View child) { |
| 768 | final Resources res = getResources(); |
| 769 | final ObjectAnimator anim = mDropAnim; |
| 770 | final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 771 | final float startX = lp.oldX - lp.x; |
| 772 | final float startY = lp.oldY - lp.y; |
| 773 | |
| 774 | // Calculate the duration of the animation based on the object's distance |
| 775 | final float dist = (float) Math.sqrt(startX*startX + startY*startY); |
| 776 | final float maxDist = (float) res.getInteger(R.integer.config_dropAnimMaxDist); |
| 777 | final int duration = (int) (res.getInteger(R.integer.config_dropAnimMaxDuration) |
| 778 | * mEaseOutInterpolator.getInterpolation(dist / maxDist)); |
| 779 | |
| 780 | anim.cancel(); // Make sure it's not already running |
| 781 | anim.setDuration(duration); |
| 782 | anim.setTarget(child); |
| 783 | anim.setPropertyName("translationX"); |
| 784 | anim.setFloatValues(startX, 0); |
| 785 | |
| 786 | anim.removeAllUpdateListeners(); |
| 787 | anim.addUpdateListener(new AnimatorUpdateListener() { |
| 788 | public void onAnimationUpdate(ValueAnimator animation) { |
| 789 | // Set the value of translationY based on the current x value |
| 790 | final float translationX = (Float) anim.getAnimatedValue(); |
| 791 | child.setTranslationY((startY / startX) * translationX); |
| 792 | } |
| 793 | }); |
| 794 | anim.start(); |
| 795 | } |
| 796 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 797 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 798 | 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] | 799 | int count = getChildCount(); |
| 800 | |
| 801 | for (int i = 0; i < count; i++) { |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 802 | final View child = getChildAt(i); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 803 | if (child.getVisibility() != GONE) { |
| 804 | |
| 805 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 806 | |
| 807 | int childLeft = lp.x; |
| 808 | int childTop = lp.y; |
| 809 | child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 810 | |
| 811 | if (lp.dropped) { |
| 812 | lp.dropped = false; |
| 813 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 814 | final int[] cellXY = mTmpCellXY; |
Romain Guy | 06762ab | 2010-01-25 16:51:08 -0800 | [diff] [blame] | 815 | getLocationOnScreen(cellXY); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 816 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop", |
Romain Guy | 06762ab | 2010-01-25 16:51:08 -0800 | [diff] [blame] | 817 | cellXY[0] + childLeft + lp.width / 2, |
| 818 | cellXY[1] + childTop + lp.height / 2, 0, null); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 819 | |
| 820 | animateChildIntoPosition(child); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 821 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | @Override |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 827 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 828 | super.onSizeChanged(w, h, oldw, oldh); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 829 | mBackgroundRect.set(0, 0, w, h); |
| 830 | updateHoverRect(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 834 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 835 | final int count = getChildCount(); |
| 836 | for (int i = 0; i < count; i++) { |
| 837 | final View view = getChildAt(i); |
| 838 | view.setDrawingCacheEnabled(enabled); |
| 839 | // Update the drawing caches |
Adam Powell | fefa0ce | 2010-05-03 10:23:50 -0700 | [diff] [blame] | 840 | view.buildDrawingCache(true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
| 844 | @Override |
| 845 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 846 | super.setChildrenDrawnWithCacheEnabled(enabled); |
| 847 | } |
| 848 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 849 | public float getBackgroundAlpha() { |
| 850 | return mBackgroundAlpha; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 853 | public void setBackgroundAlpha(float alpha) { |
| 854 | mBackgroundAlpha = alpha; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 855 | invalidate(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 858 | // Need to return true to let the view system know we know how to handle alpha-- this is |
| 859 | // because when our children have an alpha of 0.0f, they are still rendering their "dimmed" |
| 860 | // versions |
| 861 | @Override |
| 862 | protected boolean onSetAlpha(int alpha) { |
| 863 | return true; |
| 864 | } |
| 865 | |
| 866 | public void setAlpha(float alpha) { |
| 867 | setChildrenAlpha(alpha); |
| 868 | super.setAlpha(alpha); |
| 869 | } |
| 870 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 871 | private void setChildrenAlpha(float alpha) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 872 | final int childCount = getChildCount(); |
| 873 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 874 | getChildAt(i).setAlpha(alpha); |
| 875 | } |
| 876 | } |
| 877 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 878 | private boolean isVacantIgnoring( |
| 879 | int originX, int originY, int spanX, int spanY, View ignoreView) { |
| 880 | if (ignoreView != null) { |
| 881 | markCellsAsUnoccupiedForView(ignoreView); |
| 882 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 883 | boolean isVacant = true; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 884 | for (int i = 0; i < spanY; i++) { |
| 885 | if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) { |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 886 | isVacant = false; |
| 887 | break; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 888 | } |
| 889 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 890 | if (ignoreView != null) { |
| 891 | markCellsAsOccupiedForView(ignoreView); |
| 892 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 893 | return isVacant; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 896 | private boolean isVacant(int originX, int originY, int spanX, int spanY) { |
| 897 | return isVacantIgnoring(originX, originY, spanX, spanY, null); |
| 898 | } |
| 899 | |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 900 | public View getChildAt(int x, int y) { |
| 901 | final int count = getChildCount(); |
| 902 | for (int i = 0; i < count; i++) { |
| 903 | View child = getChildAt(i); |
| 904 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 905 | |
| 906 | if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) && |
| 907 | (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) { |
| 908 | return child; |
| 909 | } |
| 910 | } |
| 911 | return null; |
| 912 | } |
| 913 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 914 | /** |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 915 | * Estimate the size that a child with the given dimensions will take in the layout. |
| 916 | */ |
| 917 | void estimateChildSize(int minWidth, int minHeight, int[] result) { |
| 918 | // Assuming it's placed at 0, 0, find where the bottom right cell will land |
| 919 | rectToCell(minWidth, minHeight, result); |
| 920 | |
| 921 | // Then figure out the rect it will occupy |
| 922 | cellToRect(0, 0, result[0], result[1], mRectF); |
| 923 | result[0] = (int)mRectF.width(); |
| 924 | result[1] = (int)mRectF.height(); |
| 925 | } |
| 926 | |
| 927 | /** |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 928 | * Estimate where the top left cell of the dragged item will land if it is dropped. |
| 929 | * |
| 930 | * @param originX The X value of the top left corner of the item |
| 931 | * @param originY The Y value of the top left corner of the item |
| 932 | * @param spanX The number of horizontal cells that the item spans |
| 933 | * @param spanY The number of vertical cells that the item spans |
| 934 | * @param result The estimated drop cell X and Y. |
| 935 | */ |
| 936 | void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 937 | final int countX = mCountX; |
| 938 | final int countY = mCountY; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 939 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 940 | // pointToCellRounded takes the top left of a cell but will pad that with |
| 941 | // cellWidth/2 and cellHeight/2 when finding the matching cell |
| 942 | pointToCellRounded(originX, originY, result); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 943 | |
| 944 | // If the item isn't fully on this screen, snap to the edges |
| 945 | int rightOverhang = result[0] + spanX - countX; |
| 946 | if (rightOverhang > 0) { |
| 947 | result[0] -= rightOverhang; // Snap to right |
| 948 | } |
| 949 | result[0] = Math.max(0, result[0]); // Snap to left |
| 950 | int bottomOverhang = result[1] + spanY - countY; |
| 951 | if (bottomOverhang > 0) { |
| 952 | result[1] -= bottomOverhang; // Snap to bottom |
| 953 | } |
| 954 | result[1] = Math.max(0, result[1]); // Snap to top |
| 955 | } |
| 956 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 957 | void visualizeDropLocation( |
| 958 | View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) { |
| 959 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 960 | final int oldDragCellX = mDragCell[0]; |
| 961 | final int oldDragCellY = mDragCell[1]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 962 | final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell); |
| 963 | mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2)); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 964 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 965 | if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) { |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 966 | // Find the top left corner of the rect the object will occupy |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 967 | final int[] topLeft = mTmpPoint; |
| 968 | cellToPoint(nearest[0], nearest[1], topLeft); |
| 969 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 970 | int left = topLeft[0]; |
| 971 | int top = topLeft[1]; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 972 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 973 | if (v.getParent() instanceof CellLayout) { |
| 974 | LayoutParams lp = (LayoutParams) v.getLayoutParams(); |
| 975 | left += lp.leftMargin; |
| 976 | top += lp.topMargin; |
| 977 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 978 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 979 | // Offsets due to the size difference between the View and the dragOutline |
| 980 | left += (v.getWidth() - dragOutline.getWidth()) / 2; |
| 981 | top += (v.getHeight() - dragOutline.getHeight()) / 2; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 982 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 983 | final int oldIndex = mDragOutlineCurrent; |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 984 | mDragOutlineAnims[oldIndex].animateOut(); |
| 985 | mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 986 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 987 | mDragOutlines[mDragOutlineCurrent].set(left, top); |
| 988 | mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline); |
| 989 | mDragOutlineAnims[mDragOutlineCurrent].animateIn(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 990 | } |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 991 | |
| 992 | // If we are drawing crosshairs, the entire CellLayout needs to be invalidated |
| 993 | if (mCrosshairsDrawable != null) { |
| 994 | invalidate(); |
| 995 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 996 | } |
| 997 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 998 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 999 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1000 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1001 | * |
Romain Guy | 51afc02 | 2009-05-04 18:03:43 -0700 | [diff] [blame] | 1002 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1003 | * @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] | 1004 | * @param spanX Horizontal span of the object. |
| 1005 | * @param spanY Vertical span of the object. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1006 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1007 | * be allocated) |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1008 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1009 | * nearest the requested location. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1010 | */ |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1011 | int[] findNearestVacantArea( |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1012 | int pixelX, int pixelY, int spanX, int spanY, int[] result) { |
| 1013 | return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result); |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1014 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1015 | |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1016 | /** |
| 1017 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1018 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 1019 | * |
| 1020 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1021 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 1022 | * @param spanX Horizontal span of the object. |
| 1023 | * @param spanY Vertical span of the object. |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1024 | * @param ignoreView Considers space occupied by this view as unoccupied |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1025 | * @param result Previously returned value to possibly recycle. |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1026 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1027 | * nearest the requested location. |
| 1028 | */ |
| 1029 | int[] findNearestVacantArea( |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1030 | int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1031 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
| 1032 | markCellsAsUnoccupiedForView(ignoreView); |
| 1033 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1034 | // Keep track of best-scoring drop area |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1035 | final int[] bestXY = result != null ? result : new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1036 | double bestDistance = Double.MAX_VALUE; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1037 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1038 | final int countX = mCountX; |
| 1039 | final int countY = mCountY; |
| 1040 | final boolean[][] occupied = mOccupied; |
| 1041 | |
| 1042 | for (int x = 0; x < countX - (spanX - 1); x++) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1043 | inner: |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1044 | for (int y = 0; y < countY - (spanY - 1); y++) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1045 | for (int i = 0; i < spanX; i++) { |
| 1046 | for (int j = 0; j < spanY; j++) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1047 | if (occupied[x + i][y + j]) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1048 | // small optimization: we can skip to below the row we just found |
| 1049 | // an occupied cell |
| 1050 | y += j; |
| 1051 | continue inner; |
| 1052 | } |
| 1053 | } |
| 1054 | } |
| 1055 | final int[] cellXY = mTmpCellXY; |
| 1056 | cellToPoint(x, y, cellXY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1057 | |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1058 | double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2) |
| 1059 | + Math.pow(cellXY[1] - pixelY, 2)); |
| 1060 | if (distance <= bestDistance) { |
| 1061 | bestDistance = distance; |
| 1062 | bestXY[0] = x; |
| 1063 | bestXY[1] = y; |
| 1064 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1065 | } |
| 1066 | } |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1067 | // re-mark space taken by ignoreView as occupied |
| 1068 | markCellsAsOccupiedForView(ignoreView); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1069 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1070 | // Return null if no suitable location found |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1071 | if (bestDistance < Double.MAX_VALUE) { |
| 1072 | return bestXY; |
| 1073 | } else { |
| 1074 | return null; |
| 1075 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1076 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1077 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1078 | boolean existsEmptyCell() { |
| 1079 | return findCellForSpan(null, 1, 1); |
| 1080 | } |
| 1081 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1082 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1083 | * Finds the upper-left coordinate of the first rectangle in the grid that can |
| 1084 | * hold a cell of the specified dimensions. If intersectX and intersectY are not -1, |
| 1085 | * then this method will only return coordinates for rectangles that contain the cell |
| 1086 | * (intersectX, intersectY) |
| 1087 | * |
| 1088 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 1089 | * can be found. |
| 1090 | * @param spanX The horizontal span of the cell we want to find. |
| 1091 | * @param spanY The vertical span of the cell we want to find. |
| 1092 | * |
| 1093 | * @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] | 1094 | */ |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1095 | boolean findCellForSpan(int[] cellXY, int spanX, int spanY) { |
| 1096 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null); |
| 1097 | } |
| 1098 | |
| 1099 | /** |
| 1100 | * Like above, but ignores any cells occupied by the item "ignoreView" |
| 1101 | * |
| 1102 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 1103 | * can be found. |
| 1104 | * @param spanX The horizontal span of the cell we want to find. |
| 1105 | * @param spanY The vertical span of the cell we want to find. |
| 1106 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 1107 | * @return |
| 1108 | */ |
| 1109 | boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) { |
| 1110 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView); |
| 1111 | } |
| 1112 | |
| 1113 | /** |
| 1114 | * Like above, but if intersectX and intersectY are not -1, then this method will try to |
| 1115 | * return coordinates for rectangles that contain the cell [intersectX, intersectY] |
| 1116 | * |
| 1117 | * @param spanX The horizontal span of the cell we want to find. |
| 1118 | * @param spanY The vertical span of the cell we want to find. |
| 1119 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 1120 | * @param intersectX The X coordinate of the cell that we should try to overlap |
| 1121 | * @param intersectX The Y coordinate of the cell that we should try to overlap |
| 1122 | * |
| 1123 | * @return True if a vacant cell of the specified dimension was found, false otherwise. |
| 1124 | */ |
| 1125 | boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY, |
| 1126 | int intersectX, int intersectY) { |
| 1127 | return findCellForSpanThatIntersectsIgnoring( |
| 1128 | cellXY, spanX, spanY, intersectX, intersectY, null); |
| 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * The superset of the above two methods |
| 1133 | */ |
| 1134 | boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY, |
| 1135 | int intersectX, int intersectY, View ignoreView) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1136 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
| 1137 | markCellsAsUnoccupiedForView(ignoreView); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1138 | |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1139 | boolean foundCell = false; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1140 | while (true) { |
| 1141 | int startX = 0; |
| 1142 | if (intersectX >= 0) { |
| 1143 | startX = Math.max(startX, intersectX - (spanX - 1)); |
| 1144 | } |
| 1145 | int endX = mCountX - (spanX - 1); |
| 1146 | if (intersectX >= 0) { |
| 1147 | endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0)); |
| 1148 | } |
| 1149 | int startY = 0; |
| 1150 | if (intersectY >= 0) { |
| 1151 | startY = Math.max(startY, intersectY - (spanY - 1)); |
| 1152 | } |
| 1153 | int endY = mCountY - (spanY - 1); |
| 1154 | if (intersectY >= 0) { |
| 1155 | endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0)); |
| 1156 | } |
| 1157 | |
| 1158 | for (int x = startX; x < endX; x++) { |
| 1159 | inner: |
| 1160 | for (int y = startY; y < endY; y++) { |
| 1161 | for (int i = 0; i < spanX; i++) { |
| 1162 | for (int j = 0; j < spanY; j++) { |
| 1163 | if (mOccupied[x + i][y + j]) { |
| 1164 | // small optimization: we can skip to below the row we just found |
| 1165 | // an occupied cell |
| 1166 | y += j; |
| 1167 | continue inner; |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | if (cellXY != null) { |
| 1172 | cellXY[0] = x; |
| 1173 | cellXY[1] = y; |
| 1174 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1175 | foundCell = true; |
| 1176 | break; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | if (intersectX == -1 && intersectY == -1) { |
| 1180 | break; |
| 1181 | } else { |
| 1182 | // if we failed to find anything, try again but without any requirements of |
| 1183 | // intersecting |
| 1184 | intersectX = -1; |
| 1185 | intersectY = -1; |
| 1186 | continue; |
| 1187 | } |
| 1188 | } |
| 1189 | |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1190 | // re-mark space taken by ignoreView as occupied |
| 1191 | markCellsAsOccupiedForView(ignoreView); |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1192 | return foundCell; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * Called when drag has left this CellLayout or has been completed (successfully or not) |
| 1197 | */ |
| 1198 | void onDragExit() { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1199 | // This can actually be called when we aren't in a drag, e.g. when adding a new |
| 1200 | // item to this layout via the customize drawer. |
| 1201 | // Guard against that case. |
| 1202 | if (mDragging) { |
| 1203 | mDragging = false; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1204 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1205 | // Fade out the drag indicators |
| 1206 | if (mCrosshairsAnimator != null) { |
| 1207 | mCrosshairsAnimator.animateOut(); |
| 1208 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1209 | } |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1210 | |
| 1211 | // Invalidate the drag data |
| 1212 | mDragCell[0] = -1; |
| 1213 | mDragCell[1] = -1; |
| 1214 | mDragOutlineAnims[mDragOutlineCurrent].animateOut(); |
| 1215 | mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length; |
| 1216 | |
| 1217 | setHover(false); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1221 | * Mark a child as having been dropped. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1222 | * 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] | 1223 | * 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] | 1224 | * |
| 1225 | * @param child The child that is being dropped |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1226 | */ |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1227 | void onDropChild(View child) { |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1228 | if (child != null) { |
| 1229 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1230 | lp.isDragging = false; |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 1231 | lp.dropped = true; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 1232 | child.setVisibility(View.VISIBLE); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1233 | child.requestLayout(); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1234 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | void onDropAborted(View child) { |
| 1238 | if (child != null) { |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 1239 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1240 | lp.isDragging = false; |
| 1241 | child.setVisibility(View.VISIBLE); |
| 1242 | animateChildIntoPosition(child); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1243 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | /** |
| 1247 | * Start dragging the specified child |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1248 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1249 | * @param child The child that is being dragged |
| 1250 | */ |
| 1251 | void onDragChild(View child) { |
| 1252 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1253 | lp.isDragging = true; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 1254 | child.setVisibility(View.GONE); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | /** |
| 1258 | * A drag event has begun over this layout. |
| 1259 | * It may have begun over this layout (in which case onDragChild is called first), |
| 1260 | * or it may have begun on another layout. |
| 1261 | */ |
| 1262 | void onDragEnter(View dragView) { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 1263 | if (!mDragging) { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 1264 | // Fade in the drag indicators |
| 1265 | if (mCrosshairsAnimator != null) { |
| 1266 | mCrosshairsAnimator.animateIn(); |
| 1267 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1268 | } |
| 1269 | mDragging = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1270 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1271 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1272 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1273 | * Computes a bounding rectangle for a range of cells |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1274 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1275 | * @param cellX X coordinate of upper left corner expressed as a cell position |
| 1276 | * @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] | 1277 | * @param cellHSpan Width in cells |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1278 | * @param cellVSpan Height in cells |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1279 | * @param resultRect Rect into which to put the results |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1280 | */ |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1281 | 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] | 1282 | final int cellWidth = mCellWidth; |
| 1283 | final int cellHeight = mCellHeight; |
| 1284 | final int widthGap = mWidthGap; |
| 1285 | final int heightGap = mHeightGap; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1286 | |
| 1287 | final int hStartPadding = getLeftPadding(); |
| 1288 | final int vStartPadding = getTopPadding(); |
| 1289 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1290 | int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap); |
| 1291 | int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap); |
| 1292 | |
| 1293 | int x = hStartPadding + cellX * (cellWidth + widthGap); |
| 1294 | int y = vStartPadding + cellY * (cellHeight + heightGap); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1295 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1296 | resultRect.set(x, y, x + width, y + height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1297 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1298 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1299 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1300 | * 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] | 1301 | * fit the given rectangle. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1302 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1303 | * @param width Width in pixels |
| 1304 | * @param height Height in pixels |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1305 | * @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] | 1306 | */ |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1307 | public int[] rectToCell(int width, int height, int[] result) { |
Michael Jurka | 9987a5c | 2010-10-08 16:58:12 -0700 | [diff] [blame] | 1308 | return rectToCell(getResources(), width, height, result); |
| 1309 | } |
| 1310 | |
| 1311 | 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] | 1312 | // Always assume we're working with the smallest span to make sure we |
| 1313 | // reserve enough space in both orientations. |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1314 | int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width); |
| 1315 | int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1316 | int smallerSize = Math.min(actualWidth, actualHeight); |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1317 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1318 | // Always round up to next largest cell |
| 1319 | int spanX = (width + smallerSize) / smallerSize; |
| 1320 | int spanY = (height + smallerSize) / smallerSize; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1321 | |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1322 | if (result == null) { |
| 1323 | return new int[] { spanX, spanY }; |
| 1324 | } |
| 1325 | result[0] = spanX; |
| 1326 | result[1] = spanY; |
| 1327 | return result; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | /** |
| 1331 | * Find the first vacant cell, if there is one. |
| 1332 | * |
| 1333 | * @param vacant Holds the x and y coordinate of the vacant cell |
| 1334 | * @param spanX Horizontal cell span. |
| 1335 | * @param spanY Vertical cell span. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1336 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1337 | * @return True if a vacant cell was found |
| 1338 | */ |
| 1339 | public boolean getVacantCell(int[] vacant, int spanX, int spanY) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1340 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1341 | return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | static boolean findVacantCell(int[] vacant, int spanX, int spanY, |
| 1345 | int xCount, int yCount, boolean[][] occupied) { |
| 1346 | |
| 1347 | for (int x = 0; x < xCount; x++) { |
| 1348 | for (int y = 0; y < yCount; y++) { |
| 1349 | boolean available = !occupied[x][y]; |
| 1350 | out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) { |
| 1351 | for (int j = y; j < y + spanY - 1 && y < yCount; j++) { |
| 1352 | available = available && !occupied[i][j]; |
| 1353 | if (!available) break out; |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | if (available) { |
| 1358 | vacant[0] = x; |
| 1359 | vacant[1] = y; |
| 1360 | return true; |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | return false; |
| 1366 | } |
| 1367 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1368 | /** |
| 1369 | * Update the array of occupied cells (mOccupied), and return a flattened copy of the array. |
| 1370 | */ |
| 1371 | boolean[] getOccupiedCellsFlattened() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 1372 | final int xCount = mCountX; |
| 1373 | final int yCount = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1374 | final boolean[][] occupied = mOccupied; |
| 1375 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1376 | final boolean[] flat = new boolean[xCount * yCount]; |
| 1377 | for (int y = 0; y < yCount; y++) { |
| 1378 | for (int x = 0; x < xCount; x++) { |
| 1379 | flat[y * xCount + x] = occupied[x][y]; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | return flat; |
| 1384 | } |
| 1385 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1386 | private void clearOccupiedCells() { |
| 1387 | for (int x = 0; x < mCountX; x++) { |
| 1388 | for (int y = 0; y < mCountY; y++) { |
| 1389 | mOccupied[x][y] = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1390 | } |
| 1391 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1392 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1393 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1394 | public void onMove(View view, int newCellX, int newCellY) { |
| 1395 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1396 | markCellsAsUnoccupiedForView(view); |
| 1397 | markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true); |
| 1398 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1399 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1400 | private void markCellsAsOccupiedForView(View view) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1401 | if (view == null || view.getParent() != this) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1402 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1403 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true); |
| 1404 | } |
| 1405 | |
| 1406 | private void markCellsAsUnoccupiedForView(View view) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1407 | if (view == null || view.getParent() != this) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1408 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1409 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false); |
| 1410 | } |
| 1411 | |
| 1412 | private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) { |
| 1413 | for (int x = cellX; x < cellX + spanX && x < mCountX; x++) { |
| 1414 | for (int y = cellY; y < cellY + spanY && y < mCountY; y++) { |
| 1415 | mOccupied[x][y] = value; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1416 | } |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | @Override |
| 1421 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 1422 | return new CellLayout.LayoutParams(getContext(), attrs); |
| 1423 | } |
| 1424 | |
| 1425 | @Override |
| 1426 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 1427 | return p instanceof CellLayout.LayoutParams; |
| 1428 | } |
| 1429 | |
| 1430 | @Override |
| 1431 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 1432 | return new CellLayout.LayoutParams(p); |
| 1433 | } |
| 1434 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1435 | public static class CellLayoutAnimationController extends LayoutAnimationController { |
| 1436 | public CellLayoutAnimationController(Animation animation, float delay) { |
| 1437 | super(animation, delay); |
| 1438 | } |
| 1439 | |
| 1440 | @Override |
| 1441 | protected long getDelayForView(View view) { |
| 1442 | return (int) (Math.random() * 150); |
| 1443 | } |
| 1444 | } |
| 1445 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1446 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
| 1447 | /** |
| 1448 | * Horizontal location of the item in the grid. |
| 1449 | */ |
| 1450 | @ViewDebug.ExportedProperty |
| 1451 | public int cellX; |
| 1452 | |
| 1453 | /** |
| 1454 | * Vertical location of the item in the grid. |
| 1455 | */ |
| 1456 | @ViewDebug.ExportedProperty |
| 1457 | public int cellY; |
| 1458 | |
| 1459 | /** |
| 1460 | * Number of cells spanned horizontally by the item. |
| 1461 | */ |
| 1462 | @ViewDebug.ExportedProperty |
| 1463 | public int cellHSpan; |
| 1464 | |
| 1465 | /** |
| 1466 | * Number of cells spanned vertically by the item. |
| 1467 | */ |
| 1468 | @ViewDebug.ExportedProperty |
| 1469 | public int cellVSpan; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1470 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1471 | /** |
| 1472 | * Is this item currently being dragged |
| 1473 | */ |
| 1474 | public boolean isDragging; |
| 1475 | |
| 1476 | // X coordinate of the view in the layout. |
| 1477 | @ViewDebug.ExportedProperty |
| 1478 | int x; |
| 1479 | // Y coordinate of the view in the layout. |
| 1480 | @ViewDebug.ExportedProperty |
| 1481 | int y; |
| 1482 | |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 1483 | /** |
| 1484 | * The old X coordinate of this item, relative to its current parent. |
| 1485 | * Used to animate the item into its new position. |
| 1486 | */ |
| 1487 | int oldX; |
| 1488 | |
| 1489 | /** |
| 1490 | * The old Y coordinate of this item, relative to its current parent. |
| 1491 | * Used to animate the item into its new position. |
| 1492 | */ |
| 1493 | int oldY; |
| 1494 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 1495 | boolean dropped; |
Romain Guy | fcb9e71 | 2009-10-02 16:06:52 -0700 | [diff] [blame] | 1496 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1497 | public LayoutParams(Context c, AttributeSet attrs) { |
| 1498 | super(c, attrs); |
| 1499 | cellHSpan = 1; |
| 1500 | cellVSpan = 1; |
| 1501 | } |
| 1502 | |
| 1503 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 1504 | super(source); |
| 1505 | cellHSpan = 1; |
| 1506 | cellVSpan = 1; |
| 1507 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1508 | |
| 1509 | public LayoutParams(LayoutParams source) { |
| 1510 | super(source); |
| 1511 | this.cellX = source.cellX; |
| 1512 | this.cellY = source.cellY; |
| 1513 | this.cellHSpan = source.cellHSpan; |
| 1514 | this.cellVSpan = source.cellVSpan; |
| 1515 | } |
| 1516 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1517 | public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) { |
Romain Guy | 8f19cdd | 2010-01-08 15:07:00 -0800 | [diff] [blame] | 1518 | super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1519 | this.cellX = cellX; |
| 1520 | this.cellY = cellY; |
| 1521 | this.cellHSpan = cellHSpan; |
| 1522 | this.cellVSpan = cellVSpan; |
| 1523 | } |
| 1524 | |
| 1525 | public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap, |
| 1526 | int hStartPadding, int vStartPadding) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1527 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1528 | final int myCellHSpan = cellHSpan; |
| 1529 | final int myCellVSpan = cellVSpan; |
| 1530 | final int myCellX = cellX; |
| 1531 | final int myCellY = cellY; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1532 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1533 | width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) - |
| 1534 | leftMargin - rightMargin; |
| 1535 | height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) - |
| 1536 | topMargin - bottomMargin; |
| 1537 | |
| 1538 | x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin; |
| 1539 | y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin; |
| 1540 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1541 | |
| 1542 | public String toString() { |
| 1543 | return "(" + this.cellX + ", " + this.cellY + ")"; |
| 1544 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1545 | } |
| 1546 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1547 | // This class stores info for two purposes: |
| 1548 | // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY, |
| 1549 | // its spanX, spanY, and the screen it is on |
| 1550 | // 2. When long clicking on an empty cell in a CellLayout, we save information about the |
| 1551 | // cellX and cellY coordinates and which page was clicked. We then set this as a tag on |
| 1552 | // the CellLayout that was long clicked |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1553 | static final class CellInfo implements ContextMenu.ContextMenuInfo { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1554 | View cell; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1555 | int cellX = -1; |
| 1556 | int cellY = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1557 | int spanX; |
| 1558 | int spanY; |
| 1559 | int screen; |
| 1560 | boolean valid; |
| 1561 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1562 | @Override |
| 1563 | public String toString() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1564 | return "Cell[view=" + (cell == null ? "null" : cell.getClass()) |
| 1565 | + ", x=" + cellX + ", y=" + cellY + "]"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1566 | } |
| 1567 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1568 | } |