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