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 | |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 19 | import java.util.Arrays; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 20 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorListenerAdapter; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 23 | import android.animation.AnimatorSet; |
| 24 | import android.animation.ObjectAnimator; |
Chet Haase | 00397b1 | 2010-10-07 11:13:10 -0700 | [diff] [blame] | 25 | import android.animation.TimeInterpolator; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 26 | import android.animation.ValueAnimator; |
| 27 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 28 | import android.app.WallpaperManager; |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 29 | import android.content.ClipDescription; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.content.Context; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 31 | import android.content.res.Resources; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 32 | import android.content.res.TypedArray; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 33 | import android.graphics.Bitmap; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 34 | import android.graphics.Canvas; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 35 | import android.graphics.Paint; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 36 | import android.graphics.Point; |
| 37 | import android.graphics.PointF; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 38 | import android.graphics.Rect; |
| 39 | import android.graphics.RectF; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 40 | import android.graphics.Region; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 41 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | import android.util.AttributeSet; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 43 | import android.util.Log; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 44 | import android.view.ContextMenu; |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 45 | import android.view.DragEvent; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 46 | import android.view.MotionEvent; |
| 47 | import android.view.View; |
| 48 | import android.view.ViewDebug; |
| 49 | import android.view.ViewGroup; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 50 | import android.view.animation.Animation; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 51 | import android.view.animation.DecelerateInterpolator; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 52 | import android.view.animation.LayoutAnimationController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 53 | |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 54 | import com.android.launcher.R; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 55 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 56 | public class CellLayout extends ViewGroup implements Dimmable { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 57 | static final String TAG = "CellLayout"; |
| 58 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 59 | private int mCellWidth; |
| 60 | private int mCellHeight; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 61 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 62 | private int mLeftPadding; |
| 63 | private int mRightPadding; |
| 64 | private int mTopPadding; |
| 65 | private int mBottomPadding; |
| 66 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 67 | private int mCountX; |
| 68 | private int mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 69 | |
| 70 | private int mWidthGap; |
| 71 | private int mHeightGap; |
| 72 | |
| 73 | private final Rect mRect = new Rect(); |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 74 | private final RectF mRectF = new RectF(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 75 | private final CellInfo mCellInfo = new CellInfo(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 76 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 77 | // These are temporary variables to prevent having to allocate a new object just to |
| 78 | // return an (x, y) value from helper functions. Do NOT use them to maintain other state. |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 79 | private final int[] mTmpCellXY = new int[2]; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 80 | private final int[] mTmpPoint = new int[2]; |
| 81 | private final PointF mTmpPointF = new PointF(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 82 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 83 | boolean[][] mOccupied; |
| 84 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 85 | private OnTouchListener mInterceptTouchListener; |
| 86 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 87 | private float mBackgroundAlpha; |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 88 | private float mBackgroundAlphaMultiplier = 1.0f; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 89 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 90 | private Drawable mBackground; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 91 | private Drawable mBackgroundMini; |
| 92 | private Drawable mBackgroundMiniHover; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 93 | private Drawable mBackgroundHover; |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 94 | private Drawable mBackgroundMiniAcceptsDrops; |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 95 | private Rect mBackgroundRect; |
| 96 | private Rect mHoverRect; |
| 97 | private float mHoverScale; |
| 98 | private float mHoverAlpha; |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 99 | private boolean mAcceptsDrops; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 100 | |
| 101 | // If we're actively dragging something over this screen, mHover is true |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 102 | private boolean mHover = false; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 103 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 104 | private final Point mDragCenter = new Point(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 105 | |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 106 | // 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] | 107 | // They are used as circular arrays, indexed by mDragOutlineCurrent. |
| 108 | private Point[] mDragOutlines = new Point[8]; |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 109 | private float[] mDragOutlineAlphas = new float[mDragOutlines.length]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 110 | private InterruptibleInOutAnimator[] mDragOutlineAnims = |
| 111 | new InterruptibleInOutAnimator[mDragOutlines.length]; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 112 | |
| 113 | // 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] | 114 | private int mDragOutlineCurrent = 0; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 115 | private final Paint mDragOutlinePaint = new Paint(); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 116 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 117 | private Drawable mCrosshairsDrawable = null; |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 118 | private InterruptibleInOutAnimator mCrosshairsAnimator = null; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 119 | private float mCrosshairsVisibility = 0.0f; |
| 120 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 121 | // When a drag operation is in progress, holds the nearest cell to the touch point |
| 122 | private final int[] mDragCell = new int[2]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 123 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 124 | private final WallpaperManager mWallpaperManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 125 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 126 | private boolean mDragging = false; |
| 127 | |
Patrick Dubroy | a944215 | 2010-10-25 11:41:41 -0700 | [diff] [blame] | 128 | private ValueAnimator mDropAnim; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 129 | private TimeInterpolator mEaseOutInterpolator; |
| 130 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 131 | public CellLayout(Context context) { |
| 132 | this(context, null); |
| 133 | } |
| 134 | |
| 135 | public CellLayout(Context context, AttributeSet attrs) { |
| 136 | this(context, attrs, 0); |
| 137 | } |
| 138 | |
| 139 | public CellLayout(Context context, AttributeSet attrs, int defStyle) { |
| 140 | super(context, attrs, defStyle); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 141 | |
| 142 | // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show |
| 143 | // the user where a dragged item will land when dropped. |
| 144 | setWillNotDraw(false); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 145 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 146 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0); |
| 147 | |
| 148 | mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10); |
| 149 | mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10); |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 150 | mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1); |
| 151 | mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 152 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 153 | mLeftPadding = |
| 154 | a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10); |
| 155 | mRightPadding = |
| 156 | a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10); |
| 157 | mTopPadding = |
| 158 | a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10); |
| 159 | mBottomPadding = |
| 160 | a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 161 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 162 | mCountX = LauncherModel.getCellCountX(); |
| 163 | mCountY = LauncherModel.getCellCountY(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 164 | mOccupied = new boolean[mCountX][mCountY]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 165 | |
| 166 | a.recycle(); |
| 167 | |
| 168 | setAlwaysDrawnWithCacheEnabled(false); |
| 169 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 170 | mWallpaperManager = WallpaperManager.getInstance(context); |
| 171 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 172 | final Resources res = getResources(); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 173 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 174 | if (LauncherApplication.isScreenXLarge()) { |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 175 | mBackgroundMini = res.getDrawable(R.drawable.mini_home_screen_bg); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 176 | mBackgroundMini.setFilterBitmap(true); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 177 | mBackground = res.getDrawable(R.drawable.home_screen_bg); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 178 | mBackground.setFilterBitmap(true); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 179 | mBackgroundMiniHover = res.getDrawable(R.drawable.mini_home_screen_bg_hover); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 180 | mBackgroundMiniHover.setFilterBitmap(true); |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 181 | mBackgroundHover = res.getDrawable(R.drawable.home_screen_bg_hover); |
| 182 | mBackgroundHover.setFilterBitmap(true); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 183 | mBackgroundMiniAcceptsDrops = res.getDrawable( |
| 184 | R.drawable.mini_home_screen_bg_accepts_drops); |
| 185 | mBackgroundMiniAcceptsDrops.setFilterBitmap(true); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 186 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 187 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 188 | // Initialize the data structures used for the drag visualization. |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 189 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 190 | mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 191 | mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 192 | |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 193 | // Set up the animation for fading the crosshairs in and out |
| 194 | int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime); |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 195 | mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 196 | mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() { |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 197 | public void onAnimationUpdate(ValueAnimator animation) { |
| 198 | mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue(); |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 199 | invalidate(); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 200 | } |
| 201 | }); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 202 | mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 203 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 204 | for (int i = 0; i < mDragOutlines.length; i++) { |
| 205 | mDragOutlines[i] = new Point(-1, -1); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // When dragging things around the home screens, we show a green outline of |
| 209 | // where the item will land. The outlines gradually fade out, leaving a trail |
| 210 | // behind the drag path. |
| 211 | // Set up all the animations that are used to implement this fading. |
| 212 | final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 213 | final float fromAlphaValue = 0; |
| 214 | final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 215 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 216 | Arrays.fill(mDragOutlineAlphas, fromAlphaValue); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 217 | |
| 218 | for (int i = 0; i < mDragOutlineAnims.length; i++) { |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 219 | final InterruptibleInOutAnimator anim = |
| 220 | new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 221 | anim.getAnimator().setInterpolator(mEaseOutInterpolator); |
Patrick Dubroy | 046e7eb | 2010-10-06 12:14:43 -0700 | [diff] [blame] | 222 | final int thisIndex = i; |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 223 | anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 224 | public void onAnimationUpdate(ValueAnimator animation) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 225 | final Bitmap outline = (Bitmap)anim.getTag(); |
| 226 | |
| 227 | // If an animation is started and then stopped very quickly, we can still |
| 228 | // get spurious updates we've cleared the tag. Guard against this. |
| 229 | if (outline == null) { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 230 | if (false) { |
| 231 | Object val = animation.getAnimatedValue(); |
| 232 | Log.d(TAG, "anim " + thisIndex + " update: " + val + |
| 233 | ", isStopped " + anim.isStopped()); |
| 234 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 235 | // Try to prevent it from continuing to run |
| 236 | animation.cancel(); |
| 237 | } else { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 238 | mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue(); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 239 | final int left = mDragOutlines[thisIndex].x; |
| 240 | final int top = mDragOutlines[thisIndex].y; |
| 241 | CellLayout.this.invalidate(left, top, |
| 242 | left + outline.getWidth(), top + outline.getHeight()); |
| 243 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 244 | } |
| 245 | }); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 246 | // The animation holds a reference to the drag outline bitmap as long is it's |
| 247 | // 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] | 248 | anim.getAnimator().addListener(new AnimatorListenerAdapter() { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 249 | public void onAnimationEnd(Animator animation) { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 250 | if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 251 | anim.setTag(null); |
| 252 | } |
| 253 | } |
| 254 | }); |
| 255 | mDragOutlineAnims[i] = anim; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 256 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 257 | |
Patrick Dubroy | a944215 | 2010-10-25 11:41:41 -0700 | [diff] [blame] | 258 | mDropAnim = ValueAnimator.ofFloat(1.0f, 0.0f); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 259 | mDropAnim.setInterpolator(mEaseOutInterpolator); |
| 260 | |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 261 | mBackgroundRect = new Rect(); |
| 262 | mHoverRect = new Rect(); |
| 263 | setHoverScale(1.0f); |
| 264 | setHoverAlpha(1.0f); |
| 265 | } |
| 266 | |
| 267 | private void updateHoverRect() { |
| 268 | float marginFraction = (mHoverScale - 1.0f) / 2.0f; |
| 269 | int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left)); |
| 270 | int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top)); |
| 271 | mHoverRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY, |
| 272 | mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY); |
| 273 | invalidate(); |
| 274 | } |
| 275 | |
| 276 | public void setHoverScale(float scaleFactor) { |
| 277 | if (scaleFactor != mHoverScale) { |
| 278 | mHoverScale = scaleFactor; |
| 279 | updateHoverRect(); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | public float getHoverScale() { |
| 284 | return mHoverScale; |
| 285 | } |
| 286 | |
| 287 | public float getHoverAlpha() { |
| 288 | return mHoverAlpha; |
| 289 | } |
| 290 | |
| 291 | public void setHoverAlpha(float alpha) { |
| 292 | mHoverAlpha = alpha; |
| 293 | invalidate(); |
| 294 | } |
| 295 | |
| 296 | void animateDrop() { |
| 297 | if (LauncherApplication.isScreenXLarge()) { |
| 298 | Resources res = getResources(); |
| 299 | float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f; |
| 300 | ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale); |
| 301 | scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration)); |
| 302 | ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f); |
| 303 | scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration)); |
| 304 | ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f); |
| 305 | |
| 306 | alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay)); |
| 307 | alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay)); |
| 308 | |
| 309 | AnimatorSet bouncer = new AnimatorSet(); |
| 310 | bouncer.play(scaleUp).before(scaleDown); |
| 311 | bouncer.play(scaleUp).with(alphaFadeOut); |
| 312 | bouncer.addListener(new AnimatorListenerAdapter() { |
| 313 | public void onAnimationStart(Animator animation) { |
| 314 | setHover(true); |
| 315 | } |
| 316 | public void onAnimationEnd(Animator animation) { |
| 317 | setHover(false); |
| 318 | setHoverScale(1.0f); |
| 319 | setHoverAlpha(1.0f); |
| 320 | } |
| 321 | }); |
| 322 | bouncer.start(); |
| 323 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 326 | public void setHover(boolean value) { |
| 327 | if (mHover != value) { |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 328 | mHover = value; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 329 | invalidate(); |
| 330 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 333 | public void drawChildren(Canvas canvas) { |
| 334 | super.dispatchDraw(canvas); |
| 335 | } |
| 336 | |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 337 | @Override |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 338 | protected void onDraw(Canvas canvas) { |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 339 | // When we're large, we are either drawn in a "hover" state (ie when dragging an item to |
| 340 | // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f) |
| 341 | // When we're small, we are either drawn normally or in the "accepts drops" state (during |
| 342 | // a drag). However, we also drag the mini hover background *over* one of those two |
| 343 | // backgrounds |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 344 | if (mBackgroundAlpha > 0.0f) { |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 345 | Drawable bg; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 346 | if (getScaleX() < 0.5f) { |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 347 | bg = mAcceptsDrops ? mBackgroundMiniAcceptsDrops : mBackgroundMini; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 348 | } else { |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 349 | bg = mHover ? mBackgroundHover : mBackground; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 350 | } |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 351 | if (bg != null) { |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 352 | bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255)); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 353 | bg.setBounds(mBackgroundRect); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 354 | bg.draw(canvas); |
| 355 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 356 | if (mHover && getScaleX() < 0.5f) { |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 357 | boolean modifiedClipRect = false; |
| 358 | if (mHoverScale > 1.0f) { |
| 359 | // If the hover background's scale is greater than 1, we'll be drawing outside |
| 360 | // the bounds of this CellLayout. Get around that by temporarily increasing the |
| 361 | // size of the clip rect |
| 362 | float marginFraction = (mHoverScale - 1.0f) / 2.0f; |
| 363 | Rect clipRect = canvas.getClipBounds(); |
| 364 | int marginX = (int) (marginFraction * (clipRect.right - clipRect.left)); |
| 365 | int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top)); |
| 366 | canvas.save(Canvas.CLIP_SAVE_FLAG); |
| 367 | canvas.clipRect(-marginX, -marginY, |
| 368 | getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE); |
| 369 | modifiedClipRect = true; |
| 370 | } |
| 371 | |
| 372 | mBackgroundMiniHover.setAlpha((int) (mBackgroundAlpha * mHoverAlpha * 255)); |
| 373 | mBackgroundMiniHover.setBounds(mHoverRect); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 374 | mBackgroundMiniHover.draw(canvas); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 375 | if (modifiedClipRect) { |
| 376 | canvas.restore(); |
| 377 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 378 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 379 | } |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 380 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 381 | if (mCrosshairsVisibility > 0.0f) { |
| 382 | final int countX = mCountX; |
| 383 | final int countY = mCountY; |
| 384 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 385 | final float MAX_ALPHA = 0.4f; |
| 386 | final int MAX_VISIBLE_DISTANCE = 600; |
| 387 | final float DISTANCE_MULTIPLIER = 0.002f; |
| 388 | |
| 389 | final Drawable d = mCrosshairsDrawable; |
| 390 | final int width = d.getIntrinsicWidth(); |
| 391 | final int height = d.getIntrinsicHeight(); |
| 392 | |
| 393 | int x = getLeftPadding() - (mWidthGap / 2) - (width / 2); |
| 394 | for (int col = 0; col <= countX; col++) { |
| 395 | int y = getTopPadding() - (mHeightGap / 2) - (height / 2); |
| 396 | for (int row = 0; row <= countY; row++) { |
| 397 | mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y); |
| 398 | float dist = mTmpPointF.length(); |
| 399 | // Crosshairs further from the drag point are more faint |
| 400 | float alpha = Math.min(MAX_ALPHA, |
| 401 | DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist)); |
| 402 | if (alpha > 0.0f) { |
| 403 | d.setBounds(x, y, x + width, y + height); |
| 404 | d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility)); |
| 405 | d.draw(canvas); |
| 406 | } |
| 407 | y += mCellHeight + mHeightGap; |
| 408 | } |
| 409 | x += mCellWidth + mWidthGap; |
| 410 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 411 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 412 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 413 | final Paint paint = mDragOutlinePaint; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 414 | for (int i = 0; i < mDragOutlines.length; i++) { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 415 | final float alpha = mDragOutlineAlphas[i]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 416 | if (alpha > 0) { |
| 417 | final Point p = mDragOutlines[i]; |
| 418 | final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag(); |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 419 | paint.setAlpha((int)(alpha + .5f)); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 420 | canvas.drawBitmap(b, p.x, p.y, paint); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 421 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 425 | public void setDimmableProgress(float progress) { |
| 426 | for (int i = 0; i < getChildCount(); i++) { |
| 427 | Dimmable d = (Dimmable) getChildAt(i); |
| 428 | d.setDimmableProgress(progress); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | public float getDimmableProgress() { |
| 433 | if (getChildCount() > 0) { |
| 434 | return ((Dimmable) getChildAt(0)).getDimmableProgress(); |
| 435 | } |
| 436 | return 0.0f; |
| 437 | } |
| 438 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 439 | @Override |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 440 | public void cancelLongPress() { |
| 441 | super.cancelLongPress(); |
| 442 | |
| 443 | // Cancel long press for all children |
| 444 | final int count = getChildCount(); |
| 445 | for (int i = 0; i < count; i++) { |
| 446 | final View child = getChildAt(i); |
| 447 | child.cancelLongPress(); |
| 448 | } |
| 449 | } |
| 450 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 451 | public void setOnInterceptTouchListener(View.OnTouchListener listener) { |
| 452 | mInterceptTouchListener = listener; |
| 453 | } |
| 454 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 455 | int getCountX() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 456 | return mCountX; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | int getCountY() { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 460 | return mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 463 | public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) { |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 464 | return addViewToCellLayout(child, index, childId, params, true); |
| 465 | } |
| 466 | |
| 467 | public boolean addViewToCellLayout( |
| 468 | View child, int index, int childId, LayoutParams params, boolean markCells) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 469 | final LayoutParams lp = params; |
| 470 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 471 | // Generate an id for each view, this assumes we have at most 256x256 cells |
| 472 | // per workspace screen |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 473 | 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] | 474 | // If the horizontal or vertical span is set to -1, it is taken to |
| 475 | // mean that it spans the extent of the CellLayout |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 476 | if (lp.cellHSpan < 0) lp.cellHSpan = mCountX; |
| 477 | if (lp.cellVSpan < 0) lp.cellVSpan = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 478 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 479 | child.setId(childId); |
| 480 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 481 | // We might be in the middle or end of shrinking/fading to a dimmed view |
| 482 | // Make sure this view's alpha is set the same as all the rest of the views |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 483 | child.setAlpha(getAlpha()); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 484 | addView(child, index, lp); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 485 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 486 | if (markCells) markCellsAsOccupiedForView(child); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 487 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 488 | return true; |
| 489 | } |
| 490 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 491 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 492 | public void setAcceptsDrops(boolean acceptsDrops) { |
| 493 | if (mAcceptsDrops != acceptsDrops) { |
| 494 | mAcceptsDrops = acceptsDrops; |
| 495 | invalidate(); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | public boolean getAcceptsDrops() { |
| 500 | return mAcceptsDrops; |
| 501 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 502 | |
| 503 | @Override |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 504 | public void removeAllViews() { |
| 505 | clearOccupiedCells(); |
| 506 | } |
| 507 | |
| 508 | @Override |
| 509 | public void removeAllViewsInLayout() { |
| 510 | clearOccupiedCells(); |
| 511 | } |
| 512 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 513 | public void removeViewWithoutMarkingCells(View view) { |
| 514 | super.removeView(view); |
| 515 | } |
| 516 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 517 | @Override |
| 518 | public void removeView(View view) { |
| 519 | markCellsAsUnoccupiedForView(view); |
| 520 | super.removeView(view); |
| 521 | } |
| 522 | |
| 523 | @Override |
| 524 | public void removeViewAt(int index) { |
| 525 | markCellsAsUnoccupiedForView(getChildAt(index)); |
| 526 | super.removeViewAt(index); |
| 527 | } |
| 528 | |
| 529 | @Override |
| 530 | public void removeViewInLayout(View view) { |
| 531 | markCellsAsUnoccupiedForView(view); |
| 532 | super.removeViewInLayout(view); |
| 533 | } |
| 534 | |
| 535 | @Override |
| 536 | public void removeViews(int start, int count) { |
| 537 | for (int i = start; i < start + count; i++) { |
| 538 | markCellsAsUnoccupiedForView(getChildAt(i)); |
| 539 | } |
| 540 | super.removeViews(start, count); |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public void removeViewsInLayout(int start, int count) { |
| 545 | for (int i = start; i < start + count; i++) { |
| 546 | markCellsAsUnoccupiedForView(getChildAt(i)); |
| 547 | } |
| 548 | super.removeViewsInLayout(start, count); |
| 549 | } |
| 550 | |
| 551 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 552 | public void requestChildFocus(View child, View focused) { |
| 553 | super.requestChildFocus(child, focused); |
| 554 | if (child != null) { |
| 555 | Rect r = new Rect(); |
| 556 | child.getDrawingRect(r); |
| 557 | requestRectangleOnScreen(r); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | @Override |
| 562 | protected void onAttachedToWindow() { |
| 563 | super.onAttachedToWindow(); |
| 564 | mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this); |
| 565 | } |
| 566 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 567 | public void setTagToCellInfoForPoint(int touchX, int touchY) { |
| 568 | final CellInfo cellInfo = mCellInfo; |
| 569 | final Rect frame = mRect; |
| 570 | final int x = touchX + mScrollX; |
| 571 | final int y = touchY + mScrollY; |
| 572 | final int count = getChildCount(); |
| 573 | |
| 574 | boolean found = false; |
| 575 | for (int i = count - 1; i >= 0; i--) { |
| 576 | final View child = getChildAt(i); |
| 577 | |
| 578 | if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) { |
| 579 | child.getHitRect(frame); |
| 580 | if (frame.contains(x, y)) { |
| 581 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 582 | cellInfo.cell = child; |
| 583 | cellInfo.cellX = lp.cellX; |
| 584 | cellInfo.cellY = lp.cellY; |
| 585 | cellInfo.spanX = lp.cellHSpan; |
| 586 | cellInfo.spanY = lp.cellVSpan; |
| 587 | cellInfo.valid = true; |
| 588 | found = true; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 589 | break; |
| 590 | } |
| 591 | } |
| 592 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 593 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 594 | if (!found) { |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 595 | final int cellXY[] = mTmpCellXY; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 596 | pointToCellExact(x, y, cellXY); |
| 597 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 598 | cellInfo.cell = null; |
| 599 | cellInfo.cellX = cellXY[0]; |
| 600 | cellInfo.cellY = cellXY[1]; |
| 601 | cellInfo.spanX = 1; |
| 602 | cellInfo.spanY = 1; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 603 | cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX && |
| 604 | cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]]; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 605 | } |
| 606 | setTag(cellInfo); |
| 607 | } |
| 608 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 609 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 610 | @Override |
| 611 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 612 | if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) { |
| 613 | return true; |
| 614 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 615 | final int action = ev.getAction(); |
| 616 | final CellInfo cellInfo = mCellInfo; |
| 617 | |
| 618 | if (action == MotionEvent.ACTION_DOWN) { |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 619 | setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 620 | } else if (action == MotionEvent.ACTION_UP) { |
| 621 | cellInfo.cell = null; |
| 622 | cellInfo.cellX = -1; |
| 623 | cellInfo.cellY = -1; |
| 624 | cellInfo.spanX = 0; |
| 625 | cellInfo.spanY = 0; |
| 626 | cellInfo.valid = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 627 | setTag(cellInfo); |
| 628 | } |
| 629 | |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | @Override |
| 634 | public CellInfo getTag() { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 635 | return (CellInfo) super.getTag(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 638 | /** |
| 639 | * Check if the row 'y' is empty from columns 'left' to 'right', inclusive. |
| 640 | */ |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 641 | private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) { |
| 642 | for (int x = left; x <= right; x++) { |
| 643 | if (occupied[x][y]) { |
| 644 | return false; |
| 645 | } |
| 646 | } |
| 647 | return true; |
| 648 | } |
| 649 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 650 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 651 | * 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] | 652 | * @param x X coordinate of the point |
| 653 | * @param y Y coordinate of the point |
| 654 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 655 | */ |
| 656 | void pointToCellExact(int x, int y, int[] result) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 657 | final int hStartPadding = getLeftPadding(); |
| 658 | final int vStartPadding = getTopPadding(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 659 | |
| 660 | result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap); |
| 661 | result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap); |
| 662 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 663 | final int xAxis = mCountX; |
| 664 | final int yAxis = mCountY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 665 | |
| 666 | if (result[0] < 0) result[0] = 0; |
| 667 | if (result[0] >= xAxis) result[0] = xAxis - 1; |
| 668 | if (result[1] < 0) result[1] = 0; |
| 669 | if (result[1] >= yAxis) result[1] = yAxis - 1; |
| 670 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 671 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 672 | /** |
| 673 | * Given a point, return the cell that most closely encloses that point |
| 674 | * @param x X coordinate of the point |
| 675 | * @param y Y coordinate of the point |
| 676 | * @param result Array of 2 ints to hold the x and y coordinate of the cell |
| 677 | */ |
| 678 | void pointToCellRounded(int x, int y, int[] result) { |
| 679 | pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result); |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * 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] | 684 | * |
| 685 | * @param cellX X coordinate of the cell |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 686 | * @param cellY Y coordinate of the cell |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 687 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 688 | * @param result Array of 2 ints to hold the x and y coordinate of the point |
| 689 | */ |
| 690 | void cellToPoint(int cellX, int cellY, int[] result) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 691 | final int hStartPadding = getLeftPadding(); |
| 692 | final int vStartPadding = getTopPadding(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 693 | |
| 694 | result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap); |
| 695 | result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap); |
| 696 | } |
| 697 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 698 | int getCellWidth() { |
| 699 | return mCellWidth; |
| 700 | } |
| 701 | |
| 702 | int getCellHeight() { |
| 703 | return mCellHeight; |
| 704 | } |
| 705 | |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 706 | int getLeftPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 707 | return mLeftPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | int getTopPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 711 | return mTopPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | int getRightPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 715 | return mRightPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | int getBottomPadding() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 719 | return mBottomPadding; |
Romain Guy | 1a304a1 | 2009-11-10 00:02:32 -0800 | [diff] [blame] | 720 | } |
| 721 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 722 | @Override |
| 723 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 724 | // TODO: currently ignoring padding |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 725 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 726 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 727 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 728 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 729 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 730 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 731 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 732 | if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { |
| 733 | throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); |
| 734 | } |
| 735 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 736 | final int cellWidth = mCellWidth; |
| 737 | final int cellHeight = mCellHeight; |
| 738 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 739 | int numWidthGaps = mCountX - 1; |
| 740 | int numHeightGaps = mCountY - 1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 741 | |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 742 | if (mWidthGap < 0 || mHeightGap < 0) { |
| 743 | int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY); |
| 744 | mHeightGap = vSpaceLeft / numHeightGaps; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 745 | |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 746 | int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX); |
| 747 | mWidthGap = hSpaceLeft / numWidthGaps; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 748 | |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 749 | // center it around the min gaps |
| 750 | int minGap = Math.min(mWidthGap, mHeightGap); |
| 751 | mWidthGap = mHeightGap = minGap; |
| 752 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 753 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 754 | int count = getChildCount(); |
| 755 | |
| 756 | for (int i = 0; i < count; i++) { |
| 757 | View child = getChildAt(i); |
| 758 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 759 | lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, |
| 760 | mLeftPadding, mTopPadding); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 761 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 762 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 763 | int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, |
| 764 | MeasureSpec.EXACTLY); |
| 765 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 766 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 767 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 768 | if (widthSpecMode == MeasureSpec.AT_MOST) { |
| 769 | int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) + |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 770 | ((mCountX - 1) * mWidthGap); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 771 | int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) + |
Winson Chung | ece7f5b | 2010-10-22 14:54:12 -0700 | [diff] [blame] | 772 | ((mCountY - 1) * mHeightGap); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 773 | setMeasuredDimension(newWidth, newHeight); |
| 774 | } else if (widthSpecMode == MeasureSpec.EXACTLY) { |
| 775 | setMeasuredDimension(widthSpecSize, heightSpecSize); |
| 776 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 777 | } |
| 778 | |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 779 | /** |
| 780 | * Animate a child of this CellLayout into its current layout position. |
| 781 | * The position to animate from is given by the oldX and oldY values in its LayoutParams. |
| 782 | */ |
| 783 | private void animateChildIntoPosition(final View child) { |
| 784 | final Resources res = getResources(); |
Patrick Dubroy | a944215 | 2010-10-25 11:41:41 -0700 | [diff] [blame] | 785 | final ValueAnimator anim = mDropAnim; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 786 | final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 787 | final float startX = lp.oldX - lp.x; |
| 788 | final float startY = lp.oldY - lp.y; |
| 789 | |
| 790 | // Calculate the duration of the animation based on the object's distance |
| 791 | final float dist = (float) Math.sqrt(startX*startX + startY*startY); |
| 792 | final float maxDist = (float) res.getInteger(R.integer.config_dropAnimMaxDist); |
| 793 | final int duration = (int) (res.getInteger(R.integer.config_dropAnimMaxDuration) |
| 794 | * mEaseOutInterpolator.getInterpolation(dist / maxDist)); |
| 795 | |
Patrick Dubroy | a944215 | 2010-10-25 11:41:41 -0700 | [diff] [blame] | 796 | anim.end(); // Make sure it's not already running |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 797 | anim.setDuration(duration); |
Patrick Dubroy | a944215 | 2010-10-25 11:41:41 -0700 | [diff] [blame] | 798 | anim.setFloatValues(1.0f, 0.0f); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 799 | anim.removeAllUpdateListeners(); |
| 800 | anim.addUpdateListener(new AnimatorUpdateListener() { |
| 801 | public void onAnimationUpdate(ValueAnimator animation) { |
Patrick Dubroy | a944215 | 2010-10-25 11:41:41 -0700 | [diff] [blame] | 802 | final float value = (Float) anim.getAnimatedValue(); |
| 803 | child.setTranslationX(startX * value); |
| 804 | child.setTranslationY(startY * value); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 805 | } |
| 806 | }); |
| 807 | anim.start(); |
| 808 | } |
| 809 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 810 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 811 | 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] | 812 | int count = getChildCount(); |
| 813 | |
| 814 | for (int i = 0; i < count; i++) { |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 815 | final View child = getChildAt(i); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 816 | if (child.getVisibility() != GONE) { |
| 817 | |
| 818 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 819 | |
| 820 | int childLeft = lp.x; |
| 821 | int childTop = lp.y; |
| 822 | child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 823 | |
| 824 | if (lp.dropped) { |
| 825 | lp.dropped = false; |
| 826 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 827 | final int[] cellXY = mTmpCellXY; |
Romain Guy | 06762ab | 2010-01-25 16:51:08 -0800 | [diff] [blame] | 828 | getLocationOnScreen(cellXY); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 829 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop", |
Romain Guy | 06762ab | 2010-01-25 16:51:08 -0800 | [diff] [blame] | 830 | cellXY[0] + childLeft + lp.width / 2, |
| 831 | cellXY[1] + childTop + lp.height / 2, 0, null); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 832 | |
| 833 | animateChildIntoPosition(child); |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 834 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | @Override |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 840 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 841 | super.onSizeChanged(w, h, oldw, oldh); |
Michael Jurka | 1801479 | 2010-10-14 09:01:34 -0700 | [diff] [blame] | 842 | mBackgroundRect.set(0, 0, w, h); |
| 843 | updateHoverRect(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 847 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 848 | final int count = getChildCount(); |
| 849 | for (int i = 0; i < count; i++) { |
| 850 | final View view = getChildAt(i); |
| 851 | view.setDrawingCacheEnabled(enabled); |
| 852 | // Update the drawing caches |
Romain Guy | 3cf4c03 | 2010-10-26 14:29:35 -0700 | [diff] [blame] | 853 | if (!view.isHardwareAccelerated()) { |
| 854 | view.buildDrawingCache(true); |
| 855 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
| 859 | @Override |
| 860 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 861 | super.setChildrenDrawnWithCacheEnabled(enabled); |
| 862 | } |
| 863 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 864 | public float getBackgroundAlpha() { |
| 865 | return mBackgroundAlpha; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 868 | public void setBackgroundAlphaMultiplier(float multiplier) { |
| 869 | mBackgroundAlphaMultiplier = multiplier; |
| 870 | } |
| 871 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 872 | public void setBackgroundAlpha(float alpha) { |
| 873 | mBackgroundAlpha = alpha; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 874 | invalidate(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 877 | // Need to return true to let the view system know we know how to handle alpha-- this is |
| 878 | // because when our children have an alpha of 0.0f, they are still rendering their "dimmed" |
| 879 | // versions |
| 880 | @Override |
| 881 | protected boolean onSetAlpha(int alpha) { |
| 882 | return true; |
| 883 | } |
| 884 | |
| 885 | public void setAlpha(float alpha) { |
| 886 | setChildrenAlpha(alpha); |
| 887 | super.setAlpha(alpha); |
| 888 | } |
| 889 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 890 | private void setChildrenAlpha(float alpha) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 891 | final int childCount = getChildCount(); |
| 892 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 893 | getChildAt(i).setAlpha(alpha); |
| 894 | } |
| 895 | } |
| 896 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 897 | private boolean isVacantIgnoring( |
| 898 | int originX, int originY, int spanX, int spanY, View ignoreView) { |
| 899 | if (ignoreView != null) { |
| 900 | markCellsAsUnoccupiedForView(ignoreView); |
| 901 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 902 | boolean isVacant = true; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 903 | for (int i = 0; i < spanY; i++) { |
| 904 | if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) { |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 905 | isVacant = false; |
| 906 | break; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 909 | if (ignoreView != null) { |
| 910 | markCellsAsOccupiedForView(ignoreView); |
| 911 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 912 | return isVacant; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 915 | private boolean isVacant(int originX, int originY, int spanX, int spanY) { |
| 916 | return isVacantIgnoring(originX, originY, spanX, spanY, null); |
| 917 | } |
| 918 | |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 919 | public View getChildAt(int x, int y) { |
| 920 | final int count = getChildCount(); |
| 921 | for (int i = 0; i < count; i++) { |
| 922 | View child = getChildAt(i); |
| 923 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 924 | |
| 925 | if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) && |
| 926 | (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) { |
| 927 | return child; |
| 928 | } |
| 929 | } |
| 930 | return null; |
| 931 | } |
| 932 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 933 | /** |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 934 | * Estimate the size that a child with the given dimensions will take in the layout. |
| 935 | */ |
| 936 | void estimateChildSize(int minWidth, int minHeight, int[] result) { |
| 937 | // Assuming it's placed at 0, 0, find where the bottom right cell will land |
| 938 | rectToCell(minWidth, minHeight, result); |
| 939 | |
| 940 | // Then figure out the rect it will occupy |
| 941 | cellToRect(0, 0, result[0], result[1], mRectF); |
| 942 | result[0] = (int)mRectF.width(); |
| 943 | result[1] = (int)mRectF.height(); |
| 944 | } |
| 945 | |
| 946 | /** |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 947 | * Estimate where the top left cell of the dragged item will land if it is dropped. |
| 948 | * |
| 949 | * @param originX The X value of the top left corner of the item |
| 950 | * @param originY The Y value of the top left corner of the item |
| 951 | * @param spanX The number of horizontal cells that the item spans |
| 952 | * @param spanY The number of vertical cells that the item spans |
| 953 | * @param result The estimated drop cell X and Y. |
| 954 | */ |
| 955 | void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 956 | final int countX = mCountX; |
| 957 | final int countY = mCountY; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 958 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 959 | // pointToCellRounded takes the top left of a cell but will pad that with |
| 960 | // cellWidth/2 and cellHeight/2 when finding the matching cell |
| 961 | pointToCellRounded(originX, originY, result); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 962 | |
| 963 | // If the item isn't fully on this screen, snap to the edges |
| 964 | int rightOverhang = result[0] + spanX - countX; |
| 965 | if (rightOverhang > 0) { |
| 966 | result[0] -= rightOverhang; // Snap to right |
| 967 | } |
| 968 | result[0] = Math.max(0, result[0]); // Snap to left |
| 969 | int bottomOverhang = result[1] + spanY - countY; |
| 970 | if (bottomOverhang > 0) { |
| 971 | result[1] -= bottomOverhang; // Snap to bottom |
| 972 | } |
| 973 | result[1] = Math.max(0, result[1]); // Snap to top |
| 974 | } |
| 975 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 976 | void visualizeDropLocation( |
| 977 | View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) { |
| 978 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 979 | final int oldDragCellX = mDragCell[0]; |
| 980 | final int oldDragCellY = mDragCell[1]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 981 | final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell); |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 982 | if (v != null) { |
| 983 | mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2)); |
| 984 | } else { |
| 985 | mDragCenter.set(originX, originY); |
| 986 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 987 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 988 | if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) { |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 989 | // Find the top left corner of the rect the object will occupy |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 990 | final int[] topLeft = mTmpPoint; |
| 991 | cellToPoint(nearest[0], nearest[1], topLeft); |
| 992 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 993 | int left = topLeft[0]; |
| 994 | int top = topLeft[1]; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 995 | |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 996 | if (v != null) { |
| 997 | if (v.getParent() instanceof CellLayout) { |
| 998 | LayoutParams lp = (LayoutParams) v.getLayoutParams(); |
| 999 | left += lp.leftMargin; |
| 1000 | top += lp.topMargin; |
| 1001 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1002 | |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1003 | // Offsets due to the size difference between the View and the dragOutline |
| 1004 | left += (v.getWidth() - dragOutline.getWidth()) / 2; |
| 1005 | top += (v.getHeight() - dragOutline.getHeight()) / 2; |
| 1006 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1007 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1008 | final int oldIndex = mDragOutlineCurrent; |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1009 | mDragOutlineAnims[oldIndex].animateOut(); |
| 1010 | mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1011 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1012 | mDragOutlines[mDragOutlineCurrent].set(left, top); |
| 1013 | mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline); |
| 1014 | mDragOutlineAnims[mDragOutlineCurrent].animateIn(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1015 | } |
Patrick Dubroy | 49250ad | 2010-10-08 15:33:52 -0700 | [diff] [blame] | 1016 | |
| 1017 | // If we are drawing crosshairs, the entire CellLayout needs to be invalidated |
| 1018 | if (mCrosshairsDrawable != null) { |
| 1019 | invalidate(); |
| 1020 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1023 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1024 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1025 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1026 | * |
Romain Guy | 51afc02 | 2009-05-04 18:03:43 -0700 | [diff] [blame] | 1027 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1028 | * @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] | 1029 | * @param spanX Horizontal span of the object. |
| 1030 | * @param spanY Vertical span of the object. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1031 | * @param result Array in which to place the result, or null (in which case a new array will |
| 1032 | * be allocated) |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1033 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1034 | * nearest the requested location. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1035 | */ |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1036 | int[] findNearestVacantArea( |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1037 | int pixelX, int pixelY, int spanX, int spanY, int[] result) { |
| 1038 | return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result); |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1039 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1040 | |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1041 | /** |
| 1042 | * Find a vacant area that will fit the given bounds nearest the requested |
| 1043 | * cell location. Uses Euclidean distance to score multiple vacant areas. |
| 1044 | * |
| 1045 | * @param pixelX The X location at which you want to search for a vacant area. |
| 1046 | * @param pixelY The Y location at which you want to search for a vacant area. |
| 1047 | * @param spanX Horizontal span of the object. |
| 1048 | * @param spanY Vertical span of the object. |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1049 | * @param ignoreView Considers space occupied by this view as unoccupied |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1050 | * @param result Previously returned value to possibly recycle. |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1051 | * @return The X, Y cell of a vacant area that can contain this object, |
| 1052 | * nearest the requested location. |
| 1053 | */ |
| 1054 | int[] findNearestVacantArea( |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1055 | int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1056 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
| 1057 | markCellsAsUnoccupiedForView(ignoreView); |
| 1058 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1059 | // Keep track of best-scoring drop area |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1060 | final int[] bestXY = result != null ? result : new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1061 | double bestDistance = Double.MAX_VALUE; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1062 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1063 | final int countX = mCountX; |
| 1064 | final int countY = mCountY; |
| 1065 | final boolean[][] occupied = mOccupied; |
| 1066 | |
| 1067 | for (int x = 0; x < countX - (spanX - 1); x++) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1068 | inner: |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1069 | for (int y = 0; y < countY - (spanY - 1); y++) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1070 | for (int i = 0; i < spanX; i++) { |
| 1071 | for (int j = 0; j < spanY; j++) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1072 | if (occupied[x + i][y + j]) { |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1073 | // small optimization: we can skip to below the row we just found |
| 1074 | // an occupied cell |
| 1075 | y += j; |
| 1076 | continue inner; |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | final int[] cellXY = mTmpCellXY; |
| 1081 | cellToPoint(x, y, cellXY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1082 | |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1083 | double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2) |
| 1084 | + Math.pow(cellXY[1] - pixelY, 2)); |
| 1085 | if (distance <= bestDistance) { |
| 1086 | bestDistance = distance; |
| 1087 | bestXY[0] = x; |
| 1088 | bestXY[1] = y; |
| 1089 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1090 | } |
| 1091 | } |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1092 | // re-mark space taken by ignoreView as occupied |
| 1093 | markCellsAsOccupiedForView(ignoreView); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1094 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1095 | // Return null if no suitable location found |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1096 | if (bestDistance < Double.MAX_VALUE) { |
| 1097 | return bestXY; |
| 1098 | } else { |
| 1099 | return null; |
| 1100 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1101 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1102 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1103 | boolean existsEmptyCell() { |
| 1104 | return findCellForSpan(null, 1, 1); |
| 1105 | } |
| 1106 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1107 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1108 | * Finds the upper-left coordinate of the first rectangle in the grid that can |
| 1109 | * hold a cell of the specified dimensions. If intersectX and intersectY are not -1, |
| 1110 | * then this method will only return coordinates for rectangles that contain the cell |
| 1111 | * (intersectX, intersectY) |
| 1112 | * |
| 1113 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 1114 | * can be found. |
| 1115 | * @param spanX The horizontal span of the cell we want to find. |
| 1116 | * @param spanY The vertical span of the cell we want to find. |
| 1117 | * |
| 1118 | * @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] | 1119 | */ |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1120 | boolean findCellForSpan(int[] cellXY, int spanX, int spanY) { |
| 1121 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null); |
| 1122 | } |
| 1123 | |
| 1124 | /** |
| 1125 | * Like above, but ignores any cells occupied by the item "ignoreView" |
| 1126 | * |
| 1127 | * @param cellXY The array that will contain the position of a vacant cell if such a cell |
| 1128 | * can be found. |
| 1129 | * @param spanX The horizontal span of the cell we want to find. |
| 1130 | * @param spanY The vertical span of the cell we want to find. |
| 1131 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 1132 | * @return |
| 1133 | */ |
| 1134 | boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) { |
| 1135 | return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView); |
| 1136 | } |
| 1137 | |
| 1138 | /** |
| 1139 | * Like above, but if intersectX and intersectY are not -1, then this method will try to |
| 1140 | * return coordinates for rectangles that contain the cell [intersectX, intersectY] |
| 1141 | * |
| 1142 | * @param spanX The horizontal span of the cell we want to find. |
| 1143 | * @param spanY The vertical span of the cell we want to find. |
| 1144 | * @param ignoreView The home screen item we should treat as not occupying any space |
| 1145 | * @param intersectX The X coordinate of the cell that we should try to overlap |
| 1146 | * @param intersectX The Y coordinate of the cell that we should try to overlap |
| 1147 | * |
| 1148 | * @return True if a vacant cell of the specified dimension was found, false otherwise. |
| 1149 | */ |
| 1150 | boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY, |
| 1151 | int intersectX, int intersectY) { |
| 1152 | return findCellForSpanThatIntersectsIgnoring( |
| 1153 | cellXY, spanX, spanY, intersectX, intersectY, null); |
| 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | * The superset of the above two methods |
| 1158 | */ |
| 1159 | boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY, |
| 1160 | int intersectX, int intersectY, View ignoreView) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1161 | // mark space take by ignoreView as available (method checks if ignoreView is null) |
| 1162 | markCellsAsUnoccupiedForView(ignoreView); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1163 | |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1164 | boolean foundCell = false; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1165 | while (true) { |
| 1166 | int startX = 0; |
| 1167 | if (intersectX >= 0) { |
| 1168 | startX = Math.max(startX, intersectX - (spanX - 1)); |
| 1169 | } |
| 1170 | int endX = mCountX - (spanX - 1); |
| 1171 | if (intersectX >= 0) { |
| 1172 | endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0)); |
| 1173 | } |
| 1174 | int startY = 0; |
| 1175 | if (intersectY >= 0) { |
| 1176 | startY = Math.max(startY, intersectY - (spanY - 1)); |
| 1177 | } |
| 1178 | int endY = mCountY - (spanY - 1); |
| 1179 | if (intersectY >= 0) { |
| 1180 | endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0)); |
| 1181 | } |
| 1182 | |
| 1183 | for (int x = startX; x < endX; x++) { |
| 1184 | inner: |
| 1185 | for (int y = startY; y < endY; y++) { |
| 1186 | for (int i = 0; i < spanX; i++) { |
| 1187 | for (int j = 0; j < spanY; j++) { |
| 1188 | if (mOccupied[x + i][y + j]) { |
| 1189 | // small optimization: we can skip to below the row we just found |
| 1190 | // an occupied cell |
| 1191 | y += j; |
| 1192 | continue inner; |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | if (cellXY != null) { |
| 1197 | cellXY[0] = x; |
| 1198 | cellXY[1] = y; |
| 1199 | } |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1200 | foundCell = true; |
| 1201 | break; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | if (intersectX == -1 && intersectY == -1) { |
| 1205 | break; |
| 1206 | } else { |
| 1207 | // if we failed to find anything, try again but without any requirements of |
| 1208 | // intersecting |
| 1209 | intersectX = -1; |
| 1210 | intersectY = -1; |
| 1211 | continue; |
| 1212 | } |
| 1213 | } |
| 1214 | |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1215 | // re-mark space taken by ignoreView as occupied |
| 1216 | markCellsAsOccupiedForView(ignoreView); |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1217 | return foundCell; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | /** |
| 1221 | * Called when drag has left this CellLayout or has been completed (successfully or not) |
| 1222 | */ |
| 1223 | void onDragExit() { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1224 | // This can actually be called when we aren't in a drag, e.g. when adding a new |
| 1225 | // item to this layout via the customize drawer. |
| 1226 | // Guard against that case. |
| 1227 | if (mDragging) { |
| 1228 | mDragging = false; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1229 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1230 | // Fade out the drag indicators |
| 1231 | if (mCrosshairsAnimator != null) { |
| 1232 | mCrosshairsAnimator.animateOut(); |
| 1233 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1234 | } |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 1235 | |
| 1236 | // Invalidate the drag data |
| 1237 | mDragCell[0] = -1; |
| 1238 | mDragCell[1] = -1; |
| 1239 | mDragOutlineAnims[mDragOutlineCurrent].animateOut(); |
| 1240 | mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length; |
| 1241 | |
| 1242 | setHover(false); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1246 | * Mark a child as having been dropped. |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1247 | * 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] | 1248 | * 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] | 1249 | * |
| 1250 | * @param child The child that is being dropped |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1251 | */ |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1252 | void onDropChild(View child) { |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1253 | if (child != null) { |
| 1254 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1255 | lp.isDragging = false; |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 1256 | lp.dropped = true; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 1257 | child.setVisibility(View.VISIBLE); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1258 | child.requestLayout(); |
Romain Guy | d94533d | 2009-08-17 10:01:15 -0700 | [diff] [blame] | 1259 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | void onDropAborted(View child) { |
| 1263 | if (child != null) { |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 1264 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1265 | lp.isDragging = false; |
| 1266 | child.setVisibility(View.VISIBLE); |
| 1267 | animateChildIntoPosition(child); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1268 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | /** |
| 1272 | * Start dragging the specified child |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1273 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1274 | * @param child The child that is being dragged |
| 1275 | */ |
| 1276 | void onDragChild(View child) { |
| 1277 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1278 | lp.isDragging = true; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 1279 | child.setVisibility(View.GONE); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | /** |
| 1283 | * A drag event has begun over this layout. |
| 1284 | * It may have begun over this layout (in which case onDragChild is called first), |
| 1285 | * or it may have begun on another layout. |
| 1286 | */ |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 1287 | void onDragEnter() { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 1288 | if (!mDragging) { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 1289 | // Fade in the drag indicators |
| 1290 | if (mCrosshairsAnimator != null) { |
| 1291 | mCrosshairsAnimator.animateIn(); |
| 1292 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1293 | } |
| 1294 | mDragging = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1295 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1296 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1297 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1298 | * Computes a bounding rectangle for a range of cells |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1299 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1300 | * @param cellX X coordinate of upper left corner expressed as a cell position |
| 1301 | * @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] | 1302 | * @param cellHSpan Width in cells |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1303 | * @param cellVSpan Height in cells |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1304 | * @param resultRect Rect into which to put the results |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1305 | */ |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1306 | public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1307 | final int cellWidth = mCellWidth; |
| 1308 | final int cellHeight = mCellHeight; |
| 1309 | final int widthGap = mWidthGap; |
| 1310 | final int heightGap = mHeightGap; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1311 | |
| 1312 | final int hStartPadding = getLeftPadding(); |
| 1313 | final int vStartPadding = getTopPadding(); |
| 1314 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1315 | int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap); |
| 1316 | int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap); |
| 1317 | |
| 1318 | int x = hStartPadding + cellX * (cellWidth + widthGap); |
| 1319 | int y = vStartPadding + cellY * (cellHeight + heightGap); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1320 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1321 | resultRect.set(x, y, x + width, y + height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1322 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1323 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1324 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1325 | * 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] | 1326 | * fit the given rectangle. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1327 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1328 | * @param width Width in pixels |
| 1329 | * @param height Height in pixels |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1330 | * @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] | 1331 | */ |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1332 | public int[] rectToCell(int width, int height, int[] result) { |
Michael Jurka | 9987a5c | 2010-10-08 16:58:12 -0700 | [diff] [blame] | 1333 | return rectToCell(getResources(), width, height, result); |
| 1334 | } |
| 1335 | |
| 1336 | 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] | 1337 | // Always assume we're working with the smallest span to make sure we |
| 1338 | // reserve enough space in both orientations. |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1339 | int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width); |
| 1340 | int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1341 | int smallerSize = Math.min(actualWidth, actualHeight); |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1342 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1343 | // Always round up to next largest cell |
| 1344 | int spanX = (width + smallerSize) / smallerSize; |
| 1345 | int spanY = (height + smallerSize) / smallerSize; |
Joe Onorato | 79e5626 | 2009-09-21 15:23:04 -0400 | [diff] [blame] | 1346 | |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1347 | if (result == null) { |
| 1348 | return new int[] { spanX, spanY }; |
| 1349 | } |
| 1350 | result[0] = spanX; |
| 1351 | result[1] = spanY; |
| 1352 | return result; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | /** |
| 1356 | * Find the first vacant cell, if there is one. |
| 1357 | * |
| 1358 | * @param vacant Holds the x and y coordinate of the vacant cell |
| 1359 | * @param spanX Horizontal cell span. |
| 1360 | * @param spanY Vertical cell span. |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1361 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1362 | * @return True if a vacant cell was found |
| 1363 | */ |
| 1364 | public boolean getVacantCell(int[] vacant, int spanX, int spanY) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1365 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1366 | return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | static boolean findVacantCell(int[] vacant, int spanX, int spanY, |
| 1370 | int xCount, int yCount, boolean[][] occupied) { |
| 1371 | |
| 1372 | for (int x = 0; x < xCount; x++) { |
| 1373 | for (int y = 0; y < yCount; y++) { |
| 1374 | boolean available = !occupied[x][y]; |
| 1375 | out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) { |
| 1376 | for (int j = y; j < y + spanY - 1 && y < yCount; j++) { |
| 1377 | available = available && !occupied[i][j]; |
| 1378 | if (!available) break out; |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | if (available) { |
| 1383 | vacant[0] = x; |
| 1384 | vacant[1] = y; |
| 1385 | return true; |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | return false; |
| 1391 | } |
| 1392 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1393 | private void clearOccupiedCells() { |
| 1394 | for (int x = 0; x < mCountX; x++) { |
| 1395 | for (int y = 0; y < mCountY; y++) { |
| 1396 | mOccupied[x][y] = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1397 | } |
| 1398 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1399 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1400 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1401 | public void onMove(View view, int newCellX, int newCellY) { |
| 1402 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1403 | markCellsAsUnoccupiedForView(view); |
| 1404 | markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true); |
| 1405 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1406 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1407 | private void markCellsAsOccupiedForView(View view) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1408 | if (view == null || view.getParent() != this) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1409 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1410 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true); |
| 1411 | } |
| 1412 | |
| 1413 | private void markCellsAsUnoccupiedForView(View view) { |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 1414 | if (view == null || view.getParent() != this) return; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1415 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1416 | markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false); |
| 1417 | } |
| 1418 | |
| 1419 | private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) { |
| 1420 | for (int x = cellX; x < cellX + spanX && x < mCountX; x++) { |
| 1421 | for (int y = cellY; y < cellY + spanY && y < mCountY; y++) { |
| 1422 | mOccupied[x][y] = value; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1423 | } |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | @Override |
| 1428 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 1429 | return new CellLayout.LayoutParams(getContext(), attrs); |
| 1430 | } |
| 1431 | |
| 1432 | @Override |
| 1433 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 1434 | return p instanceof CellLayout.LayoutParams; |
| 1435 | } |
| 1436 | |
| 1437 | @Override |
| 1438 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 1439 | return new CellLayout.LayoutParams(p); |
| 1440 | } |
| 1441 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1442 | public static class CellLayoutAnimationController extends LayoutAnimationController { |
| 1443 | public CellLayoutAnimationController(Animation animation, float delay) { |
| 1444 | super(animation, delay); |
| 1445 | } |
| 1446 | |
| 1447 | @Override |
| 1448 | protected long getDelayForView(View view) { |
| 1449 | return (int) (Math.random() * 150); |
| 1450 | } |
| 1451 | } |
| 1452 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1453 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
| 1454 | /** |
| 1455 | * Horizontal location of the item in the grid. |
| 1456 | */ |
| 1457 | @ViewDebug.ExportedProperty |
| 1458 | public int cellX; |
| 1459 | |
| 1460 | /** |
| 1461 | * Vertical location of the item in the grid. |
| 1462 | */ |
| 1463 | @ViewDebug.ExportedProperty |
| 1464 | public int cellY; |
| 1465 | |
| 1466 | /** |
| 1467 | * Number of cells spanned horizontally by the item. |
| 1468 | */ |
| 1469 | @ViewDebug.ExportedProperty |
| 1470 | public int cellHSpan; |
| 1471 | |
| 1472 | /** |
| 1473 | * Number of cells spanned vertically by the item. |
| 1474 | */ |
| 1475 | @ViewDebug.ExportedProperty |
| 1476 | public int cellVSpan; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1477 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1478 | /** |
| 1479 | * Is this item currently being dragged |
| 1480 | */ |
| 1481 | public boolean isDragging; |
| 1482 | |
| 1483 | // X coordinate of the view in the layout. |
| 1484 | @ViewDebug.ExportedProperty |
| 1485 | int x; |
| 1486 | // Y coordinate of the view in the layout. |
| 1487 | @ViewDebug.ExportedProperty |
| 1488 | int y; |
| 1489 | |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 1490 | /** |
| 1491 | * The old X coordinate of this item, relative to its current parent. |
| 1492 | * Used to animate the item into its new position. |
| 1493 | */ |
| 1494 | int oldX; |
| 1495 | |
| 1496 | /** |
| 1497 | * The old Y coordinate of this item, relative to its current parent. |
| 1498 | * Used to animate the item into its new position. |
| 1499 | */ |
| 1500 | int oldY; |
| 1501 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 1502 | boolean dropped; |
Romain Guy | fcb9e71 | 2009-10-02 16:06:52 -0700 | [diff] [blame] | 1503 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1504 | public LayoutParams(Context c, AttributeSet attrs) { |
| 1505 | super(c, attrs); |
| 1506 | cellHSpan = 1; |
| 1507 | cellVSpan = 1; |
| 1508 | } |
| 1509 | |
| 1510 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 1511 | super(source); |
| 1512 | cellHSpan = 1; |
| 1513 | cellVSpan = 1; |
| 1514 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1515 | |
| 1516 | public LayoutParams(LayoutParams source) { |
| 1517 | super(source); |
| 1518 | this.cellX = source.cellX; |
| 1519 | this.cellY = source.cellY; |
| 1520 | this.cellHSpan = source.cellHSpan; |
| 1521 | this.cellVSpan = source.cellVSpan; |
| 1522 | } |
| 1523 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1524 | public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) { |
Romain Guy | 8f19cdd | 2010-01-08 15:07:00 -0800 | [diff] [blame] | 1525 | super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1526 | this.cellX = cellX; |
| 1527 | this.cellY = cellY; |
| 1528 | this.cellHSpan = cellHSpan; |
| 1529 | this.cellVSpan = cellVSpan; |
| 1530 | } |
| 1531 | |
| 1532 | public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap, |
| 1533 | int hStartPadding, int vStartPadding) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1534 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1535 | final int myCellHSpan = cellHSpan; |
| 1536 | final int myCellVSpan = cellVSpan; |
| 1537 | final int myCellX = cellX; |
| 1538 | final int myCellY = cellY; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1539 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1540 | width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) - |
| 1541 | leftMargin - rightMargin; |
| 1542 | height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) - |
| 1543 | topMargin - bottomMargin; |
| 1544 | |
| 1545 | x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin; |
| 1546 | y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin; |
| 1547 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1548 | |
| 1549 | public String toString() { |
| 1550 | return "(" + this.cellX + ", " + this.cellY + ")"; |
| 1551 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1552 | } |
| 1553 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1554 | // This class stores info for two purposes: |
| 1555 | // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY, |
| 1556 | // its spanX, spanY, and the screen it is on |
| 1557 | // 2. When long clicking on an empty cell in a CellLayout, we save information about the |
| 1558 | // cellX and cellY coordinates and which page was clicked. We then set this as a tag on |
| 1559 | // the CellLayout that was long clicked |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1560 | static final class CellInfo implements ContextMenu.ContextMenuInfo { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1561 | View cell; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1562 | int cellX = -1; |
| 1563 | int cellY = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1564 | int spanX; |
| 1565 | int spanY; |
| 1566 | int screen; |
| 1567 | boolean valid; |
| 1568 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1569 | @Override |
| 1570 | public String toString() { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1571 | return "Cell[view=" + (cell == null ? "null" : cell.getClass()) |
| 1572 | + ", x=" + cellX + ", y=" + cellY + "]"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1573 | } |
| 1574 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1575 | } |