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