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