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