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 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 21 | import android.animation.AnimatorSet; |
| 22 | import android.animation.ObjectAnimator; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 23 | import android.animation.PropertyValuesHolder; |
Chet Haase | 00397b1 | 2010-10-07 11:13:10 -0700 | [diff] [blame] | 24 | import android.animation.TimeInterpolator; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 25 | import android.animation.ValueAnimator; |
| 26 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 27 | import android.content.Context; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 28 | import android.content.res.Resources; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 29 | import android.content.res.TypedArray; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 30 | import android.graphics.Bitmap; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 31 | import android.graphics.Canvas; |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame^] | 32 | import android.graphics.Matrix; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 33 | import android.graphics.Paint; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 34 | import android.graphics.Point; |
| 35 | import android.graphics.PointF; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | import android.graphics.Rect; |
| 37 | import android.graphics.RectF; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 38 | import android.graphics.Region; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 39 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 40 | import android.util.AttributeSet; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 41 | import android.util.Log; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | import android.view.MotionEvent; |
| 43 | import android.view.View; |
| 44 | import android.view.ViewDebug; |
| 45 | import android.view.ViewGroup; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 46 | import android.view.animation.Animation; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 47 | import android.view.animation.DecelerateInterpolator; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 48 | import android.view.animation.LayoutAnimationController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 49 | |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 50 | import com.android.launcher.R; |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame^] | 51 | import com.android.launcher2.FolderIcon.FolderRingAnimator; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 52 | |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame^] | 53 | import java.util.ArrayList; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 54 | import java.util.Arrays; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 55 | import java.util.HashMap; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 56 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 57 | public class CellLayout extends ViewGroup { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 58 | static final String TAG = "CellLayout"; |
| 59 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 60 | private int mOriginalCellWidth; |
| 61 | private int mOriginalCellHeight; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 62 | private int mCellWidth; |
| 63 | private int mCellHeight; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 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; |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 70 | private int mMaxGap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 71 | |
| 72 | private final Rect mRect = new Rect(); |
| 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. |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 77 | private final int[] mTmpXY = 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(); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame^] | 80 | int[] mTempLocation = new int[2]; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 81 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 82 | boolean[][] mOccupied; |
| 83 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 84 | private OnTouchListener mInterceptTouchListener; |
| 85 | |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame^] | 86 | private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>(); |
| 87 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 88 | private float mBackgroundAlpha; |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 89 | private float mBackgroundAlphaMultiplier = 1.0f; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 90 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 91 | private Drawable mNormalBackground; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 92 | private Drawable mActiveBackground; |
| 93 | private Drawable mActiveGlowBackground; |
| 94 | private Drawable mNormalBackgroundMini; |
| 95 | private Drawable mNormalGlowBackgroundMini; |
| 96 | private Drawable mActiveBackgroundMini; |
| 97 | private Drawable mActiveGlowBackgroundMini; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 98 | private Rect mBackgroundRect; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 99 | private Rect mGlowBackgroundRect; |
| 100 | private float mGlowBackgroundScale; |
| 101 | private float mGlowBackgroundAlpha; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 102 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 103 | private boolean mAcceptsDrops = true; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 104 | // If we're actively dragging something over this screen, mIsDragOverlapping is true |
| 105 | private boolean mIsDragOverlapping = false; |
| 106 | private boolean mIsDragOccuring = false; |
| 107 | private boolean mIsDefaultDropTarget = false; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 108 | private final Point mDragCenter = new Point(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 109 | |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 110 | // 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] | 111 | // They are used as circular arrays, indexed by mDragOutlineCurrent. |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 112 | private Point[] mDragOutlines = new Point[4]; |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 113 | private float[] mDragOutlineAlphas = new float[mDragOutlines.length]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 114 | private InterruptibleInOutAnimator[] mDragOutlineAnims = |
| 115 | new InterruptibleInOutAnimator[mDragOutlines.length]; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 116 | |
| 117 | // 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] | 118 | private int mDragOutlineCurrent = 0; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 119 | private final Paint mDragOutlinePaint = new Paint(); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 120 | |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 121 | private BubbleTextView mPressedOrFocusedIcon; |
| 122 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 123 | private Drawable mCrosshairsDrawable = null; |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 124 | private InterruptibleInOutAnimator mCrosshairsAnimator = null; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 125 | private float mCrosshairsVisibility = 0.0f; |
| 126 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 127 | private HashMap<CellLayout.LayoutParams, ObjectAnimator> mReorderAnimators = new |
| 128 | HashMap<CellLayout.LayoutParams, ObjectAnimator>(); |
| 129 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 130 | // When a drag operation is in progress, holds the nearest cell to the touch point |
| 131 | private final int[] mDragCell = new int[2]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 132 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 133 | private boolean mDragging = false; |
| 134 | |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 135 | private TimeInterpolator mEaseOutInterpolator; |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 136 | private CellLayoutChildren mChildren; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 137 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 138 | public CellLayout(Context context) { |
| 139 | this(context, null); |
| 140 | } |
| 141 | |
| 142 | public CellLayout(Context context, AttributeSet attrs) { |
| 143 | this(context, attrs, 0); |
| 144 | } |
| 145 | |
| 146 | public CellLayout(Context context, AttributeSet attrs, int defStyle) { |
| 147 | super(context, attrs, defStyle); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 148 | |
| 149 | // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show |
| 150 | // the user where a dragged item will land when dropped. |
| 151 | setWillNotDraw(false); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 152 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 153 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0); |
| 154 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 155 | mOriginalCellWidth = |
| 156 | mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10); |
| 157 | mOriginalCellHeight = |
| 158 | mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10); |
| 159 | mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0); |
| 160 | mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0); |
| 161 | mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0); |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 162 | mCountX = LauncherModel.getCellCountX(); |
| 163 | mCountY = LauncherModel.getCellCountY(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 164 | mOccupied = new boolean[mCountX][mCountY]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 165 | |
| 166 | a.recycle(); |
| 167 | |
| 168 | setAlwaysDrawnWithCacheEnabled(false); |
| 169 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 170 | final Resources res = getResources(); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 171 | |
Winson Chung | 967289b | 2011-06-30 18:09:30 -0700 | [diff] [blame] | 172 | mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo); |
| 173 | mActiveBackground = res.getDrawable(R.drawable.homescreen_green_normal_holo); |
| 174 | mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_green_strong_holo); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 175 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 176 | mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue); |
| 177 | mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong); |
| 178 | mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green); |
| 179 | mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 180 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 181 | mNormalBackground.setFilterBitmap(true); |
| 182 | mActiveBackground.setFilterBitmap(true); |
| 183 | mActiveGlowBackground.setFilterBitmap(true); |
| 184 | mNormalBackgroundMini.setFilterBitmap(true); |
| 185 | mNormalGlowBackgroundMini.setFilterBitmap(true); |
| 186 | mActiveBackgroundMini.setFilterBitmap(true); |
| 187 | mActiveGlowBackgroundMini.setFilterBitmap(true); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 188 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 189 | // Initialize the data structures used for the drag visualization. |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 190 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 191 | mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 192 | mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 193 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 194 | // Set up the animation for fading the crosshairs in and out |
| 195 | int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime); |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 196 | mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 197 | mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() { |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 198 | public void onAnimationUpdate(ValueAnimator animation) { |
| 199 | mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue(); |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 200 | invalidate(); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 201 | } |
| 202 | }); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 203 | mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 204 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 205 | for (int i = 0; i < mDragOutlines.length; i++) { |
| 206 | mDragOutlines[i] = new Point(-1, -1); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // When dragging things around the home screens, we show a green outline of |
| 210 | // where the item will land. The outlines gradually fade out, leaving a trail |
| 211 | // behind the drag path. |
| 212 | // Set up all the animations that are used to implement this fading. |
| 213 | final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 214 | final float fromAlphaValue = 0; |
| 215 | final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 216 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 217 | Arrays.fill(mDragOutlineAlphas, fromAlphaValue); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 218 | |
| 219 | for (int i = 0; i < mDragOutlineAnims.length; i++) { |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 220 | final InterruptibleInOutAnimator anim = |
| 221 | new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 222 | anim.getAnimator().setInterpolator(mEaseOutInterpolator); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 223 | final int thisIndex = i; |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 224 | anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 225 | public void onAnimationUpdate(ValueAnimator animation) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 226 | final Bitmap outline = (Bitmap)anim.getTag(); |
| 227 | |
| 228 | // If an animation is started and then stopped very quickly, we can still |
| 229 | // get spurious updates we've cleared the tag. Guard against this. |
| 230 | if (outline == null) { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 231 | if (false) { |
| 232 | Object val = animation.getAnimatedValue(); |
| 233 | Log.d(TAG, "anim " + thisIndex + " update: " + val + |
| 234 | ", isStopped " + anim.isStopped()); |
| 235 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 236 | // Try to prevent it from continuing to run |
| 237 | animation.cancel(); |
| 238 | } else { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 239 | mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue(); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 240 | final int left = mDragOutlines[thisIndex].x; |
| 241 | final int top = mDragOutlines[thisIndex].y; |
| 242 | CellLayout.this.invalidate(left, top, |
| 243 | left + outline.getWidth(), top + outline.getHeight()); |
| 244 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 245 | } |
| 246 | }); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 247 | // The animation holds a reference to the drag outline bitmap as long is it's |
| 248 | // 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] | 249 | anim.getAnimator().addListener(new AnimatorListenerAdapter() { |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 250 | @Override |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 251 | public void onAnimationEnd(Animator animation) { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 252 | if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 253 | anim.setTag(null); |
| 254 | } |
| 255 | } |
| 256 | }); |
| 257 | mDragOutlineAnims[i] = anim; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 258 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 259 | |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 260 | mBackgroundRect = new Rect(); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 261 | mGlowBackgroundRect = new Rect(); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 262 | setHoverScale(1.0f); |
| 263 | setHoverAlpha(1.0f); |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 264 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 265 | mChildren = new CellLayoutChildren(context); |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 266 | mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 267 | addView(mChildren); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 270 | static int widthInPortrait(Resources r, int numCells) { |
| 271 | // We use this method from Workspace to figure out how many rows/columns Launcher should |
| 272 | // have. We ignore the left/right padding on CellLayout because it turns out in our design |
| 273 | // the padding extends outside the visible screen size, but it looked fine anyway. |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 274 | int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 275 | int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap), |
| 276 | r.getDimensionPixelSize(R.dimen.workspace_height_gap)); |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 277 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 278 | return minGap * (numCells - 1) + cellWidth * numCells; |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 281 | static int heightInLandscape(Resources r, int numCells) { |
| 282 | // We use this method from Workspace to figure out how many rows/columns Launcher should |
| 283 | // have. We ignore the left/right padding on CellLayout because it turns out in our design |
| 284 | // the padding extends outside the visible screen size, but it looked fine anyway. |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 285 | int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 286 | int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap), |
| 287 | r.getDimensionPixelSize(R.dimen.workspace_height_gap)); |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 288 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 289 | return minGap * (numCells - 1) + cellHeight * numCells; |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 292 | public void enableHardwareLayers() { |
| 293 | mChildren.enableHardwareLayers(); |
| 294 | } |
| 295 | |
| 296 | public void setGridSize(int x, int y) { |
| 297 | mCountX = x; |
| 298 | mCountY = y; |
| 299 | mOccupied = new boolean[mCountX][mCountY]; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 300 | requestLayout(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 303 | private void invalidateBubbleTextView(BubbleTextView icon) { |
| 304 | final int padding = icon.getPressedOrFocusedBackgroundPadding(); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 305 | invalidate(icon.getLeft() + getPaddingLeft() - padding, |
| 306 | icon.getTop() + getPaddingTop() - padding, |
| 307 | icon.getRight() + getPaddingLeft() + padding, |
| 308 | icon.getBottom() + getPaddingTop() + padding); |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void setPressedOrFocusedIcon(BubbleTextView icon) { |
| 312 | // We draw the pressed or focused BubbleTextView's background in CellLayout because it |
| 313 | // requires an expanded clip rect (due to the glow's blur radius) |
| 314 | BubbleTextView oldIcon = mPressedOrFocusedIcon; |
| 315 | mPressedOrFocusedIcon = icon; |
| 316 | if (oldIcon != null) { |
| 317 | invalidateBubbleTextView(oldIcon); |
| 318 | } |
| 319 | if (mPressedOrFocusedIcon != null) { |
| 320 | invalidateBubbleTextView(mPressedOrFocusedIcon); |
| 321 | } |
| 322 | } |
| 323 | |
Winson Chung | 6e31408 | 2011-01-27 16:46:51 -0800 | [diff] [blame] | 324 | public CellLayoutChildren getChildrenLayout() { |
| 325 | if (getChildCount() > 0) { |
| 326 | return (CellLayoutChildren) getChildAt(0); |
| 327 | } |
| 328 | return null; |
| 329 | } |
| 330 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 331 | public void setIsDefaultDropTarget(boolean isDefaultDropTarget) { |
| 332 | if (mIsDefaultDropTarget != isDefaultDropTarget) { |
| 333 | mIsDefaultDropTarget = isDefaultDropTarget; |
| 334 | invalidate(); |
| 335 | } |
| 336 | } |
| 337 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 338 | void setIsDragOccuring(boolean isDragOccuring) { |
| 339 | if (mIsDragOccuring != isDragOccuring) { |
| 340 | mIsDragOccuring = isDragOccuring; |
| 341 | invalidate(); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | void setIsDragOverlapping(boolean isDragOverlapping) { |
| 346 | if (mIsDragOverlapping != isDragOverlapping) { |
| 347 | mIsDragOverlapping = isDragOverlapping; |
| 348 | invalidate(); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | boolean getIsDragOverlapping() { |
| 353 | return mIsDragOverlapping; |
| 354 | } |
| 355 | |
| 356 | private void updateGlowRect() { |
| 357 | float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 358 | int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left)); |
| 359 | int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top)); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 360 | mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY, |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 361 | mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY); |
| 362 | invalidate(); |
| 363 | } |
| 364 | |
| 365 | public void setHoverScale(float scaleFactor) { |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 366 | if (scaleFactor != mGlowBackgroundScale) { |
| 367 | mGlowBackgroundScale = scaleFactor; |
| 368 | updateGlowRect(); |
Michael Jurka | 8deb1e6 | 2011-01-25 16:27:43 -0800 | [diff] [blame] | 369 | if (getParent() != null) { |
| 370 | ((View) getParent()).invalidate(); |
| 371 | } |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | public float getHoverScale() { |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 376 | return mGlowBackgroundScale; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | public float getHoverAlpha() { |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 380 | return mGlowBackgroundAlpha; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | public void setHoverAlpha(float alpha) { |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 384 | mGlowBackgroundAlpha = alpha; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 385 | invalidate(); |
| 386 | } |
| 387 | |
| 388 | void animateDrop() { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 389 | Resources res = getResources(); |
| 390 | float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f; |
| 391 | ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale); |
| 392 | scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration)); |
| 393 | ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f); |
| 394 | scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration)); |
| 395 | ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 396 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 397 | alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay)); |
| 398 | alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDuration)); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 399 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 400 | AnimatorSet bouncer = new AnimatorSet(); |
| 401 | bouncer.play(scaleUp).before(scaleDown); |
| 402 | bouncer.play(scaleUp).with(alphaFadeOut); |
| 403 | bouncer.addListener(new AnimatorListenerAdapter() { |
| 404 | @Override |
| 405 | public void onAnimationStart(Animator animation) { |
| 406 | setIsDragOverlapping(true); |
| 407 | } |
| 408 | @Override |
| 409 | public void onAnimationEnd(Animator animation) { |
| 410 | setIsDragOverlapping(false); |
| 411 | setHoverScale(1.0f); |
| 412 | setHoverAlpha(1.0f); |
| 413 | } |
| 414 | }); |
| 415 | bouncer.start(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 418 | @Override |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 419 | protected void onDraw(Canvas canvas) { |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 420 | // When we're large, we are either drawn in a "hover" state (ie when dragging an item to |
| 421 | // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f) |
| 422 | // When we're small, we are either drawn normally or in the "accepts drops" state (during |
| 423 | // a drag). However, we also drag the mini hover background *over* one of those two |
| 424 | // backgrounds |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 425 | if (mBackgroundAlpha > 0.0f) { |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 426 | Drawable bg; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 427 | boolean mini = getScaleX() < 0.5f; |
| 428 | |
| 429 | if (mIsDragOverlapping) { |
| 430 | // In the mini case, we draw the active_glow bg *over* the active background |
| 431 | bg = mini ? mActiveBackgroundMini : mActiveGlowBackground; |
| 432 | } else if (mIsDragOccuring && mAcceptsDrops) { |
| 433 | bg = mini ? mActiveBackgroundMini : mActiveBackground; |
Adam Cohen | 3af863b | 2011-01-25 12:16:51 -0800 | [diff] [blame] | 434 | } else if (mIsDefaultDropTarget && mini) { |
| 435 | bg = mNormalGlowBackgroundMini; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 436 | } else { |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 437 | bg = mini ? mNormalBackgroundMini : mNormalBackground; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 438 | } |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 439 | |
| 440 | bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255)); |
| 441 | bg.setBounds(mBackgroundRect); |
| 442 | bg.draw(canvas); |
| 443 | |
| 444 | if (mini && mIsDragOverlapping) { |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 445 | boolean modifiedClipRect = false; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 446 | if (mGlowBackgroundScale > 1.0f) { |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 447 | // If the hover background's scale is greater than 1, we'll be drawing outside |
| 448 | // the bounds of this CellLayout. Get around that by temporarily increasing the |
| 449 | // size of the clip rect |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 450 | float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 451 | Rect clipRect = canvas.getClipBounds(); |
| 452 | int marginX = (int) (marginFraction * (clipRect.right - clipRect.left)); |
| 453 | int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top)); |
| 454 | canvas.save(Canvas.CLIP_SAVE_FLAG); |
| 455 | canvas.clipRect(-marginX, -marginY, |
| 456 | getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE); |
| 457 | modifiedClipRect = true; |
| 458 | } |
| 459 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 460 | mActiveGlowBackgroundMini.setAlpha( |
| 461 | (int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255)); |
| 462 | mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect); |
| 463 | mActiveGlowBackgroundMini.draw(canvas); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 464 | if (modifiedClipRect) { |
| 465 | canvas.restore(); |
| 466 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 467 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 468 | } |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 469 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 470 | if (mCrosshairsVisibility > 0.0f) { |
| 471 | final int countX = mCountX; |
| 472 | final int countY = mCountY; |
| 473 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 474 | final float MAX_ALPHA = 0.4f; |
| 475 | final int MAX_VISIBLE_DISTANCE = 600; |
| 476 | final float DISTANCE_MULTIPLIER = 0.002f; |
| 477 | |
| 478 | final Drawable d = mCrosshairsDrawable; |
| 479 | final int width = d.getIntrinsicWidth(); |
| 480 | final int height = d.getIntrinsicHeight(); |
| 481 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 482 | int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 483 | for (int col = 0; col <= countX; col++) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 484 | int y = getPaddingTop() - (mHeightGap / 2) - (height / 2); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 485 | for (int row = 0; row <= countY; row++) { |
| 486 | mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y); |
| 487 | float dist = mTmpPointF.length(); |
| 488 | // Crosshairs further from the drag point are more faint |
| 489 | float alpha = Math.min(MAX_ALPHA, |
| 490 | DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist)); |
| 491 | if (alpha > 0.0f) { |
| 492 | d.setBounds(x, y, x + width, y + height); |
| 493 | d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility)); |
| 494 | d.draw(canvas); |
| 495 | } |
| 496 | y += mCellHeight + mHeightGap; |
| 497 | } |
| 498 | x += mCellWidth + mWidthGap; |
| 499 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 500 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 501 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 502 | final Paint paint = mDragOutlinePaint; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 503 | for (int i = 0; i < mDragOutlines.length; i++) { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 504 | final float alpha = mDragOutlineAlphas[i]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 505 | if (alpha > 0) { |
| 506 | final Point p = mDragOutlines[i]; |
| 507 | final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag(); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 508 | paint.setAlpha((int)(alpha + .5f)); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 509 | canvas.drawBitmap(b, p.x, p.y, paint); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 510 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 511 | } |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 512 | |
| 513 | // We draw the pressed or focused BubbleTextView's background in CellLayout because it |
| 514 | // requires an expanded clip rect (due to the glow's blur radius) |
| 515 | if (mPressedOrFocusedIcon != null) { |
| 516 | final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding(); |
| 517 | final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground(); |
| 518 | if (b != null) { |
| 519 | canvas.drawBitmap(b, |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 520 | mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding, |
| 521 | mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding, |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 522 | null); |
| 523 | } |
| 524 | } |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame^] | 525 | |
| 526 | // The folder outer / inner ring image(s) |
| 527 | for (int i = 0; i < mFolderOuterRings.size(); i++) { |
| 528 | FolderRingAnimator fra = mFolderOuterRings.get(i); |
| 529 | |
| 530 | // Draw outer ring |
| 531 | Drawable d = FolderRingAnimator.sSharedOuterRingDrawable; |
| 532 | int width = (int) fra.getOuterRingSize(); |
| 533 | int height = width; |
| 534 | cellToPoint(fra.mCellX, fra.mCellY, mTempLocation); |
| 535 | |
| 536 | int centerX = mTempLocation[0] + mCellWidth / 2; |
| 537 | int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2; |
| 538 | |
| 539 | canvas.save(); |
| 540 | canvas.translate(centerX - width / 2, centerY - height / 2); |
| 541 | d.setBounds(0, 0, width, height); |
| 542 | d.draw(canvas); |
| 543 | canvas.restore(); |
| 544 | |
| 545 | // Draw inner ring |
| 546 | d = FolderRingAnimator.sSharedInnerRingDrawable; |
| 547 | width = (int) fra.getInnerRingSize(); |
| 548 | height = width; |
| 549 | cellToPoint(fra.mCellX, fra.mCellY, mTempLocation); |
| 550 | |
| 551 | centerX = mTempLocation[0] + mCellWidth / 2; |
| 552 | centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2; |
| 553 | canvas.save(); |
| 554 | canvas.translate(centerX - width / 2, centerY - width / 2); |
| 555 | d.setBounds(0, 0, width, height); |
| 556 | d.draw(canvas); |
| 557 | canvas.restore(); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | public void showFolderAccept(FolderRingAnimator fra) { |
| 562 | mFolderOuterRings.add(fra); |
| 563 | } |
| 564 | |
| 565 | public void hideFolderAccept(FolderRingAnimator fra) { |
| 566 | if (mFolderOuterRings.contains(fra)) { |
| 567 | mFolderOuterRings.remove(fra); |
| 568 | } |
| 569 | invalidate(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | @Override |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 573 | public void cancelLongPress() { |
| 574 | super.cancelLongPress(); |
| 575 | |
| 576 | // Cancel long press for all children |
| 577 | final int count = getChildCount(); |
| 578 | for (int i = 0; i < count; i++) { |
| 579 | final View child = getChildAt(i); |
| 580 | child.cancelLongPress(); |
| 581 | } |
| 582 | } |
| 583 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 584 | public void setOnInterceptTouchListener(View.OnTouchListener listener) { |
| 585 | mInterceptTouchListener = listener; |
| 586 | } |
| 587 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 588 | int getCountX() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 589 | return mCountX; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | int getCountY() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 593 | return mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 596 | public boolean addViewToCellLayout( |
| 597 | View child, int index, int childId, LayoutParams params, boolean markCells) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 598 | final LayoutParams lp = params; |
| 599 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 600 | // Generate an id for each view, this assumes we have at most 256x256 cells |
| 601 | // per workspace screen |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 602 | 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] | 603 | // If the horizontal or vertical span is set to -1, it is taken to |
| 604 | // mean that it spans the extent of the CellLayout |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 605 | if (lp.cellHSpan < 0) lp.cellHSpan = mCountX; |
| 606 | if (lp.cellVSpan < 0) lp.cellVSpan = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 607 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 608 | child.setId(childId); |
| 609 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 610 | mChildren.addView(child, index, lp); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 611 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 612 | if (markCells) markCellsAsOccupiedForView(child); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 613 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 614 | return true; |
| 615 | } |
| 616 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 617 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 618 | |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 619 | public void setAcceptsDrops(boolean acceptsDrops) { |
| 620 | if (mAcceptsDrops != acceptsDrops) { |
| 621 | mAcceptsDrops = acceptsDrops; |
| 622 | invalidate(); |
| 623 | } |
| 624 | } |
| 625 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 626 | @Override |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 627 | public void removeAllViews() { |
| 628 | clearOccupiedCells(); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 629 | mChildren.removeAllViews(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | @Override |
| 633 | public void removeAllViewsInLayout() { |
| 634 | clearOccupiedCells(); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 635 | mChildren.removeAllViewsInLayout(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 638 | public void removeViewWithoutMarkingCells(View view) { |
Michael Jurka | cf6125c | 2011-01-28 15:20:01 -0800 | [diff] [blame] | 639 | mChildren.removeView(view); |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 642 | @Override |
| 643 | public void removeView(View view) { |
| 644 | markCellsAsUnoccupiedForView(view); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 645 | mChildren.removeView(view); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | @Override |
| 649 | public void removeViewAt(int index) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 650 | markCellsAsUnoccupiedForView(mChildren.getChildAt(index)); |
| 651 | mChildren.removeViewAt(index); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | @Override |
| 655 | public void removeViewInLayout(View view) { |
| 656 | markCellsAsUnoccupiedForView(view); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 657 | mChildren.removeViewInLayout(view); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | @Override |
| 661 | public void removeViews(int start, int count) { |
| 662 | for (int i = start; i < start + count; i++) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 663 | markCellsAsUnoccupiedForView(mChildren.getChildAt(i)); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 664 | } |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 665 | mChildren.removeViews(start, count); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | @Override |
| 669 | public void removeViewsInLayout(int start, int count) { |
| 670 | for (int i = start; i < start + count; i++) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 671 | markCellsAsUnoccupiedForView(mChildren.getChildAt(i)); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 672 | } |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 673 | mChildren.removeViewsInLayout(start, count); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 676 | public void drawChildren(Canvas canvas) { |
| 677 | mChildren.draw(canvas); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 680 | void buildChildrenLayer() { |
| 681 | mChildren.buildLayer(); |
| 682 | } |
| 683 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 684 | @Override |
| 685 | protected void onAttachedToWindow() { |
| 686 | super.onAttachedToWindow(); |
| 687 | mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this); |
| 688 | } |
| 689 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 690 | public void setTagToCellInfoForPoint(int touchX, int touchY) { |
| 691 | final CellInfo cellInfo = mCellInfo; |
| 692 | final Rect frame = mRect; |
| 693 | final int x = touchX + mScrollX; |
| 694 | final int y = touchY + mScrollY; |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 695 | final int count = mChildren.getChildCount(); |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 696 | |
| 697 | boolean found = false; |
| 698 | for (int i = count - 1; i >= 0; i--) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 699 | final View child = mChildren.getChildAt(i); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 700 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 701 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 702 | if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) && |
| 703 | lp.isLockedToGrid) { |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 704 | child.getHitRect(frame); |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 705 | |
| 706 | // The child hit rect is relative to the CellLayoutChildren parent, so we need to |
| 707 | // offset that by this CellLayout's padding to test an (x,y) point that is relative |
| 708 | // to this view. |
| 709 | final int tmpXY[] = mTmpXY; |
| 710 | child.getLocationOnScreen(tmpXY); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 711 | frame.offset(mPaddingLeft, mPaddingTop); |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 712 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 713 | if (frame.contains(x, y)) { |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 714 | cellInfo.cell = child; |
| 715 | cellInfo.cellX = lp.cellX; |
| 716 | cellInfo.cellY = lp.cellY; |
| 717 | cellInfo.spanX = lp.cellHSpan; |
| 718 | cellInfo.spanY = lp.cellVSpan; |
| 719 | cellInfo.valid = true; |
| 720 | found = true; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | } |
| 724 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 725 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 726 | if (!found) { |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 727 | final int cellXY[] = mTmpXY; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 728 | pointToCellExact(x, y, cellXY); |
| 729 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 730 | cellInfo.cell = null; |
| 731 | cellInfo.cellX = cellXY[0]; |
| 732 | cellInfo.cellY = cellXY[1]; |
| 733 | cellInfo.spanX = 1; |
| 734 | cellInfo.spanY = 1; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 735 | cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX && |
| 736 | cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]]; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 737 | } |
| 738 | setTag(cellInfo); |
| 739 | } |
| 740 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 741 | @Override |
| 742 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 743 | if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) { |
| 744 | return true; |
| 745 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 746 | final int action = ev.getAction(); |
| 747 | final CellInfo cellInfo = mCellInfo; |
| 748 | |
| 749 | if (action == MotionEvent.ACTION_DOWN) { |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 750 | setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 751 | } else if (action == MotionEvent.ACTION_UP) { |
| 752 | cellInfo.cell = null; |
| 753 | cellInfo.cellX = -1; |
| 754 | cellInfo.cellY = -1; |
| 755 | cellInfo.spanX = 0; |
| 756 | cellInfo.spanY = 0; |
| 757 | cellInfo.valid = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 758 | setTag(cellInfo); |
| 759 | } |
| 760 | |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | @Override |
| 765 | public CellInfo getTag() { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 766 | return (CellInfo) super.getTag(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 767 | } |
| 768 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 769 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 770 | * 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] | 771 | * @param x X coordinate of the point |
| 772 | * @param y Y coordinate of the point |
| 773 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 774 | */ |
| 775 | void pointToCellExact(int x, int y, int[] result) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 776 | final int hStartPadding = getPaddingLeft(); |
| 777 | final int vStartPadding = getPaddingTop(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 778 | |
| 779 | result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap); |
| 780 | result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap); |
| 781 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 782 | final int xAxis = mCountX; |
| 783 | final int yAxis = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 784 | |
| 785 | if (result[0] < 0) result[0] = 0; |
| 786 | if (result[0] >= xAxis) result[0] = xAxis - 1; |
| 787 | if (result[1] < 0) result[1] = 0; |
| 788 | if (result[1] >= yAxis) result[1] = yAxis - 1; |
| 789 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 790 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 791 | /** |
| 792 | * Given a point, return the cell that most closely encloses that point |
| 793 | * @param x X coordinate of the point |
| 794 | * @param y Y coordinate of the point |
| 795 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 796 | */ |
| 797 | void pointToCellRounded(int x, int y, int[] result) { |
| 798 | pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result); |
| 799 | } |
| 800 | |
| 801 | /** |
| 802 | * 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] | 803 | * |
| 804 | * @param cellX X coordinate of the cell |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 805 | * @param cellY Y coordinate of the cell |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 806 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 807 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 808 | */ |
| 809 | void cellToPoint(int cellX, int cellY, int[] result) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 810 | final int hStartPadding = getPaddingLeft(); |
| 811 | final int vStartPadding = getPaddingTop(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 812 | |
| 813 | result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap); |
| 814 | result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap); |
| 815 | } |
| 816 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 817 | /** |
| 818 | * Given a cell coordinate, return the point that represents the upper left corner of that cell |
| 819 | * |
| 820 | * @param cellX X coordinate of the cell |
| 821 | * @param cellY Y coordinate of the cell |
| 822 | * |
| 823 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 824 | */ |
| 825 | void cellToCenterPoint(int cellX, int cellY, int[] result) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 826 | final int hStartPadding = getPaddingLeft(); |
| 827 | final int vStartPadding = getPaddingTop(); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 828 | |
| 829 | result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2; |
| 830 | result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2; |
| 831 | } |
| 832 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 833 | int getCellWidth() { |
| 834 | return mCellWidth; |
| 835 | } |
| 836 | |
| 837 | int getCellHeight() { |
| 838 | return mCellHeight; |
| 839 | } |
| 840 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 841 | int getWidthGap() { |
| 842 | return mWidthGap; |
| 843 | } |
| 844 | |
| 845 | int getHeightGap() { |
| 846 | return mHeightGap; |
| 847 | } |
| 848 | |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 849 | Rect getContentRect(Rect r) { |
| 850 | if (r == null) { |
| 851 | r = new Rect(); |
| 852 | } |
| 853 | int left = getPaddingLeft(); |
| 854 | int top = getPaddingTop(); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 855 | int right = left + getWidth() - mPaddingLeft - mPaddingRight; |
| 856 | int bottom = top + getHeight() - mPaddingTop - mPaddingBottom; |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 857 | r.set(left, top, right, bottom); |
| 858 | return r; |
| 859 | } |
| 860 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 861 | @Override |
| 862 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 863 | // TODO: currently ignoring padding |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 864 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 865 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 866 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 867 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 868 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 869 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 870 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 871 | if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { |
| 872 | throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); |
| 873 | } |
| 874 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 875 | int numWidthGaps = mCountX - 1; |
| 876 | int numHeightGaps = mCountY - 1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 877 | |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 878 | if (mWidthGap < 0 || mHeightGap < 0) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 879 | int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight; |
| 880 | int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom; |
| 881 | int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth); |
| 882 | int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight); |
| 883 | mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0); |
| 884 | mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0); |
| 885 | int remainingHSpace = hFreeSpace - (numWidthGaps * mWidthGap); |
| 886 | int remainingVSpace = vFreeSpace - (numHeightGaps * mHeightGap); |
| 887 | mCellWidth = mOriginalCellWidth + remainingHSpace / mCountX; |
| 888 | mCellHeight = mOriginalCellHeight + remainingVSpace / mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 889 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 890 | mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap); |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 891 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 892 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 893 | // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY |
| 894 | int newWidth = widthSpecSize; |
| 895 | int newHeight = heightSpecSize; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 896 | if (widthSpecMode == MeasureSpec.AT_MOST) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 897 | newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) + |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 898 | ((mCountX - 1) * mWidthGap); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 899 | newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) + |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 900 | ((mCountY - 1) * mHeightGap); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 901 | setMeasuredDimension(newWidth, newHeight); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 902 | } |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 903 | |
| 904 | int count = getChildCount(); |
| 905 | for (int i = 0; i < count; i++) { |
| 906 | View child = getChildAt(i); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 907 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft - |
| 908 | mPaddingRight, MeasureSpec.EXACTLY); |
| 909 | int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop - |
| 910 | mPaddingBottom, MeasureSpec.EXACTLY); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 911 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 912 | } |
| 913 | setMeasuredDimension(newWidth, newHeight); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 917 | 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] | 918 | int count = getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 919 | for (int i = 0; i < count; i++) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 920 | View child = getChildAt(i); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 921 | child.layout(mPaddingLeft, mPaddingTop, |
| 922 | r - l - mPaddingRight, b - t - mPaddingBottom); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
| 926 | @Override |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 927 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 928 | super.onSizeChanged(w, h, oldw, oldh); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 929 | mBackgroundRect.set(0, 0, w, h); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 930 | updateGlowRect(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 934 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 935 | mChildren.setChildrenDrawingCacheEnabled(enabled); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | @Override |
| 939 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 940 | mChildren.setChildrenDrawnWithCacheEnabled(enabled); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 941 | } |
| 942 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 943 | public float getBackgroundAlpha() { |
| 944 | return mBackgroundAlpha; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 947 | public void setFastBackgroundAlpha(float alpha) { |
| 948 | mBackgroundAlpha = alpha; |
| 949 | } |
| 950 | |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 951 | public void setBackgroundAlphaMultiplier(float multiplier) { |
| 952 | mBackgroundAlphaMultiplier = multiplier; |
| 953 | } |
| 954 | |
Adam Cohen | ddb8219 | 2010-11-10 16:32:54 -0800 | [diff] [blame] | 955 | public float getBackgroundAlphaMultiplier() { |
| 956 | return mBackgroundAlphaMultiplier; |
| 957 | } |
| 958 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 959 | public void setBackgroundAlpha(float alpha) { |
| 960 | mBackgroundAlpha = alpha; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 961 | invalidate(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 964 | // Need to return true to let the view system know we know how to handle alpha-- this is |
| 965 | // because when our children have an alpha of 0.0f, they are still rendering their "dimmed" |
| 966 | // versions |
| 967 | @Override |
| 968 | protected boolean onSetAlpha(int alpha) { |
| 969 | return true; |
| 970 | } |
| 971 | |
| 972 | public void setAlpha(float alpha) { |
| 973 | setChildrenAlpha(alpha); |
| 974 | super.setAlpha(alpha); |
| 975 | } |
| 976 | |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 977 | public void setFastAlpha(float alpha) { |
| 978 | setFastChildrenAlpha(alpha); |
| 979 | super.setFastAlpha(alpha); |
| 980 | } |
| 981 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 982 | private void setChildrenAlpha(float alpha) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 983 | final int childCount = getChildCount(); |
| 984 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 985 | getChildAt(i).setAlpha(alpha); |
| 986 | } |
| 987 | } |
| 988 | |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 989 | private void setFastChildrenAlpha(float alpha) { |
| 990 | final int childCount = getChildCount(); |
| 991 | for (int i = 0; i < childCount; i++) { |
| 992 | getChildAt(i).setFastAlpha(alpha); |
| 993 | } |
| 994 | } |
| 995 | |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 996 | public View getChildAt(int x, int y) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 997 | return mChildren.getChildAt(x, y); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1000 | public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration, |
| 1001 | int delay) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1002 | CellLayoutChildren clc = getChildrenLayout(); |
| 1003 | if (clc.indexOfChild(child) != -1 && !mOccupied[cellX][cellY]) { |
| 1004 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1005 | final ItemInfo info = (ItemInfo) child.getTag(); |
| 1006 | |
| 1007 | // We cancel any existing animations |
| 1008 | if (mReorderAnimators.containsKey(lp)) { |
| 1009 | mReorderAnimators.get(lp).cancel(); |
| 1010 | mReorderAnimators.remove(lp); |
| 1011 | } |
| 1012 | |
| 1013 | int oldX = lp.x; |
| 1014 | int oldY = lp.y; |
| 1015 | mOccupied[lp.cellX][lp.cellY] = false; |
| 1016 | mOccupied[cellX][cellY] = true; |
| 1017 | |
| 1018 | lp.isLockedToGrid = true; |
| 1019 | lp.cellX = info.cellX = cellX; |
| 1020 | lp.cellY = info.cellY = cellY; |
| 1021 | clc.setupLp(lp); |
| 1022 | lp.isLockedToGrid = false; |
| 1023 | int newX = lp.x; |
| 1024 | int newY = lp.y; |
| 1025 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1026 | lp.x = oldX; |
| 1027 | lp.y = oldY; |
| 1028 | child.requestLayout(); |
| 1029 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1030 | PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", oldX, newX); |
| 1031 | PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", oldY, newY); |
| 1032 | ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, x, y); |
| 1033 | oa.setDuration(duration); |
| 1034 | mReorderAnimators.put(lp, oa); |
| 1035 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 1036 | public void onAnimationUpdate(ValueAnimator animation) { |
| 1037 | child.requestLayout(); |
| 1038 | } |
| 1039 | }); |
| 1040 | oa.addListener(new AnimatorListenerAdapter() { |
| 1041 | boolean cancelled = false; |
| 1042 | public void onAnimationEnd(Animator animation) { |
| 1043 | // If the animation was cancelled, it means that another animation |
| 1044 | // has interrupted this one, and we don't want to lock the item into |
| 1045 | // place just yet. |
| 1046 | if (!cancelled) { |
| 1047 | lp.isLockedToGrid = true; |
| 1048 | } |
| 1049 | if (mReorderAnimators.containsKey(lp)) { |
| 1050 | mReorderAnimators.remove(lp); |
| 1051 | } |
| 1052 | } |
| 1053 | public void onAnimationCancel(Animator animation) { |
| 1054 | cancelled = true; |
| 1055 | } |
| 1056 | }); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1057 | oa.setStartDelay(delay); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1058 | oa.start(); |
| 1059 | return true; |
| 1060 | } |
| 1061 | return false; |
| 1062 | } |
| 1063 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1064 | /** |
| 1065 | * Estimate where the top left cell of the dragged item will land if it is dropped. |
| 1066 | * |
| 1067 | * @param originX The X value of the top left corner of the item |
| 1068 | * @param originY The Y value of the top left corner of the item |
| 1069 | * @param spanX The number of horizontal cells that the item spans |
| 1070 | * @param spanY The number of vertical cells that the item spans |
| 1071 | * @param result The estimated drop cell X and Y. |
| 1072 | */ |
| 1073 | void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 1074 | final int countX = mCountX; |
| 1075 | final int countY = mCountY; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1076 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1077 | // pointToCellRounded takes the top left of a cell but will pad that with |
| 1078 | // cellWidth/2 and cellHeight/2 when finding the matching cell |
| 1079 | pointToCellRounded(originX, originY, result); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1080 | |
| 1081 | // If the item isn't fully on this screen, snap to the edges |
| 1082 | int rightOverhang = result[0] + spanX - countX; |
| 1083 | if (rightOverhang > 0) { |
| 1084 | result[0] -= rightOverhang; // Snap to right |
| 1085 | } |
| 1086 | result[0] = Math.max(0, result[0]); // Snap to left |
| 1087 | int bottomOverhang = result[1] + spanY - countY; |
| 1088 | if (bottomOverhang > 0) { |
| 1089 | result[1] -= bottomOverhang; // Snap to bottom |
| 1090 | } |
| 1091 | result[1] = Math.max(0, result[1]); // Snap to top |
| 1092 | } |
| 1093 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1094 | void visualizeDropLocation( |
| 1095 | View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) { |
| 1096 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1097 | final int oldDragCellX = mDragCell[0]; |
| 1098 | final int oldDragCellY = mDragCell[1]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1099 | final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell); |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1100 | if (v != null) { |
| 1101 | mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2)); |
| 1102 | } else { |
| 1103 | mDragCenter.set(originX, originY); |
| 1104 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1105 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1106 | if (dragOutline == null && v == null) { |
| 1107 | if (mCrosshairsDrawable != null) { |
| 1108 | invalidate(); |
| 1109 | } |
| 1110 | return; |
| 1111 | } |
| 1112 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1113 | if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) { |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1114 | // Find the top left corner of the rect the object will occupy |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1115 | final int[] topLeft = mTmpPoint; |
| 1116 | cellToPoint(nearest[0], nearest[1], topLeft); |
| 1117 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1118 | int left = topLeft[0]; |
| 1119 | int top = topLeft[1]; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1120 | |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1121 | if (v != null) { |
Adam Cohen | 99e8b40 | 2011-03-25 19:23:43 -0700 | [diff] [blame] | 1122 | // When drawing the drag outline, it did not account for margin offsets |
| 1123 | // added by the view's parent. |
| 1124 | MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams(); |
| 1125 | left += lp.leftMargin; |
| 1126 | top += lp.topMargin; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1127 | |
Adam Cohen | 99e8b40 | 2011-03-25 19:23:43 -0700 | [diff] [blame] | 1128 | // Offsets due to the size difference between the View and the dragOutline. |
| 1129 | // There is a size difference to account for the outer blur, which may lie |
| 1130 | // outside the bounds of the view. |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1131 | left += (v.getWidth() - dragOutline.getWidth()) / 2; |
| 1132 | top += (v.getHeight() - dragOutline.getHeight()) / 2; |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 1133 | } else { |
| 1134 | // Center the drag outline in the cell |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1135 | left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap) |
| 1136 | - dragOutline.getWidth()) / 2; |
| 1137 | top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap) |
| 1138 | - dragOutline.getHeight()) / 2; |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1139 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1140 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1141 | final int oldIndex = mDragOutlineCurrent; |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1142 | mDragOutlineAnims[oldIndex].animateOut(); |
| 1143 | mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1144 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1145 | mDragOutlines[mDragOutlineCurrent].set(left, top); |
| 1146 | mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline); |
| 1147 | mDragOutlineAnims[mDragOutlineCurrent].animateIn(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1148 | } |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 1149 | |
| 1150 | // If we are drawing crosshairs, the entire CellLayout needs to be invalidated |
| 1151 | if (mCrosshairsDrawable != null) { |
| 1152 | invalidate(); |
| 1153 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 1156 | public void clearDragOutlines() { |
| 1157 | final int oldIndex = mDragOutlineCurrent; |
| 1158 | mDragOutlineAnims[oldIndex].animateOut(); |
| 1159 | mDragCell[0] = -1; |
| 1160 | mDragCell[1] = -1; |
| 1161 | } |
| 1162 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1163 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1164 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1165 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1166 | * |
Romain Guy | 51afc02 | 2009-05-04 18:03:43 -0700 | [diff] [blame] | 1167 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1168 | * @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] | 1169 | * @param spanX Horizontal span of the object. |
| 1170 | * @param spanY Vertical span of the object. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1171 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1172 | * be allocated) |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1173 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1174 | * nearest the requested location. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1175 | */ |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1176 | int[] findNearestVacantArea( |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1177 | int pixelX, int pixelY, int spanX, int spanY, int[] result) { |
| 1178 | return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result); |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1179 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1180 | |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1181 | /** |
| 1182 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1183 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 1184 | * |
| 1185 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1186 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 1187 | * @param spanX Horizontal span of the object. |
| 1188 | * @param spanY Vertical span of the object. |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1189 | * @param ignoreOccupied If true, the result can be an occupied cell |
| 1190 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1191 | * be allocated) |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1192 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1193 | * nearest the requested location. |
| 1194 | */ |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1195 | int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView, |
| 1196 | boolean ignoreOccupied, int[] result) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1197 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
| 1198 | markCellsAsUnoccupiedForView(ignoreView); |
| 1199 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1200 | // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds |
| 1201 | // to the center of the item, but we are searching based on the top-left cell, so |
| 1202 | // we translate the point over to correspond to the top-left. |
| 1203 | pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f; |
| 1204 | pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f; |
| 1205 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1206 | // Keep track of best-scoring drop area |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1207 | final int[] bestXY = result != null ? result : new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1208 | double bestDistance = Double.MAX_VALUE; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1209 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1210 | final int countX = mCountX; |
| 1211 | final int countY = mCountY; |
| 1212 | final boolean[][] occupied = mOccupied; |
| 1213 | |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1214 | for (int y = 0; y < countY - (spanY - 1); y++) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1215 | inner: |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1216 | for (int x = 0; x < countX - (spanX - 1); x++) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1217 | if (ignoreOccupied) { |
| 1218 | for (int i = 0; i < spanX; i++) { |
| 1219 | for (int j = 0; j < spanY; j++) { |
| 1220 | if (occupied[x + i][y + j]) { |
| 1221 | // small optimization: we can skip to after the column we |
| 1222 | // just found an occupied cell |
| 1223 | x += i; |
| 1224 | continue inner; |
| 1225 | } |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | } |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 1229 | final int[] cellXY = mTmpXY; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1230 | cellToCenterPoint(x, y, cellXY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1231 | |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1232 | double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2) |
| 1233 | + Math.pow(cellXY[1] - pixelY, 2)); |
| 1234 | if (distance <= bestDistance) { |
| 1235 | bestDistance = distance; |
| 1236 | bestXY[0] = x; |
| 1237 | bestXY[1] = y; |
| 1238 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1239 | } |
| 1240 | } |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1241 | // re-mark space taken by ignoreView as occupied |
| 1242 | markCellsAsOccupiedForView(ignoreView); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1243 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1244 | // Return -1, -1 if no suitable location found |
| 1245 | if (bestDistance == Double.MAX_VALUE) { |
| 1246 | bestXY[0] = -1; |
| 1247 | bestXY[1] = -1; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1248 | } |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1249 | return bestXY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1250 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1251 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1252 | /** |
| 1253 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1254 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 1255 | * |
| 1256 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1257 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 1258 | * @param spanX Horizontal span of the object. |
| 1259 | * @param spanY Vertical span of the object. |
| 1260 | * @param ignoreView Considers space occupied by this view as unoccupied |
| 1261 | * @param result Previously returned value to possibly recycle. |
| 1262 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1263 | * nearest the requested location. |
| 1264 | */ |
| 1265 | int[] findNearestVacantArea( |
| 1266 | int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) { |
| 1267 | return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result); |
| 1268 | } |
| 1269 | |
| 1270 | /** |
| 1271 | * Find a starting cell position that will fit the given bounds nearest the requested |
| 1272 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 1273 | * |
| 1274 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1275 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 1276 | * @param spanX Horizontal span of the object. |
| 1277 | * @param spanY Vertical span of the object. |
| 1278 | * @param ignoreView Considers space occupied by this view as unoccupied |
| 1279 | * @param result Previously returned value to possibly recycle. |
| 1280 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1281 | * nearest the requested location. |
| 1282 | */ |
| 1283 | int[] findNearestArea( |
| 1284 | int pixelX, int pixelY, int spanX, int spanY, int[] result) { |
| 1285 | return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result); |
| 1286 | } |
| 1287 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1288 | boolean existsEmptyCell() { |
| 1289 | return findCellForSpan(null, 1, 1); |
| 1290 | } |
| 1291 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1292 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1293 | * Finds the upper-left coordinate of the first rectangle in the grid that can |
| 1294 | * hold a cell of the specified dimensions. If intersectX and intersectY are not -1, |
| 1295 | * then this method will only return coordinates for rectangles that contain the cell |
| 1296 | * (intersectX, intersectY) |
| 1297 | * |
| 1298 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 1299 | * can be found. |
| 1300 | * @param spanX The horizontal span of the cell we want to find. |
| 1301 | * @param spanY The vertical span of the cell we want to find. |
| 1302 | * |
| 1303 | * @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] | 1304 | */ |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1305 | boolean findCellForSpan(int[] cellXY, int spanX, int spanY) { |
| 1306 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null); |
| 1307 | } |
| 1308 | |
| 1309 | /** |
| 1310 | * Like above, but ignores any cells occupied by the item "ignoreView" |
| 1311 | * |
| 1312 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 1313 | * can be found. |
| 1314 | * @param spanX The horizontal span of the cell we want to find. |
| 1315 | * @param spanY The vertical span of the cell we want to find. |
| 1316 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 1317 | * @return |
| 1318 | */ |
| 1319 | boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) { |
| 1320 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView); |
| 1321 | } |
| 1322 | |
| 1323 | /** |
| 1324 | * Like above, but if intersectX and intersectY are not -1, then this method will try to |
| 1325 | * return coordinates for rectangles that contain the cell [intersectX, intersectY] |
| 1326 | * |
| 1327 | * @param spanX The horizontal span of the cell we want to find. |
| 1328 | * @param spanY The vertical span of the cell we want to find. |
| 1329 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 1330 | * @param intersectX The X coordinate of the cell that we should try to overlap |
| 1331 | * @param intersectX The Y coordinate of the cell that we should try to overlap |
| 1332 | * |
| 1333 | * @return True if a vacant cell of the specified dimension was found, false otherwise. |
| 1334 | */ |
| 1335 | boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY, |
| 1336 | int intersectX, int intersectY) { |
| 1337 | return findCellForSpanThatIntersectsIgnoring( |
| 1338 | cellXY, spanX, spanY, intersectX, intersectY, null); |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * The superset of the above two methods |
| 1343 | */ |
| 1344 | boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY, |
| 1345 | int intersectX, int intersectY, View ignoreView) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1346 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
| 1347 | markCellsAsUnoccupiedForView(ignoreView); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1348 | |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1349 | boolean foundCell = false; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1350 | while (true) { |
| 1351 | int startX = 0; |
| 1352 | if (intersectX >= 0) { |
| 1353 | startX = Math.max(startX, intersectX - (spanX - 1)); |
| 1354 | } |
| 1355 | int endX = mCountX - (spanX - 1); |
| 1356 | if (intersectX >= 0) { |
| 1357 | endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0)); |
| 1358 | } |
| 1359 | int startY = 0; |
| 1360 | if (intersectY >= 0) { |
| 1361 | startY = Math.max(startY, intersectY - (spanY - 1)); |
| 1362 | } |
| 1363 | int endY = mCountY - (spanY - 1); |
| 1364 | if (intersectY >= 0) { |
| 1365 | endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0)); |
| 1366 | } |
| 1367 | |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1368 | for (int y = startY; y < endY && !foundCell; y++) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1369 | inner: |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1370 | for (int x = startX; x < endX; x++) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1371 | for (int i = 0; i < spanX; i++) { |
| 1372 | for (int j = 0; j < spanY; j++) { |
| 1373 | if (mOccupied[x + i][y + j]) { |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1374 | // small optimization: we can skip to after the column we just found |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1375 | // an occupied cell |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 1376 | x += i; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1377 | continue inner; |
| 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | if (cellXY != null) { |
| 1382 | cellXY[0] = x; |
| 1383 | cellXY[1] = y; |
| 1384 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1385 | foundCell = true; |
| 1386 | break; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | if (intersectX == -1 && intersectY == -1) { |
| 1390 | break; |
| 1391 | } else { |
| 1392 | // if we failed to find anything, try again but without any requirements of |
| 1393 | // intersecting |
| 1394 | intersectX = -1; |
| 1395 | intersectY = -1; |
| 1396 | continue; |
| 1397 | } |
| 1398 | } |
| 1399 | |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1400 | // re-mark space taken by ignoreView as occupied |
| 1401 | markCellsAsOccupiedForView(ignoreView); |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1402 | return foundCell; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | /** |
| 1406 | * Called when drag has left this CellLayout or has been completed (successfully or not) |
| 1407 | */ |
| 1408 | void onDragExit() { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1409 | // This can actually be called when we aren't in a drag, e.g. when adding a new |
| 1410 | // item to this layout via the customize drawer. |
| 1411 | // Guard against that case. |
| 1412 | if (mDragging) { |
| 1413 | mDragging = false; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1414 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1415 | // Fade out the drag indicators |
| 1416 | if (mCrosshairsAnimator != null) { |
| 1417 | mCrosshairsAnimator.animateOut(); |
| 1418 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1419 | } |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1420 | |
| 1421 | // Invalidate the drag data |
| 1422 | mDragCell[0] = -1; |
| 1423 | mDragCell[1] = -1; |
| 1424 | mDragOutlineAnims[mDragOutlineCurrent].animateOut(); |
| 1425 | mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length; |
| 1426 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 1427 | setIsDragOverlapping(false); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1431 | * Mark a child as having been dropped. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1432 | * 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] | 1433 | * 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] | 1434 | * |
| 1435 | * @param child The child that is being dropped |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1436 | */ |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 1437 | void onDropChild(View child) { |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1438 | if (child != null) { |
| 1439 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1440 | lp.isDragging = false; |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 1441 | lp.dropped = true; |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1442 | child.requestLayout(); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1443 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1444 | } |
| 1445 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1446 | /** |
| 1447 | * Start dragging the specified child |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1448 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1449 | * @param child The child that is being dragged |
| 1450 | */ |
| 1451 | void onDragChild(View child) { |
| 1452 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1453 | lp.isDragging = true; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | /** |
| 1457 | * A drag event has begun over this layout. |
| 1458 | * It may have begun over this layout (in which case onDragChild is called first), |
| 1459 | * or it may have begun on another layout. |
| 1460 | */ |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1461 | void onDragEnter() { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 1462 | if (!mDragging) { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 1463 | // Fade in the drag indicators |
| 1464 | if (mCrosshairsAnimator != null) { |
| 1465 | mCrosshairsAnimator.animateIn(); |
| 1466 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1467 | } |
| 1468 | mDragging = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1469 | } |
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 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1472 | * Computes a bounding rectangle for a range of cells |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1473 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1474 | * @param cellX X coordinate of upper left corner expressed as a cell position |
| 1475 | * @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] | 1476 | * @param cellHSpan Width in cells |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1477 | * @param cellVSpan Height in cells |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1478 | * @param resultRect Rect into which to put the results |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1479 | */ |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1480 | 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] | 1481 | final int cellWidth = mCellWidth; |
| 1482 | final int cellHeight = mCellHeight; |
| 1483 | final int widthGap = mWidthGap; |
| 1484 | final int heightGap = mHeightGap; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1485 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 1486 | final int hStartPadding = getPaddingLeft(); |
| 1487 | final int vStartPadding = getPaddingTop(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1488 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1489 | int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap); |
| 1490 | int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap); |
| 1491 | |
| 1492 | int x = hStartPadding + cellX * (cellWidth + widthGap); |
| 1493 | int y = vStartPadding + cellY * (cellHeight + heightGap); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1494 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1495 | resultRect.set(x, y, x + width, y + height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1496 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1497 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1498 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1499 | * 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] | 1500 | * fit the given rectangle. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1501 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1502 | * @param width Width in pixels |
| 1503 | * @param height Height in pixels |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1504 | * @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] | 1505 | */ |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1506 | public int[] rectToCell(int width, int height, int[] result) { |
Michael Jurka | 9987a5c | 2010-10-08 16:58:12 -0700 | [diff] [blame] | 1507 | return rectToCell(getResources(), width, height, result); |
| 1508 | } |
| 1509 | |
| 1510 | 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] | 1511 | // Always assume we're working with the smallest span to make sure we |
| 1512 | // reserve enough space in both orientations. |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1513 | int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width); |
| 1514 | int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1515 | int smallerSize = Math.min(actualWidth, actualHeight); |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1516 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1517 | // Always round up to next largest cell |
| 1518 | int spanX = (width + smallerSize) / smallerSize; |
| 1519 | int spanY = (height + smallerSize) / smallerSize; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1520 | |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1521 | if (result == null) { |
| 1522 | return new int[] { spanX, spanY }; |
| 1523 | } |
| 1524 | result[0] = spanX; |
| 1525 | result[1] = spanY; |
| 1526 | return result; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1527 | } |
| 1528 | |
Michael Jurka | f12c75c | 2011-01-25 22:41:40 -0800 | [diff] [blame] | 1529 | public int[] cellSpansToSize(int hSpans, int vSpans) { |
| 1530 | int[] size = new int[2]; |
| 1531 | size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap; |
| 1532 | size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap; |
| 1533 | return size; |
| 1534 | } |
| 1535 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1536 | /** |
Patrick Dubroy | 047379a | 2010-12-19 22:02:04 -0800 | [diff] [blame] | 1537 | * Calculate the grid spans needed to fit given item |
| 1538 | */ |
| 1539 | public void calculateSpans(ItemInfo info) { |
| 1540 | final int minWidth; |
| 1541 | final int minHeight; |
| 1542 | |
| 1543 | if (info instanceof LauncherAppWidgetInfo) { |
| 1544 | minWidth = ((LauncherAppWidgetInfo) info).minWidth; |
| 1545 | minHeight = ((LauncherAppWidgetInfo) info).minHeight; |
| 1546 | } else if (info instanceof PendingAddWidgetInfo) { |
| 1547 | minWidth = ((PendingAddWidgetInfo) info).minWidth; |
| 1548 | minHeight = ((PendingAddWidgetInfo) info).minHeight; |
| 1549 | } else { |
| 1550 | // It's not a widget, so it must be 1x1 |
| 1551 | info.spanX = info.spanY = 1; |
| 1552 | return; |
| 1553 | } |
| 1554 | int[] spans = rectToCell(minWidth, minHeight, null); |
| 1555 | info.spanX = spans[0]; |
| 1556 | info.spanY = spans[1]; |
| 1557 | } |
| 1558 | |
| 1559 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1560 | * Find the first vacant cell, if there is one. |
| 1561 | * |
| 1562 | * @param vacant Holds the x and y coordinate of the vacant cell |
| 1563 | * @param spanX Horizontal cell span. |
| 1564 | * @param spanY Vertical cell span. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1565 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1566 | * @return True if a vacant cell was found |
| 1567 | */ |
| 1568 | public boolean getVacantCell(int[] vacant, int spanX, int spanY) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1569 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1570 | return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | static boolean findVacantCell(int[] vacant, int spanX, int spanY, |
| 1574 | int xCount, int yCount, boolean[][] occupied) { |
| 1575 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1576 | for (int y = 0; y < yCount; y++) { |
| 1577 | for (int x = 0; x < xCount; x++) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1578 | boolean available = !occupied[x][y]; |
| 1579 | out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) { |
| 1580 | for (int j = y; j < y + spanY - 1 && y < yCount; j++) { |
| 1581 | available = available && !occupied[i][j]; |
| 1582 | if (!available) break out; |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | if (available) { |
| 1587 | vacant[0] = x; |
| 1588 | vacant[1] = y; |
| 1589 | return true; |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | return false; |
| 1595 | } |
| 1596 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1597 | private void clearOccupiedCells() { |
| 1598 | for (int x = 0; x < mCountX; x++) { |
| 1599 | for (int y = 0; y < mCountY; y++) { |
| 1600 | mOccupied[x][y] = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1601 | } |
| 1602 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1603 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1604 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1605 | /** |
| 1606 | * Given a view, determines how much that view can be expanded in all directions, in terms of |
| 1607 | * whether or not there are other items occupying adjacent cells. Used by the |
| 1608 | * AppWidgetResizeFrame to determine how the widget can be resized. |
| 1609 | */ |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1610 | public void getExpandabilityArrayForView(View view, int[] expandability) { |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1611 | final LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1612 | boolean flag; |
| 1613 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1614 | expandability[AppWidgetResizeFrame.LEFT] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1615 | for (int x = lp.cellX - 1; x >= 0; x--) { |
| 1616 | flag = false; |
| 1617 | for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) { |
| 1618 | if (mOccupied[x][y]) flag = true; |
| 1619 | } |
| 1620 | if (flag) break; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1621 | expandability[AppWidgetResizeFrame.LEFT]++; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1622 | } |
| 1623 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1624 | expandability[AppWidgetResizeFrame.TOP] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1625 | for (int y = lp.cellY - 1; y >= 0; y--) { |
| 1626 | flag = false; |
| 1627 | for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) { |
| 1628 | if (mOccupied[x][y]) flag = true; |
| 1629 | } |
| 1630 | if (flag) break; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1631 | expandability[AppWidgetResizeFrame.TOP]++; |
| 1632 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1633 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1634 | expandability[AppWidgetResizeFrame.RIGHT] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1635 | for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) { |
| 1636 | flag = false; |
| 1637 | for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) { |
| 1638 | if (mOccupied[x][y]) flag = true; |
| 1639 | } |
| 1640 | if (flag) break; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1641 | expandability[AppWidgetResizeFrame.RIGHT]++; |
| 1642 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1643 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1644 | expandability[AppWidgetResizeFrame.BOTTOM] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1645 | for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) { |
| 1646 | flag = false; |
| 1647 | for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) { |
| 1648 | if (mOccupied[x][y]) flag = true; |
| 1649 | } |
| 1650 | if (flag) break; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1651 | expandability[AppWidgetResizeFrame.BOTTOM]++; |
| 1652 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1655 | public void onMove(View view, int newCellX, int newCellY) { |
| 1656 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1657 | markCellsAsUnoccupiedForView(view); |
| 1658 | markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true); |
| 1659 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1660 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1661 | public void markCellsAsOccupiedForView(View view) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 1662 | if (view == null || view.getParent() != mChildren) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1663 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1664 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true); |
| 1665 | } |
| 1666 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1667 | public void markCellsAsUnoccupiedForView(View view) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 1668 | if (view == null || view.getParent() != mChildren) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1669 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1670 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false); |
| 1671 | } |
| 1672 | |
| 1673 | private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) { |
| 1674 | for (int x = cellX; x < cellX + spanX && x < mCountX; x++) { |
| 1675 | for (int y = cellY; y < cellY + spanY && y < mCountY; y++) { |
| 1676 | mOccupied[x][y] = value; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1677 | } |
| 1678 | } |
| 1679 | } |
| 1680 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1681 | public int getDesiredWidth() { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 1682 | return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) + |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1683 | (Math.max((mCountX - 1), 0) * mWidthGap); |
| 1684 | } |
| 1685 | |
| 1686 | public int getDesiredHeight() { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 1687 | return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) + |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1688 | (Math.max((mCountY - 1), 0) * mHeightGap); |
| 1689 | } |
| 1690 | |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 1691 | public boolean isOccupied(int x, int y) { |
| 1692 | if (x < mCountX && y < mCountY) { |
| 1693 | return mOccupied[x][y]; |
| 1694 | } else { |
| 1695 | throw new RuntimeException("Position exceeds the bound of this CellLayout"); |
| 1696 | } |
| 1697 | } |
| 1698 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1699 | @Override |
| 1700 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 1701 | return new CellLayout.LayoutParams(getContext(), attrs); |
| 1702 | } |
| 1703 | |
| 1704 | @Override |
| 1705 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 1706 | return p instanceof CellLayout.LayoutParams; |
| 1707 | } |
| 1708 | |
| 1709 | @Override |
| 1710 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 1711 | return new CellLayout.LayoutParams(p); |
| 1712 | } |
| 1713 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1714 | public static class CellLayoutAnimationController extends LayoutAnimationController { |
| 1715 | public CellLayoutAnimationController(Animation animation, float delay) { |
| 1716 | super(animation, delay); |
| 1717 | } |
| 1718 | |
| 1719 | @Override |
| 1720 | protected long getDelayForView(View view) { |
| 1721 | return (int) (Math.random() * 150); |
| 1722 | } |
| 1723 | } |
| 1724 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1725 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
| 1726 | /** |
| 1727 | * Horizontal location of the item in the grid. |
| 1728 | */ |
| 1729 | @ViewDebug.ExportedProperty |
| 1730 | public int cellX; |
| 1731 | |
| 1732 | /** |
| 1733 | * Vertical location of the item in the grid. |
| 1734 | */ |
| 1735 | @ViewDebug.ExportedProperty |
| 1736 | public int cellY; |
| 1737 | |
| 1738 | /** |
| 1739 | * Number of cells spanned horizontally by the item. |
| 1740 | */ |
| 1741 | @ViewDebug.ExportedProperty |
| 1742 | public int cellHSpan; |
| 1743 | |
| 1744 | /** |
| 1745 | * Number of cells spanned vertically by the item. |
| 1746 | */ |
| 1747 | @ViewDebug.ExportedProperty |
| 1748 | public int cellVSpan; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1749 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1750 | /** |
| 1751 | * Indicates whether the item will set its x, y, width and height parameters freely, |
| 1752 | * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan. |
| 1753 | */ |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1754 | public boolean isLockedToGrid = true; |
| 1755 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1756 | /** |
| 1757 | * Is this item currently being dragged |
| 1758 | */ |
| 1759 | public boolean isDragging; |
| 1760 | |
| 1761 | // X coordinate of the view in the layout. |
| 1762 | @ViewDebug.ExportedProperty |
| 1763 | int x; |
| 1764 | // Y coordinate of the view in the layout. |
| 1765 | @ViewDebug.ExportedProperty |
| 1766 | int y; |
| 1767 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 1768 | boolean dropped; |
Romain Guy | fcb9e71 | 2009-10-02 16:06:52 -0700 | [diff] [blame] | 1769 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1770 | public LayoutParams(Context c, AttributeSet attrs) { |
| 1771 | super(c, attrs); |
| 1772 | cellHSpan = 1; |
| 1773 | cellVSpan = 1; |
| 1774 | } |
| 1775 | |
| 1776 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 1777 | super(source); |
| 1778 | cellHSpan = 1; |
| 1779 | cellVSpan = 1; |
| 1780 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1781 | |
| 1782 | public LayoutParams(LayoutParams source) { |
| 1783 | super(source); |
| 1784 | this.cellX = source.cellX; |
| 1785 | this.cellY = source.cellY; |
| 1786 | this.cellHSpan = source.cellHSpan; |
| 1787 | this.cellVSpan = source.cellVSpan; |
| 1788 | } |
| 1789 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1790 | public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) { |
Romain Guy | 8f19cdd | 2010-01-08 15:07:00 -0800 | [diff] [blame] | 1791 | super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1792 | this.cellX = cellX; |
| 1793 | this.cellY = cellY; |
| 1794 | this.cellHSpan = cellHSpan; |
| 1795 | this.cellVSpan = cellVSpan; |
| 1796 | } |
| 1797 | |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 1798 | public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1799 | if (isLockedToGrid) { |
| 1800 | final int myCellHSpan = cellHSpan; |
| 1801 | final int myCellVSpan = cellVSpan; |
| 1802 | final int myCellX = cellX; |
| 1803 | final int myCellY = cellY; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 1804 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1805 | width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) - |
| 1806 | leftMargin - rightMargin; |
| 1807 | height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) - |
| 1808 | topMargin - bottomMargin; |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 1809 | x = myCellX * (cellWidth + widthGap) + leftMargin; |
| 1810 | y = myCellY * (cellHeight + heightGap) + topMargin; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1811 | } |
| 1812 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1813 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1814 | public String toString() { |
| 1815 | return "(" + this.cellX + ", " + this.cellY + ")"; |
| 1816 | } |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 1817 | |
| 1818 | public void setWidth(int width) { |
| 1819 | this.width = width; |
| 1820 | } |
| 1821 | |
| 1822 | public int getWidth() { |
| 1823 | return width; |
| 1824 | } |
| 1825 | |
| 1826 | public void setHeight(int height) { |
| 1827 | this.height = height; |
| 1828 | } |
| 1829 | |
| 1830 | public int getHeight() { |
| 1831 | return height; |
| 1832 | } |
| 1833 | |
| 1834 | public void setX(int x) { |
| 1835 | this.x = x; |
| 1836 | } |
| 1837 | |
| 1838 | public int getX() { |
| 1839 | return x; |
| 1840 | } |
| 1841 | |
| 1842 | public void setY(int y) { |
| 1843 | this.y = y; |
| 1844 | } |
| 1845 | |
| 1846 | public int getY() { |
| 1847 | return y; |
| 1848 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1849 | } |
| 1850 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1851 | // This class stores info for two purposes: |
| 1852 | // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY, |
| 1853 | // its spanX, spanY, and the screen it is on |
| 1854 | // 2. When long clicking on an empty cell in a CellLayout, we save information about the |
| 1855 | // cellX and cellY coordinates and which page was clicked. We then set this as a tag on |
| 1856 | // the CellLayout that was long clicked |
Michael Jurka | e5fb0f2 | 2011-04-11 13:27:46 -0700 | [diff] [blame] | 1857 | static final class CellInfo { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1858 | View cell; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1859 | int cellX = -1; |
| 1860 | int cellY = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1861 | int spanX; |
| 1862 | int spanY; |
| 1863 | int screen; |
| 1864 | boolean valid; |
| 1865 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1866 | @Override |
| 1867 | public String toString() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1868 | return "Cell[view=" + (cell == null ? "null" : cell.getClass()) |
| 1869 | + ", x=" + cellX + ", y=" + cellY + "]"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1870 | } |
| 1871 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1872 | } |