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