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