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