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