blob: 11c0c1822d714a9d751ccd7990eb1bc2841d3cd2 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Michael Jurka3c4c20f2010-10-28 15:36:06 -070019import com.android.launcher.R;
Winson Chungaafa03c2010-06-11 17:34:16 -070020
Joe Onorato4be866d2010-10-10 11:26:02 -070021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Michael Jurka18014792010-10-14 09:01:34 -070023import android.animation.AnimatorSet;
24import android.animation.ObjectAnimator;
Chet Haase00397b12010-10-07 11:13:10 -070025import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070026import android.animation.ValueAnimator;
27import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040030import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070031import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070033import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070034import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070035import android.graphics.Point;
36import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.graphics.Rect;
38import android.graphics.RectF;
Michael Jurka18014792010-10-14 09:01:34 -070039import android.graphics.Region;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070040import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070042import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.ContextMenu;
44import android.view.MotionEvent;
45import android.view.View;
46import android.view.ViewDebug;
47import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070049import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051
Michael Jurka3c4c20f2010-10-28 15:36:06 -070052import java.util.Arrays;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070053
Adam Cohenf34bab52010-09-30 14:11:56 -070054public class CellLayout extends ViewGroup implements Dimmable {
Winson Chungaafa03c2010-06-11 17:34:16 -070055 static final String TAG = "CellLayout";
56
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057 private int mCellWidth;
58 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070059
Winson Chungaafa03c2010-06-11 17:34:16 -070060 private int mLeftPadding;
61 private int mRightPadding;
62 private int mTopPadding;
63 private int mBottomPadding;
64
Adam Cohend22015c2010-07-26 22:02:18 -070065 private int mCountX;
66 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
68 private int mWidthGap;
69 private int mHeightGap;
70
71 private final Rect mRect = new Rect();
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -070072 private final RectF mRectF = new RectF();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070074
Patrick Dubroyde7658b2010-09-27 11:15:43 -070075 // These are temporary variables to prevent having to allocate a new object just to
76 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070077 private final int[] mTmpCellXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070078 private final int[] mTmpPoint = new int[2];
79 private final PointF mTmpPointF = new PointF();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070080
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 boolean[][] mOccupied;
82
Michael Jurkadee05892010-07-27 10:01:56 -070083 private OnTouchListener mInterceptTouchListener;
84
Michael Jurka5f1c5092010-09-03 14:15:02 -070085 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070086 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070087
Michael Jurka5f1c5092010-09-03 14:15:02 -070088 private Drawable mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -070089 private Drawable mBackgroundMini;
90 private Drawable mBackgroundMiniHover;
Patrick Dubroy1262e362010-10-06 15:49:50 -070091 private Drawable mBackgroundHover;
Michael Jurka3e7c7632010-10-02 16:01:03 -070092 private Drawable mBackgroundMiniAcceptsDrops;
Michael Jurka18014792010-10-14 09:01:34 -070093 private Rect mBackgroundRect;
94 private Rect mHoverRect;
95 private float mHoverScale;
96 private float mHoverAlpha;
Michael Jurka3e7c7632010-10-02 16:01:03 -070097 private boolean mAcceptsDrops;
Patrick Dubroy1262e362010-10-06 15:49:50 -070098
99 // If we're actively dragging something over this screen, mHover is true
Michael Jurkaa63c4522010-08-19 13:52:27 -0700100 private boolean mHover = false;
Michael Jurkadee05892010-07-27 10:01:56 -0700101
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700102 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700103
Winson Chung150fbab2010-09-29 17:14:26 -0700104 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700105 // They are used as circular arrays, indexed by mDragOutlineCurrent.
106 private Point[] mDragOutlines = new Point[8];
Chet Haase472b2812010-10-14 07:02:04 -0700107 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700108 private InterruptibleInOutAnimator[] mDragOutlineAnims =
109 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700110
111 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700112 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700113 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700114
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700115 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700116 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700117 private float mCrosshairsVisibility = 0.0f;
118
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700119 // When a drag operation is in progress, holds the nearest cell to the touch point
120 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Winson Chungaafa03c2010-06-11 17:34:16 -0700122 private final WallpaperManager mWallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato4be866d2010-10-10 11:26:02 -0700124 private boolean mDragging = false;
125
Patrick Dubroyce34a972010-10-19 10:34:32 -0700126 private TimeInterpolator mEaseOutInterpolator;
127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 public CellLayout(Context context) {
129 this(context, null);
130 }
131
132 public CellLayout(Context context, AttributeSet attrs) {
133 this(context, attrs, 0);
134 }
135
136 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
137 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700138
139 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
140 // the user where a dragged item will land when dropped.
141 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
144
145 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
146 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Winson Chungece7f5b2010-10-22 14:54:12 -0700147 mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
148 mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
Winson Chungaafa03c2010-06-11 17:34:16 -0700149
Adam Cohend22015c2010-07-26 22:02:18 -0700150 mLeftPadding =
151 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
152 mRightPadding =
153 a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
154 mTopPadding =
155 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
156 mBottomPadding =
157 a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
Winson Chungaafa03c2010-06-11 17:34:16 -0700158
Adam Cohend22015c2010-07-26 22:02:18 -0700159 mCountX = LauncherModel.getCellCountX();
160 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700161 mOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
163 a.recycle();
164
165 setAlwaysDrawnWithCacheEnabled(false);
166
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700167 mWallpaperManager = WallpaperManager.getInstance(context);
168
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700169 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700170
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700171 if (LauncherApplication.isScreenXLarge()) {
Winson Chung150fbab2010-09-29 17:14:26 -0700172 mBackgroundMini = res.getDrawable(R.drawable.mini_home_screen_bg);
Adam Cohenf34bab52010-09-30 14:11:56 -0700173 mBackgroundMini.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700174 mBackground = res.getDrawable(R.drawable.home_screen_bg);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700175 mBackground.setFilterBitmap(true);
Winson Chung150fbab2010-09-29 17:14:26 -0700176 mBackgroundMiniHover = res.getDrawable(R.drawable.mini_home_screen_bg_hover);
Adam Cohenf34bab52010-09-30 14:11:56 -0700177 mBackgroundMiniHover.setFilterBitmap(true);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700178 mBackgroundHover = res.getDrawable(R.drawable.home_screen_bg_hover);
179 mBackgroundHover.setFilterBitmap(true);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700180 mBackgroundMiniAcceptsDrops = res.getDrawable(
181 R.drawable.mini_home_screen_bg_accepts_drops);
182 mBackgroundMiniAcceptsDrops.setFilterBitmap(true);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700183 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700184
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700185 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700186
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700187 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700188 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700189
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700190 // Set up the animation for fading the crosshairs in and out
191 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700192 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700193 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700194 public void onAnimationUpdate(ValueAnimator animation) {
195 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700196 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700197 }
198 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700199 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700200
Joe Onorato4be866d2010-10-10 11:26:02 -0700201 for (int i = 0; i < mDragOutlines.length; i++) {
202 mDragOutlines[i] = new Point(-1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700203 }
204
205 // When dragging things around the home screens, we show a green outline of
206 // where the item will land. The outlines gradually fade out, leaving a trail
207 // behind the drag path.
208 // Set up all the animations that are used to implement this fading.
209 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700210 final float fromAlphaValue = 0;
211 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700212
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700213 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700214
215 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700216 final InterruptibleInOutAnimator anim =
217 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700218 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700219 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700220 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700221 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700222 final Bitmap outline = (Bitmap)anim.getTag();
223
224 // If an animation is started and then stopped very quickly, we can still
225 // get spurious updates we've cleared the tag. Guard against this.
226 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700227 if (false) {
228 Object val = animation.getAnimatedValue();
229 Log.d(TAG, "anim " + thisIndex + " update: " + val +
230 ", isStopped " + anim.isStopped());
231 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700232 // Try to prevent it from continuing to run
233 animation.cancel();
234 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700235 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Joe Onorato4be866d2010-10-10 11:26:02 -0700236 final int left = mDragOutlines[thisIndex].x;
237 final int top = mDragOutlines[thisIndex].y;
238 CellLayout.this.invalidate(left, top,
239 left + outline.getWidth(), top + outline.getHeight());
240 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700241 }
242 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700243 // The animation holds a reference to the drag outline bitmap as long is it's
244 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700245 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700246 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700247 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700248 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700249 anim.setTag(null);
250 }
251 }
252 });
253 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700254 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700255
Michael Jurka18014792010-10-14 09:01:34 -0700256 mBackgroundRect = new Rect();
257 mHoverRect = new Rect();
258 setHoverScale(1.0f);
259 setHoverAlpha(1.0f);
260 }
261
262 private void updateHoverRect() {
263 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
264 int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
265 int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
266 mHoverRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
267 mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
268 invalidate();
269 }
270
271 public void setHoverScale(float scaleFactor) {
272 if (scaleFactor != mHoverScale) {
273 mHoverScale = scaleFactor;
274 updateHoverRect();
275 }
276 }
277
278 public float getHoverScale() {
279 return mHoverScale;
280 }
281
282 public float getHoverAlpha() {
283 return mHoverAlpha;
284 }
285
286 public void setHoverAlpha(float alpha) {
287 mHoverAlpha = alpha;
288 invalidate();
289 }
290
291 void animateDrop() {
292 if (LauncherApplication.isScreenXLarge()) {
293 Resources res = getResources();
294 float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
295 ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
296 scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
297 ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
298 scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
299 ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
300
301 alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
302 alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
303
304 AnimatorSet bouncer = new AnimatorSet();
305 bouncer.play(scaleUp).before(scaleDown);
306 bouncer.play(scaleUp).with(alphaFadeOut);
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700307 bouncer.addListener(new LauncherAnimatorListenerAdapter() {
308 @Override
Michael Jurka18014792010-10-14 09:01:34 -0700309 public void onAnimationStart(Animator animation) {
310 setHover(true);
311 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700312 @Override
313 public void onAnimationEndOrCancel(Animator animation) {
Michael Jurka18014792010-10-14 09:01:34 -0700314 setHover(false);
315 setHoverScale(1.0f);
316 setHoverAlpha(1.0f);
317 }
318 });
319 bouncer.start();
320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
322
Michael Jurkaa63c4522010-08-19 13:52:27 -0700323 public void setHover(boolean value) {
324 if (mHover != value) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700325 mHover = value;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700326 invalidate();
327 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700328 }
329
Patrick Dubroy0207c522010-11-03 22:12:02 -0700330 public boolean getHover() {
331 return mHover;
332 }
333
Patrick Dubroy1262e362010-10-06 15:49:50 -0700334 public void drawChildren(Canvas canvas) {
335 super.dispatchDraw(canvas);
336 }
337
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700338 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700339 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700340 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
341 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
342 // When we're small, we are either drawn normally or in the "accepts drops" state (during
343 // a drag). However, we also drag the mini hover background *over* one of those two
344 // backgrounds
Michael Jurka5f1c5092010-09-03 14:15:02 -0700345 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700346 Drawable bg;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700347 if (getScaleX() < 0.5f) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700348 bg = mAcceptsDrops ? mBackgroundMiniAcceptsDrops : mBackgroundMini;
Adam Cohenf34bab52010-09-30 14:11:56 -0700349 } else {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700350 bg = mHover ? mBackgroundHover : mBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700351 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700352 if (bg != null) {
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700353 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
Michael Jurka18014792010-10-14 09:01:34 -0700354 bg.setBounds(mBackgroundRect);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700355 bg.draw(canvas);
356 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700357 if (mHover && getScaleX() < 0.5f) {
Michael Jurka18014792010-10-14 09:01:34 -0700358 boolean modifiedClipRect = false;
359 if (mHoverScale > 1.0f) {
360 // If the hover background's scale is greater than 1, we'll be drawing outside
361 // the bounds of this CellLayout. Get around that by temporarily increasing the
362 // size of the clip rect
363 float marginFraction = (mHoverScale - 1.0f) / 2.0f;
364 Rect clipRect = canvas.getClipBounds();
365 int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
366 int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
367 canvas.save(Canvas.CLIP_SAVE_FLAG);
368 canvas.clipRect(-marginX, -marginY,
369 getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
370 modifiedClipRect = true;
371 }
372
373 mBackgroundMiniHover.setAlpha((int) (mBackgroundAlpha * mHoverAlpha * 255));
374 mBackgroundMiniHover.setBounds(mHoverRect);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700375 mBackgroundMiniHover.draw(canvas);
Michael Jurka18014792010-10-14 09:01:34 -0700376 if (modifiedClipRect) {
377 canvas.restore();
378 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700379 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700380 }
Romain Guya6abce82009-11-10 02:54:41 -0800381
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700382 if (mCrosshairsVisibility > 0.0f) {
383 final int countX = mCountX;
384 final int countY = mCountY;
385
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700386 final float MAX_ALPHA = 0.4f;
387 final int MAX_VISIBLE_DISTANCE = 600;
388 final float DISTANCE_MULTIPLIER = 0.002f;
389
390 final Drawable d = mCrosshairsDrawable;
391 final int width = d.getIntrinsicWidth();
392 final int height = d.getIntrinsicHeight();
393
394 int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
395 for (int col = 0; col <= countX; col++) {
396 int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
397 for (int row = 0; row <= countY; row++) {
398 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
399 float dist = mTmpPointF.length();
400 // Crosshairs further from the drag point are more faint
401 float alpha = Math.min(MAX_ALPHA,
402 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
403 if (alpha > 0.0f) {
404 d.setBounds(x, y, x + width, y + height);
405 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
406 d.draw(canvas);
407 }
408 y += mCellHeight + mHeightGap;
409 }
410 x += mCellWidth + mWidthGap;
411 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700412 }
Winson Chung150fbab2010-09-29 17:14:26 -0700413
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700414 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700415 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700416 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700417 if (alpha > 0) {
418 final Point p = mDragOutlines[i];
419 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700420 paint.setAlpha((int)(alpha + .5f));
Joe Onorato4be866d2010-10-10 11:26:02 -0700421 canvas.drawBitmap(b, p.x, p.y, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700422 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700423 }
424 }
425
Adam Cohenf34bab52010-09-30 14:11:56 -0700426 public void setDimmableProgress(float progress) {
427 for (int i = 0; i < getChildCount(); i++) {
428 Dimmable d = (Dimmable) getChildAt(i);
429 d.setDimmableProgress(progress);
430 }
431 }
432
433 public float getDimmableProgress() {
434 if (getChildCount() > 0) {
435 return ((Dimmable) getChildAt(0)).getDimmableProgress();
436 }
437 return 0.0f;
438 }
439
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700440 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700441 public void cancelLongPress() {
442 super.cancelLongPress();
443
444 // Cancel long press for all children
445 final int count = getChildCount();
446 for (int i = 0; i < count; i++) {
447 final View child = getChildAt(i);
448 child.cancelLongPress();
449 }
450 }
451
Michael Jurkadee05892010-07-27 10:01:56 -0700452 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
453 mInterceptTouchListener = listener;
454 }
455
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700457 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
459
460 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700461 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 }
463
Winson Chungaafa03c2010-06-11 17:34:16 -0700464 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700465 return addViewToCellLayout(child, index, childId, params, true);
466 }
467
468 public boolean addViewToCellLayout(
469 View child, int index, int childId, LayoutParams params, boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700470 final LayoutParams lp = params;
471
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 // Generate an id for each view, this assumes we have at most 256x256 cells
473 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700474 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700475 // If the horizontal or vertical span is set to -1, it is taken to
476 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700477 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
478 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479
Winson Chungaafa03c2010-06-11 17:34:16 -0700480 child.setId(childId);
481
Michael Jurkadee05892010-07-27 10:01:56 -0700482 // We might be in the middle or end of shrinking/fading to a dimmed view
483 // Make sure this view's alpha is set the same as all the rest of the views
Michael Jurka5f1c5092010-09-03 14:15:02 -0700484 child.setAlpha(getAlpha());
Winson Chungaafa03c2010-06-11 17:34:16 -0700485 addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700486
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700487 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700488
Winson Chungaafa03c2010-06-11 17:34:16 -0700489 return true;
490 }
491 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700493 public void setAcceptsDrops(boolean acceptsDrops) {
494 if (mAcceptsDrops != acceptsDrops) {
495 mAcceptsDrops = acceptsDrops;
496 invalidate();
497 }
498 }
499
500 public boolean getAcceptsDrops() {
501 return mAcceptsDrops;
502 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800503
504 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700505 public void removeAllViews() {
506 clearOccupiedCells();
507 }
508
509 @Override
510 public void removeAllViewsInLayout() {
511 clearOccupiedCells();
512 }
513
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700514 public void removeViewWithoutMarkingCells(View view) {
515 super.removeView(view);
516 }
517
Michael Jurka0280c3b2010-09-17 15:00:07 -0700518 @Override
519 public void removeView(View view) {
520 markCellsAsUnoccupiedForView(view);
521 super.removeView(view);
522 }
523
524 @Override
525 public void removeViewAt(int index) {
526 markCellsAsUnoccupiedForView(getChildAt(index));
527 super.removeViewAt(index);
528 }
529
530 @Override
531 public void removeViewInLayout(View view) {
532 markCellsAsUnoccupiedForView(view);
533 super.removeViewInLayout(view);
534 }
535
536 @Override
537 public void removeViews(int start, int count) {
538 for (int i = start; i < start + count; i++) {
539 markCellsAsUnoccupiedForView(getChildAt(i));
540 }
541 super.removeViews(start, count);
542 }
543
544 @Override
545 public void removeViewsInLayout(int start, int count) {
546 for (int i = start; i < start + count; i++) {
547 markCellsAsUnoccupiedForView(getChildAt(i));
548 }
549 super.removeViewsInLayout(start, count);
550 }
551
552 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 public void requestChildFocus(View child, View focused) {
554 super.requestChildFocus(child, focused);
555 if (child != null) {
556 Rect r = new Rect();
557 child.getDrawingRect(r);
558 requestRectangleOnScreen(r);
559 }
560 }
561
562 @Override
563 protected void onAttachedToWindow() {
564 super.onAttachedToWindow();
565 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
566 }
567
Michael Jurkaaf442092010-06-10 17:01:57 -0700568 public void setTagToCellInfoForPoint(int touchX, int touchY) {
569 final CellInfo cellInfo = mCellInfo;
570 final Rect frame = mRect;
571 final int x = touchX + mScrollX;
572 final int y = touchY + mScrollY;
573 final int count = getChildCount();
574
575 boolean found = false;
576 for (int i = count - 1; i >= 0; i--) {
577 final View child = getChildAt(i);
578
579 if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
580 child.getHitRect(frame);
581 if (frame.contains(x, y)) {
582 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
583 cellInfo.cell = child;
584 cellInfo.cellX = lp.cellX;
585 cellInfo.cellY = lp.cellY;
586 cellInfo.spanX = lp.cellHSpan;
587 cellInfo.spanY = lp.cellVSpan;
588 cellInfo.valid = true;
589 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700590 break;
591 }
592 }
593 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700594
Michael Jurkaaf442092010-06-10 17:01:57 -0700595 if (!found) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700596 final int cellXY[] = mTmpCellXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700597 pointToCellExact(x, y, cellXY);
598
Michael Jurkaaf442092010-06-10 17:01:57 -0700599 cellInfo.cell = null;
600 cellInfo.cellX = cellXY[0];
601 cellInfo.cellY = cellXY[1];
602 cellInfo.spanX = 1;
603 cellInfo.spanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700604 cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
605 cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
Michael Jurkaaf442092010-06-10 17:01:57 -0700606 }
607 setTag(cellInfo);
608 }
609
Winson Chungaafa03c2010-06-11 17:34:16 -0700610
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 @Override
612 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkadee05892010-07-27 10:01:56 -0700613 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
614 return true;
615 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 final int action = ev.getAction();
617 final CellInfo cellInfo = mCellInfo;
618
619 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700620 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 } else if (action == MotionEvent.ACTION_UP) {
622 cellInfo.cell = null;
623 cellInfo.cellX = -1;
624 cellInfo.cellY = -1;
625 cellInfo.spanX = 0;
626 cellInfo.spanY = 0;
627 cellInfo.valid = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 setTag(cellInfo);
629 }
630
631 return false;
632 }
633
634 @Override
635 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700636 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
638
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700639 /**
640 * Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
641 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
643 for (int x = left; x <= right; x++) {
644 if (occupied[x][y]) {
645 return false;
646 }
647 }
648 return true;
649 }
650
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700652 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 * @param x X coordinate of the point
654 * @param y Y coordinate of the point
655 * @param result Array of 2 ints to hold the x and y coordinate of the cell
656 */
657 void pointToCellExact(int x, int y, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700658 final int hStartPadding = getLeftPadding();
659 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660
661 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
662 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
663
Adam Cohend22015c2010-07-26 22:02:18 -0700664 final int xAxis = mCountX;
665 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666
667 if (result[0] < 0) result[0] = 0;
668 if (result[0] >= xAxis) result[0] = xAxis - 1;
669 if (result[1] < 0) result[1] = 0;
670 if (result[1] >= yAxis) result[1] = yAxis - 1;
671 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 /**
674 * Given a point, return the cell that most closely encloses that point
675 * @param x X coordinate of the point
676 * @param y Y coordinate of the point
677 * @param result Array of 2 ints to hold the x and y coordinate of the cell
678 */
679 void pointToCellRounded(int x, int y, int[] result) {
680 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
681 }
682
683 /**
684 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700685 *
686 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700688 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 * @param result Array of 2 ints to hold the x and y coordinate of the point
690 */
691 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700692 final int hStartPadding = getLeftPadding();
693 final int vStartPadding = getTopPadding();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694
695 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
696 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
697 }
698
Romain Guy84f296c2009-11-04 15:00:44 -0800699 int getCellWidth() {
700 return mCellWidth;
701 }
702
703 int getCellHeight() {
704 return mCellHeight;
705 }
706
Romain Guy1a304a12009-11-10 00:02:32 -0800707 int getLeftPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700708 return mLeftPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800709 }
710
711 int getTopPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700712 return mTopPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800713 }
714
715 int getRightPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700716 return mRightPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800717 }
718
719 int getBottomPadding() {
Winson Chungaafa03c2010-06-11 17:34:16 -0700720 return mBottomPadding;
Romain Guy1a304a12009-11-10 00:02:32 -0800721 }
722
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 @Override
724 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
725 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700726
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700728 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
729
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
731 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700732
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
734 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
735 }
736
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 final int cellWidth = mCellWidth;
738 final int cellHeight = mCellHeight;
739
Adam Cohend22015c2010-07-26 22:02:18 -0700740 int numWidthGaps = mCountX - 1;
741 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742
Winson Chungece7f5b2010-10-22 14:54:12 -0700743 if (mWidthGap < 0 || mHeightGap < 0) {
744 int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
745 mHeightGap = vSpaceLeft / numHeightGaps;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746
Winson Chungece7f5b2010-10-22 14:54:12 -0700747 int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
748 mWidthGap = hSpaceLeft / numWidthGaps;
Winson Chungaafa03c2010-06-11 17:34:16 -0700749
Winson Chungece7f5b2010-10-22 14:54:12 -0700750 // center it around the min gaps
751 int minGap = Math.min(mWidthGap, mHeightGap);
752 mWidthGap = mHeightGap = minGap;
753 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700754
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 int count = getChildCount();
756
757 for (int i = 0; i < count; i++) {
758 View child = getChildAt(i);
759 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Winson Chungaafa03c2010-06-11 17:34:16 -0700760 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
761 mLeftPadding, mTopPadding);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762
Michael Jurka0280c3b2010-09-17 15:00:07 -0700763 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700764 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
765 MeasureSpec.EXACTLY);
766
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
768 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700769 if (widthSpecMode == MeasureSpec.AT_MOST) {
770 int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700771 ((mCountX - 1) * mWidthGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700772 int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700773 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700774 setMeasuredDimension(newWidth, newHeight);
775 } else if (widthSpecMode == MeasureSpec.EXACTLY) {
776 setMeasuredDimension(widthSpecSize, heightSpecSize);
777 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 }
779
780 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700781 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 int count = getChildCount();
783
784 for (int i = 0; i < count; i++) {
Patrick Dubroyce34a972010-10-19 10:34:32 -0700785 final View child = getChildAt(i);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 if (child.getVisibility() != GONE) {
787
788 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
789
790 int childLeft = lp.x;
791 int childTop = lp.y;
792 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
Romain Guy84f296c2009-11-04 15:00:44 -0800793
794 if (lp.dropped) {
795 lp.dropped = false;
796
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700797 final int[] cellXY = mTmpCellXY;
Romain Guy06762ab2010-01-25 16:51:08 -0800798 getLocationOnScreen(cellXY);
Romain Guy84f296c2009-11-04 15:00:44 -0800799 mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
Romain Guy06762ab2010-01-25 16:51:08 -0800800 cellXY[0] + childLeft + lp.width / 2,
801 cellXY[1] + childTop + lp.height / 2, 0, null);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700802
Patrick Dubroycd68ff52010-10-28 17:57:05 -0700803 ((Workspace) mParent).animateViewIntoPosition(child);
Romain Guy84f296c2009-11-04 15:00:44 -0800804 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 }
806 }
807 }
808
809 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700810 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
811 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700812 mBackgroundRect.set(0, 0, w, h);
813 updateHoverRect();
Michael Jurkadee05892010-07-27 10:01:56 -0700814 }
815
816 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
818 final int count = getChildCount();
819 for (int i = 0; i < count; i++) {
820 final View view = getChildAt(i);
821 view.setDrawingCacheEnabled(enabled);
822 // Update the drawing caches
Romain Guy3cf4c032010-10-26 14:29:35 -0700823 if (!view.isHardwareAccelerated()) {
824 view.buildDrawingCache(true);
825 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 }
827 }
828
829 @Override
830 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
831 super.setChildrenDrawnWithCacheEnabled(enabled);
832 }
833
Michael Jurka5f1c5092010-09-03 14:15:02 -0700834 public float getBackgroundAlpha() {
835 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -0700836 }
837
Adam Cohen1b0aaac2010-10-28 11:11:18 -0700838 public void setBackgroundAlphaMultiplier(float multiplier) {
839 mBackgroundAlphaMultiplier = multiplier;
840 }
841
Michael Jurka5f1c5092010-09-03 14:15:02 -0700842 public void setBackgroundAlpha(float alpha) {
843 mBackgroundAlpha = alpha;
Michael Jurka0142d492010-08-25 17:46:15 -0700844 invalidate();
Michael Jurkadee05892010-07-27 10:01:56 -0700845 }
846
Michael Jurka5f1c5092010-09-03 14:15:02 -0700847 // Need to return true to let the view system know we know how to handle alpha-- this is
848 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
849 // versions
850 @Override
851 protected boolean onSetAlpha(int alpha) {
852 return true;
853 }
854
855 public void setAlpha(float alpha) {
856 setChildrenAlpha(alpha);
857 super.setAlpha(alpha);
858 }
859
Michael Jurkadee05892010-07-27 10:01:56 -0700860 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -0700861 final int childCount = getChildCount();
862 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -0700863 getChildAt(i).setAlpha(alpha);
864 }
865 }
866
Michael Jurka0280c3b2010-09-17 15:00:07 -0700867 private boolean isVacantIgnoring(
868 int originX, int originY, int spanX, int spanY, View ignoreView) {
869 if (ignoreView != null) {
870 markCellsAsUnoccupiedForView(ignoreView);
871 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700872 boolean isVacant = true;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700873 for (int i = 0; i < spanY; i++) {
874 if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
Michael Jurka28750fb2010-09-24 17:43:49 -0700875 isVacant = false;
876 break;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700877 }
878 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700879 if (ignoreView != null) {
880 markCellsAsOccupiedForView(ignoreView);
881 }
Michael Jurka28750fb2010-09-24 17:43:49 -0700882 return isVacant;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700883 }
884
Michael Jurka0280c3b2010-09-17 15:00:07 -0700885 private boolean isVacant(int originX, int originY, int spanX, int spanY) {
886 return isVacantIgnoring(originX, originY, spanX, spanY, null);
887 }
888
Patrick Dubroy440c3602010-07-13 17:50:32 -0700889 public View getChildAt(int x, int y) {
890 final int count = getChildCount();
891 for (int i = 0; i < count; i++) {
892 View child = getChildAt(i);
893 LayoutParams lp = (LayoutParams) child.getLayoutParams();
894
895 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
896 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
897 return child;
898 }
899 }
900 return null;
901 }
902
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700903 /**
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700904 * Estimate the size that a child with the given dimensions will take in the layout.
905 */
906 void estimateChildSize(int minWidth, int minHeight, int[] result) {
907 // Assuming it's placed at 0, 0, find where the bottom right cell will land
908 rectToCell(minWidth, minHeight, result);
909
910 // Then figure out the rect it will occupy
911 cellToRect(0, 0, result[0], result[1], mRectF);
912 result[0] = (int)mRectF.width();
913 result[1] = (int)mRectF.height();
914 }
915
916 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700917 * Estimate where the top left cell of the dragged item will land if it is dropped.
918 *
919 * @param originX The X value of the top left corner of the item
920 * @param originY The Y value of the top left corner of the item
921 * @param spanX The number of horizontal cells that the item spans
922 * @param spanY The number of vertical cells that the item spans
923 * @param result The estimated drop cell X and Y.
924 */
925 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -0700926 final int countX = mCountX;
927 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700928
Michael Jurkaa63c4522010-08-19 13:52:27 -0700929 // pointToCellRounded takes the top left of a cell but will pad that with
930 // cellWidth/2 and cellHeight/2 when finding the matching cell
931 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700932
933 // If the item isn't fully on this screen, snap to the edges
934 int rightOverhang = result[0] + spanX - countX;
935 if (rightOverhang > 0) {
936 result[0] -= rightOverhang; // Snap to right
937 }
938 result[0] = Math.max(0, result[0]); // Snap to left
939 int bottomOverhang = result[1] + spanY - countY;
940 if (bottomOverhang > 0) {
941 result[1] -= bottomOverhang; // Snap to bottom
942 }
943 result[1] = Math.max(0, result[1]); // Snap to top
944 }
945
Joe Onorato4be866d2010-10-10 11:26:02 -0700946 void visualizeDropLocation(
947 View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
948
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700949 final int oldDragCellX = mDragCell[0];
950 final int oldDragCellY = mDragCell[1];
Joe Onorato4be866d2010-10-10 11:26:02 -0700951 final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
Winson Chunga9abd0e2010-10-27 17:18:37 -0700952 if (v != null) {
953 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
954 } else {
955 mDragCenter.set(originX, originY);
956 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700957
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700958 if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700959 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700960 final int[] topLeft = mTmpPoint;
961 cellToPoint(nearest[0], nearest[1], topLeft);
962
Joe Onorato4be866d2010-10-10 11:26:02 -0700963 int left = topLeft[0];
964 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700965
Winson Chunga9abd0e2010-10-27 17:18:37 -0700966 if (v != null) {
967 if (v.getParent() instanceof CellLayout) {
968 LayoutParams lp = (LayoutParams) v.getLayoutParams();
969 left += lp.leftMargin;
970 top += lp.topMargin;
971 }
Winson Chung150fbab2010-09-29 17:14:26 -0700972
Winson Chunga9abd0e2010-10-27 17:18:37 -0700973 // Offsets due to the size difference between the View and the dragOutline
974 left += (v.getWidth() - dragOutline.getWidth()) / 2;
975 top += (v.getHeight() - dragOutline.getHeight()) / 2;
976 }
Winson Chung150fbab2010-09-29 17:14:26 -0700977
Joe Onorato4be866d2010-10-10 11:26:02 -0700978 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700979 mDragOutlineAnims[oldIndex].animateOut();
980 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Winson Chung150fbab2010-09-29 17:14:26 -0700981
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -0700982 mDragOutlines[mDragOutlineCurrent].set(left, top);
983 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
984 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700985 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700986
987 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
988 if (mCrosshairsDrawable != null) {
989 invalidate();
990 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700991 }
992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700994 * Find a vacant area that will fit the given bounds nearest the requested
995 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -0700996 *
Romain Guy51afc022009-05-04 18:03:43 -0700997 * @param pixelX The X location at which you want to search for a vacant area.
998 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -0700999 * @param spanX Horizontal span of the object.
1000 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001001 * @param result Array in which to place the result, or null (in which case a new array will
1002 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001003 * @return The X, Y cell of a vacant area that can contain this object,
1004 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001006 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001007 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1008 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001009 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001010
Michael Jurka6a1435d2010-09-27 17:35:12 -07001011 /**
1012 * Find a vacant area that will fit the given bounds nearest the requested
1013 * cell location. Uses Euclidean distance to score multiple vacant areas.
1014 *
1015 * @param pixelX The X location at which you want to search for a vacant area.
1016 * @param pixelY The Y location at which you want to search for a vacant area.
1017 * @param spanX Horizontal span of the object.
1018 * @param spanY Vertical span of the object.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001019 * @param ignoreView Considers space occupied by this view as unoccupied
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001020 * @param result Previously returned value to possibly recycle.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001021 * @return The X, Y cell of a vacant area that can contain this object,
1022 * nearest the requested location.
1023 */
1024 int[] findNearestVacantArea(
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001025 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001026 // mark space take by ignoreView as available (method checks if ignoreView is null)
1027 markCellsAsUnoccupiedForView(ignoreView);
1028
Jeff Sharkey70864282009-04-07 21:08:40 -07001029 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001030 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001031 double bestDistance = Double.MAX_VALUE;
Winson Chungaafa03c2010-06-11 17:34:16 -07001032
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001033 final int countX = mCountX;
1034 final int countY = mCountY;
1035 final boolean[][] occupied = mOccupied;
1036
1037 for (int x = 0; x < countX - (spanX - 1); x++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001038 inner:
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001039 for (int y = 0; y < countY - (spanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001040 for (int i = 0; i < spanX; i++) {
1041 for (int j = 0; j < spanY; j++) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001042 if (occupied[x + i][y + j]) {
Michael Jurkac28de512010-08-13 11:27:44 -07001043 // small optimization: we can skip to below the row we just found
1044 // an occupied cell
1045 y += j;
1046 continue inner;
1047 }
1048 }
1049 }
1050 final int[] cellXY = mTmpCellXY;
1051 cellToPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052
Michael Jurkac28de512010-08-13 11:27:44 -07001053 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1054 + Math.pow(cellXY[1] - pixelY, 2));
1055 if (distance <= bestDistance) {
1056 bestDistance = distance;
1057 bestXY[0] = x;
1058 bestXY[1] = y;
1059 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 }
1061 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001062 // re-mark space taken by ignoreView as occupied
1063 markCellsAsOccupiedForView(ignoreView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064
Winson Chungaafa03c2010-06-11 17:34:16 -07001065 // Return null if no suitable location found
Jeff Sharkey70864282009-04-07 21:08:40 -07001066 if (bestDistance < Double.MAX_VALUE) {
1067 return bestXY;
1068 } else {
1069 return null;
1070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001072
Michael Jurka0280c3b2010-09-17 15:00:07 -07001073 boolean existsEmptyCell() {
1074 return findCellForSpan(null, 1, 1);
1075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001078 * Finds the upper-left coordinate of the first rectangle in the grid that can
1079 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1080 * then this method will only return coordinates for rectangles that contain the cell
1081 * (intersectX, intersectY)
1082 *
1083 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1084 * can be found.
1085 * @param spanX The horizontal span of the cell we want to find.
1086 * @param spanY The vertical span of the cell we want to find.
1087 *
1088 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001089 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001090 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
1091 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
1092 }
1093
1094 /**
1095 * Like above, but ignores any cells occupied by the item "ignoreView"
1096 *
1097 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1098 * can be found.
1099 * @param spanX The horizontal span of the cell we want to find.
1100 * @param spanY The vertical span of the cell we want to find.
1101 * @param ignoreView The home screen item we should treat as not occupying any space
1102 * @return
1103 */
1104 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
1105 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
1106 }
1107
1108 /**
1109 * Like above, but if intersectX and intersectY are not -1, then this method will try to
1110 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
1111 *
1112 * @param spanX The horizontal span of the cell we want to find.
1113 * @param spanY The vertical span of the cell we want to find.
1114 * @param ignoreView The home screen item we should treat as not occupying any space
1115 * @param intersectX The X coordinate of the cell that we should try to overlap
1116 * @param intersectX The Y coordinate of the cell that we should try to overlap
1117 *
1118 * @return True if a vacant cell of the specified dimension was found, false otherwise.
1119 */
1120 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
1121 int intersectX, int intersectY) {
1122 return findCellForSpanThatIntersectsIgnoring(
1123 cellXY, spanX, spanY, intersectX, intersectY, null);
1124 }
1125
1126 /**
1127 * The superset of the above two methods
1128 */
1129 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
1130 int intersectX, int intersectY, View ignoreView) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001131 // mark space take by ignoreView as available (method checks if ignoreView is null)
1132 markCellsAsUnoccupiedForView(ignoreView);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001133
Michael Jurka28750fb2010-09-24 17:43:49 -07001134 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001135 while (true) {
1136 int startX = 0;
1137 if (intersectX >= 0) {
1138 startX = Math.max(startX, intersectX - (spanX - 1));
1139 }
1140 int endX = mCountX - (spanX - 1);
1141 if (intersectX >= 0) {
1142 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
1143 }
1144 int startY = 0;
1145 if (intersectY >= 0) {
1146 startY = Math.max(startY, intersectY - (spanY - 1));
1147 }
1148 int endY = mCountY - (spanY - 1);
1149 if (intersectY >= 0) {
1150 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
1151 }
1152
1153 for (int x = startX; x < endX; x++) {
1154 inner:
1155 for (int y = startY; y < endY; y++) {
1156 for (int i = 0; i < spanX; i++) {
1157 for (int j = 0; j < spanY; j++) {
1158 if (mOccupied[x + i][y + j]) {
1159 // small optimization: we can skip to below the row we just found
1160 // an occupied cell
1161 y += j;
1162 continue inner;
1163 }
1164 }
1165 }
1166 if (cellXY != null) {
1167 cellXY[0] = x;
1168 cellXY[1] = y;
1169 }
Michael Jurka28750fb2010-09-24 17:43:49 -07001170 foundCell = true;
1171 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001172 }
1173 }
1174 if (intersectX == -1 && intersectY == -1) {
1175 break;
1176 } else {
1177 // if we failed to find anything, try again but without any requirements of
1178 // intersecting
1179 intersectX = -1;
1180 intersectY = -1;
1181 continue;
1182 }
1183 }
1184
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001185 // re-mark space taken by ignoreView as occupied
1186 markCellsAsOccupiedForView(ignoreView);
Michael Jurka28750fb2010-09-24 17:43:49 -07001187 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001188 }
1189
1190 /**
1191 * Called when drag has left this CellLayout or has been completed (successfully or not)
1192 */
1193 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07001194 // This can actually be called when we aren't in a drag, e.g. when adding a new
1195 // item to this layout via the customize drawer.
1196 // Guard against that case.
1197 if (mDragging) {
1198 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001199
Joe Onorato4be866d2010-10-10 11:26:02 -07001200 // Fade out the drag indicators
1201 if (mCrosshairsAnimator != null) {
1202 mCrosshairsAnimator.animateOut();
1203 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001204 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001205
1206 // Invalidate the drag data
1207 mDragCell[0] = -1;
1208 mDragCell[1] = -1;
1209 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
1210 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
1211
1212 setHover(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001213 }
1214
1215 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001216 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001217 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07001218 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 *
1220 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001222 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07001223 if (child != null) {
1224 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guyd94533d2009-08-17 10:01:15 -07001225 lp.isDragging = false;
Romain Guy84f296c2009-11-04 15:00:44 -08001226 lp.dropped = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001227 child.setVisibility(View.VISIBLE);
Romain Guyd94533d2009-08-17 10:01:15 -07001228 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07001229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
1231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 /**
1233 * Start dragging the specified child
Winson Chungaafa03c2010-06-11 17:34:16 -07001234 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 * @param child The child that is being dragged
1236 */
1237 void onDragChild(View child) {
1238 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1239 lp.isDragging = true;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001240 child.setVisibility(View.GONE);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001241 }
1242
1243 /**
1244 * A drag event has begun over this layout.
1245 * It may have begun over this layout (in which case onDragChild is called first),
1246 * or it may have begun on another layout.
1247 */
Winson Chunga9abd0e2010-10-27 17:18:37 -07001248 void onDragEnter() {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001249 if (!mDragging) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -07001250 // Fade in the drag indicators
1251 if (mCrosshairsAnimator != null) {
1252 mCrosshairsAnimator.animateIn();
1253 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001254 }
1255 mDragging = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07001260 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 * @param cellX X coordinate of upper left corner expressed as a cell position
1262 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07001263 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001265 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001267 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 final int cellWidth = mCellWidth;
1269 final int cellHeight = mCellHeight;
1270 final int widthGap = mWidthGap;
1271 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07001272
1273 final int hStartPadding = getLeftPadding();
1274 final int vStartPadding = getTopPadding();
1275
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
1277 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
1278
1279 int x = hStartPadding + cellX * (cellWidth + widthGap);
1280 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07001281
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001282 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001284
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001286 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07001288 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 * @param width Width in pixels
1290 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001291 * @param result An array of length 2 in which to store the result (may be null).
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001293 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07001294 return rectToCell(getResources(), width, height, result);
1295 }
1296
1297 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 // Always assume we're working with the smallest span to make sure we
1299 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04001300 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
1301 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04001303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 // Always round up to next largest cell
1305 int spanX = (width + smallerSize) / smallerSize;
1306 int spanY = (height + smallerSize) / smallerSize;
Joe Onorato79e56262009-09-21 15:23:04 -04001307
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001308 if (result == null) {
1309 return new int[] { spanX, spanY };
1310 }
1311 result[0] = spanX;
1312 result[1] = spanY;
1313 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
1315
1316 /**
1317 * Find the first vacant cell, if there is one.
1318 *
1319 * @param vacant Holds the x and y coordinate of the vacant cell
1320 * @param spanX Horizontal cell span.
1321 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07001322 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 * @return True if a vacant cell was found
1324 */
1325 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326
Michael Jurka0280c3b2010-09-17 15:00:07 -07001327 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 }
1329
1330 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
1331 int xCount, int yCount, boolean[][] occupied) {
1332
1333 for (int x = 0; x < xCount; x++) {
1334 for (int y = 0; y < yCount; y++) {
1335 boolean available = !occupied[x][y];
1336out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
1337 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
1338 available = available && !occupied[i][j];
1339 if (!available) break out;
1340 }
1341 }
1342
1343 if (available) {
1344 vacant[0] = x;
1345 vacant[1] = y;
1346 return true;
1347 }
1348 }
1349 }
1350
1351 return false;
1352 }
1353
Michael Jurka0280c3b2010-09-17 15:00:07 -07001354 private void clearOccupiedCells() {
1355 for (int x = 0; x < mCountX; x++) {
1356 for (int y = 0; y < mCountY; y++) {
1357 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 }
1359 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001360 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361
Michael Jurka0280c3b2010-09-17 15:00:07 -07001362 public void onMove(View view, int newCellX, int newCellY) {
1363 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1364 markCellsAsUnoccupiedForView(view);
1365 markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
1366 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367
Michael Jurka0280c3b2010-09-17 15:00:07 -07001368 private void markCellsAsOccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001369 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001370 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1371 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
1372 }
1373
1374 private void markCellsAsUnoccupiedForView(View view) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001375 if (view == null || view.getParent() != this) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001376 LayoutParams lp = (LayoutParams) view.getLayoutParams();
1377 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
1378 }
1379
1380 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
1381 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
1382 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
1383 mOccupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
1385 }
1386 }
1387
1388 @Override
1389 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
1390 return new CellLayout.LayoutParams(getContext(), attrs);
1391 }
1392
1393 @Override
1394 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1395 return p instanceof CellLayout.LayoutParams;
1396 }
1397
1398 @Override
1399 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1400 return new CellLayout.LayoutParams(p);
1401 }
1402
Winson Chungaafa03c2010-06-11 17:34:16 -07001403 public static class CellLayoutAnimationController extends LayoutAnimationController {
1404 public CellLayoutAnimationController(Animation animation, float delay) {
1405 super(animation, delay);
1406 }
1407
1408 @Override
1409 protected long getDelayForView(View view) {
1410 return (int) (Math.random() * 150);
1411 }
1412 }
1413
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1415 /**
1416 * Horizontal location of the item in the grid.
1417 */
1418 @ViewDebug.ExportedProperty
1419 public int cellX;
1420
1421 /**
1422 * Vertical location of the item in the grid.
1423 */
1424 @ViewDebug.ExportedProperty
1425 public int cellY;
1426
1427 /**
1428 * Number of cells spanned horizontally by the item.
1429 */
1430 @ViewDebug.ExportedProperty
1431 public int cellHSpan;
1432
1433 /**
1434 * Number of cells spanned vertically by the item.
1435 */
1436 @ViewDebug.ExportedProperty
1437 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07001438
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 /**
1440 * Is this item currently being dragged
1441 */
1442 public boolean isDragging;
1443
1444 // X coordinate of the view in the layout.
1445 @ViewDebug.ExportedProperty
1446 int x;
1447 // Y coordinate of the view in the layout.
1448 @ViewDebug.ExportedProperty
1449 int y;
1450
Patrick Dubroyce34a972010-10-19 10:34:32 -07001451 /**
1452 * The old X coordinate of this item, relative to its current parent.
1453 * Used to animate the item into its new position.
1454 */
1455 int oldX;
1456
1457 /**
1458 * The old Y coordinate of this item, relative to its current parent.
1459 * Used to animate the item into its new position.
1460 */
1461 int oldY;
1462
Romain Guy84f296c2009-11-04 15:00:44 -08001463 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07001464
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 public LayoutParams(Context c, AttributeSet attrs) {
1466 super(c, attrs);
1467 cellHSpan = 1;
1468 cellVSpan = 1;
1469 }
1470
1471 public LayoutParams(ViewGroup.LayoutParams source) {
1472 super(source);
1473 cellHSpan = 1;
1474 cellVSpan = 1;
1475 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001476
1477 public LayoutParams(LayoutParams source) {
1478 super(source);
1479 this.cellX = source.cellX;
1480 this.cellY = source.cellY;
1481 this.cellHSpan = source.cellHSpan;
1482 this.cellVSpan = source.cellVSpan;
1483 }
1484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08001486 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 this.cellX = cellX;
1488 this.cellY = cellY;
1489 this.cellHSpan = cellHSpan;
1490 this.cellVSpan = cellVSpan;
1491 }
1492
1493 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
1494 int hStartPadding, int vStartPadding) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 final int myCellHSpan = cellHSpan;
1497 final int myCellVSpan = cellVSpan;
1498 final int myCellX = cellX;
1499 final int myCellY = cellY;
Winson Chungaafa03c2010-06-11 17:34:16 -07001500
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
1502 leftMargin - rightMargin;
1503 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
1504 topMargin - bottomMargin;
1505
1506 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
1507 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
1508 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001509
1510 public String toString() {
1511 return "(" + this.cellX + ", " + this.cellY + ")";
1512 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 }
1514
Michael Jurka0280c3b2010-09-17 15:00:07 -07001515 // This class stores info for two purposes:
1516 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
1517 // its spanX, spanY, and the screen it is on
1518 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
1519 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
1520 // the CellLayout that was long clicked
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 static final class CellInfo implements ContextMenu.ContextMenuInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001523 int cellX = -1;
1524 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 int spanX;
1526 int spanY;
1527 int screen;
1528 boolean valid;
1529
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 @Override
1531 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001532 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
1533 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
1535 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536}