The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 21 | import android.animation.ObjectAnimator; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 22 | import android.animation.PropertyValuesHolder; |
Chet Haase | 00397b1 | 2010-10-07 11:13:10 -0700 | [diff] [blame] | 23 | import android.animation.TimeInterpolator; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 24 | import android.animation.ValueAnimator; |
| 25 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | import android.content.Context; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 27 | import android.content.res.Resources; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 28 | import android.content.res.TypedArray; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 29 | import android.graphics.Bitmap; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 30 | import android.graphics.Canvas; |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 31 | import android.graphics.Color; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 32 | import android.graphics.Paint; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 33 | import android.graphics.Point; |
| 34 | import android.graphics.PointF; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 35 | import android.graphics.PorterDuff; |
| 36 | import android.graphics.PorterDuffXfermode; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 37 | import android.graphics.Rect; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 38 | import android.graphics.drawable.ColorDrawable; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 39 | import android.graphics.drawable.Drawable; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 40 | import android.graphics.drawable.NinePatchDrawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | import android.util.AttributeSet; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 42 | import android.util.Log; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | import android.view.MotionEvent; |
| 44 | import android.view.View; |
| 45 | import android.view.ViewDebug; |
| 46 | import android.view.ViewGroup; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 47 | import android.view.animation.Animation; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 48 | import android.view.animation.DecelerateInterpolator; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 49 | import android.view.animation.LayoutAnimationController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 50 | |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 51 | import com.android.launcher.R; |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 52 | import com.android.launcher2.FolderIcon.FolderRingAnimator; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 53 | |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 54 | import java.util.ArrayList; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 55 | import java.util.Arrays; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 56 | import java.util.HashMap; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 57 | import java.util.Stack; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 58 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 59 | public class CellLayout extends ViewGroup { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 60 | static final String TAG = "CellLayout"; |
| 61 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 62 | private int mOriginalCellWidth; |
| 63 | private int mOriginalCellHeight; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 64 | private int mCellWidth; |
| 65 | private int mCellHeight; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 66 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 67 | private int mCountX; |
| 68 | private int mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 69 | |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame] | 70 | private int mOriginalWidthGap; |
| 71 | private int mOriginalHeightGap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 72 | private int mWidthGap; |
| 73 | private int mHeightGap; |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 74 | private int mMaxGap; |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 75 | private boolean mScrollingTransformsDirty = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 76 | |
| 77 | private final Rect mRect = new Rect(); |
| 78 | private final CellInfo mCellInfo = new CellInfo(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 79 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 80 | // These are temporary variables to prevent having to allocate a new object just to |
| 81 | // 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] | 82 | private final int[] mTmpXY = new int[2]; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 83 | private final int[] mTmpPoint = new int[2]; |
| 84 | private final PointF mTmpPointF = new PointF(); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 85 | int[] mTempLocation = new int[2]; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 86 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 87 | boolean[][] mOccupied; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 88 | boolean[][] mTmpOccupied; |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 89 | private boolean mLastDownOnOccupiedCell = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 90 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 91 | private OnTouchListener mInterceptTouchListener; |
| 92 | |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 93 | private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>(); |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 94 | private int[] mFolderLeaveBehindCell = {-1, -1}; |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 95 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 96 | private int mForegroundAlpha = 0; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 97 | private float mBackgroundAlpha; |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 98 | private float mBackgroundAlphaMultiplier = 1.0f; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 99 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 100 | private Drawable mNormalBackground; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 101 | private Drawable mActiveGlowBackground; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 102 | private Drawable mOverScrollForegroundDrawable; |
| 103 | private Drawable mOverScrollLeft; |
| 104 | private Drawable mOverScrollRight; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 105 | private Rect mBackgroundRect; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 106 | private Rect mForegroundRect; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 107 | private int mForegroundPadding; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 108 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 109 | // If we're actively dragging something over this screen, mIsDragOverlapping is true |
| 110 | private boolean mIsDragOverlapping = false; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 111 | private final Point mDragCenter = new Point(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 112 | |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 113 | // 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] | 114 | // They are used as circular arrays, indexed by mDragOutlineCurrent. |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 115 | private Rect[] mDragOutlines = new Rect[4]; |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 116 | private float[] mDragOutlineAlphas = new float[mDragOutlines.length]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 117 | private InterruptibleInOutAnimator[] mDragOutlineAnims = |
| 118 | new InterruptibleInOutAnimator[mDragOutlines.length]; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 119 | |
| 120 | // 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] | 121 | private int mDragOutlineCurrent = 0; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 122 | private final Paint mDragOutlinePaint = new Paint(); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 123 | |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 124 | private BubbleTextView mPressedOrFocusedIcon; |
| 125 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 126 | private Drawable mCrosshairsDrawable = null; |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 127 | private InterruptibleInOutAnimator mCrosshairsAnimator = null; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 128 | private float mCrosshairsVisibility = 0.0f; |
| 129 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 130 | private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new |
| 131 | HashMap<CellLayout.LayoutParams, Animator>(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 132 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 133 | // When a drag operation is in progress, holds the nearest cell to the touch point |
| 134 | private final int[] mDragCell = new int[2]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 135 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 136 | private boolean mDragging = false; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 137 | private boolean mItemLocationsDirty = false; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 138 | |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 139 | private TimeInterpolator mEaseOutInterpolator; |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 140 | private ShortcutAndWidgetContainer mShortcutsAndWidgets; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 141 | |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 142 | private boolean mIsHotseat = false; |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 143 | private float mChildScale = 1f; |
| 144 | private float mHotseatChildScale = 1f; |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 145 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 146 | public static final int MODE_DRAG_OVER = 0; |
| 147 | public static final int MODE_ON_DROP = 1; |
| 148 | public static final int MODE_ON_DROP_EXTERNAL = 2; |
| 149 | public static final int MODE_ACCEPT_DROP = 3; |
| 150 | private static final boolean DESTRUCTIVE_REORDER = true; |
| 151 | private static final boolean DEBUG_VISUALIZE_OCCUPIED = false; |
| 152 | |
| 153 | private ArrayList<View> mIntersectingViews = new ArrayList<View>(); |
| 154 | private Rect mOccupiedRect = new Rect(); |
| 155 | private int[] mDirectionVector = new int[2]; |
| 156 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 157 | public CellLayout(Context context) { |
| 158 | this(context, null); |
| 159 | } |
| 160 | |
| 161 | public CellLayout(Context context, AttributeSet attrs) { |
| 162 | this(context, attrs, 0); |
| 163 | } |
| 164 | |
| 165 | public CellLayout(Context context, AttributeSet attrs, int defStyle) { |
| 166 | super(context, attrs, defStyle); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 167 | |
| 168 | // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show |
| 169 | // the user where a dragged item will land when dropped. |
| 170 | setWillNotDraw(false); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 171 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 172 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0); |
| 173 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 174 | mOriginalCellWidth = |
| 175 | mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10); |
| 176 | mOriginalCellHeight = |
| 177 | mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10); |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame] | 178 | mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0); |
| 179 | mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 180 | mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0); |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 181 | mCountX = LauncherModel.getCellCountX(); |
| 182 | mCountY = LauncherModel.getCellCountY(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 183 | mOccupied = new boolean[mCountX][mCountY]; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 184 | mTmpOccupied = new boolean[mCountX][mCountY]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 185 | |
| 186 | a.recycle(); |
| 187 | |
| 188 | setAlwaysDrawnWithCacheEnabled(false); |
| 189 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 190 | final Resources res = getResources(); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 191 | |
Winson Chung | 967289b | 2011-06-30 18:09:30 -0700 | [diff] [blame] | 192 | mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo); |
Winson Chung | dea74b7 | 2011-09-13 18:06:43 -0700 | [diff] [blame] | 193 | mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 194 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 195 | mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left); |
| 196 | mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right); |
| 197 | mForegroundPadding = |
| 198 | res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 199 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 200 | mNormalBackground.setFilterBitmap(true); |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 201 | mActiveGlowBackground.setFilterBitmap(true); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 202 | |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 203 | int iconScale = res.getInteger(R.integer.app_icon_scale_percent); |
| 204 | if (iconScale >= 0) { |
| 205 | mChildScale = iconScale / 100f; |
| 206 | } |
| 207 | int hotseatIconScale = res.getInteger(R.integer.app_icon_hotseat_scale_percent); |
| 208 | if (hotseatIconScale >= 0) { |
| 209 | mHotseatChildScale = hotseatIconScale / 100f; |
| 210 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 211 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 212 | // Initialize the data structures used for the drag visualization. |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 213 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 214 | mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 215 | mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 216 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 217 | // Set up the animation for fading the crosshairs in and out |
| 218 | int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime); |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 219 | mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 220 | mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() { |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 221 | public void onAnimationUpdate(ValueAnimator animation) { |
| 222 | mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue(); |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 223 | invalidate(); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 224 | } |
| 225 | }); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 226 | mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 227 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 228 | mDragCell[0] = mDragCell[1] = -1; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 229 | for (int i = 0; i < mDragOutlines.length; i++) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 230 | mDragOutlines[i] = new Rect(-1, -1, -1, -1); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // When dragging things around the home screens, we show a green outline of |
| 234 | // where the item will land. The outlines gradually fade out, leaving a trail |
| 235 | // behind the drag path. |
| 236 | // Set up all the animations that are used to implement this fading. |
| 237 | final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 238 | final float fromAlphaValue = 0; |
| 239 | final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 240 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 241 | Arrays.fill(mDragOutlineAlphas, fromAlphaValue); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 242 | |
| 243 | for (int i = 0; i < mDragOutlineAnims.length; i++) { |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 244 | final InterruptibleInOutAnimator anim = |
| 245 | new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 246 | anim.getAnimator().setInterpolator(mEaseOutInterpolator); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 247 | final int thisIndex = i; |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 248 | anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 249 | public void onAnimationUpdate(ValueAnimator animation) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 250 | final Bitmap outline = (Bitmap)anim.getTag(); |
| 251 | |
| 252 | // If an animation is started and then stopped very quickly, we can still |
| 253 | // get spurious updates we've cleared the tag. Guard against this. |
| 254 | if (outline == null) { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 255 | if (false) { |
| 256 | Object val = animation.getAnimatedValue(); |
| 257 | Log.d(TAG, "anim " + thisIndex + " update: " + val + |
| 258 | ", isStopped " + anim.isStopped()); |
| 259 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 260 | // Try to prevent it from continuing to run |
| 261 | animation.cancel(); |
| 262 | } else { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 263 | mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue(); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 264 | CellLayout.this.invalidate(mDragOutlines[thisIndex]); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 265 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 266 | } |
| 267 | }); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 268 | // The animation holds a reference to the drag outline bitmap as long is it's |
| 269 | // 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] | 270 | anim.getAnimator().addListener(new AnimatorListenerAdapter() { |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 271 | @Override |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 272 | public void onAnimationEnd(Animator animation) { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 273 | if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 274 | anim.setTag(null); |
| 275 | } |
| 276 | } |
| 277 | }); |
| 278 | mDragOutlineAnims[i] = anim; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 279 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 280 | |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 281 | mBackgroundRect = new Rect(); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 282 | mForegroundRect = new Rect(); |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 283 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 284 | mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context); |
| 285 | mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap); |
| 286 | addView(mShortcutsAndWidgets); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 289 | static int widthInPortrait(Resources r, int numCells) { |
| 290 | // We use this method from Workspace to figure out how many rows/columns Launcher should |
| 291 | // have. We ignore the left/right padding on CellLayout because it turns out in our design |
| 292 | // 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] | 293 | int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 294 | int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap), |
| 295 | r.getDimensionPixelSize(R.dimen.workspace_height_gap)); |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 296 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 297 | return minGap * (numCells - 1) + cellWidth * numCells; |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 300 | static int heightInLandscape(Resources r, int numCells) { |
| 301 | // We use this method from Workspace to figure out how many rows/columns Launcher should |
| 302 | // have. We ignore the left/right padding on CellLayout because it turns out in our design |
| 303 | // 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] | 304 | int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 305 | int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap), |
| 306 | r.getDimensionPixelSize(R.dimen.workspace_height_gap)); |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 307 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 308 | return minGap * (numCells - 1) + cellHeight * numCells; |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 311 | public void enableHardwareLayers() { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 312 | mShortcutsAndWidgets.enableHardwareLayers(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | public void setGridSize(int x, int y) { |
| 316 | mCountX = x; |
| 317 | mCountY = y; |
| 318 | mOccupied = new boolean[mCountX][mCountY]; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 319 | mTmpOccupied = new boolean[mCountX][mCountY]; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 320 | requestLayout(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 323 | private void invalidateBubbleTextView(BubbleTextView icon) { |
| 324 | final int padding = icon.getPressedOrFocusedBackgroundPadding(); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 325 | invalidate(icon.getLeft() + getPaddingLeft() - padding, |
| 326 | icon.getTop() + getPaddingTop() - padding, |
| 327 | icon.getRight() + getPaddingLeft() + padding, |
| 328 | icon.getBottom() + getPaddingTop() + padding); |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 331 | void setOverScrollAmount(float r, boolean left) { |
| 332 | if (left && mOverScrollForegroundDrawable != mOverScrollLeft) { |
| 333 | mOverScrollForegroundDrawable = mOverScrollLeft; |
| 334 | } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) { |
| 335 | mOverScrollForegroundDrawable = mOverScrollRight; |
| 336 | } |
| 337 | |
| 338 | mForegroundAlpha = (int) Math.round((r * 255)); |
| 339 | mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha); |
| 340 | invalidate(); |
| 341 | } |
| 342 | |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 343 | void setPressedOrFocusedIcon(BubbleTextView icon) { |
| 344 | // We draw the pressed or focused BubbleTextView's background in CellLayout because it |
| 345 | // requires an expanded clip rect (due to the glow's blur radius) |
| 346 | BubbleTextView oldIcon = mPressedOrFocusedIcon; |
| 347 | mPressedOrFocusedIcon = icon; |
| 348 | if (oldIcon != null) { |
| 349 | invalidateBubbleTextView(oldIcon); |
| 350 | } |
| 351 | if (mPressedOrFocusedIcon != null) { |
| 352 | invalidateBubbleTextView(mPressedOrFocusedIcon); |
| 353 | } |
| 354 | } |
| 355 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 356 | void setIsDragOverlapping(boolean isDragOverlapping) { |
| 357 | if (mIsDragOverlapping != isDragOverlapping) { |
| 358 | mIsDragOverlapping = isDragOverlapping; |
| 359 | invalidate(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | boolean getIsDragOverlapping() { |
| 364 | return mIsDragOverlapping; |
| 365 | } |
| 366 | |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 367 | protected void setOverscrollTransformsDirty(boolean dirty) { |
| 368 | mScrollingTransformsDirty = dirty; |
| 369 | } |
| 370 | |
| 371 | protected void resetOverscrollTransforms() { |
| 372 | if (mScrollingTransformsDirty) { |
| 373 | setOverscrollTransformsDirty(false); |
| 374 | setTranslationX(0); |
| 375 | setRotationY(0); |
| 376 | // It doesn't matter if we pass true or false here, the important thing is that we |
| 377 | // pass 0, which results in the overscroll drawable not being drawn any more. |
| 378 | setOverScrollAmount(0, false); |
| 379 | setPivotX(getMeasuredWidth() / 2); |
| 380 | setPivotY(getMeasuredHeight() / 2); |
| 381 | } |
| 382 | } |
| 383 | |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 384 | @Override |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 385 | protected void onDraw(Canvas canvas) { |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 386 | // When we're large, we are either drawn in a "hover" state (ie when dragging an item to |
| 387 | // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f) |
| 388 | // When we're small, we are either drawn normally or in the "accepts drops" state (during |
| 389 | // a drag). However, we also drag the mini hover background *over* one of those two |
| 390 | // backgrounds |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 391 | if (mBackgroundAlpha > 0.0f) { |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 392 | Drawable bg; |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 393 | |
| 394 | if (mIsDragOverlapping) { |
| 395 | // 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] | 396 | bg = mActiveGlowBackground; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 397 | } else { |
Michael Jurka | bdf7855 | 2011-10-31 14:34:25 -0700 | [diff] [blame] | 398 | bg = mNormalBackground; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 399 | } |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 400 | |
| 401 | bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255)); |
| 402 | bg.setBounds(mBackgroundRect); |
| 403 | bg.draw(canvas); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 404 | } |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 405 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 406 | if (mCrosshairsVisibility > 0.0f) { |
| 407 | final int countX = mCountX; |
| 408 | final int countY = mCountY; |
| 409 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 410 | final float MAX_ALPHA = 0.4f; |
| 411 | final int MAX_VISIBLE_DISTANCE = 600; |
| 412 | final float DISTANCE_MULTIPLIER = 0.002f; |
| 413 | |
| 414 | final Drawable d = mCrosshairsDrawable; |
| 415 | final int width = d.getIntrinsicWidth(); |
| 416 | final int height = d.getIntrinsicHeight(); |
| 417 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 418 | int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 419 | for (int col = 0; col <= countX; col++) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 420 | int y = getPaddingTop() - (mHeightGap / 2) - (height / 2); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 421 | for (int row = 0; row <= countY; row++) { |
| 422 | mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y); |
| 423 | float dist = mTmpPointF.length(); |
| 424 | // Crosshairs further from the drag point are more faint |
| 425 | float alpha = Math.min(MAX_ALPHA, |
| 426 | DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist)); |
| 427 | if (alpha > 0.0f) { |
| 428 | d.setBounds(x, y, x + width, y + height); |
| 429 | d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility)); |
| 430 | d.draw(canvas); |
| 431 | } |
| 432 | y += mCellHeight + mHeightGap; |
| 433 | } |
| 434 | x += mCellWidth + mWidthGap; |
| 435 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 436 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 437 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 438 | final Paint paint = mDragOutlinePaint; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 439 | for (int i = 0; i < mDragOutlines.length; i++) { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 440 | final float alpha = mDragOutlineAlphas[i]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 441 | if (alpha > 0) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 442 | final Rect r = mDragOutlines[i]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 443 | final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag(); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 444 | paint.setAlpha((int)(alpha + .5f)); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 445 | canvas.drawBitmap(b, null, r, paint); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 446 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 447 | } |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 448 | |
| 449 | // We draw the pressed or focused BubbleTextView's background in CellLayout because it |
| 450 | // requires an expanded clip rect (due to the glow's blur radius) |
| 451 | if (mPressedOrFocusedIcon != null) { |
| 452 | final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding(); |
| 453 | final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground(); |
| 454 | if (b != null) { |
| 455 | canvas.drawBitmap(b, |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 456 | mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding, |
| 457 | mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding, |
Patrick Dubroy | 96864c3 | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 458 | null); |
| 459 | } |
| 460 | } |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 461 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 462 | if (DEBUG_VISUALIZE_OCCUPIED) { |
| 463 | int[] pt = new int[2]; |
| 464 | ColorDrawable cd = new ColorDrawable(Color.RED); |
| 465 | cd.setBounds(0, 0, 80, 80); |
| 466 | for (int i = 0; i < mCountX; i++) { |
| 467 | for (int j = 0; j < mCountY; j++) { |
| 468 | if (mOccupied[i][j]) { |
| 469 | cellToPoint(i, j, pt); |
| 470 | canvas.save(); |
| 471 | canvas.translate(pt[0], pt[1]); |
| 472 | cd.draw(canvas); |
| 473 | canvas.restore(); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 479 | // The folder outer / inner ring image(s) |
| 480 | for (int i = 0; i < mFolderOuterRings.size(); i++) { |
| 481 | FolderRingAnimator fra = mFolderOuterRings.get(i); |
| 482 | |
| 483 | // Draw outer ring |
| 484 | Drawable d = FolderRingAnimator.sSharedOuterRingDrawable; |
| 485 | int width = (int) fra.getOuterRingSize(); |
| 486 | int height = width; |
| 487 | cellToPoint(fra.mCellX, fra.mCellY, mTempLocation); |
| 488 | |
| 489 | int centerX = mTempLocation[0] + mCellWidth / 2; |
| 490 | int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2; |
| 491 | |
| 492 | canvas.save(); |
| 493 | canvas.translate(centerX - width / 2, centerY - height / 2); |
| 494 | d.setBounds(0, 0, width, height); |
| 495 | d.draw(canvas); |
| 496 | canvas.restore(); |
| 497 | |
| 498 | // Draw inner ring |
| 499 | d = FolderRingAnimator.sSharedInnerRingDrawable; |
| 500 | width = (int) fra.getInnerRingSize(); |
| 501 | height = width; |
| 502 | cellToPoint(fra.mCellX, fra.mCellY, mTempLocation); |
| 503 | |
| 504 | centerX = mTempLocation[0] + mCellWidth / 2; |
| 505 | centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2; |
| 506 | canvas.save(); |
| 507 | canvas.translate(centerX - width / 2, centerY - width / 2); |
| 508 | d.setBounds(0, 0, width, height); |
| 509 | d.draw(canvas); |
| 510 | canvas.restore(); |
| 511 | } |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 512 | |
| 513 | if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) { |
| 514 | Drawable d = FolderIcon.sSharedFolderLeaveBehind; |
| 515 | int width = d.getIntrinsicWidth(); |
| 516 | int height = d.getIntrinsicHeight(); |
| 517 | |
| 518 | cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation); |
| 519 | int centerX = mTempLocation[0] + mCellWidth / 2; |
| 520 | int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2; |
| 521 | |
| 522 | canvas.save(); |
| 523 | canvas.translate(centerX - width / 2, centerY - width / 2); |
| 524 | d.setBounds(0, 0, width, height); |
| 525 | d.draw(canvas); |
| 526 | canvas.restore(); |
| 527 | } |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 530 | @Override |
| 531 | protected void dispatchDraw(Canvas canvas) { |
| 532 | super.dispatchDraw(canvas); |
| 533 | if (mForegroundAlpha > 0) { |
| 534 | mOverScrollForegroundDrawable.setBounds(mForegroundRect); |
| 535 | Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint(); |
| 536 | p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD)); |
| 537 | mOverScrollForegroundDrawable.draw(canvas); |
| 538 | p.setXfermode(null); |
| 539 | } |
| 540 | } |
| 541 | |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 542 | public void showFolderAccept(FolderRingAnimator fra) { |
| 543 | mFolderOuterRings.add(fra); |
| 544 | } |
| 545 | |
| 546 | public void hideFolderAccept(FolderRingAnimator fra) { |
| 547 | if (mFolderOuterRings.contains(fra)) { |
| 548 | mFolderOuterRings.remove(fra); |
| 549 | } |
| 550 | invalidate(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 553 | public void setFolderLeaveBehindCell(int x, int y) { |
| 554 | mFolderLeaveBehindCell[0] = x; |
| 555 | mFolderLeaveBehindCell[1] = y; |
| 556 | invalidate(); |
| 557 | } |
| 558 | |
| 559 | public void clearFolderLeaveBehind() { |
| 560 | mFolderLeaveBehindCell[0] = -1; |
| 561 | mFolderLeaveBehindCell[1] = -1; |
| 562 | invalidate(); |
| 563 | } |
| 564 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 565 | @Override |
Michael Jurka | e6235dd | 2011-10-04 15:02:05 -0700 | [diff] [blame] | 566 | public boolean shouldDelayChildPressedState() { |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | @Override |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 571 | public void cancelLongPress() { |
| 572 | super.cancelLongPress(); |
| 573 | |
| 574 | // Cancel long press for all children |
| 575 | final int count = getChildCount(); |
| 576 | for (int i = 0; i < count; i++) { |
| 577 | final View child = getChildAt(i); |
| 578 | child.cancelLongPress(); |
| 579 | } |
| 580 | } |
| 581 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 582 | public void setOnInterceptTouchListener(View.OnTouchListener listener) { |
| 583 | mInterceptTouchListener = listener; |
| 584 | } |
| 585 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 586 | int getCountX() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 587 | return mCountX; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | int getCountY() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 591 | return mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 592 | } |
| 593 | |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 594 | public void setIsHotseat(boolean isHotseat) { |
| 595 | mIsHotseat = isHotseat; |
| 596 | } |
| 597 | |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 598 | public float getChildrenScale() { |
| 599 | return mIsHotseat ? mHotseatChildScale : mChildScale; |
| 600 | } |
| 601 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 602 | public boolean addViewToCellLayout( |
| 603 | View child, int index, int childId, LayoutParams params, boolean markCells) { |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 604 | return addViewToCellLayout(child, index, childId, params, markCells, false); |
| 605 | } |
| 606 | |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 607 | private void scaleChild(BubbleTextView bubbleChild, float pivot, float scale) { |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 608 | // If we haven't measured the child yet, do it now |
| 609 | // (this happens if we're being dropped from all-apps |
| 610 | if (bubbleChild.getLayoutParams() instanceof LayoutParams && |
| 611 | (bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 612 | getShortcutsAndWidgets().measureChild(bubbleChild); |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 613 | } |
| 614 | int measuredWidth = bubbleChild.getMeasuredWidth(); |
| 615 | int measuredHeight = bubbleChild.getMeasuredHeight(); |
| 616 | |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 617 | bubbleChild.setScaleX(scale); |
| 618 | bubbleChild.setScaleY(scale); |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | private void resetChild(BubbleTextView bubbleChild) { |
| 622 | bubbleChild.setScaleX(1f); |
| 623 | bubbleChild.setScaleY(1f); |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 624 | |
| 625 | bubbleChild.setTextColor(getResources().getColor(R.color.workspace_icon_text_color)); |
| 626 | } |
| 627 | |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 628 | public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params, |
| 629 | boolean markCells, boolean allApps) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 630 | final LayoutParams lp = params; |
| 631 | |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 632 | // Hotseat icons - scale down and remove text |
| 633 | // Don't scale the all apps button |
| 634 | // scale percent set to -1 means do not scale |
| 635 | // Only scale BubbleTextViews |
| 636 | if (child instanceof BubbleTextView) { |
| 637 | BubbleTextView bubbleChild = (BubbleTextView) child; |
| 638 | |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 639 | // Start the child with 100% scale and visible text |
| 640 | resetChild(bubbleChild); |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 641 | |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 642 | if (mIsHotseat && !allApps && mHotseatChildScale >= 0) { |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 643 | // Scale/make transparent for a hotseat |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 644 | scaleChild(bubbleChild, 0f, mHotseatChildScale); |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 645 | |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 646 | bubbleChild.setTextColor(getResources().getColor(android.R.color.transparent)); |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 647 | } else if (mChildScale >= 0) { |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 648 | // Else possibly still scale it if we need to for smaller icons |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 649 | scaleChild(bubbleChild, 0f, mChildScale); |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 653 | // Generate an id for each view, this assumes we have at most 256x256 cells |
| 654 | // per workspace screen |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 655 | 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] | 656 | // If the horizontal or vertical span is set to -1, it is taken to |
| 657 | // mean that it spans the extent of the CellLayout |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 658 | if (lp.cellHSpan < 0) lp.cellHSpan = mCountX; |
| 659 | if (lp.cellVSpan < 0) lp.cellVSpan = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 660 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 661 | child.setId(childId); |
| 662 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 663 | mShortcutsAndWidgets.addView(child, index, lp); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 664 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 665 | if (markCells) markCellsAsOccupiedForView(child); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 666 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 667 | return true; |
| 668 | } |
| 669 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 670 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 671 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 672 | @Override |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 673 | public void removeAllViews() { |
| 674 | clearOccupiedCells(); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 675 | mShortcutsAndWidgets.removeAllViews(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | @Override |
| 679 | public void removeAllViewsInLayout() { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 680 | if (mShortcutsAndWidgets.getChildCount() > 0) { |
Michael Jurka | 7cfc282 | 2011-08-02 20:19:24 -0700 | [diff] [blame] | 681 | clearOccupiedCells(); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 682 | mShortcutsAndWidgets.removeAllViewsInLayout(); |
Michael Jurka | 7cfc282 | 2011-08-02 20:19:24 -0700 | [diff] [blame] | 683 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 686 | public void removeViewWithoutMarkingCells(View view) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 687 | mShortcutsAndWidgets.removeView(view); |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 690 | @Override |
| 691 | public void removeView(View view) { |
| 692 | markCellsAsUnoccupiedForView(view); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 693 | mShortcutsAndWidgets.removeView(view); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | @Override |
| 697 | public void removeViewAt(int index) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 698 | markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index)); |
| 699 | mShortcutsAndWidgets.removeViewAt(index); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | @Override |
| 703 | public void removeViewInLayout(View view) { |
| 704 | markCellsAsUnoccupiedForView(view); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 705 | mShortcutsAndWidgets.removeViewInLayout(view); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | @Override |
| 709 | public void removeViews(int start, int count) { |
| 710 | for (int i = start; i < start + count; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 711 | markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i)); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 712 | } |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 713 | mShortcutsAndWidgets.removeViews(start, count); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | @Override |
| 717 | public void removeViewsInLayout(int start, int count) { |
| 718 | for (int i = start; i < start + count; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 719 | markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i)); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 720 | } |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 721 | mShortcutsAndWidgets.removeViewsInLayout(start, count); |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 722 | } |
| 723 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 724 | @Override |
| 725 | protected void onAttachedToWindow() { |
| 726 | super.onAttachedToWindow(); |
| 727 | mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this); |
| 728 | } |
| 729 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 730 | public void setTagToCellInfoForPoint(int touchX, int touchY) { |
| 731 | final CellInfo cellInfo = mCellInfo; |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 732 | Rect frame = mRect; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 733 | final int x = touchX + mScrollX; |
| 734 | final int y = touchY + mScrollY; |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 735 | final int count = mShortcutsAndWidgets.getChildCount(); |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 736 | |
| 737 | boolean found = false; |
| 738 | for (int i = count - 1; i >= 0; i--) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 739 | final View child = mShortcutsAndWidgets.getChildAt(i); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 740 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 741 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 742 | if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) && |
| 743 | lp.isLockedToGrid) { |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 744 | child.getHitRect(frame); |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 745 | |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 746 | float scale = child.getScaleX(); |
| 747 | frame = new Rect(child.getLeft(), child.getTop(), child.getRight(), |
| 748 | child.getBottom()); |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 749 | // The child hit rect is relative to the CellLayoutChildren parent, so we need to |
| 750 | // offset that by this CellLayout's padding to test an (x,y) point that is relative |
| 751 | // to this view. |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 752 | frame.offset(mPaddingLeft, mPaddingTop); |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 753 | frame.inset((int) (frame.width() * (1f - scale) / 2), |
| 754 | (int) (frame.height() * (1f - scale) / 2)); |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 755 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 756 | if (frame.contains(x, y)) { |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 757 | cellInfo.cell = child; |
| 758 | cellInfo.cellX = lp.cellX; |
| 759 | cellInfo.cellY = lp.cellY; |
| 760 | cellInfo.spanX = lp.cellHSpan; |
| 761 | cellInfo.spanY = lp.cellVSpan; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 762 | found = true; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 763 | break; |
| 764 | } |
| 765 | } |
| 766 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 767 | |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 768 | mLastDownOnOccupiedCell = found; |
| 769 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 770 | if (!found) { |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 771 | final int cellXY[] = mTmpXY; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 772 | pointToCellExact(x, y, cellXY); |
| 773 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 774 | cellInfo.cell = null; |
| 775 | cellInfo.cellX = cellXY[0]; |
| 776 | cellInfo.cellY = cellXY[1]; |
| 777 | cellInfo.spanX = 1; |
| 778 | cellInfo.spanY = 1; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 779 | } |
| 780 | setTag(cellInfo); |
| 781 | } |
| 782 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 783 | @Override |
| 784 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Adam Cohen | c1997fd | 2011-08-15 18:26:39 -0700 | [diff] [blame] | 785 | // First we clear the tag to ensure that on every touch down we start with a fresh slate, |
| 786 | // even in the case where we return early. Not clearing here was causing bugs whereby on |
| 787 | // long-press we'd end up picking up an item from a previous drag operation. |
| 788 | final int action = ev.getAction(); |
| 789 | |
| 790 | if (action == MotionEvent.ACTION_DOWN) { |
| 791 | clearTagCellInfo(); |
| 792 | } |
| 793 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 794 | if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) { |
| 795 | return true; |
| 796 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 797 | |
| 798 | if (action == MotionEvent.ACTION_DOWN) { |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 799 | setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 800 | } |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 801 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 802 | return false; |
| 803 | } |
| 804 | |
Adam Cohen | c1997fd | 2011-08-15 18:26:39 -0700 | [diff] [blame] | 805 | private void clearTagCellInfo() { |
| 806 | final CellInfo cellInfo = mCellInfo; |
| 807 | cellInfo.cell = null; |
| 808 | cellInfo.cellX = -1; |
| 809 | cellInfo.cellY = -1; |
| 810 | cellInfo.spanX = 0; |
| 811 | cellInfo.spanY = 0; |
| 812 | setTag(cellInfo); |
| 813 | } |
| 814 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 815 | public CellInfo getTag() { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 816 | return (CellInfo) super.getTag(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 817 | } |
| 818 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 819 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 820 | * 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] | 821 | * @param x X coordinate of the point |
| 822 | * @param y Y coordinate of the point |
| 823 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 824 | */ |
| 825 | void pointToCellExact(int x, int y, int[] result) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 826 | final int hStartPadding = getPaddingLeft(); |
| 827 | final int vStartPadding = getPaddingTop(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 828 | |
| 829 | result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap); |
| 830 | result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap); |
| 831 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 832 | final int xAxis = mCountX; |
| 833 | final int yAxis = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 834 | |
| 835 | if (result[0] < 0) result[0] = 0; |
| 836 | if (result[0] >= xAxis) result[0] = xAxis - 1; |
| 837 | if (result[1] < 0) result[1] = 0; |
| 838 | if (result[1] >= yAxis) result[1] = yAxis - 1; |
| 839 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 840 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 841 | /** |
| 842 | * Given a point, return the cell that most closely encloses that point |
| 843 | * @param x X coordinate of the point |
| 844 | * @param y Y coordinate of the point |
| 845 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 846 | */ |
| 847 | void pointToCellRounded(int x, int y, int[] result) { |
| 848 | pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result); |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * 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] | 853 | * |
| 854 | * @param cellX X coordinate of the cell |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 855 | * @param cellY Y coordinate of the cell |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 856 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 857 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 858 | */ |
| 859 | void cellToPoint(int cellX, int cellY, int[] result) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 860 | final int hStartPadding = getPaddingLeft(); |
| 861 | final int vStartPadding = getPaddingTop(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 862 | |
| 863 | result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap); |
| 864 | result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap); |
| 865 | } |
| 866 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 867 | /** |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 868 | * Given a cell coordinate, return the point that represents the center of the cell |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 869 | * |
| 870 | * @param cellX X coordinate of the cell |
| 871 | * @param cellY Y coordinate of the cell |
| 872 | * |
| 873 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 874 | */ |
| 875 | void cellToCenterPoint(int cellX, int cellY, int[] result) { |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 876 | regionToCenterPoint(cellX, cellY, 1, 1, result); |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * Given a cell coordinate and span return the point that represents the center of the regio |
| 881 | * |
| 882 | * @param cellX X coordinate of the cell |
| 883 | * @param cellY Y coordinate of the cell |
| 884 | * |
| 885 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 886 | */ |
| 887 | void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 888 | final int hStartPadding = getPaddingLeft(); |
| 889 | final int vStartPadding = getPaddingTop(); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 890 | |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 891 | result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + |
| 892 | (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2; |
| 893 | result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + |
| 894 | (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 897 | public float getDistanceFromCell(float x, float y, int[] cell) { |
| 898 | cellToCenterPoint(cell[0], cell[1], mTmpPoint); |
| 899 | float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) + |
| 900 | Math.pow(y - mTmpPoint[1], 2)); |
| 901 | return distance; |
| 902 | } |
| 903 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 904 | int getCellWidth() { |
| 905 | return mCellWidth; |
| 906 | } |
| 907 | |
| 908 | int getCellHeight() { |
| 909 | return mCellHeight; |
| 910 | } |
| 911 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 912 | int getWidthGap() { |
| 913 | return mWidthGap; |
| 914 | } |
| 915 | |
| 916 | int getHeightGap() { |
| 917 | return mHeightGap; |
| 918 | } |
| 919 | |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 920 | Rect getContentRect(Rect r) { |
| 921 | if (r == null) { |
| 922 | r = new Rect(); |
| 923 | } |
| 924 | int left = getPaddingLeft(); |
| 925 | int top = getPaddingTop(); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 926 | int right = left + getWidth() - mPaddingLeft - mPaddingRight; |
| 927 | int bottom = top + getHeight() - mPaddingTop - mPaddingBottom; |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 928 | r.set(left, top, right, bottom); |
| 929 | return r; |
| 930 | } |
| 931 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 932 | @Override |
| 933 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 934 | // TODO: currently ignoring padding |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 935 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 936 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 937 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 938 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 939 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 940 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 941 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 942 | if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { |
| 943 | throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); |
| 944 | } |
| 945 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 946 | int numWidthGaps = mCountX - 1; |
| 947 | int numHeightGaps = mCountY - 1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 948 | |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame] | 949 | if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 950 | int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight; |
| 951 | int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom; |
| 952 | int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth); |
| 953 | int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight); |
| 954 | mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0); |
| 955 | mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 956 | mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap); |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame] | 957 | } else { |
| 958 | mWidthGap = mOriginalWidthGap; |
| 959 | mHeightGap = mOriginalHeightGap; |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 960 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 961 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 962 | // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY |
| 963 | int newWidth = widthSpecSize; |
| 964 | int newHeight = heightSpecSize; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 965 | if (widthSpecMode == MeasureSpec.AT_MOST) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 966 | newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) + |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 967 | ((mCountX - 1) * mWidthGap); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 968 | newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) + |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 969 | ((mCountY - 1) * mHeightGap); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 970 | setMeasuredDimension(newWidth, newHeight); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 971 | } |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 972 | |
| 973 | int count = getChildCount(); |
| 974 | for (int i = 0; i < count; i++) { |
| 975 | View child = getChildAt(i); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 976 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft - |
| 977 | mPaddingRight, MeasureSpec.EXACTLY); |
| 978 | int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop - |
| 979 | mPaddingBottom, MeasureSpec.EXACTLY); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 980 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 981 | } |
| 982 | setMeasuredDimension(newWidth, newHeight); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 986 | 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] | 987 | int count = getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 988 | for (int i = 0; i < count; i++) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 989 | View child = getChildAt(i); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 990 | child.layout(mPaddingLeft, mPaddingTop, |
| 991 | r - l - mPaddingRight, b - t - mPaddingBottom); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
| 995 | @Override |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 996 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 997 | super.onSizeChanged(w, h, oldw, oldh); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 998 | mBackgroundRect.set(0, 0, w, h); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 999 | mForegroundRect.set(mForegroundPadding, mForegroundPadding, |
| 1000 | w - 2 * mForegroundPadding, h - 2 * mForegroundPadding); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1004 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1005 | mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | @Override |
| 1009 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1010 | mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1011 | } |
| 1012 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1013 | public float getBackgroundAlpha() { |
| 1014 | return mBackgroundAlpha; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 1017 | public void setBackgroundAlphaMultiplier(float multiplier) { |
| 1018 | mBackgroundAlphaMultiplier = multiplier; |
| 1019 | } |
| 1020 | |
Adam Cohen | ddb8219 | 2010-11-10 16:32:54 -0800 | [diff] [blame] | 1021 | public float getBackgroundAlphaMultiplier() { |
| 1022 | return mBackgroundAlphaMultiplier; |
| 1023 | } |
| 1024 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1025 | public void setBackgroundAlpha(float alpha) { |
Michael Jurka | afaa050 | 2011-12-13 18:22:50 -0800 | [diff] [blame] | 1026 | if (mBackgroundAlpha != alpha) { |
| 1027 | mBackgroundAlpha = alpha; |
| 1028 | invalidate(); |
| 1029 | } |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1032 | public void setShortcutAndWidgetAlpha(float alpha) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1033 | final int childCount = getChildCount(); |
| 1034 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1035 | getChildAt(i).setAlpha(alpha); |
| 1036 | } |
| 1037 | } |
| 1038 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1039 | public ShortcutAndWidgetContainer getShortcutsAndWidgets() { |
| 1040 | if (getChildCount() > 0) { |
| 1041 | return (ShortcutAndWidgetContainer) getChildAt(0); |
| 1042 | } |
| 1043 | return null; |
| 1044 | } |
| 1045 | |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 1046 | public View getChildAt(int x, int y) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1047 | return mShortcutsAndWidgets.getChildAt(x, y); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1050 | public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1051 | int delay, boolean permanent, boolean adjustOccupied) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1052 | ShortcutAndWidgetContainer clc = getShortcutsAndWidgets(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1053 | boolean[][] occupied = mOccupied; |
| 1054 | if (!permanent) { |
| 1055 | occupied = mTmpOccupied; |
| 1056 | } |
| 1057 | |
| 1058 | if (clc.indexOfChild(child) != -1 && !occupied[cellX][cellY]) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1059 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1060 | final ItemInfo info = (ItemInfo) child.getTag(); |
| 1061 | |
| 1062 | // We cancel any existing animations |
| 1063 | if (mReorderAnimators.containsKey(lp)) { |
| 1064 | mReorderAnimators.get(lp).cancel(); |
| 1065 | mReorderAnimators.remove(lp); |
| 1066 | } |
| 1067 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1068 | final int oldX = lp.x; |
| 1069 | final int oldY = lp.y; |
| 1070 | if (adjustOccupied) { |
| 1071 | occupied[lp.cellX][lp.cellY] = false; |
| 1072 | occupied[cellX][cellY] = true; |
| 1073 | } |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1074 | lp.isLockedToGrid = true; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1075 | if (permanent) { |
| 1076 | lp.cellX = info.cellX = cellX; |
| 1077 | lp.cellY = info.cellY = cellY; |
| 1078 | } else { |
| 1079 | lp.tmpCellX = cellX; |
| 1080 | lp.tmpCellY = cellY; |
| 1081 | } |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1082 | clc.setupLp(lp); |
| 1083 | lp.isLockedToGrid = false; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1084 | final int newX = lp.x; |
| 1085 | final int newY = lp.y; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1086 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1087 | lp.x = oldX; |
| 1088 | lp.y = oldY; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1089 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1090 | // Exit early if we're not actually moving the view |
| 1091 | if (oldX == newX && oldY == newY) { |
| 1092 | lp.isLockedToGrid = true; |
| 1093 | return true; |
| 1094 | } |
| 1095 | |
| 1096 | ValueAnimator va = ValueAnimator.ofFloat(0f, 1f); |
| 1097 | va.setDuration(duration); |
| 1098 | mReorderAnimators.put(lp, va); |
| 1099 | |
| 1100 | va.addUpdateListener(new AnimatorUpdateListener() { |
| 1101 | @Override |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1102 | public void onAnimationUpdate(ValueAnimator animation) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1103 | float r = ((Float) animation.getAnimatedValue()).floatValue(); |
Adam Cohen | 6b8a02d | 2012-03-22 15:13:40 -0700 | [diff] [blame] | 1104 | lp.x = (int) (r * newX + (1 - r) * oldX); |
| 1105 | lp.y = (int) (r * newY + (1 - r) * oldY); |
| 1106 | child.requestLayout(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1107 | } |
| 1108 | }); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1109 | va.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1110 | boolean cancelled = false; |
| 1111 | public void onAnimationEnd(Animator animation) { |
| 1112 | // If the animation was cancelled, it means that another animation |
| 1113 | // has interrupted this one, and we don't want to lock the item into |
| 1114 | // place just yet. |
| 1115 | if (!cancelled) { |
| 1116 | lp.isLockedToGrid = true; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1117 | child.requestLayout(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1118 | } |
| 1119 | if (mReorderAnimators.containsKey(lp)) { |
| 1120 | mReorderAnimators.remove(lp); |
| 1121 | } |
| 1122 | } |
| 1123 | public void onAnimationCancel(Animator animation) { |
| 1124 | cancelled = true; |
| 1125 | } |
| 1126 | }); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1127 | va.setStartDelay(delay); |
| 1128 | va.start(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1129 | return true; |
| 1130 | } |
| 1131 | return false; |
| 1132 | } |
| 1133 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1134 | /** |
| 1135 | * Estimate where the top left cell of the dragged item will land if it is dropped. |
| 1136 | * |
| 1137 | * @param originX The X value of the top left corner of the item |
| 1138 | * @param originY The Y value of the top left corner of the item |
| 1139 | * @param spanX The number of horizontal cells that the item spans |
| 1140 | * @param spanY The number of vertical cells that the item spans |
| 1141 | * @param result The estimated drop cell X and Y. |
| 1142 | */ |
| 1143 | void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 1144 | final int countX = mCountX; |
| 1145 | final int countY = mCountY; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1146 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1147 | // pointToCellRounded takes the top left of a cell but will pad that with |
| 1148 | // cellWidth/2 and cellHeight/2 when finding the matching cell |
| 1149 | pointToCellRounded(originX, originY, result); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1150 | |
| 1151 | // If the item isn't fully on this screen, snap to the edges |
| 1152 | int rightOverhang = result[0] + spanX - countX; |
| 1153 | if (rightOverhang > 0) { |
| 1154 | result[0] -= rightOverhang; // Snap to right |
| 1155 | } |
| 1156 | result[0] = Math.max(0, result[0]); // Snap to left |
| 1157 | int bottomOverhang = result[1] + spanY - countY; |
| 1158 | if (bottomOverhang > 0) { |
| 1159 | result[1] -= bottomOverhang; // Snap to bottom |
| 1160 | } |
| 1161 | result[1] = Math.max(0, result[1]); // Snap to top |
| 1162 | } |
| 1163 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1164 | void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX, |
| 1165 | int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) { |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1166 | final int oldDragCellX = mDragCell[0]; |
| 1167 | final int oldDragCellY = mDragCell[1]; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1168 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 1169 | if (v != null && dragOffset == null) { |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1170 | mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2)); |
| 1171 | } else { |
| 1172 | mDragCenter.set(originX, originY); |
| 1173 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1174 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1175 | if (dragOutline == null && v == null) { |
| 1176 | if (mCrosshairsDrawable != null) { |
| 1177 | invalidate(); |
| 1178 | } |
| 1179 | return; |
| 1180 | } |
| 1181 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1182 | if (cellX != oldDragCellX || cellY != oldDragCellY) { |
| 1183 | mDragCell[0] = cellX; |
| 1184 | mDragCell[1] = cellY; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1185 | // Find the top left corner of the rect the object will occupy |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1186 | final int[] topLeft = mTmpPoint; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1187 | cellToPoint(cellX, cellY, topLeft); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1188 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1189 | int left = topLeft[0]; |
| 1190 | int top = topLeft[1]; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1191 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 1192 | if (v != null && dragOffset == null) { |
Adam Cohen | 99e8b40 | 2011-03-25 19:23:43 -0700 | [diff] [blame] | 1193 | // When drawing the drag outline, it did not account for margin offsets |
| 1194 | // added by the view's parent. |
| 1195 | MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams(); |
| 1196 | left += lp.leftMargin; |
| 1197 | top += lp.topMargin; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1198 | |
Adam Cohen | 99e8b40 | 2011-03-25 19:23:43 -0700 | [diff] [blame] | 1199 | // Offsets due to the size difference between the View and the dragOutline. |
| 1200 | // There is a size difference to account for the outer blur, which may lie |
| 1201 | // outside the bounds of the view. |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1202 | top += (v.getHeight() - dragOutline.getHeight()) / 2; |
Adam Cohen | ae915ce | 2011-08-25 13:47:22 -0700 | [diff] [blame] | 1203 | // We center about the x axis |
| 1204 | left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap) |
| 1205 | - dragOutline.getWidth()) / 2; |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 1206 | } else { |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 1207 | if (dragOffset != null && dragRegion != null) { |
| 1208 | // Center the drag region *horizontally* in the cell and apply a drag |
| 1209 | // outline offset |
| 1210 | left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap) |
| 1211 | - dragRegion.width()) / 2; |
| 1212 | top += dragOffset.y; |
| 1213 | } else { |
| 1214 | // Center the drag outline in the cell |
| 1215 | left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap) |
| 1216 | - dragOutline.getWidth()) / 2; |
| 1217 | top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap) |
| 1218 | - dragOutline.getHeight()) / 2; |
| 1219 | } |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1220 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1221 | final int oldIndex = mDragOutlineCurrent; |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1222 | mDragOutlineAnims[oldIndex].animateOut(); |
| 1223 | mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1224 | Rect r = mDragOutlines[mDragOutlineCurrent]; |
| 1225 | r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight()); |
| 1226 | if (resize) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1227 | cellToRect(cellX, cellY, spanX, spanY, r); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1228 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1229 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1230 | mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline); |
| 1231 | mDragOutlineAnims[mDragOutlineCurrent].animateIn(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1232 | } |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 1233 | |
| 1234 | // If we are drawing crosshairs, the entire CellLayout needs to be invalidated |
| 1235 | if (mCrosshairsDrawable != null) { |
| 1236 | invalidate(); |
| 1237 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 1240 | public void clearDragOutlines() { |
| 1241 | final int oldIndex = mDragOutlineCurrent; |
| 1242 | mDragOutlineAnims[oldIndex].animateOut(); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1243 | mDragCell[0] = mDragCell[1] = -1; |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1246 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1247 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1248 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1249 | * |
Romain Guy | 51afc02 | 2009-05-04 18:03:43 -0700 | [diff] [blame] | 1250 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1251 | * @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] | 1252 | * @param spanX Horizontal span of the object. |
| 1253 | * @param spanY Vertical span of the object. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1254 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1255 | * be allocated) |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1256 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1257 | * nearest the requested location. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1258 | */ |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1259 | int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY, |
| 1260 | int[] result) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1261 | return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result); |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1262 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1263 | |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1264 | /** |
| 1265 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1266 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 1267 | * |
| 1268 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1269 | * @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] | 1270 | * @param minSpanX The minimum horizontal span required |
| 1271 | * @param minSpanY The minimum vertical span required |
| 1272 | * @param spanX Horizontal span of the object. |
| 1273 | * @param spanY Vertical span of the object. |
| 1274 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1275 | * be allocated) |
| 1276 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1277 | * nearest the requested location. |
| 1278 | */ |
| 1279 | int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, |
| 1280 | int spanY, int[] result, int[] resultSpan) { |
| 1281 | return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, |
| 1282 | result, resultSpan); |
| 1283 | } |
| 1284 | |
| 1285 | /** |
| 1286 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1287 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 1288 | * |
| 1289 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1290 | * @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] | 1291 | * @param spanX Horizontal span of the object. |
| 1292 | * @param spanY Vertical span of the object. |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1293 | * @param ignoreOccupied If true, the result can be an occupied cell |
| 1294 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1295 | * be allocated) |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1296 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1297 | * nearest the requested location. |
| 1298 | */ |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1299 | int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView, |
| 1300 | boolean ignoreOccupied, int[] result) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1301 | return findNearestArea(pixelX, pixelY, spanX, spanY, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1302 | spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | private final Stack<Rect> mTempRectStack = new Stack<Rect>(); |
| 1306 | private void lazyInitTempRectStack() { |
| 1307 | if (mTempRectStack.isEmpty()) { |
| 1308 | for (int i = 0; i < mCountX * mCountY; i++) { |
| 1309 | mTempRectStack.push(new Rect()); |
| 1310 | } |
| 1311 | } |
| 1312 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1313 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1314 | private void recycleTempRects(Stack<Rect> used) { |
| 1315 | while (!used.isEmpty()) { |
| 1316 | mTempRectStack.push(used.pop()); |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | /** |
| 1321 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1322 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 1323 | * |
| 1324 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1325 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 1326 | * @param minSpanX The minimum horizontal span required |
| 1327 | * @param minSpanY The minimum vertical span required |
| 1328 | * @param spanX Horizontal span of the object. |
| 1329 | * @param spanY Vertical span of the object. |
| 1330 | * @param ignoreOccupied If true, the result can be an occupied cell |
| 1331 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1332 | * be allocated) |
| 1333 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1334 | * nearest the requested location. |
| 1335 | */ |
| 1336 | int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1337 | View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan, |
| 1338 | boolean[][] occupied) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1339 | lazyInitTempRectStack(); |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1340 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1341 | markCellsAsUnoccupiedForView(ignoreView, occupied); |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1342 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1343 | // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds |
| 1344 | // to the center of the item, but we are searching based on the top-left cell, so |
| 1345 | // we translate the point over to correspond to the top-left. |
| 1346 | pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f; |
| 1347 | pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f; |
| 1348 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1349 | // Keep track of best-scoring drop area |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1350 | final int[] bestXY = result != null ? result : new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1351 | double bestDistance = Double.MAX_VALUE; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1352 | final Rect bestRect = new Rect(-1, -1, -1, -1); |
| 1353 | final Stack<Rect> validRegions = new Stack<Rect>(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1354 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1355 | final int countX = mCountX; |
| 1356 | final int countY = mCountY; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1357 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1358 | if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 || |
| 1359 | spanX < minSpanX || spanY < minSpanY) { |
| 1360 | return bestXY; |
| 1361 | } |
| 1362 | |
| 1363 | for (int y = 0; y < countY - (minSpanY - 1); y++) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1364 | inner: |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1365 | for (int x = 0; x < countX - (minSpanX - 1); x++) { |
| 1366 | int ySize = -1; |
| 1367 | int xSize = -1; |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1368 | if (ignoreOccupied) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1369 | // First, let's see if this thing fits anywhere |
| 1370 | for (int i = 0; i < minSpanX; i++) { |
| 1371 | for (int j = 0; j < minSpanY; j++) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1372 | if (occupied[x + i][y + j]) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 1373 | continue inner; |
| 1374 | } |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1375 | } |
| 1376 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1377 | xSize = minSpanX; |
| 1378 | ySize = minSpanY; |
| 1379 | |
| 1380 | // We know that the item will fit at _some_ acceptable size, now let's see |
| 1381 | // how big we can make it. We'll alternate between incrementing x and y spans |
| 1382 | // until we hit a limit. |
| 1383 | boolean incX = true; |
| 1384 | boolean hitMaxX = xSize >= spanX; |
| 1385 | boolean hitMaxY = ySize >= spanY; |
| 1386 | while (!(hitMaxX && hitMaxY)) { |
| 1387 | if (incX && !hitMaxX) { |
| 1388 | for (int j = 0; j < ySize; j++) { |
| 1389 | if (x + xSize > countX -1 || occupied[x + xSize][y + j]) { |
| 1390 | // We can't move out horizontally |
| 1391 | hitMaxX = true; |
| 1392 | } |
| 1393 | } |
| 1394 | if (!hitMaxX) { |
| 1395 | xSize++; |
| 1396 | } |
| 1397 | } else if (!hitMaxY) { |
| 1398 | for (int i = 0; i < xSize; i++) { |
| 1399 | if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) { |
| 1400 | // We can't move out vertically |
| 1401 | hitMaxY = true; |
| 1402 | } |
| 1403 | } |
| 1404 | if (!hitMaxY) { |
| 1405 | ySize++; |
| 1406 | } |
| 1407 | } |
| 1408 | hitMaxX |= xSize >= spanX; |
| 1409 | hitMaxY |= ySize >= spanY; |
| 1410 | incX = !incX; |
| 1411 | } |
| 1412 | incX = true; |
| 1413 | hitMaxX = xSize >= spanX; |
| 1414 | hitMaxY = ySize >= spanY; |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1415 | } |
Winson Chung | 0be025d | 2011-05-23 17:45:09 -0700 | [diff] [blame] | 1416 | final int[] cellXY = mTmpXY; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 1417 | cellToCenterPoint(x, y, cellXY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1418 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1419 | // We verify that the current rect is not a sub-rect of any of our previous |
| 1420 | // candidates. In this case, the current rect is disqualified in favour of the |
| 1421 | // containing rect. |
| 1422 | Rect currentRect = mTempRectStack.pop(); |
| 1423 | currentRect.set(x, y, x + xSize, y + ySize); |
| 1424 | boolean contained = false; |
| 1425 | for (Rect r : validRegions) { |
| 1426 | if (r.contains(currentRect)) { |
| 1427 | contained = true; |
| 1428 | break; |
| 1429 | } |
| 1430 | } |
| 1431 | validRegions.push(currentRect); |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1432 | double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2) |
| 1433 | + Math.pow(cellXY[1] - pixelY, 2)); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1434 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1435 | if ((distance <= bestDistance && !contained) || |
| 1436 | currentRect.contains(bestRect)) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1437 | bestDistance = distance; |
| 1438 | bestXY[0] = x; |
| 1439 | bestXY[1] = y; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1440 | if (resultSpan != null) { |
| 1441 | resultSpan[0] = xSize; |
| 1442 | resultSpan[1] = ySize; |
| 1443 | } |
| 1444 | bestRect.set(currentRect); |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1445 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1446 | } |
| 1447 | } |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1448 | // re-mark space taken by ignoreView as occupied |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1449 | markCellsAsOccupiedForView(ignoreView, occupied); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1450 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1451 | // Return -1, -1 if no suitable location found |
| 1452 | if (bestDistance == Double.MAX_VALUE) { |
| 1453 | bestXY[0] = -1; |
| 1454 | bestXY[1] = -1; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1455 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 1456 | recycleTempRects(validRegions); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1457 | return bestXY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1458 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1459 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1460 | /** |
| 1461 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1462 | * cell location, and will also weigh in a suggested direction vector of the |
| 1463 | * desired location. This method computers distance based on unit grid distances, |
| 1464 | * not pixel distances. |
| 1465 | * |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1466 | * @param cellX The X cell nearest to which you want to search for a vacant area. |
| 1467 | * @param cellY The Y cell nearest which you want to search for a vacant area. |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1468 | * @param spanX Horizontal span of the object. |
| 1469 | * @param spanY Vertical span of the object. |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1470 | * @param direction The favored direction in which the views should move from x, y |
| 1471 | * @param exactDirectionOnly If this parameter is true, then only solutions where the direction |
| 1472 | * matches exactly. Otherwise we find the best matching direction. |
| 1473 | * @param occoupied The array which represents which cells in the CellLayout are occupied |
| 1474 | * @param blockOccupied The array which represents which cells in the specified block (cellX, |
| 1475 | * cellY, spanX, spanY) are occupied. This is used when try to move a group of views. |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1476 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1477 | * be allocated) |
| 1478 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1479 | * nearest the requested location. |
| 1480 | */ |
| 1481 | private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction, |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1482 | boolean[][] occupied, boolean blockOccupied[][], int[] result) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1483 | // Keep track of best-scoring drop area |
| 1484 | final int[] bestXY = result != null ? result : new int[2]; |
| 1485 | float bestDistance = Float.MAX_VALUE; |
| 1486 | int bestDirectionScore = Integer.MIN_VALUE; |
| 1487 | |
| 1488 | final int countX = mCountX; |
| 1489 | final int countY = mCountY; |
| 1490 | |
| 1491 | for (int y = 0; y < countY - (spanY - 1); y++) { |
| 1492 | inner: |
| 1493 | for (int x = 0; x < countX - (spanX - 1); x++) { |
| 1494 | // First, let's see if this thing fits anywhere |
| 1495 | for (int i = 0; i < spanX; i++) { |
| 1496 | for (int j = 0; j < spanY; j++) { |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1497 | if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1498 | continue inner; |
| 1499 | } |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | float distance = (float) |
| 1504 | Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY)); |
| 1505 | int[] curDirection = mTmpPoint; |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1506 | computeDirectionVector(x - cellX, y - cellY, curDirection); |
| 1507 | // The direction score is just the dot product of the two candidate direction |
| 1508 | // and that passed in. |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1509 | int curDirectionScore = direction[0] * curDirection[0] + |
| 1510 | direction[1] * curDirection[1]; |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1511 | boolean exactDirectionOnly = false; |
| 1512 | boolean directionMatches = direction[0] == curDirection[0] && |
| 1513 | direction[0] == curDirection[0]; |
| 1514 | if ((directionMatches || !exactDirectionOnly) && |
| 1515 | Float.compare(distance, bestDistance) < 0 || (Float.compare(distance, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1516 | bestDistance) == 0 && curDirectionScore > bestDirectionScore)) { |
| 1517 | bestDistance = distance; |
| 1518 | bestDirectionScore = curDirectionScore; |
| 1519 | bestXY[0] = x; |
| 1520 | bestXY[1] = y; |
| 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | // Return -1, -1 if no suitable location found |
| 1526 | if (bestDistance == Float.MAX_VALUE) { |
| 1527 | bestXY[0] = -1; |
| 1528 | bestXY[1] = -1; |
| 1529 | } |
| 1530 | return bestXY; |
| 1531 | } |
| 1532 | |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1533 | private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY, |
| 1534 | int[] direction,boolean[][] occupied, |
| 1535 | boolean blockOccupied[][], int[] result) { |
| 1536 | // Keep track of best-scoring drop area |
| 1537 | final int[] bestXY = result != null ? result : new int[2]; |
| 1538 | bestXY[0] = -1; |
| 1539 | bestXY[1] = -1; |
| 1540 | float bestDistance = Float.MAX_VALUE; |
| 1541 | |
| 1542 | // We use this to march in a single direction |
| 1543 | if (direction[0] != 0 && direction[1] != 0) { |
| 1544 | return bestXY; |
| 1545 | } |
| 1546 | |
| 1547 | // This will only incrememnet one of x or y based on the assertion above |
| 1548 | int x = cellX + direction[0]; |
| 1549 | int y = cellY + direction[1]; |
| 1550 | while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) { |
| 1551 | |
| 1552 | boolean fail = false; |
| 1553 | for (int i = 0; i < spanX; i++) { |
| 1554 | for (int j = 0; j < spanY; j++) { |
| 1555 | if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) { |
| 1556 | fail = true; |
| 1557 | } |
| 1558 | } |
| 1559 | } |
| 1560 | if (!fail) { |
| 1561 | float distance = (float) |
| 1562 | Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY)); |
| 1563 | if (Float.compare(distance, bestDistance) < 0) { |
| 1564 | bestDistance = distance; |
| 1565 | bestXY[0] = x; |
| 1566 | bestXY[1] = y; |
| 1567 | } |
| 1568 | } |
| 1569 | x += direction[0]; |
| 1570 | y += direction[1]; |
| 1571 | } |
| 1572 | return bestXY; |
| 1573 | } |
| 1574 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1575 | private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop, |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1576 | int[] direction, ItemConfiguration currentState) { |
| 1577 | CellAndSpan c = currentState.map.get(v); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1578 | boolean success = false; |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1579 | markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1580 | markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true); |
| 1581 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1582 | findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1583 | |
| 1584 | if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1585 | c.x = mTempLocation[0]; |
| 1586 | c.y = mTempLocation[1]; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1587 | success = true; |
| 1588 | |
| 1589 | } |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1590 | markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1591 | return success; |
| 1592 | } |
| 1593 | |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1594 | // This method looks in the specified direction to see if there is an additional view |
| 1595 | // immediately adjecent in that direction |
| 1596 | private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction, |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1597 | boolean[][] occupied, ItemConfiguration currentState) { |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1598 | boolean found = false; |
| 1599 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1600 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1601 | Rect r0 = new Rect(boundingRect); |
| 1602 | Rect r1 = new Rect(); |
| 1603 | |
| 1604 | int deltaX = 0; |
| 1605 | int deltaY = 0; |
| 1606 | if (direction[1] < 0) { |
| 1607 | r0.set(r0.left, r0.top - 1, r0.right, r0.bottom); |
| 1608 | deltaY = -1; |
| 1609 | } else if (direction[1] > 0) { |
| 1610 | r0.set(r0.left, r0.top, r0.right, r0.bottom + 1); |
| 1611 | deltaY = 1; |
| 1612 | } else if (direction[0] < 0) { |
| 1613 | r0.set(r0.left - 1, r0.top, r0.right, r0.bottom); |
| 1614 | deltaX = -1; |
| 1615 | } else if (direction[0] > 0) { |
| 1616 | r0.set(r0.left, r0.top, r0.right + 1, r0.bottom); |
| 1617 | deltaX = 1; |
| 1618 | } |
| 1619 | |
| 1620 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1621 | View child = mShortcutsAndWidgets.getChildAt(i); |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1622 | if (views.contains(child)) continue; |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1623 | CellAndSpan c = currentState.map.get(child); |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1624 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1625 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1626 | r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY); |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1627 | if (Rect.intersects(r0, r1)) { |
| 1628 | if (!lp.canReorder) { |
| 1629 | return false; |
| 1630 | } |
| 1631 | boolean pushed = false; |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1632 | for (int x = c.x; x < c.x + c.spanX; x++) { |
| 1633 | for (int y = c.y; y < c.y + c.spanY; y++) { |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1634 | boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX |
| 1635 | && y - deltaY >= 0 && y - deltaY < mCountY; |
| 1636 | if (inBounds && occupied[x - deltaX][y - deltaY]) { |
| 1637 | pushed = true; |
| 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 | if (pushed) { |
| 1642 | views.add(child); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1643 | boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY); |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1644 | found = true; |
| 1645 | } |
| 1646 | } |
| 1647 | } |
| 1648 | return found; |
| 1649 | } |
| 1650 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1651 | private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop, |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1652 | int[] direction, boolean push, ItemConfiguration currentState) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1653 | if (views.size() == 0) return true; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1654 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1655 | boolean success = false; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1656 | Rect boundingRect = null; |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1657 | // We construct a rect which represents the entire group of views passed in |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1658 | for (View v: views) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1659 | CellAndSpan c = currentState.map.get(v); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1660 | if (boundingRect == null) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1661 | boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1662 | } else { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1663 | boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1664 | } |
| 1665 | } |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1666 | |
| 1667 | @SuppressWarnings("unchecked") |
| 1668 | ArrayList<View> dup = (ArrayList<View>) views.clone(); |
| 1669 | // We try and expand the group of views in the direction vector passed, based on |
| 1670 | // whether they are physically adjacent, ie. based on "push mechanics". |
| 1671 | while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied, |
| 1672 | currentState)) { |
| 1673 | } |
| 1674 | |
| 1675 | // Mark the occupied state as false for the group of views we want to move. |
| 1676 | for (View v: dup) { |
| 1677 | CellAndSpan c = currentState.map.get(v); |
| 1678 | markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false); |
| 1679 | } |
| 1680 | |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1681 | boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()]; |
| 1682 | int top = boundingRect.top; |
| 1683 | int left = boundingRect.left; |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1684 | // We mark more precisely which parts of the bounding rect are truly occupied, allowing |
| 1685 | // for tetris-style interlocking. |
| 1686 | for (View v: dup) { |
| 1687 | CellAndSpan c = currentState.map.get(v); |
| 1688 | markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true); |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1689 | } |
| 1690 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1691 | markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true); |
| 1692 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1693 | if (push) { |
| 1694 | findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(), |
| 1695 | boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation); |
| 1696 | } else { |
| 1697 | findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(), |
| 1698 | boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation); |
| 1699 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1700 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1701 | // If we successfuly found a location by pushing the block of views, we commit it |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1702 | if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1703 | int deltaX = mTempLocation[0] - boundingRect.left; |
| 1704 | int deltaY = mTempLocation[1] - boundingRect.top; |
| 1705 | for (View v: dup) { |
| 1706 | CellAndSpan c = currentState.map.get(v); |
| 1707 | c.x += deltaX; |
| 1708 | c.y += deltaY; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1709 | } |
| 1710 | success = true; |
| 1711 | } |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1712 | |
| 1713 | // In either case, we set the occupied array as marked for the location of the views |
| 1714 | for (View v: dup) { |
| 1715 | CellAndSpan c = currentState.map.get(v); |
| 1716 | markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1717 | } |
| 1718 | return success; |
| 1719 | } |
| 1720 | |
| 1721 | private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) { |
| 1722 | markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value); |
| 1723 | } |
| 1724 | |
| 1725 | private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction, |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1726 | View ignoreView, ItemConfiguration solution) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1727 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1728 | mIntersectingViews.clear(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1729 | mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1730 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1731 | // Mark the desired location of the view currently being dragged. |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1732 | if (ignoreView != null) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1733 | CellAndSpan c = solution.map.get(ignoreView); |
| 1734 | c.x = cellX; |
| 1735 | c.y = cellY; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1736 | } |
| 1737 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1738 | //int childCount = mChildren.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1739 | Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY); |
| 1740 | Rect r1 = new Rect(); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1741 | for (View child: solution.map.keySet()) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1742 | if (child == ignoreView) continue; |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1743 | CellAndSpan c = solution.map.get(child); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1744 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1745 | r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1746 | if (Rect.intersects(r0, r1)) { |
| 1747 | if (!lp.canReorder) { |
| 1748 | return false; |
| 1749 | } |
| 1750 | mIntersectingViews.add(child); |
| 1751 | } |
| 1752 | } |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1753 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1754 | // We try to move the intersecting views as a block using the push mechanic |
| 1755 | if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, solution)) { |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1756 | return true; |
| 1757 | } |
| 1758 | // Try the opposite direction |
| 1759 | direction[0] *= -1; |
| 1760 | direction[1] *= -1; |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1761 | if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, solution)) { |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1762 | return true; |
| 1763 | } |
| 1764 | // Switch the direction back |
| 1765 | direction[0] *= -1; |
| 1766 | direction[1] *= -1; |
| 1767 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1768 | // Next we try moving the views as a block , but without requiring the push mechanic |
| 1769 | if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, solution)) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1770 | return true; |
| 1771 | } |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1772 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1773 | // Ok, they couldn't move as a block, let's move them individually |
| 1774 | for (View v : mIntersectingViews) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1775 | if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1776 | return false; |
| 1777 | } |
| 1778 | } |
| 1779 | return true; |
| 1780 | } |
| 1781 | |
| 1782 | /* |
| 1783 | * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between |
| 1784 | * the provided point and the provided cell |
| 1785 | */ |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1786 | private void computeDirectionVector(float deltaX, float deltaY, int[] result) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1787 | double angle = Math.atan(((float) deltaY) / deltaX); |
| 1788 | |
| 1789 | result[0] = 0; |
| 1790 | result[1] = 0; |
| 1791 | if (Math.abs(Math.cos(angle)) > 0.5f) { |
| 1792 | result[0] = (int) Math.signum(deltaX); |
| 1793 | } |
| 1794 | if (Math.abs(Math.sin(angle)) > 0.5f) { |
| 1795 | result[1] = (int) Math.signum(deltaY); |
| 1796 | } |
| 1797 | } |
| 1798 | |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1799 | private void copyOccupiedArray(boolean[][] occupied) { |
| 1800 | for (int i = 0; i < mCountX; i++) { |
| 1801 | for (int j = 0; j < mCountY; j++) { |
| 1802 | occupied[i][j] = mOccupied[i][j]; |
| 1803 | } |
| 1804 | } |
| 1805 | } |
| 1806 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1807 | ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, |
| 1808 | int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1809 | // Copy the current state into the solution. This solution will be manipulated as necessary. |
| 1810 | copyCurrentStateToSolution(solution, false); |
| 1811 | // Copy the current occupied array into the temporary occupied array. This array will be |
| 1812 | // manipulated as necessary to find a solution. |
| 1813 | copyOccupiedArray(mTmpOccupied); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1814 | |
| 1815 | // We find the nearest cell into which we would place the dragged item, assuming there's |
| 1816 | // nothing in its way. |
| 1817 | int result[] = new int[2]; |
| 1818 | result = findNearestArea(pixelX, pixelY, spanX, spanY, result); |
| 1819 | |
| 1820 | boolean success = false; |
| 1821 | // First we try the exact nearest position of the item being dragged, |
| 1822 | // we will then want to try to move this around to other neighbouring positions |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1823 | success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView, |
| 1824 | solution); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1825 | |
| 1826 | if (!success) { |
| 1827 | // We try shrinking the widget down to size in an alternating pattern, shrink 1 in |
| 1828 | // x, then 1 in y etc. |
| 1829 | if (spanX > minSpanX && (minSpanY == spanY || decX)) { |
| 1830 | return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction, |
| 1831 | dragView, false, solution); |
| 1832 | } else if (spanY > minSpanY) { |
| 1833 | return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction, |
| 1834 | dragView, true, solution); |
| 1835 | } |
| 1836 | solution.isSolution = false; |
| 1837 | } else { |
| 1838 | solution.isSolution = true; |
| 1839 | solution.dragViewX = result[0]; |
| 1840 | solution.dragViewY = result[1]; |
| 1841 | solution.dragViewSpanX = spanX; |
| 1842 | solution.dragViewSpanY = spanY; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1843 | } |
| 1844 | return solution; |
| 1845 | } |
| 1846 | |
| 1847 | private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1848 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1849 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1850 | View child = mShortcutsAndWidgets.getChildAt(i); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1851 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1852 | CellAndSpan c; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1853 | if (temp) { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1854 | c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1855 | } else { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1856 | c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1857 | } |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1858 | solution.map.put(child, c); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | private void copySolutionToTempState(ItemConfiguration solution, View dragView) { |
| 1863 | for (int i = 0; i < mCountX; i++) { |
| 1864 | for (int j = 0; j < mCountY; j++) { |
| 1865 | mTmpOccupied[i][j] = false; |
| 1866 | } |
| 1867 | } |
| 1868 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1869 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1870 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1871 | View child = mShortcutsAndWidgets.getChildAt(i); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1872 | if (child == dragView) continue; |
| 1873 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1874 | CellAndSpan c = solution.map.get(child); |
| 1875 | if (c != null) { |
| 1876 | lp.tmpCellX = c.x; |
| 1877 | lp.tmpCellY = c.y; |
| 1878 | lp.cellHSpan = c.spanX; |
| 1879 | lp.cellVSpan = c.spanY; |
| 1880 | markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1881 | } |
| 1882 | } |
| 1883 | markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX, |
| 1884 | solution.dragViewSpanY, mTmpOccupied, true); |
| 1885 | } |
| 1886 | |
| 1887 | private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean |
| 1888 | commitDragView) { |
| 1889 | |
| 1890 | boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied; |
| 1891 | for (int i = 0; i < mCountX; i++) { |
| 1892 | for (int j = 0; j < mCountY; j++) { |
| 1893 | occupied[i][j] = false; |
| 1894 | } |
| 1895 | } |
| 1896 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1897 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1898 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1899 | View child = mShortcutsAndWidgets.getChildAt(i); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1900 | if (child == dragView) continue; |
| 1901 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1902 | CellAndSpan c = solution.map.get(child); |
| 1903 | if (c != null) { |
| 1904 | if (lp.cellX != c.x || lp.cellY != c.y) { |
| 1905 | animateChildToPosition(child, c.x, c.y, 150, 0, DESTRUCTIVE_REORDER, false); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1906 | } |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 1907 | markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | if (commitDragView) { |
| 1911 | markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX, |
| 1912 | solution.dragViewSpanY, occupied, true); |
| 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | private void commitTempPlacement() { |
| 1917 | for (int i = 0; i < mCountX; i++) { |
| 1918 | for (int j = 0; j < mCountY; j++) { |
| 1919 | mOccupied[i][j] = mTmpOccupied[i][j]; |
| 1920 | } |
| 1921 | } |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1922 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1923 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1924 | LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1925 | lp.cellX = lp.tmpCellX; |
| 1926 | lp.cellY = lp.tmpCellY; |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | public void setUseTempCoords(boolean useTempCoords) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1931 | int childCount = mShortcutsAndWidgets.getChildCount(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1932 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1933 | LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1934 | lp.useTmpCoords = useTempCoords; |
| 1935 | } |
| 1936 | } |
| 1937 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1938 | ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY, |
| 1939 | int spanX, int spanY, View dragView, ItemConfiguration solution) { |
| 1940 | int[] result = new int[2]; |
| 1941 | int[] resultSpan = new int[2]; |
| 1942 | findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result, |
| 1943 | resultSpan); |
| 1944 | if (result[0] >= 0 && result[1] >= 0) { |
| 1945 | copyCurrentStateToSolution(solution, false); |
| 1946 | solution.dragViewX = result[0]; |
| 1947 | solution.dragViewY = result[1]; |
| 1948 | solution.dragViewSpanX = resultSpan[0]; |
| 1949 | solution.dragViewSpanY = resultSpan[1]; |
| 1950 | solution.isSolution = true; |
| 1951 | } else { |
| 1952 | solution.isSolution = false; |
| 1953 | } |
| 1954 | return solution; |
| 1955 | } |
| 1956 | |
| 1957 | public void prepareChildForDrag(View child) { |
| 1958 | markCellsAsUnoccupiedForView(child); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY, |
| 1962 | View dragView, int[] result, int resultSpan[], int mode) { |
| 1963 | |
| 1964 | // First we determine if things have moved enough to cause a different layout |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1965 | result = findNearestArea(pixelX, pixelY, spanX, spanY, result); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1966 | |
| 1967 | if (resultSpan == null) { |
| 1968 | resultSpan = new int[2]; |
| 1969 | } |
| 1970 | |
| 1971 | // We attempt the first algorithm |
Adam Cohen | 47a876d | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 1972 | regionToCenterPoint(result[0], result[1], spanX, spanY, mTmpPoint); |
| 1973 | computeDirectionVector((mTmpPoint[0] - pixelX) / spanX, (mTmpPoint[1] - pixelY) / spanY, |
| 1974 | mDirectionVector); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 1975 | ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY, |
| 1976 | spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration()); |
| 1977 | |
| 1978 | // We attempt the approach which doesn't shuffle views at all |
| 1979 | ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX, |
| 1980 | minSpanY, spanX, spanY, dragView, new ItemConfiguration()); |
| 1981 | |
| 1982 | ItemConfiguration finalSolution = null; |
| 1983 | if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) { |
| 1984 | finalSolution = swapSolution; |
| 1985 | } else if (noShuffleSolution.isSolution) { |
| 1986 | finalSolution = noShuffleSolution; |
| 1987 | } |
| 1988 | |
| 1989 | boolean foundSolution = true; |
| 1990 | if (!DESTRUCTIVE_REORDER) { |
| 1991 | setUseTempCoords(true); |
| 1992 | } |
| 1993 | |
| 1994 | if (finalSolution != null) { |
| 1995 | result[0] = finalSolution.dragViewX; |
| 1996 | result[1] = finalSolution.dragViewY; |
| 1997 | resultSpan[0] = finalSolution.dragViewSpanX; |
| 1998 | resultSpan[1] = finalSolution.dragViewSpanY; |
| 1999 | |
| 2000 | // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother |
| 2001 | // committing anything or animating anything as we just want to determine if a solution |
| 2002 | // exists |
| 2003 | if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) { |
| 2004 | if (!DESTRUCTIVE_REORDER) { |
| 2005 | copySolutionToTempState(finalSolution, dragView); |
| 2006 | } |
| 2007 | setItemPlacementDirty(true); |
| 2008 | animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP); |
| 2009 | |
| 2010 | if (!DESTRUCTIVE_REORDER && mode == MODE_ON_DROP) { |
| 2011 | commitTempPlacement(); |
| 2012 | } |
| 2013 | } |
| 2014 | } else { |
| 2015 | foundSolution = false; |
| 2016 | result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1; |
| 2017 | } |
| 2018 | |
| 2019 | if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) { |
| 2020 | setUseTempCoords(false); |
| 2021 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2022 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2023 | mShortcutsAndWidgets.requestLayout(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2024 | return result; |
| 2025 | } |
| 2026 | |
| 2027 | public boolean isItemPlacementDirty() { |
| 2028 | return mItemLocationsDirty; |
| 2029 | } |
| 2030 | |
| 2031 | public void setItemPlacementDirty(boolean dirty) { |
| 2032 | mItemLocationsDirty = dirty; |
| 2033 | } |
| 2034 | |
| 2035 | private class ItemConfiguration { |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 2036 | HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2037 | boolean isSolution = false; |
| 2038 | int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY; |
| 2039 | |
| 2040 | int area() { |
| 2041 | return dragViewSpanX * dragViewSpanY; |
| 2042 | } |
Adam Cohen | 8baab35 | 2012-03-20 17:39:21 -0700 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | private class CellAndSpan { |
| 2046 | int x, y; |
| 2047 | int spanX, spanY; |
| 2048 | |
| 2049 | public CellAndSpan(int x, int y, int spanX, int spanY) { |
| 2050 | this.x = x; |
| 2051 | this.y = y; |
| 2052 | this.spanX = spanX; |
| 2053 | this.spanY = spanY; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2054 | } |
| 2055 | } |
| 2056 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2057 | /** |
| 2058 | * Find a vacant area that will fit the given bounds nearest the requested |
| 2059 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 2060 | * |
| 2061 | * @param pixelX The X location at which you want to search for a vacant area. |
| 2062 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 2063 | * @param spanX Horizontal span of the object. |
| 2064 | * @param spanY Vertical span of the object. |
| 2065 | * @param ignoreView Considers space occupied by this view as unoccupied |
| 2066 | * @param result Previously returned value to possibly recycle. |
| 2067 | * @return The X, Y cell of a vacant area that can contain this object, |
| 2068 | * nearest the requested location. |
| 2069 | */ |
| 2070 | int[] findNearestVacantArea( |
| 2071 | int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) { |
| 2072 | return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result); |
| 2073 | } |
| 2074 | |
| 2075 | /** |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2076 | * Find a vacant area that will fit the given bounds nearest the requested |
| 2077 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 2078 | * |
| 2079 | * @param pixelX The X location at which you want to search for a vacant area. |
| 2080 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 2081 | * @param minSpanX The minimum horizontal span required |
| 2082 | * @param minSpanY The minimum vertical span required |
| 2083 | * @param spanX Horizontal span of the object. |
| 2084 | * @param spanY Vertical span of the object. |
| 2085 | * @param ignoreView Considers space occupied by this view as unoccupied |
| 2086 | * @param result Previously returned value to possibly recycle. |
| 2087 | * @return The X, Y cell of a vacant area that can contain this object, |
| 2088 | * nearest the requested location. |
| 2089 | */ |
| 2090 | int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, |
| 2091 | int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2092 | return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true, |
| 2093 | result, resultSpan, mOccupied); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | /** |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2097 | * Find a starting cell position that will fit the given bounds nearest the requested |
| 2098 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 2099 | * |
| 2100 | * @param pixelX The X location at which you want to search for a vacant area. |
| 2101 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 2102 | * @param spanX Horizontal span of the object. |
| 2103 | * @param spanY Vertical span of the object. |
| 2104 | * @param ignoreView Considers space occupied by this view as unoccupied |
| 2105 | * @param result Previously returned value to possibly recycle. |
| 2106 | * @return The X, Y cell of a vacant area that can contain this object, |
| 2107 | * nearest the requested location. |
| 2108 | */ |
| 2109 | int[] findNearestArea( |
| 2110 | int pixelX, int pixelY, int spanX, int spanY, int[] result) { |
| 2111 | return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result); |
| 2112 | } |
| 2113 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2114 | boolean existsEmptyCell() { |
| 2115 | return findCellForSpan(null, 1, 1); |
| 2116 | } |
| 2117 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2118 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2119 | * Finds the upper-left coordinate of the first rectangle in the grid that can |
| 2120 | * hold a cell of the specified dimensions. If intersectX and intersectY are not -1, |
| 2121 | * then this method will only return coordinates for rectangles that contain the cell |
| 2122 | * (intersectX, intersectY) |
| 2123 | * |
| 2124 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 2125 | * can be found. |
| 2126 | * @param spanX The horizontal span of the cell we want to find. |
| 2127 | * @param spanY The vertical span of the cell we want to find. |
| 2128 | * |
| 2129 | * @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] | 2130 | */ |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2131 | boolean findCellForSpan(int[] cellXY, int spanX, int spanY) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2132 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | /** |
| 2136 | * Like above, but ignores any cells occupied by the item "ignoreView" |
| 2137 | * |
| 2138 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 2139 | * can be found. |
| 2140 | * @param spanX The horizontal span of the cell we want to find. |
| 2141 | * @param spanY The vertical span of the cell we want to find. |
| 2142 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 2143 | * @return |
| 2144 | */ |
| 2145 | boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2146 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, |
| 2147 | ignoreView, mOccupied); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | /** |
| 2151 | * Like above, but if intersectX and intersectY are not -1, then this method will try to |
| 2152 | * return coordinates for rectangles that contain the cell [intersectX, intersectY] |
| 2153 | * |
| 2154 | * @param spanX The horizontal span of the cell we want to find. |
| 2155 | * @param spanY The vertical span of the cell we want to find. |
| 2156 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 2157 | * @param intersectX The X coordinate of the cell that we should try to overlap |
| 2158 | * @param intersectX The Y coordinate of the cell that we should try to overlap |
| 2159 | * |
| 2160 | * @return True if a vacant cell of the specified dimension was found, false otherwise. |
| 2161 | */ |
| 2162 | boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY, |
| 2163 | int intersectX, int intersectY) { |
| 2164 | return findCellForSpanThatIntersectsIgnoring( |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2165 | cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | /** |
| 2169 | * The superset of the above two methods |
| 2170 | */ |
| 2171 | boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2172 | int intersectX, int intersectY, View ignoreView, boolean occupied[][]) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 2173 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2174 | markCellsAsUnoccupiedForView(ignoreView, occupied); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2175 | |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 2176 | boolean foundCell = false; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2177 | while (true) { |
| 2178 | int startX = 0; |
| 2179 | if (intersectX >= 0) { |
| 2180 | startX = Math.max(startX, intersectX - (spanX - 1)); |
| 2181 | } |
| 2182 | int endX = mCountX - (spanX - 1); |
| 2183 | if (intersectX >= 0) { |
| 2184 | endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0)); |
| 2185 | } |
| 2186 | int startY = 0; |
| 2187 | if (intersectY >= 0) { |
| 2188 | startY = Math.max(startY, intersectY - (spanY - 1)); |
| 2189 | } |
| 2190 | int endY = mCountY - (spanY - 1); |
| 2191 | if (intersectY >= 0) { |
| 2192 | endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0)); |
| 2193 | } |
| 2194 | |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 2195 | for (int y = startY; y < endY && !foundCell; y++) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2196 | inner: |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 2197 | for (int x = startX; x < endX; x++) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2198 | for (int i = 0; i < spanX; i++) { |
| 2199 | for (int j = 0; j < spanY; j++) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2200 | if (occupied[x + i][y + j]) { |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 2201 | // small optimization: we can skip to after the column we just found |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2202 | // an occupied cell |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 2203 | x += i; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2204 | continue inner; |
| 2205 | } |
| 2206 | } |
| 2207 | } |
| 2208 | if (cellXY != null) { |
| 2209 | cellXY[0] = x; |
| 2210 | cellXY[1] = y; |
| 2211 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 2212 | foundCell = true; |
| 2213 | break; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2214 | } |
| 2215 | } |
| 2216 | if (intersectX == -1 && intersectY == -1) { |
| 2217 | break; |
| 2218 | } else { |
| 2219 | // if we failed to find anything, try again but without any requirements of |
| 2220 | // intersecting |
| 2221 | intersectX = -1; |
| 2222 | intersectY = -1; |
| 2223 | continue; |
| 2224 | } |
| 2225 | } |
| 2226 | |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 2227 | // re-mark space taken by ignoreView as occupied |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2228 | markCellsAsOccupiedForView(ignoreView, occupied); |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 2229 | return foundCell; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | /** |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2233 | * A drag event has begun over this layout. |
| 2234 | * It may have begun over this layout (in which case onDragChild is called first), |
| 2235 | * or it may have begun on another layout. |
| 2236 | */ |
| 2237 | void onDragEnter() { |
| 2238 | if (!mDragging) { |
| 2239 | // Fade in the drag indicators |
| 2240 | if (mCrosshairsAnimator != null) { |
| 2241 | mCrosshairsAnimator.animateIn(); |
| 2242 | } |
| 2243 | } |
| 2244 | mDragging = true; |
| 2245 | } |
| 2246 | |
| 2247 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2248 | * Called when drag has left this CellLayout or has been completed (successfully or not) |
| 2249 | */ |
| 2250 | void onDragExit() { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2251 | // This can actually be called when we aren't in a drag, e.g. when adding a new |
| 2252 | // item to this layout via the customize drawer. |
| 2253 | // Guard against that case. |
| 2254 | if (mDragging) { |
| 2255 | mDragging = false; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 2256 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2257 | // Fade out the drag indicators |
| 2258 | if (mCrosshairsAnimator != null) { |
| 2259 | mCrosshairsAnimator.animateOut(); |
| 2260 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 2261 | } |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 2262 | |
| 2263 | // Invalidate the drag data |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2264 | mDragCell[0] = mDragCell[1] = -1; |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 2265 | mDragOutlineAnims[mDragOutlineCurrent].animateOut(); |
| 2266 | mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length; |
| 2267 | |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 2268 | setIsDragOverlapping(false); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 2269 | } |
| 2270 | |
| 2271 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2272 | * Mark a child as having been dropped. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 2273 | * 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] | 2274 | * 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] | 2275 | * |
| 2276 | * @param child The child that is being dropped |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2277 | */ |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 2278 | void onDropChild(View child) { |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 2279 | if (child != null) { |
| 2280 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 2281 | lp.dropped = true; |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 2282 | child.requestLayout(); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 2283 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2284 | } |
| 2285 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2286 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2287 | * Computes a bounding rectangle for a range of cells |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2288 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2289 | * @param cellX X coordinate of upper left corner expressed as a cell position |
| 2290 | * @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] | 2291 | * @param cellHSpan Width in cells |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2292 | * @param cellVSpan Height in cells |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 2293 | * @param resultRect Rect into which to put the results |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2294 | */ |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2295 | 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] | 2296 | final int cellWidth = mCellWidth; |
| 2297 | final int cellHeight = mCellHeight; |
| 2298 | final int widthGap = mWidthGap; |
| 2299 | final int heightGap = mHeightGap; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2300 | |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 2301 | final int hStartPadding = getPaddingLeft(); |
| 2302 | final int vStartPadding = getPaddingTop(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2303 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2304 | int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap); |
| 2305 | int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap); |
| 2306 | |
| 2307 | int x = hStartPadding + cellX * (cellWidth + widthGap); |
| 2308 | int y = vStartPadding + cellY * (cellHeight + heightGap); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2309 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 2310 | resultRect.set(x, y, x + width, y + height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2311 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2312 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2313 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2314 | * 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] | 2315 | * fit the given rectangle. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2316 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2317 | * @param width Width in pixels |
| 2318 | * @param height Height in pixels |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 2319 | * @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] | 2320 | */ |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 2321 | public int[] rectToCell(int width, int height, int[] result) { |
Michael Jurka | 9987a5c | 2010-10-08 16:58:12 -0700 | [diff] [blame] | 2322 | return rectToCell(getResources(), width, height, result); |
| 2323 | } |
| 2324 | |
| 2325 | 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] | 2326 | // Always assume we're working with the smallest span to make sure we |
| 2327 | // reserve enough space in both orientations. |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 2328 | int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width); |
| 2329 | int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2330 | int smallerSize = Math.min(actualWidth, actualHeight); |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 2331 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2332 | // Always round up to next largest cell |
Winson Chung | 54c725c | 2011-08-03 12:03:40 -0700 | [diff] [blame] | 2333 | int spanX = (int) Math.ceil(width / (float) smallerSize); |
| 2334 | int spanY = (int) Math.ceil(height / (float) smallerSize); |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 2335 | |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 2336 | if (result == null) { |
| 2337 | return new int[] { spanX, spanY }; |
| 2338 | } |
| 2339 | result[0] = spanX; |
| 2340 | result[1] = spanY; |
| 2341 | return result; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2342 | } |
| 2343 | |
Michael Jurka | f12c75c | 2011-01-25 22:41:40 -0800 | [diff] [blame] | 2344 | public int[] cellSpansToSize(int hSpans, int vSpans) { |
| 2345 | int[] size = new int[2]; |
| 2346 | size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap; |
| 2347 | size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap; |
| 2348 | return size; |
| 2349 | } |
| 2350 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2351 | /** |
Patrick Dubroy | 047379a | 2010-12-19 22:02:04 -0800 | [diff] [blame] | 2352 | * Calculate the grid spans needed to fit given item |
| 2353 | */ |
| 2354 | public void calculateSpans(ItemInfo info) { |
| 2355 | final int minWidth; |
| 2356 | final int minHeight; |
| 2357 | |
| 2358 | if (info instanceof LauncherAppWidgetInfo) { |
| 2359 | minWidth = ((LauncherAppWidgetInfo) info).minWidth; |
| 2360 | minHeight = ((LauncherAppWidgetInfo) info).minHeight; |
| 2361 | } else if (info instanceof PendingAddWidgetInfo) { |
| 2362 | minWidth = ((PendingAddWidgetInfo) info).minWidth; |
| 2363 | minHeight = ((PendingAddWidgetInfo) info).minHeight; |
| 2364 | } else { |
| 2365 | // It's not a widget, so it must be 1x1 |
| 2366 | info.spanX = info.spanY = 1; |
| 2367 | return; |
| 2368 | } |
| 2369 | int[] spans = rectToCell(minWidth, minHeight, null); |
| 2370 | info.spanX = spans[0]; |
| 2371 | info.spanY = spans[1]; |
| 2372 | } |
| 2373 | |
| 2374 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2375 | * Find the first vacant cell, if there is one. |
| 2376 | * |
| 2377 | * @param vacant Holds the x and y coordinate of the vacant cell |
| 2378 | * @param spanX Horizontal cell span. |
| 2379 | * @param spanY Vertical cell span. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2380 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2381 | * @return True if a vacant cell was found |
| 2382 | */ |
| 2383 | public boolean getVacantCell(int[] vacant, int spanX, int spanY) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2384 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2385 | return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | static boolean findVacantCell(int[] vacant, int spanX, int spanY, |
| 2389 | int xCount, int yCount, boolean[][] occupied) { |
| 2390 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 2391 | for (int y = 0; y < yCount; y++) { |
| 2392 | for (int x = 0; x < xCount; x++) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2393 | boolean available = !occupied[x][y]; |
| 2394 | out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) { |
| 2395 | for (int j = y; j < y + spanY - 1 && y < yCount; j++) { |
| 2396 | available = available && !occupied[i][j]; |
| 2397 | if (!available) break out; |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | if (available) { |
| 2402 | vacant[0] = x; |
| 2403 | vacant[1] = y; |
| 2404 | return true; |
| 2405 | } |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | return false; |
| 2410 | } |
| 2411 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2412 | private void clearOccupiedCells() { |
| 2413 | for (int x = 0; x < mCountX; x++) { |
| 2414 | for (int y = 0; y < mCountY; y++) { |
| 2415 | mOccupied[x][y] = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2416 | } |
| 2417 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2418 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2419 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2420 | /** |
| 2421 | * Given a view, determines how much that view can be expanded in all directions, in terms of |
| 2422 | * whether or not there are other items occupying adjacent cells. Used by the |
| 2423 | * AppWidgetResizeFrame to determine how the widget can be resized. |
| 2424 | */ |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2425 | public void getExpandabilityArrayForView(View view, int[] expandability) { |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2426 | final LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2427 | boolean flag; |
| 2428 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2429 | expandability[AppWidgetResizeFrame.LEFT] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2430 | for (int x = lp.cellX - 1; x >= 0; x--) { |
| 2431 | flag = false; |
| 2432 | for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) { |
| 2433 | if (mOccupied[x][y]) flag = true; |
| 2434 | } |
| 2435 | if (flag) break; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2436 | expandability[AppWidgetResizeFrame.LEFT]++; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2437 | } |
| 2438 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2439 | expandability[AppWidgetResizeFrame.TOP] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2440 | for (int y = lp.cellY - 1; y >= 0; y--) { |
| 2441 | flag = false; |
| 2442 | for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) { |
| 2443 | if (mOccupied[x][y]) flag = true; |
| 2444 | } |
| 2445 | if (flag) break; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2446 | expandability[AppWidgetResizeFrame.TOP]++; |
| 2447 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2448 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2449 | expandability[AppWidgetResizeFrame.RIGHT] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2450 | for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) { |
| 2451 | flag = false; |
| 2452 | for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) { |
| 2453 | if (mOccupied[x][y]) flag = true; |
| 2454 | } |
| 2455 | if (flag) break; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2456 | expandability[AppWidgetResizeFrame.RIGHT]++; |
| 2457 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2458 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2459 | expandability[AppWidgetResizeFrame.BOTTOM] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2460 | for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) { |
| 2461 | flag = false; |
| 2462 | for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) { |
| 2463 | if (mOccupied[x][y]) flag = true; |
| 2464 | } |
| 2465 | if (flag) break; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2466 | expandability[AppWidgetResizeFrame.BOTTOM]++; |
| 2467 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2468 | } |
| 2469 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2470 | 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] | 2471 | markCellsAsUnoccupiedForView(view); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2472 | markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2473 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2474 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2475 | public void markCellsAsOccupiedForView(View view) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2476 | markCellsAsOccupiedForView(view, mOccupied); |
| 2477 | } |
| 2478 | public void markCellsAsOccupiedForView(View view, boolean[][] occupied) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2479 | if (view == null || view.getParent() != mShortcutsAndWidgets) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2480 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2481 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2482 | } |
| 2483 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2484 | public void markCellsAsUnoccupiedForView(View view) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2485 | markCellsAsUnoccupiedForView(view, mOccupied); |
| 2486 | } |
| 2487 | public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2488 | if (view == null || view.getParent() != mShortcutsAndWidgets) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2489 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2490 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2491 | } |
| 2492 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2493 | private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied, |
| 2494 | boolean value) { |
| 2495 | if (cellX < 0 || cellY < 0) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2496 | for (int x = cellX; x < cellX + spanX && x < mCountX; x++) { |
| 2497 | for (int y = cellY; y < cellY + spanY && y < mCountY; y++) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2498 | occupied[x][y] = value; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2499 | } |
| 2500 | } |
| 2501 | } |
| 2502 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 2503 | public int getDesiredWidth() { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 2504 | return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) + |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 2505 | (Math.max((mCountX - 1), 0) * mWidthGap); |
| 2506 | } |
| 2507 | |
| 2508 | public int getDesiredHeight() { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 2509 | return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) + |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 2510 | (Math.max((mCountY - 1), 0) * mHeightGap); |
| 2511 | } |
| 2512 | |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 2513 | public boolean isOccupied(int x, int y) { |
| 2514 | if (x < mCountX && y < mCountY) { |
| 2515 | return mOccupied[x][y]; |
| 2516 | } else { |
| 2517 | throw new RuntimeException("Position exceeds the bound of this CellLayout"); |
| 2518 | } |
| 2519 | } |
| 2520 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2521 | @Override |
| 2522 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 2523 | return new CellLayout.LayoutParams(getContext(), attrs); |
| 2524 | } |
| 2525 | |
| 2526 | @Override |
| 2527 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 2528 | return p instanceof CellLayout.LayoutParams; |
| 2529 | } |
| 2530 | |
| 2531 | @Override |
| 2532 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 2533 | return new CellLayout.LayoutParams(p); |
| 2534 | } |
| 2535 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2536 | public static class CellLayoutAnimationController extends LayoutAnimationController { |
| 2537 | public CellLayoutAnimationController(Animation animation, float delay) { |
| 2538 | super(animation, delay); |
| 2539 | } |
| 2540 | |
| 2541 | @Override |
| 2542 | protected long getDelayForView(View view) { |
| 2543 | return (int) (Math.random() * 150); |
| 2544 | } |
| 2545 | } |
| 2546 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2547 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
| 2548 | /** |
| 2549 | * Horizontal location of the item in the grid. |
| 2550 | */ |
| 2551 | @ViewDebug.ExportedProperty |
| 2552 | public int cellX; |
| 2553 | |
| 2554 | /** |
| 2555 | * Vertical location of the item in the grid. |
| 2556 | */ |
| 2557 | @ViewDebug.ExportedProperty |
| 2558 | public int cellY; |
| 2559 | |
| 2560 | /** |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2561 | * Temporary horizontal location of the item in the grid during reorder |
| 2562 | */ |
| 2563 | public int tmpCellX; |
| 2564 | |
| 2565 | /** |
| 2566 | * Temporary vertical location of the item in the grid during reorder |
| 2567 | */ |
| 2568 | public int tmpCellY; |
| 2569 | |
| 2570 | /** |
| 2571 | * Indicates that the temporary coordinates should be used to layout the items |
| 2572 | */ |
| 2573 | public boolean useTmpCoords; |
| 2574 | |
| 2575 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2576 | * Number of cells spanned horizontally by the item. |
| 2577 | */ |
| 2578 | @ViewDebug.ExportedProperty |
| 2579 | public int cellHSpan; |
| 2580 | |
| 2581 | /** |
| 2582 | * Number of cells spanned vertically by the item. |
| 2583 | */ |
| 2584 | @ViewDebug.ExportedProperty |
| 2585 | public int cellVSpan; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2586 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2587 | /** |
| 2588 | * Indicates whether the item will set its x, y, width and height parameters freely, |
| 2589 | * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan. |
| 2590 | */ |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2591 | public boolean isLockedToGrid = true; |
| 2592 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2593 | /** |
| 2594 | * Indicates whether this item can be reordered. Always true except in the case of the |
| 2595 | * the AllApps button. |
| 2596 | */ |
| 2597 | public boolean canReorder = true; |
| 2598 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2599 | // X coordinate of the view in the layout. |
| 2600 | @ViewDebug.ExportedProperty |
| 2601 | int x; |
| 2602 | // Y coordinate of the view in the layout. |
| 2603 | @ViewDebug.ExportedProperty |
| 2604 | int y; |
| 2605 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 2606 | boolean dropped; |
Romain Guy | fcb9e71 | 2009-10-02 16:06:52 -0700 | [diff] [blame] | 2607 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2608 | public LayoutParams(Context c, AttributeSet attrs) { |
| 2609 | super(c, attrs); |
| 2610 | cellHSpan = 1; |
| 2611 | cellVSpan = 1; |
| 2612 | } |
| 2613 | |
| 2614 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 2615 | super(source); |
| 2616 | cellHSpan = 1; |
| 2617 | cellVSpan = 1; |
| 2618 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2619 | |
| 2620 | public LayoutParams(LayoutParams source) { |
| 2621 | super(source); |
| 2622 | this.cellX = source.cellX; |
| 2623 | this.cellY = source.cellY; |
| 2624 | this.cellHSpan = source.cellHSpan; |
| 2625 | this.cellVSpan = source.cellVSpan; |
| 2626 | } |
| 2627 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2628 | public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) { |
Romain Guy | 8f19cdd | 2010-01-08 15:07:00 -0800 | [diff] [blame] | 2629 | super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2630 | this.cellX = cellX; |
| 2631 | this.cellY = cellY; |
| 2632 | this.cellHSpan = cellHSpan; |
| 2633 | this.cellVSpan = cellVSpan; |
| 2634 | } |
| 2635 | |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 2636 | public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2637 | if (isLockedToGrid) { |
| 2638 | final int myCellHSpan = cellHSpan; |
| 2639 | final int myCellVSpan = cellVSpan; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2640 | final int myCellX = useTmpCoords ? tmpCellX : cellX; |
| 2641 | final int myCellY = useTmpCoords ? tmpCellY : cellY; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 2642 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2643 | width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) - |
| 2644 | leftMargin - rightMargin; |
| 2645 | height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) - |
| 2646 | topMargin - bottomMargin; |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 2647 | x = (int) (myCellX * (cellWidth + widthGap) + leftMargin); |
| 2648 | y = (int) (myCellY * (cellHeight + heightGap) + topMargin); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2649 | } |
| 2650 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2651 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2652 | public String toString() { |
| 2653 | return "(" + this.cellX + ", " + this.cellY + ")"; |
| 2654 | } |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 2655 | |
| 2656 | public void setWidth(int width) { |
| 2657 | this.width = width; |
| 2658 | } |
| 2659 | |
| 2660 | public int getWidth() { |
| 2661 | return width; |
| 2662 | } |
| 2663 | |
| 2664 | public void setHeight(int height) { |
| 2665 | this.height = height; |
| 2666 | } |
| 2667 | |
| 2668 | public int getHeight() { |
| 2669 | return height; |
| 2670 | } |
| 2671 | |
| 2672 | public void setX(int x) { |
| 2673 | this.x = x; |
| 2674 | } |
| 2675 | |
| 2676 | public int getX() { |
| 2677 | return x; |
| 2678 | } |
| 2679 | |
| 2680 | public void setY(int y) { |
| 2681 | this.y = y; |
| 2682 | } |
| 2683 | |
| 2684 | public int getY() { |
| 2685 | return y; |
| 2686 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2687 | } |
| 2688 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2689 | // This class stores info for two purposes: |
| 2690 | // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY, |
| 2691 | // its spanX, spanY, and the screen it is on |
| 2692 | // 2. When long clicking on an empty cell in a CellLayout, we save information about the |
| 2693 | // cellX and cellY coordinates and which page was clicked. We then set this as a tag on |
| 2694 | // the CellLayout that was long clicked |
Michael Jurka | e5fb0f2 | 2011-04-11 13:27:46 -0700 | [diff] [blame] | 2695 | static final class CellInfo { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2696 | View cell; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 2697 | int cellX = -1; |
| 2698 | int cellY = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2699 | int spanX; |
| 2700 | int spanY; |
| 2701 | int screen; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2702 | long container; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2703 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2704 | @Override |
| 2705 | public String toString() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2706 | return "Cell[view=" + (cell == null ? "null" : cell.getClass()) |
| 2707 | + ", x=" + cellX + ", y=" + cellY + "]"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2708 | } |
| 2709 | } |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 2710 | |
| 2711 | public boolean lastDownOnOccupiedCell() { |
| 2712 | return mLastDownOnOccupiedCell; |
| 2713 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2714 | } |